feat: support promise based signatures for execute method by alkatrivedi · Pull Request #2301 · googleapis/nodejs-spanner · GitHub
Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 64 additions & 34 deletions src/batch-transaction.ts
75 changes: 73 additions & 2 deletions system-test/spanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
ExecuteSqlRequest,
TimestampBounds,
MutationGroup,
ReadResponse,
RunResponse,
} from '../src/transaction';
import {Row} from '../src/partial-result-stream';
import {GetDatabaseConfig} from '../src/database';
Expand All @@ -52,6 +54,10 @@ const singer = require('../test/data/singer');
const music = singer.examples.spanner.music;
import {util} from 'protobufjs';
import Long = util.Long;
import {
CreateQueryPartitionsResponse,
CreateReadPartitionsResponse,
} from '../src/batch-transaction';
const fs = require('fs');

const SKIP_BACKUPS = process.env.SKIP_BACKUPS;
Expand Down Expand Up @@ -9697,7 +9703,7 @@ describe('Spanner', () => {
}
});

it('should create and execute a query partition', function (done) {
it('should create and execute a query partition using callback', function (done) {
if (IS_EMULATOR_ENABLED) {
this.skip();
}
Expand Down Expand Up @@ -9727,7 +9733,7 @@ describe('Spanner', () => {
});
});

it('should create and execute a read partition', function (done) {
it('should create and execute a read partition using callback', function (done) {
if (IS_EMULATOR_ENABLED) {
this.skip();
}
Expand Down Expand Up @@ -9758,6 +9764,71 @@ describe('Spanner', () => {
});
});
});

it('should create and execute a query partition using await', async function () {
if (IS_EMULATOR_ENABLED) {
this.skip();
}

const [transaction] = await DATABASE.createBatchTransaction();
const selectQuery = {
sql: 'SELECT * FROM TxnTable where Key = @id',
params: {
id: 'k998',
},
};

let row_count = 0;
try {
const [queryPartitions]: CreateQueryPartitionsResponse =
await transaction.createQueryPartitions(selectQuery);
assert.deepStrictEqual(queryPartitions.length, 1);

const promises = queryPartitions.map(async queryPartition => {
const [results]: RunResponse =
await transaction.execute(queryPartition);
row_count += results.map(row => row.toJSON()).length;
assert.strictEqual(row_count, 1);
});

await Promise.all(promises);
} catch (err) {
assert.ifError(err);
}
});

it('should create and execute a read partition using await', async function () {
if (IS_EMULATOR_ENABLED) {
this.skip();
}
const [transaction] = await DATABASE.createBatchTransaction();
const key = 'k998';
const QUERY = {
table: googleSqlTable.name,
// Set databoostenabled to true for enabling serveless analytics.
dataBoostEnabled: true,
keys: [key],
columns: ['Key'],
};

let read_row_count = 0;
try {
const [readPartitions]: CreateReadPartitionsResponse =
await transaction.createReadPartitions(QUERY);
assert.deepStrictEqual(readPartitions.length, 1);

const promises = readPartitions.map(async readPartition => {
const [results]: ReadResponse =
await transaction.execute(readPartition);
read_row_count += results.map(row => row.toJSON()).length;
assert.strictEqual(read_row_count, 1);
});

await Promise.all(promises);
} catch (err) {
assert.ifError(err);
}
});
});
});
});
Expand Down
13 changes: 12 additions & 1 deletion test/batch-transaction.ts
Loading