Skip to content

Commit 7347a16

Browse files
authored
fix: type check for key in deleteRows (#2486)
1 parent 0047e94 commit 7347a16

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/table.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,22 @@ import {
4040
import {google} from '../protos/protos';
4141
import IsolationLevel = google.spanner.v1.TransactionOptions.IsolationLevel;
4242
import ReadLockMode = google.spanner.v1.TransactionOptions.ReadWrite.ReadLockMode;
43+
import {Int, SpannerDate, Numeric, Float32, Float} from './codec';
4344

44-
export type Key = string | string[];
45+
// valid scalar types that Spanner accepts in a Key
46+
export type KeyScalar =
47+
| string
48+
| number
49+
| boolean
50+
| SpannerDate
51+
| Buffer
52+
| Uint8Array
53+
| Int
54+
| Numeric
55+
| Float32
56+
| Float;
57+
58+
export type Key = KeyScalar | KeyScalar[];
4559

4660
export type CreateTableResponse = [
4761
Table,

test/spanner.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5067,6 +5067,41 @@ describe('Spanner with mock server', () => {
50675067
assert.strictEqual(commitRequest.mutations.length, 2);
50685068
});
50695069

5070+
it('should execute deleteRows with various key types', async () => {
5071+
const database = newTestDatabase();
5072+
const mutations = new MutationSet();
5073+
5074+
// define different types of keys
5075+
const intKey = 123;
5076+
const boolKey = true;
5077+
const floatKey = 3.14;
5078+
const bytesKey = Buffer.from('test-buffer');
5079+
const dateKey = new SpannerDate('2023-09-22');
5080+
const timestampKey = new PreciseDate('2023-09-22T10:00:00.123Z');
5081+
const numericKey = new Numeric('123.456');
5082+
5083+
// queue deletes for each type
5084+
mutations.deleteRows('IntTable', [intKey]);
5085+
mutations.deleteRows('BoolTable', [boolKey]);
5086+
mutations.deleteRows('FloatTable', [floatKey]);
5087+
mutations.deleteRows('BytesTable', [bytesKey]);
5088+
mutations.deleteRows('DateTable', [dateKey]);
5089+
mutations.deleteRows('TimestampTable', [timestampKey]);
5090+
mutations.deleteRows('NumericTable', [numericKey]);
5091+
5092+
// execute blind writes
5093+
await database.writeAtLeastOnce(mutations, {});
5094+
5095+
// get the request sent to mock server
5096+
const request = spannerMock.getRequests().find(val => {
5097+
return (val as v1.CommitRequest).mutations;
5098+
}) as v1.CommitRequest;
5099+
5100+
assert.strictEqual(request.mutations!.length, 7);
5101+
5102+
await database.close();
5103+
});
5104+
50705105
it('should apply blind writes only once with isolationLevel option', async () => {
50715106
const database = newTestDatabase();
50725107
const mutations = new MutationSet();

0 commit comments

Comments
 (0)