diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 13cde29..9ae8d81 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -29,16 +29,16 @@ jobs: npm run build - name: Run overview showcase run: | - docker run -d --rm --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -p 3322:3322 --name immudb codenotary/immudb:1.4.1 - npx ts-node --esm ./immudb-node-showcase/src/overview-showcase.ts + docker run -d --rm --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -p 3322:3322 --name immudb codenotary/immudb:1.9.3 + npx tsx ./immudb-node-showcase/src/overview-showcase.ts docker stop immudb - name: Run sql showcase run: | - docker run -d --rm --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -p 3322:3322 --name immudb codenotary/immudb:1.4.1 - npx ts-node --esm ./immudb-node-showcase/src/sql-showcase.ts + docker run -d --rm --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -p 3322:3322 --name immudb codenotary/immudb:1.9.3 + npx tsx ./immudb-node-showcase/src/sql-showcase.ts docker stop immudb - name: Run zSet showcase run: | - docker run -d --rm --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -p 3322:3322 --name immudb codenotary/immudb:1.4.1 - npx ts-node --esm ./immudb-node-showcase/src/zSet-showcase.ts + docker run -d --rm --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -p 3322:3322 --name immudb codenotary/immudb:1.9.3 + npx tsx ./immudb-node-showcase/src/zSet-showcase.ts docker stop immudb diff --git a/immudb-node-grpcjs/schema.proto b/immudb-node-grpcjs/schema.proto index 0e611dd..7553084 100644 --- a/immudb-node-grpcjs/schema.proto +++ b/immudb-node-grpcjs/schema.proto @@ -694,6 +694,7 @@ message SQLQueryRequest { string sql = 1; repeated NamedParam params = 2; bool reuseSnapshot = 3; + bool acceptStream = 4; } message NamedParam { @@ -842,7 +843,7 @@ service ImmuService { rpc Rollback (google.protobuf.Empty) returns (google.protobuf.Empty){}; rpc TxSQLExec(SQLExecRequest) returns (google.protobuf.Empty) {}; - rpc TxSQLQuery(SQLQueryRequest) returns (SQLQueryResult) {}; + rpc TxSQLQuery(SQLQueryRequest) returns (stream SQLQueryResult) {}; rpc Login (LoginRequest) returns (LoginResponse){ option deprecated = true; @@ -1153,7 +1154,7 @@ service ImmuService { }; }; - rpc SQLQuery(SQLQueryRequest) returns (SQLQueryResult) { + rpc SQLQuery(SQLQueryRequest) returns (stream SQLQueryResult) { option (google.api.http) = { post: "/db/sqlquery" body: "*" diff --git a/immudb-node-grpcjs/src/immudb/schema/ImmuService.ts b/immudb-node-grpcjs/src/immudb/schema/ImmuService.ts index 33d5cb6..a5e27d0 100644 --- a/immudb-node-grpcjs/src/immudb/schema/ImmuService.ts +++ b/immudb-node-grpcjs/src/immudb/schema/ImmuService.ts @@ -402,14 +402,10 @@ export interface ImmuServiceClient extends grpc.Client { sqlExec(argument: _immudb_schema_SQLExecRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_immudb_schema_SQLExecResult__Output>): grpc.ClientUnaryCall; sqlExec(argument: _immudb_schema_SQLExecRequest, callback: grpc.requestCallback<_immudb_schema_SQLExecResult__Output>): grpc.ClientUnaryCall; - SQLQuery(argument: _immudb_schema_SQLQueryRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - SQLQuery(argument: _immudb_schema_SQLQueryRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - SQLQuery(argument: _immudb_schema_SQLQueryRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - SQLQuery(argument: _immudb_schema_SQLQueryRequest, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - sqlQuery(argument: _immudb_schema_SQLQueryRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - sqlQuery(argument: _immudb_schema_SQLQueryRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - sqlQuery(argument: _immudb_schema_SQLQueryRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - sqlQuery(argument: _immudb_schema_SQLQueryRequest, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; + SQLQuery(argument: _immudb_schema_SQLQueryRequest, metadata: grpc.Metadata, options?: grpc.CallOptions): grpc.ClientReadableStream<_immudb_schema_SQLQueryResult__Output>; + SQLQuery(argument: _immudb_schema_SQLQueryRequest, options?: grpc.CallOptions): grpc.ClientReadableStream<_immudb_schema_SQLQueryResult__Output>; + sqlQuery(argument: _immudb_schema_SQLQueryRequest, metadata: grpc.Metadata, options?: grpc.CallOptions): grpc.ClientReadableStream<_immudb_schema_SQLQueryResult__Output>; + sqlQuery(argument: _immudb_schema_SQLQueryRequest, options?: grpc.CallOptions): grpc.ClientReadableStream<_immudb_schema_SQLQueryResult__Output>; Scan(argument: _immudb_schema_ScanRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_immudb_schema_Entries__Output>): grpc.ClientUnaryCall; Scan(argument: _immudb_schema_ScanRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_immudb_schema_Entries__Output>): grpc.ClientUnaryCall; @@ -465,14 +461,10 @@ export interface ImmuServiceClient extends grpc.Client { txSqlExec(argument: _immudb_schema_SQLExecRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_google_protobuf_Empty__Output>): grpc.ClientUnaryCall; txSqlExec(argument: _immudb_schema_SQLExecRequest, callback: grpc.requestCallback<_google_protobuf_Empty__Output>): grpc.ClientUnaryCall; - TxSQLQuery(argument: _immudb_schema_SQLQueryRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - TxSQLQuery(argument: _immudb_schema_SQLQueryRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - TxSQLQuery(argument: _immudb_schema_SQLQueryRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - TxSQLQuery(argument: _immudb_schema_SQLQueryRequest, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - txSqlQuery(argument: _immudb_schema_SQLQueryRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - txSqlQuery(argument: _immudb_schema_SQLQueryRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - txSqlQuery(argument: _immudb_schema_SQLQueryRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; - txSqlQuery(argument: _immudb_schema_SQLQueryRequest, callback: grpc.requestCallback<_immudb_schema_SQLQueryResult__Output>): grpc.ClientUnaryCall; + TxSQLQuery(argument: _immudb_schema_SQLQueryRequest, metadata: grpc.Metadata, options?: grpc.CallOptions): grpc.ClientReadableStream<_immudb_schema_SQLQueryResult__Output>; + TxSQLQuery(argument: _immudb_schema_SQLQueryRequest, options?: grpc.CallOptions): grpc.ClientReadableStream<_immudb_schema_SQLQueryResult__Output>; + txSqlQuery(argument: _immudb_schema_SQLQueryRequest, metadata: grpc.Metadata, options?: grpc.CallOptions): grpc.ClientReadableStream<_immudb_schema_SQLQueryResult__Output>; + txSqlQuery(argument: _immudb_schema_SQLQueryRequest, options?: grpc.CallOptions): grpc.ClientReadableStream<_immudb_schema_SQLQueryResult__Output>; TxScan(argument: _immudb_schema_TxScanRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_immudb_schema_TxList__Output>): grpc.ClientUnaryCall; TxScan(argument: _immudb_schema_TxScanRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_immudb_schema_TxList__Output>): grpc.ClientUnaryCall; @@ -750,7 +742,7 @@ export interface ImmuServiceHandlers extends grpc.UntypedServiceImplementation { SQLExec: grpc.handleUnaryCall<_immudb_schema_SQLExecRequest__Output, _immudb_schema_SQLExecResult>; - SQLQuery: grpc.handleUnaryCall<_immudb_schema_SQLQueryRequest__Output, _immudb_schema_SQLQueryResult>; + SQLQuery: grpc.handleServerStreamingCall<_immudb_schema_SQLQueryRequest__Output, _immudb_schema_SQLQueryResult>; Scan: grpc.handleUnaryCall<_immudb_schema_ScanRequest__Output, _immudb_schema_Entries>; @@ -764,7 +756,7 @@ export interface ImmuServiceHandlers extends grpc.UntypedServiceImplementation { TxSQLExec: grpc.handleUnaryCall<_immudb_schema_SQLExecRequest__Output, _google_protobuf_Empty>; - TxSQLQuery: grpc.handleUnaryCall<_immudb_schema_SQLQueryRequest__Output, _immudb_schema_SQLQueryResult>; + TxSQLQuery: grpc.handleServerStreamingCall<_immudb_schema_SQLQueryRequest__Output, _immudb_schema_SQLQueryResult>; TxScan: grpc.handleUnaryCall<_immudb_schema_TxScanRequest__Output, _immudb_schema_TxList>; diff --git a/immudb-node-grpcjs/src/immudb/schema/SQLQueryRequest.ts b/immudb-node-grpcjs/src/immudb/schema/SQLQueryRequest.ts index 7a59306..9b75205 100644 --- a/immudb-node-grpcjs/src/immudb/schema/SQLQueryRequest.ts +++ b/immudb-node-grpcjs/src/immudb/schema/SQLQueryRequest.ts @@ -6,10 +6,12 @@ export interface SQLQueryRequest { 'sql'?: (string); 'params'?: (_immudb_schema_NamedParam)[]; 'reuseSnapshot'?: (boolean); + 'acceptStream'?: (boolean); } export interface SQLQueryRequest__Output { 'sql': (string); 'params': (_immudb_schema_NamedParam__Output)[]; 'reuseSnapshot': (boolean); + 'acceptStream': (boolean); } diff --git a/immudb-node-showcase/src/overview-showcase.ts b/immudb-node-showcase/src/overview-showcase.ts index 112c626..2d8d4cc 100644 --- a/immudb-node-showcase/src/overview-showcase.ts +++ b/immudb-node-showcase/src/overview-showcase.ts @@ -1,28 +1,24 @@ import Long from 'long' import { - Client, - verifyVerification, - types, - stream, + Client, + verifyVerification, + types, + stream, } from '@codenotary/immudb-node' - - - - overviewSchowcase() -.catch(console.error) + .catch(console.error) async function overviewSchowcase() { - + const client = new Client({ - host: '127.0.0.1', - port: 3322, - user: 'immudb', - password: 'immudb', - database: 'defaultdb', + host: '127.0.0.1', + port: 3322, + user: 'immudb', + password: 'immudb', + database: 'defaultdb', }) @@ -35,8 +31,8 @@ async function overviewSchowcase() { const valEntries2 = await client.setValEntries({ kvms: [ - {key: Buffer.of(0), val: Buffer.of(0)}, - {key: Buffer.of(1), val: Buffer.of(1)}, + { key: Buffer.of(0), val: Buffer.of(0) }, + { key: Buffer.of(1), val: Buffer.of(1) }, ] }) console.log('valEntries2:') @@ -45,7 +41,7 @@ async function overviewSchowcase() { const valEntry3 = await client.setValEntries({ kvms: [ - {key: Buffer.of(2), val: Buffer.of(2)}, + { key: Buffer.of(2), val: Buffer.of(2) }, ] }) console.log('valEntry3:') @@ -53,19 +49,19 @@ async function overviewSchowcase() { const refEntry4 = await client.setRefEntry({ - key: Buffer.of(3), + key: Buffer.of(3), referToKey: valEntries2.valEntries[0].key, - keyTxId: valEntries2.valEntries[0].id, - boundRef: true, + keyTxId: valEntries2.valEntries[0].id, + boundRef: true, }) console.log('refEntry4:') console.log(refEntry4) const zSetEntry5 = await client.setZSetEntry({ - zSet: Buffer.of(4), - referredKey: valEntry3.valEntries[0].key, - referredKeyScore: 3, + zSet: Buffer.of(4), + referredKey: valEntry3.valEntries[0].key, + referredKeyScore: 3, }) console.log('zSetEntry5:') console.log(zSetEntry5) @@ -73,19 +69,19 @@ async function overviewSchowcase() { const entries6 = await client.setValRefZSetEntries({ ops: [ { - type: 'val', - key: Buffer.of(2), - val: Buffer.of(6), + type: 'val', + key: Buffer.of(2), + val: Buffer.of(6), }, { - type: 'ref', - key: Buffer.of(3), + type: 'ref', + key: Buffer.of(3), referToKey: valEntry3.valEntries[0].key }, { - type: 'zSet', + type: 'zSet', referredKey: valEntries2.valEntries[1].key, - zSet: zSetEntry5.zSetTxEntry.zSet, + zSet: zSetEntry5.zSetTxEntry.zSet, referredKeyScore: 9, } ] @@ -126,7 +122,8 @@ async function overviewSchowcase() { console.log(stream.toKVEntries(Buffer.concat(buffs))) - const sqlExecCreateTable7 = await client.sqlExec({sql: ` + const sqlExecCreateTable7 = await client.sqlExec({ + sql: ` create table if not exists testtable ( id1 integer not null, id2 varchar[3] null, @@ -140,7 +137,8 @@ async function overviewSchowcase() { console.log(sqlExecCreateTable7) - const sqlExecUpsert8 = await client.sqlExec({sql: ` + const sqlExecUpsert8 = await client.sqlExec({ + sql: ` upsert into testtable (id1, id2, created, data, isactive) values @@ -153,15 +151,18 @@ async function overviewSchowcase() { const sqlTxAt8 = await client.executeSqlTx('ReadWrite', async (txApi) => { - const sqlQueryInTxAt8 = await txApi.query({sql: ` + const sqlQueryInTxAt8 = await txApi.query({ + sql: ` select * from testtable; `}) console.log('sqlQueryInTxAt8') - console.log(sqlQueryInTxAt8) - + for await (const row of sqlQueryInTxAt8) { + console.log(row) + } // sqlExecUpsert9 - const sqlExecUpsertInTx9 = txApi.exec({sql:` + const sqlExecUpsertInTx9 = txApi.exec({ + sql: ` upsert into testtable (id1, id2, created, data, isactive) values @@ -170,11 +171,14 @@ async function overviewSchowcase() { `}) - const sqlQueryInTxAt9 = await txApi.query({sql: ` + const sqlQueryInTxAt9 = await txApi.query({ + sql: ` select * from testtable; `}) console.log('sqlQueryInTxAt9') - console.log(sqlQueryInTxAt9) + for await (const row of sqlQueryInTxAt9) { + console.log(row) + } throw 'I would like to cancel' @@ -184,18 +188,19 @@ async function overviewSchowcase() { console.log(sqlTxAt8) - const sqlQueryAt8 = await client.sqlQuery({sql: ` + const sqlQueryAt8 = await client.sqlQuery({ + sql: ` select * from testtable; `}) console.log('sqlQueryInTxAt8') console.log(sqlQueryAt8) - const k = sqlQueryAt8[0] - const d = k[0] - - + + const row = await sqlQueryAt8.next() + console.log(row) + const dbScanAt8 = await client.scanDbEntries({ - scanStartAtTxId: Long.fromValue(1, true), + scanStartAtTxId: Long.fromValue(1, true), }) console.log('dbScanAt8') console.log(dbScanAt8) @@ -208,12 +213,12 @@ async function overviewSchowcase() { const setAndProof9 = await client.setValEntriesGetVerification({ - kvms: [{key: Buffer.from('yo'), val: Buffer.from('man')}], + kvms: [{ key: Buffer.from('yo'), val: Buffer.from('man') }], refTxId: stateAt8.txId, refHash: stateAt8.txHash, }) console.log('setAndProof9') - console.dir(setAndProof9, {depth: 10}) + console.dir(setAndProof9, { depth: 10 }) console.log('verifyVerification(setAndProof9) result:') console.log(verifyVerification(setAndProof9.verification)) @@ -232,7 +237,7 @@ async function overviewSchowcase() { refTxId: stateAt9.txId, }) console.log('getTx2AndVerification') - console.log(getTx2AndVerification, {depth: 10}) + console.log(getTx2AndVerification, { depth: 10 }) console.log('verifyVerification(getTx2AndVerification) result:') console.log(verifyVerification(getTx2AndVerification.verification)) @@ -241,12 +246,12 @@ async function overviewSchowcase() { // entries6 const getTx6AndVerification = await client.getTxAndVerification({ - txId: entries6.tx.id, + txId: entries6.tx.id, refHash: stateAt9.txHash, refTxId: stateAt9.txId, }) console.log('getTx6AndVerification') - console.log(getTx6AndVerification, {depth: 10}) + console.log(getTx6AndVerification, { depth: 10 }) console.log('verifyVerification(getTx6AndVerification) result:') console.log(verifyVerification(getTx6AndVerification.verification)) @@ -254,27 +259,27 @@ async function overviewSchowcase() { // sqlExecCreateTable7.subTxes[0].tx?.id const getTx7AndVerification = await client.getTxAndVerification({ - txId: Long.fromInt(7, true), + txId: Long.fromInt(7, true), refHash: stateAt9.txHash, refTxId: stateAt9.txId, }) console.log('getTx7AndVerification') - console.log(getTx7AndVerification, {depth: 10}) + console.log(getTx7AndVerification, { depth: 10 }) console.log('verifyVerification(getTx7AndVerification) result:') console.log(verifyVerification(getTx7AndVerification.verification)) // sqlExecUpsert8 const getTx8AndVerification = await client.getTxAndVerification({ - txId: Long.fromInt(8, true), + txId: Long.fromInt(8, true), refHash: stateAt9.txHash, refTxId: stateAt9.txId, }) console.log('getTx8AndVerification') - console.log(getTx8AndVerification, {depth: 10}) + console.log(getTx8AndVerification, { depth: 10 }) console.log('verifyVerification(getTx8AndVerification) result:') console.log(verifyVerification(getTx8AndVerification.verification)) - + diff --git a/immudb-node-showcase/src/sql-showcase.ts b/immudb-node-showcase/src/sql-showcase.ts index 8d4f4c4..15e35e2 100644 --- a/immudb-node-showcase/src/sql-showcase.ts +++ b/immudb-node-showcase/src/sql-showcase.ts @@ -7,41 +7,40 @@ */ import Long from 'long' import { - Client, - verifyVerification, - types, - stream, + Client, + verifyVerification, + types, + stream, } from '@codenotary/immudb-node' +sqlShowcase() + .catch(console.error) - - - -sqlSchowcase() -.catch(console.error) - -async function sqlSchowcase() { +async function sqlShowcase() { const client = new Client({ - host: '127.0.0.1', - port: 3322, - user: 'immudb', - password: 'immudb', - database: 'defaultdb', + host: '127.0.0.1', + port: 3322, + user: 'immudb', + password: 'immudb', + database: 'defaultdb', }) // since tx used for verification reference // cannot be first db transaction lets insert some dummy value: - const {valEntries: [dummyValEntry]} = await client.setValEntries({kvms: [ - {key: Buffer.of(0), val: Buffer.of(0)} - ]}) + const { valEntries: [dummyValEntry] } = await client.setValEntries({ + kvms: [ + { key: Buffer.of(0), val: Buffer.of(0) } + ] + }) // state will be dummyValEntry if database was empty const stateId2 = await client.getDbCurrentState() console.log('stateId2:', stateId2) - const {subTxes: [{tx: createTestTableTx}]} = await client.sqlExec({sql: ` + const { subTxes: [{ tx: createTestTableTx }] } = await client.sqlExec({ + sql: ` create table if not exists testtable ( id1 integer not null, id2 varchar[3] null, @@ -52,14 +51,15 @@ async function sqlSchowcase() { ); `}) console.log('createTestTableTx:', createTestTableTx) - - const {subTxes: [{ - tx: insertTestTableTx, + + const { subTxes: [{ + tx: insertTestTableTx, lastPK: insertTestTableLastPK, firstPK: insertTestTableFirstPK, updatedRowsCount: insertTestTableUpdatedRowsCount - }]} = await client.sqlExec({sql: ` + }] } = await client.sqlExec({ + sql: ` upsert into testtable (id1, id2, created, data, isactive) values @@ -72,34 +72,40 @@ async function sqlSchowcase() { console.log('insertTestTableFirstPK:', insertTestTableFirstPK) console.log('insertTestTableUpdatedRowsCount:', insertTestTableUpdatedRowsCount) + const res = await client.sqlQuery({ sql: 'SELECT * FROM testtable' }) + for await (let row of res) { + console.log(row); + } + // state at last sql insert (assuming empty db) const stateId4 = await client.getDbCurrentState() console.log('stateId4:', stateId4) - - const {valEntries: [dummyValEntry1]} = await client.setValEntries({kvms: [ - {key: Buffer.of(0), val: Buffer.of(1)} - ]}) + + const { valEntries: [dummyValEntry1] } = await client.setValEntries({ + kvms: [ + { key: Buffer.of(0), val: Buffer.of(1) } + ] + }) // state 1 transactions after last sql insert (assuming empty db) const stateId5 = await client.getDbCurrentState() console.log('stateId5:', stateId5) - const {valEntries: [dummyValEntry2]} = await client.setValEntries({kvms: [ - {key: Buffer.of(0), val: Buffer.of(2)} - ]}) + const { valEntries: [dummyValEntry2] } = await client.setValEntries({ + kvms: [ + { key: Buffer.of(0), val: Buffer.of(2) } + ] + }) // state 2 transactions after last sql insert (assuming empty db) const stateId6 = await client.getDbCurrentState() console.log('stateId6:', stateId6) - - - - if(createTestTableTx) { + if (createTestTableTx) { const createTestTableTxVer = await client.getTxAndVerification({ - txId: createTestTableTx.id, + txId: createTestTableTx.id, refHash: stateId5.txHash, refTxId: stateId5.txId, }) @@ -107,11 +113,9 @@ async function sqlSchowcase() { console.log('createTestTableTxVer has been verified.') } - - - if(insertTestTableTx) { + if (insertTestTableTx) { const insertTestTableTxVer = await client.getTxAndVerification({ - txId: insertTestTableTx.id, + txId: insertTestTableTx.id, refHash: stateId5.txHash, refTxId: stateId5.txId, }) @@ -130,10 +134,7 @@ async function sqlSchowcase() { // }) } - - console.log('dbScan:', await client.scanDbEntries()) - await client.close() } \ No newline at end of file diff --git a/immudb-node/src/immu-api/sql-query.ts b/immudb-node/src/immu-api/sql-query.ts index 49fcdbb..0490685 100644 --- a/immudb-node/src/immu-api/sql-query.ts +++ b/immudb-node/src/immu-api/sql-query.ts @@ -9,7 +9,7 @@ import * as igs from '../immu-grpc-sql/index.js' export type SqlQueryProps = { - + /** * Sql statements to execute. (May be multiple, all will be executed inside * automatic transaction.) @@ -37,44 +37,30 @@ export type SqlQueryProps = { } - - - export function createSqlQuery(client: igrpc.ImmuServiceClient) { - const sqlQueryGrpc = immuGrpc.unaryCall.createSqlQuery(client) + const sqlQueryGrpc = immuGrpc.readerCall.createSqlQuery(client) - return function sqlQuery(props: SqlQueryProps & { credentials: grpcjs.CallCredentials, }) { - - return sqlQueryGrpc({ + const gen = sqlQueryGrpc({ request: { - sql: props.sql, + sql: props.sql, params: props.params?.map(igs.sqlNamedValueToGrpcSqlNamedParam), - reuseSnapshot: props.reuseSnapshot - }, - options: { - credentials: props.credentials, + reuseSnapshot: props.reuseSnapshot, + acceptStream: true }, + credentials: props.credentials, }) - .then(maybeResponse => maybeResponse - ? maybeResponse - : Promise.reject('SQLQueryResult__Output must be defined') - ) - .then(grpcSqlRows => { - return igs.grpcQueryResultToListoOfSqlNamedValues(grpcSqlRows) - }) + return igs.generateRows(gen) } } - - export function createSqlQueryTables(client: igrpc.ImmuServiceClient) { const sqlQueryTablesGrpc = immuGrpc.unaryCall.createListTables(client) - + return function sqlQueryTables(props: { credentials: grpcjs.CallCredentials, }) { @@ -86,13 +72,13 @@ export function createSqlQueryTables(client: igrpc.ImmuServiceClient) { credentials: props.credentials, }, }) - .then(maybeResponse => maybeResponse - ? maybeResponse - : Promise.reject('SQLQueryResult__Output must be defined') - ) - .then(grpcSqlRows => { - return igs.grpcQueryResultToListoOfSqlNamedValues(grpcSqlRows) - }) + .then(maybeResponse => maybeResponse + ? maybeResponse + : Promise.reject('SQLQueryResult__Output must be defined') + ) + .then(grpcSqlRows => { + return igs.grpcQueryResultToListoOfSqlNamedValues(grpcSqlRows) + }) } } @@ -111,7 +97,7 @@ export type SqlQueryTableProps = { export function createSqlQueryTable(client: igrpc.ImmuServiceClient) { const sqlQueryTableGrpc = immuGrpc.unaryCall.createDescribeTable(client) - + return function sqlQueryTable(props: SqlQueryTableProps & { credentials: grpcjs.CallCredentials, }) { @@ -124,12 +110,12 @@ export function createSqlQueryTable(client: igrpc.ImmuServiceClient) { credentials: props.credentials, }, }) - .then(maybeResponse => maybeResponse - ? maybeResponse - : Promise.reject('SQLQueryResult__Output must be defined') - ) - .then(grpcSqlRows => { - return igs.grpcQueryResultToListoOfSqlNamedValues(grpcSqlRows) - }) + .then(maybeResponse => maybeResponse + ? maybeResponse + : Promise.reject('SQLQueryResult__Output must be defined') + ) + .then(grpcSqlRows => { + return igs.grpcQueryResultToListoOfSqlNamedValues(grpcSqlRows) + }) } } diff --git a/immudb-node/src/immu-api/sql-tx.ts b/immudb-node/src/immu-api/sql-tx.ts index feb5b10..778d295 100644 --- a/immudb-node/src/immu-api/sql-tx.ts +++ b/immudb-node/src/immu-api/sql-tx.ts @@ -37,17 +37,17 @@ export function createSqlTxNew(client: igrpc.ImmuServiceClient) { credentials: props.credentials, }, }) - .then(maybeResponse => maybeResponse - ? maybeResponse - : Promise.reject('NewTxResponse__Output must be defined') - ) - .then(grpcNewTxResponse => { - - const token: immu.TransactionTokens = { - transactionid: grpcNewTxResponse.transactionID - } - return token - }) + .then(maybeResponse => maybeResponse + ? maybeResponse + : Promise.reject('NewTxResponse__Output must be defined') + ) + .then(grpcNewTxResponse => { + + const token: immu.TransactionTokens = { + transactionid: grpcNewTxResponse.transactionID + } + return token + }) } } @@ -70,30 +70,30 @@ export function createSqlTxCommit(client: igrpc.ImmuServiceClient) { credentials: props.credentials, }, }) - .then(maybeResponse => maybeResponse - ? maybeResponse - : Promise.reject('CommittedSQLTx__Output must be defined') - ) - .then(grpcSqlCommitedTxResult => { - - // execution may not cause effects!!! - const tx = grpcSqlCommitedTxResult.header == undefined - ? undefined - : igt.grpcTxHeaderToTxCore(grpcSqlCommitedTxResult.header) - const updatedRowsCount = grpcSqlCommitedTxResult.updatedRows - const firstPK = igs.grpcSqlObjectNamedValueToNamedValues( - grpcSqlCommitedTxResult.firstInsertedPKs - ) - const lastPK = igs.grpcSqlObjectNamedValueToNamedValues( - grpcSqlCommitedTxResult.lastInsertedPKs + .then(maybeResponse => maybeResponse + ? maybeResponse + : Promise.reject('CommittedSQLTx__Output must be defined') ) - return { - tx, - firstPK, - lastPK, - updatedRowsCount, - } - }) + .then(grpcSqlCommitedTxResult => { + + // execution may not cause effects!!! + const tx = grpcSqlCommitedTxResult.header == undefined + ? undefined + : igt.grpcTxHeaderToTxCore(grpcSqlCommitedTxResult.header) + const updatedRowsCount = grpcSqlCommitedTxResult.updatedRows + const firstPK = igs.grpcSqlObjectNamedValueToNamedValues( + grpcSqlCommitedTxResult.firstInsertedPKs + ) + const lastPK = igs.grpcSqlObjectNamedValueToNamedValues( + grpcSqlCommitedTxResult.lastInsertedPKs + ) + return { + tx, + firstPK, + lastPK, + updatedRowsCount, + } + }) } } @@ -117,11 +117,11 @@ export function createSqlTxRollback(client: igrpc.ImmuServiceClient) { credentials: props.credentials, }, }) - .then(maybeResponse => maybeResponse - ? maybeResponse - : Promise.reject('Empty__Output must be defined') - ) - .then(res => {}) + .then(maybeResponse => maybeResponse + ? maybeResponse + : Promise.reject('Empty__Output must be defined') + ) + .then(res => { }) } } @@ -166,20 +166,20 @@ export type SqlTxExecProps = { * ``` */ params?: immu.SqlNamedValue[], - + } export function createSqlTxExec(client: igrpc.ImmuServiceClient) { const sqlTxExecGrpc = immuGrpc.unaryCall.createTxSqlExec(client) - + return function sqlTxExec(props: SqlTxExecProps & { credentials: grpcjs.CallCredentials, }) { return sqlTxExecGrpc({ request: { - sql: props.sql, + sql: props.sql, params: props.params?.map(igs.sqlNamedValueToGrpcSqlNamedParam), noWait: props.options?.dontWaitForIndexer, }, @@ -187,18 +187,18 @@ export function createSqlTxExec(client: igrpc.ImmuServiceClient) { credentials: props.credentials, }, }) - .then(maybeResponse => maybeResponse - ? maybeResponse - : Promise.reject('Empty__Output must be defined') - ) - .then(res => {}) + .then(maybeResponse => maybeResponse + ? maybeResponse + : Promise.reject('Empty__Output must be defined') + ) + .then(res => { }) } } export type SqlTxQueryProps = { - + /** * Sql statements to execute. (May be multiple, all will be executed inside * automatic transaction.) @@ -230,30 +230,21 @@ export type SqlTxQueryProps = { export function createSqlTxQuery(client: igrpc.ImmuServiceClient) { - const sqlTxQueryGrpc = immuGrpc.unaryCall.createTxSqlQuery(client) + const sqlTxQueryGrpc = immuGrpc.readerCall.createTxSqlQuery(client) + - return function sqlTxQuery(props: SqlTxQueryProps & { credentials: grpcjs.CallCredentials, }) { - return sqlTxQueryGrpc({ + const res = sqlTxQueryGrpc({ request: { - sql: props.sql, + sql: props.sql, params: props.params?.map(igs.sqlNamedValueToGrpcSqlNamedParam), reuseSnapshot: props.reuseSnapshot }, - options: { - credentials: props.credentials, - }, - }) - .then(maybeResponse => maybeResponse - ? maybeResponse - : Promise.reject('SQLQueryResult__Output must be defined') - ) - .then(grpcSqlRows => { - return igs.grpcQueryResultToListoOfSqlNamedValues(grpcSqlRows) + credentials: props.credentials, }) - + return igs.generateRows(res) } } \ No newline at end of file diff --git a/immudb-node/src/immu-client.ts b/immudb-node/src/immu-client.ts index 9eb32a7..6de0cad 100644 --- a/immudb-node/src/immu-client.ts +++ b/immudb-node/src/immu-client.ts @@ -14,86 +14,86 @@ import type * as immu from './types/index.js' function createImmuGrpcApi(grpcClient: igrpc.ImmuServiceClient) { return { // session - openSession: api.createOpenSession(grpcClient), - closeSession: api.createCloseSession(grpcClient), - keepAlive: api.createKeepAlive(grpcClient), - useDb: api.createUseDb(grpcClient), + openSession: api.createOpenSession(grpcClient), + closeSession: api.createCloseSession(grpcClient), + keepAlive: api.createKeepAlive(grpcClient), + useDb: api.createUseDb(grpcClient), // user - createUser: api.createCreateUser(grpcClient), - listUsers: api.createListUsers(grpcClient), - setUserActive: api.createSetUserActive(grpcClient), - setUserPassword: api.createSetUserPassword(grpcClient), - setUserDbPermissions: api.createSetUserDbPermissions(grpcClient), + createUser: api.createCreateUser(grpcClient), + listUsers: api.createListUsers(grpcClient), + setUserActive: api.createSetUserActive(grpcClient), + setUserPassword: api.createSetUserPassword(grpcClient), + setUserDbPermissions: api.createSetUserDbPermissions(grpcClient), // db - createDb: api.createCreateDb(grpcClient), - loadDb: api.createLoadDb(grpcClient), - unloadDb: api.createUnloadDb(grpcClient), - deleteDb: api.createDeleteDb(grpcClient), - flushDbIndex: api.createFlushDbIndex(grpcClient), - compactDbIndex: api.createCompactDbIndex(grpcClient), - listDbs: api.createListDbs(grpcClient), - getDbSettings: api.createGetDbSettings(grpcClient), - getDbCurrentState: api.createGetDbCurrentState(grpcClient), - setDbSettings: api.createSetDbSettings(grpcClient), + createDb: api.createCreateDb(grpcClient), + loadDb: api.createLoadDb(grpcClient), + unloadDb: api.createUnloadDb(grpcClient), + deleteDb: api.createDeleteDb(grpcClient), + flushDbIndex: api.createFlushDbIndex(grpcClient), + compactDbIndex: api.createCompactDbIndex(grpcClient), + listDbs: api.createListDbs(grpcClient), + getDbSettings: api.createGetDbSettings(grpcClient), + getDbCurrentState: api.createGetDbCurrentState(grpcClient), + setDbSettings: api.createSetDbSettings(grpcClient), // gets - scanValRefEntries: api.createScanValRefEntries(grpcClient), + scanValRefEntries: api.createScanValRefEntries(grpcClient), scanValRefEntriesStreaming: api.createScanValRefEntriesStreaming(grpcClient), - scanZEntries: api.createScanZEntries(grpcClient), - scanZEntriesStreaming: api.createScanZEntriesStreaming(grpcClient), - scanTxes: api.createScanTxes(grpcClient), - scanDbEntries: api.createScanDb(grpcClient), - scanHistory: api.createScanHistory(grpcClient), - scanHistoryStreaming: api.createScanHistoryStreaming(grpcClient), - getTxWithEntries: api.createGetTxWithEntries(grpcClient), - getTxGenericEntries: api.createGetTxGenericEntries(grpcClient), - getValRef: api.createGetValRef(grpcClient), - getValRefs: api.createGetValRefs(grpcClient), - getValRefStreaming: api.createGetValRefStreaming(grpcClient), + scanZEntries: api.createScanZEntries(grpcClient), + scanZEntriesStreaming: api.createScanZEntriesStreaming(grpcClient), + scanTxes: api.createScanTxes(grpcClient), + scanDbEntries: api.createScanDb(grpcClient), + scanHistory: api.createScanHistory(grpcClient), + scanHistoryStreaming: api.createScanHistoryStreaming(grpcClient), + getTxWithEntries: api.createGetTxWithEntries(grpcClient), + getTxGenericEntries: api.createGetTxGenericEntries(grpcClient), + getValRef: api.createGetValRef(grpcClient), + getValRefs: api.createGetValRefs(grpcClient), + getValRefStreaming: api.createGetValRefStreaming(grpcClient), // sets - setValRefZSetEntries: api.createSetEntries(grpcClient), + setValRefZSetEntries: api.createSetEntries(grpcClient), setValZSetEntriesStreaming: api.createSetEntriesStreaming(grpcClient), - setValEntries: api.createSetValEntries(grpcClient), - setValEntriesStreaming: api.createSetValEntriesStreaming(grpcClient), - setZSetEntry: api.createSetZSetEntry(grpcClient), - setRefEntry: api.createSetRefEntry(grpcClient), - deleteValRef: api.createDeleteValRef(grpcClient), - - + setValEntries: api.createSetValEntries(grpcClient), + setValEntriesStreaming: api.createSetValEntriesStreaming(grpcClient), + setZSetEntry: api.createSetZSetEntry(grpcClient), + setRefEntry: api.createSetRefEntry(grpcClient), + deleteValRef: api.createDeleteValRef(grpcClient), + + // sql - sqlExec: api.createSqlExec(grpcClient), - sqlQuery: api.createSqlQuery(grpcClient), - sqlQueryTable: api.createSqlQueryTable(grpcClient), - sqlQueryTables: api.createSqlQueryTables(grpcClient), - + sqlExec: api.createSqlExec(grpcClient), + sqlQuery: api.createSqlQuery(grpcClient), + sqlQueryTable: api.createSqlQueryTable(grpcClient), + sqlQueryTables: api.createSqlQueryTables(grpcClient), + // sql tx - sqlTxNew: api.createSqlTxNew(grpcClient), - sqlTxCommit: api.createSqlTxCommit(grpcClient), - sqlTxRollback: api.createSqlTxRollback(grpcClient), - sqlTxExec: api.createSqlTxExec(grpcClient), - sqlTxQuery: api.createSqlTxQuery(grpcClient), - + sqlTxNew: api.createSqlTxNew(grpcClient), + sqlTxCommit: api.createSqlTxCommit(grpcClient), + sqlTxRollback: api.createSqlTxRollback(grpcClient), + sqlTxExec: api.createSqlTxExec(grpcClient), + sqlTxQuery: api.createSqlTxQuery(grpcClient), + // instance - replicateTx: api.createReplicateTx(grpcClient), - exportTx: api.createExportTx(grpcClient), - - + replicateTx: api.createReplicateTx(grpcClient), + exportTx: api.createExportTx(grpcClient), + + // with verification - getTxAndVerification: api.createGetTxAndVerification(grpcClient), - getSqlRowEntryAndVerification: api.createGetSqlRowEntryAndVerification(grpcClient), - getValRefAndVerification: api.createGetValRefAndVerification(grpcClient), + getTxAndVerification: api.createGetTxAndVerification(grpcClient), + getSqlRowEntryAndVerification: api.createGetSqlRowEntryAndVerification(grpcClient), + getValRefAndVerification: api.createGetValRefAndVerification(grpcClient), - setValEntriesGetVerification: api.createSetValEntriesGetVerification(grpcClient), - setRefEntryGetVerification: api.createSetRefEntryGetVerification(grpcClient), - setZSetEntryGetVerification: api.createSetZSetEntryGetVerification(grpcClient), + setValEntriesGetVerification: api.createSetValEntriesGetVerification(grpcClient), + setRefEntryGetVerification: api.createSetRefEntryGetVerification(grpcClient), + setZSetEntryGetVerification: api.createSetZSetEntryGetVerification(grpcClient), } } @@ -106,22 +106,22 @@ function createImmuGrpcApi(grpcClient: igrpc.ImmuServiceClient) { export class Client { - private readonly conf: Config - private readonly immuGrpcClient: igrpc.ImmuServiceClient - private readonly immuGrpcApi: ReturnType - private sessionTokens?: immu.SessionTokens - private callCredentials?: grpcjs.CallCredentials + private readonly conf: Config + private readonly immuGrpcClient: igrpc.ImmuServiceClient + private readonly immuGrpcApi: ReturnType + private sessionTokens?: immu.SessionTokens + private callCredentials?: grpcjs.CallCredentials constructor(conf: Config) { this.conf = conf - - + + this.immuGrpcClient = igrpc.grpcClientFactory({ - address: buildAddress(this.conf), - credentials: grpcjs.credentials.createInsecure(), + address: buildAddress(this.conf), + credentials: grpcjs.credentials.createInsecure(), options: { - "grpc.max_send_message_length": 1<<4<<10<<10, // 32 MB - "grpc.max_receive_message_length": 1<<4<<10<<10, // 32 MB + "grpc.max_send_message_length": 1 << 4 << 10 << 10, // 32 MB + "grpc.max_receive_message_length": 1 << 4 << 10 << 10, // 32 MB } }) this.immuGrpcApi = createImmuGrpcApi(this.immuGrpcClient) @@ -131,7 +131,7 @@ export class Client { - + @@ -148,7 +148,7 @@ export class Client { * Gets and caches session tokens. */ private async getSessionTokens() { - if(this.sessionTokens) { + if (this.sessionTokens) { return this.sessionTokens } @@ -160,7 +160,7 @@ export class Client { * Creates and caches session credentials. */ private async getCallCredentials() { - if(this.callCredentials) { + if (this.callCredentials) { return this.callCredentials } @@ -187,7 +187,7 @@ export class Client { */ async keepAlive() { return this.immuGrpcApi.keepAlive({ - credentials: await this.getCallCredentials(), + credentials: await this.getCallCredentials(), }) } @@ -211,21 +211,21 @@ export class Client { ) { return this.immuGrpcApi.setValRefZSetEntries({ ...props, - credentials: await this.getCallCredentials() + credentials: await this.getCallCredentials() }) } - + /** * Sets multiple ValEntries or ZSetEntries in one transaction. */ async setValZSetEntriesStreaming( - props: api.SetEntriesStreamingProps + props: api.SetEntriesStreamingProps ) { return this.immuGrpcApi.setValZSetEntriesStreaming({ ...props, - credentials: await this.getCallCredentials() + credentials: await this.getCallCredentials() }) } @@ -237,25 +237,25 @@ export class Client { props: api.SetValEntryProps ) { return this.immuGrpcApi.setValEntries({ - kvms: props.kvms, - preconditions: props.preconditions, - options: props.options, - credentials: await this.getCallCredentials() + kvms: props.kvms, + preconditions: props.preconditions, + options: props.options, + credentials: await this.getCallCredentials() }) } - + /** * Sets multiple ValEntries in one transaction. */ async setValEntriesStreaming( - props: api.SetValEntriesStreamingProps + props: api.SetValEntriesStreamingProps ) { return this.immuGrpcApi.setValEntriesStreaming({ ...props, - credentials: await this.getCallCredentials() + credentials: await this.getCallCredentials() }) } @@ -267,7 +267,7 @@ export class Client { ) { return this.immuGrpcApi.setZSetEntry({ ...props, - credentials: await this.getCallCredentials() + credentials: await this.getCallCredentials() }) } @@ -280,12 +280,12 @@ export class Client { ) { return this.immuGrpcApi.setRefEntry({ ...props, - credentials: await this.getCallCredentials() + credentials: await this.getCallCredentials() }) } - + /** * Deletes keys or references to keys in one transaction. * @@ -305,8 +305,8 @@ export class Client { seenSinceTxId: props.seenSinceTxId, }) - - + + return delTxEntries } @@ -339,7 +339,7 @@ export class Client { * Scans database VEntries and RefEntries in one transaction. * Returns output as stream. */ - async scanValRefEntriesStreaming(props: api.ScanValRefEntriesProps) { + async scanValRefEntriesStreaming(props: api.ScanValRefEntriesProps) { return this.immuGrpcApi.scanValRefEntriesStreaming({ ...props, credentials: await this.getCallCredentials(), @@ -349,7 +349,7 @@ export class Client { /** * Scans database ZEntries in one transaction. */ - async scanZEntries(props: api.ScanZSetEntriesProps) { + async scanZEntries(props: api.ScanZSetEntriesProps) { return this.immuGrpcApi.scanZEntries({ ...props, credentials: await this.getCallCredentials(), @@ -367,7 +367,7 @@ export class Client { credentials: await this.getCallCredentials(), }) } - + /** @@ -456,12 +456,12 @@ export class Client { }) } - + /** * Gets value for provided key in one transaction. Key may refer to value or * reference. */ - async getValRefStreaming(props: api.GetValRefProps) { + async getValRefStreaming(props: api.GetValRefProps) { return this.immuGrpcApi.getValRefStreaming({ ...props, credentials: await this.getCallCredentials(), @@ -640,7 +640,7 @@ export class Client { async executeSqlTx( mode: "ReadOnly" | "WriteOnly" | "ReadWrite", run: (txApi: { - query(props: api.SqlTxQueryProps): Promise, + query(props: api.SqlTxQueryProps): Promise>, exec(props: api.SqlTxExecProps): Promise, }) => Promise, ) { @@ -664,38 +664,38 @@ export class Client { }) resolve(commitResult) } - + const errorRollBack = async (reason?: any) => { await this.immuGrpcApi.sqlTxRollback({ credentials: txCredentials, }) resolve('rolled back, reason: ' + reason) } - + const exec = async (props: api.SqlTxExecProps) => { await this.immuGrpcApi.sqlTxExec({ ...props, credentials: txCredentials, }) } - + const query = async (props: api.SqlTxQueryProps) => { - return await this.immuGrpcApi.sqlTxQuery({ + return this.immuGrpcApi.sqlTxQuery({ ...props, credentials: txCredentials, }) } - - + + return run({ exec, query, }) - .then(commit) - .catch(errorRollBack) + .then(commit) + .catch(errorRollBack) }) - + return await operation } @@ -703,7 +703,7 @@ export class Client { - + // ********************************************************** @@ -718,7 +718,7 @@ export class Client { /** * Creates immudb server user */ - async createUser(props: api.CreateUsersProps) { + async createUser(props: api.CreateUsersProps) { return this.immuGrpcApi.createUser({ ...props, credentials: await this.getCallCredentials(), @@ -758,7 +758,7 @@ export class Client { /** * Sets immudb server user password. */ - async setUserPassword(props: api.SetUserPasswordProps) { + async setUserPassword(props: api.SetUserPasswordProps) { return this.immuGrpcApi.setUserPassword({ ...props, credentials: await this.getCallCredentials(), @@ -769,7 +769,7 @@ export class Client { - + // ********************************************************** // db @@ -944,7 +944,7 @@ export class Client { * Gets ValEntry or RefEntry (and ref associated ValEntry) and its (val or * ref) verification structure by looking for ValEntry or RefEntry key. */ - async getValRefAndVerification(props: api.GetValRefAndVerificationProps) { + async getValRefAndVerification(props: api.GetValRefAndVerificationProps) { return this.immuGrpcApi.getValRefAndVerification({ ...props, credentials: await this.getCallCredentials(), @@ -963,7 +963,7 @@ export class Client { ) { return this.immuGrpcApi.setValEntriesGetVerification({ ...props, - credentials: await this.getCallCredentials() + credentials: await this.getCallCredentials() }) } @@ -973,12 +973,12 @@ export class Client { * * Returns RefEntry set and its verification structure. */ - async setRefEntryGetVerification( + async setRefEntryGetVerification( props: api.SetRefEntryProps & api.ProofRequestProps ) { return this.immuGrpcApi.setRefEntryGetVerification({ ...props, - credentials: await this.getCallCredentials() + credentials: await this.getCallCredentials() }) } @@ -988,12 +988,12 @@ export class Client { * * Returns ZSetEntry set and its verification structure. */ - async setZSetEntryGetVerification( + async setZSetEntryGetVerification( props: api.SetZSetEntryProps & api.ProofRequestProps ) { return this.immuGrpcApi.setZSetEntryGetVerification({ ...props, - credentials: await this.getCallCredentials() + credentials: await this.getCallCredentials() }) } } @@ -1018,38 +1018,38 @@ export class Client { /** * Session configuration. */ - export type Config = { +export type Config = { /** * Immudb server host address. */ - host: string, + host: string, /** * Immudb server port number. */ - port?: number, + port?: number, /** * Immudb server instance user name. */ - user: string, + user: string, /** * Immudb server instance user password. */ - password: string, + password: string, /** * Immudb server instance database name. */ - database: string, + database: string, } /** * Config for local develpoment. */ export const devConfig: Config = { - host: '127.0.0.1', - port: 3322, - user: 'immudb', - password: 'immudb', - database: 'defaultdb', + host: '127.0.0.1', + port: 3322, + user: 'immudb', + password: 'immudb', + database: 'defaultdb', } @@ -1073,7 +1073,7 @@ export const devConfig: Config = { * // '127.0.0.1:3322' * ``` */ - function buildAddress(conf: Config) { +function buildAddress(conf: Config) { const host = conf.host const port = conf.port != undefined ? ':' + conf.port : '' return host + port diff --git a/immudb-node/src/immu-grpc-sql/index.ts b/immudb-node/src/immu-grpc-sql/index.ts index c4c24af..e6100ce 100644 --- a/immudb-node/src/immu-grpc-sql/index.ts +++ b/immudb-node/src/immu-grpc-sql/index.ts @@ -2,7 +2,15 @@ import type * as immu from "../types/index.js"; import type * as igrpc from '@codenotary/immudb-node-grpcjs' +export async function* generateRows(gen: AsyncGenerator) { + for await (const res of gen) { + const rows = grpcQueryResultToListoOfSqlNamedValues(res) + for (const row of rows) { + yield row + } + } +} export function grpcSqlObjectNamedValueToNamedValues( objectNamedValue: { @@ -23,7 +31,7 @@ export function grpcSqlObjectNamedValueToNamedValues( export function grpcQueryResultToListoOfSqlNamedValues( queryResult: igrpc.SQLQueryResult__Output ): immu.SqlNamedValue[][] { - + return queryResult.rows.map(grpcSqlRowToSqlNamedValues) } @@ -42,19 +50,19 @@ export function grpcSqlRowToSqlNamedValues( export function sqlNamedValueToGrpcSqlNamedParam( param: immu.SqlNamedValue ): igrpc.NamedParam { - switch(param.type) { - case 'BOOLEAN': - return {name: param.name, value: { value: 'b', b: param.value}} - case 'BLOB': - return {name: param.name, value: { value: 'bs', bs: param.value}} - case 'INTEGER': - return {name: param.name, value: { value: 'n', n: param.value}} - case 'NULL': - return {name: param.name, value: { value: 'null' }} - case 'VARCHAR': - return {name: param.name, value: { value: 's', s: param.value}} - case 'TIMESTAMP': - return {name: param.name, value: { value: 'ts', ts: param.value}} + switch (param.type) { + case 'BOOLEAN': + return { name: param.name, value: { value: 'b', b: param.value } } + case 'BLOB': + return { name: param.name, value: { value: 'bs', bs: param.value } } + case 'INTEGER': + return { name: param.name, value: { value: 'n', n: param.value } } + case 'NULL': + return { name: param.name, value: { value: 'null' } } + case 'VARCHAR': + return { name: param.name, value: { value: 's', s: param.value } } + case 'TIMESTAMP': + return { name: param.name, value: { value: 'ts', ts: param.value } } } } @@ -63,19 +71,19 @@ export function sqlNamedValueToGrpcSqlNamedParam( export function sqlValueToGrpcSqlValue( param: immu.SqlValue ): igrpc.SQLValue__Output { - switch(param.type) { - case 'BOOLEAN': - return {value: 'b', b: param.value} - case 'BLOB': - return {value: 'bs', bs: param.value} - case 'INTEGER': - return {value: 'n', n: param.value} - case 'NULL': - return {value: 'null'} - case 'VARCHAR': - return {value: 's', s: param.value} - case 'TIMESTAMP': - return {value: 'ts', ts: param.value} + switch (param.type) { + case 'BOOLEAN': + return { value: 'b', b: param.value } + case 'BLOB': + return { value: 'bs', bs: param.value } + case 'INTEGER': + return { value: 'n', n: param.value } + case 'NULL': + return { value: 'null' } + case 'VARCHAR': + return { value: 's', s: param.value } + case 'TIMESTAMP': + return { value: 'ts', ts: param.value } } } @@ -85,8 +93,8 @@ export function sqlValueToGrpcSqlValue( export function grpcSqlNamedParamToSqlNamedValue( param: igrpc.NamedParam__Output ): immu.SqlNamedValue { - - if(param.value == undefined) { + + if (param.value == undefined) { throw 'grpc sql param must have value' } @@ -103,35 +111,35 @@ export function grpcSqlNamedParamToSqlNamedValue( export function grpcSqlValueToSqlValue( param: igrpc.SQLValue__Output ): immu.SqlValue { - - switch(param.value) { - case 'b': - if(param.b === undefined) { + + switch (param.value) { + case 'b': + if (param.b === undefined) { throw 'parsing grpc sql param error on BOOLEAN.' } - return {type: 'BOOLEAN', value: param.b} - case 'bs': - if(param.bs === undefined) { + return { type: 'BOOLEAN', value: param.b } + case 'bs': + if (param.bs === undefined) { throw 'parsing grpc sql param error on BLOB.' } - return {type: 'BLOB', value: param.bs} - case 'n': - if(param.n === undefined) { + return { type: 'BLOB', value: param.bs } + case 'n': + if (param.n === undefined) { throw 'parsing grpc sql param error on INTEGER.' } - return {type: 'INTEGER', value: param.n} - case 'null': - return {type: 'NULL', } - case 's': - if(param.s === undefined) { + return { type: 'INTEGER', value: param.n } + case 'null': + return { type: 'NULL', } + case 's': + if (param.s === undefined) { throw 'parsing grpc sql param error on VARCHAR.' } - return {type: 'VARCHAR', value: param.s} - case 'ts': - if(param.ts === undefined) { + return { type: 'VARCHAR', value: param.s } + case 'ts': + if (param.ts === undefined) { throw 'parsing grpc sql param error on TIMESTAMP.' } - return {type: 'TIMESTAMP', value: param.ts} + return { type: 'TIMESTAMP', value: param.ts } default: throw 'parsing grpc sql param error on value.' } diff --git a/immudb-node/src/immu-grpc/reader-call.ts b/immudb-node/src/immu-grpc/reader-call.ts index 62ebcf6..037a245 100644 --- a/immudb-node/src/immu-grpc/reader-call.ts +++ b/immudb-node/src/immu-grpc/reader-call.ts @@ -51,4 +51,24 @@ export function createExportTx(client: igrpc.ImmuServiceClient) { return promis.promisifyGrpcReaderAsync( client.exportTx.bind(client) ) -} \ No newline at end of file +} + +// ************************** +// SQL transactional +// ************************** + +export function createTxSqlQuery(client: igrpc.ImmuServiceClient) { + return promis.promisifyGrpcReaderAsync( + client.txSqlQuery.bind(client) + ) +} + +// ************************** +// SQL +// *** + +export function createSqlQuery(client: igrpc.ImmuServiceClient) { + return promis.promisifyGrpcReaderAsync( + client.sqlQuery.bind(client) + ) +} diff --git a/immudb-node/src/immu-grpc/unary-call.ts b/immudb-node/src/immu-grpc/unary-call.ts index 60de09b..93e81ee 100644 --- a/immudb-node/src/immu-grpc/unary-call.ts +++ b/immudb-node/src/immu-grpc/unary-call.ts @@ -45,13 +45,13 @@ export function createCommit(client: igrpc.ImmuServiceClient) { return promis.promisifyGrpcCall( client.commit.bind(client) ) -} +} export function createRollback(client: igrpc.ImmuServiceClient) { return promis.promisifyGrpcCall( client.rollback.bind(client) ) -} +} @@ -59,13 +59,7 @@ export function createTxSqlExec(client: igrpc.ImmuServiceClient) { return promis.promisifyGrpcCall( client.txSqlExec.bind(client) ) -} - -export function createTxSqlQuery(client: igrpc.ImmuServiceClient) { - return promis.promisifyGrpcCall( - client.txSqlQuery.bind(client) - ) -} +} // ************************** // SQL @@ -75,13 +69,7 @@ export function createSqlExec(client: igrpc.ImmuServiceClient) { return promis.promisifyGrpcCall( client.sqlExec.bind(client) ) -} - -export function createSqlQuery(client: igrpc.ImmuServiceClient) { - return promis.promisifyGrpcCall( - client.sqlQuery.bind(client) - ) -} +} // ************************** @@ -92,24 +80,24 @@ export function createListTables(client: igrpc.ImmuServiceClient) { return promis.promisifyGrpcCall( client.listTables.bind(client) ) -} +} export function createDescribeTable(client: igrpc.ImmuServiceClient) { return promis.promisifyGrpcCall( client.describeTable.bind(client) ) -} +} // ************************** // SQL and proof // ************************** export function createVerifiableSqlGet(client: igrpc.ImmuServiceClient) { - + return promis.promisifyGrpcCall( client.verifiableSqlGet.bind(client) ) -} +}