1+ /**
2+ * Run with:
3+ *
4+ * ```sh
5+ * npx ts-node --esm .\immudb-node-showcase\src\sql-showcase.ts
6+ * ```
7+ */
8+ import Long from 'long'
9+ import {
10+ Client ,
11+ verifyVerification ,
12+ types ,
13+ stream ,
14+ } from 'immudb-node'
15+
16+
17+
18+
19+
20+ sqlSchowcase ( )
21+ . catch ( console . error )
22+
23+ async function sqlSchowcase ( ) {
24+
25+ const client = new Client ( {
26+ host : '127.0.0.1' ,
27+ port : 3322 ,
28+ user : 'immudb' ,
29+ password : 'immudb' ,
30+ database : 'defaultdb' ,
31+ } )
32+
33+ // since tx used for verification reference
34+ // cannot be first db transaction lets insert some dummy value:
35+ const { valEntries : [ dummyValEntry ] } = await client . setValEntries ( { kvms : [
36+ { key : Buffer . of ( 0 ) , val : Buffer . of ( 0 ) }
37+ ] } )
38+
39+ // state will be dummyValEntry if database was empty
40+ const stateId2 = await client . getDbCurrentState ( )
41+ console . log ( 'stateId2:' , stateId2 )
42+
43+
44+ const { subTxes : [ { tx : createTestTableTx } ] } = await client . sqlExec ( { sql : `
45+ create table if not exists testtable (
46+ id1 integer not null,
47+ id2 varchar[3] null,
48+ created timestamp null,
49+ data varchar[512] not null,
50+ isActive boolean not null,
51+ primary key (id1, id2)
52+ );
53+ ` } )
54+ console . log ( 'createTestTableTx:' , createTestTableTx )
55+
56+
57+ const { subTxes : [ {
58+ tx : insertTestTableTx ,
59+ lastPK : insertTestTableLastPK ,
60+ firstPK : insertTestTableFirstPK ,
61+ updatedRowsCount : insertTestTableUpdatedRowsCount
62+ } ] } = await client . sqlExec ( { sql : `
63+ upsert into testtable
64+ (id1, id2, created, data, isactive)
65+ values
66+ (-2, 'kkk', NOW(), 'upsert existing', true),
67+ (10, 'yoy', NOW(), 'upsert operation 2', false),
68+ (11, 'qoy', NOW(), 'upsert operation 3', true);
69+ ` } )
70+ console . log ( 'insertTestTableTx:' , insertTestTableTx )
71+ console . log ( 'insertTestTableLastPK:' , insertTestTableLastPK )
72+ console . log ( 'insertTestTableFirstPK:' , insertTestTableFirstPK )
73+ console . log ( 'insertTestTableUpdatedRowsCount:' , insertTestTableUpdatedRowsCount )
74+
75+ // state at last sql insert (assuming empty db)
76+ const stateId4 = await client . getDbCurrentState ( )
77+ console . log ( 'stateId4:' , stateId4 )
78+
79+
80+ const { valEntries : [ dummyValEntry1 ] } = await client . setValEntries ( { kvms : [
81+ { key : Buffer . of ( 0 ) , val : Buffer . of ( 1 ) }
82+ ] } )
83+
84+ // state 1 transactions after last sql insert (assuming empty db)
85+ const stateId5 = await client . getDbCurrentState ( )
86+ console . log ( 'stateId5:' , stateId5 )
87+
88+
89+ const { valEntries : [ dummyValEntry2 ] } = await client . setValEntries ( { kvms : [
90+ { key : Buffer . of ( 0 ) , val : Buffer . of ( 2 ) }
91+ ] } )
92+
93+ // state 2 transactions after last sql insert (assuming empty db)
94+ const stateId6 = await client . getDbCurrentState ( )
95+ console . log ( 'stateId6:' , stateId6 )
96+
97+
98+
99+
100+ if ( createTestTableTx ) {
101+ const createTestTableTxVer = await client . getTxAndVerification ( {
102+ txId : createTestTableTx . id ,
103+ refHash : stateId5 . txHash ,
104+ refTxId : stateId5 . txId ,
105+ } )
106+ verifyVerification ( createTestTableTxVer . verification )
107+ console . log ( 'createTestTableTxVer has been verified.' )
108+ }
109+
110+
111+
112+ if ( insertTestTableTx ) {
113+ const insertTestTableTxVer = await client . getTxAndVerification ( {
114+ txId : insertTestTableTx . id ,
115+ refHash : stateId5 . txHash ,
116+ refTxId : stateId5 . txId ,
117+ } )
118+ verifyVerification ( insertTestTableTxVer . verification )
119+ console . log ( 'insertTestTableTxVer has been verified.' )
120+ // console.log('insertTestTableTxVer')
121+ // console.log(insertTestTableTxVer, {depth: 10})
122+
123+
124+
125+ // await client.getSqlRowEntryAndVerification({
126+ // pk: is.encodeAsPK([
127+ // {type: 'INTEGER', isNotNullable: true, val: -2},
128+ // {type: 'VARCHAR', isNotNullable: false, val: 'kkk'},
129+ // ])
130+ // })
131+ }
132+
133+
134+
135+ console . log ( 'dbScan:' , await client . scanDbEntries ( ) )
136+
137+
138+ await client . close ( )
139+ }
0 commit comments