datastore-api 4.0.0 → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,30 +6,30 @@
6
6
  */
7
7
  // @ts-nocheck
8
8
  // import { Datastore, Key } from '@google-cloud/datastore';
9
- import { afterAll, assert, beforeAll, describe, expect, test } from 'vitest'
9
+ import { afterAll, assert, beforeAll, describe, expect, test } from 'vitest';
10
10
 
11
- import { Datastore } from '../mock'
11
+ import { Datastore } from '../mock';
12
12
 
13
- import { Dstore } from './dstore-api'
13
+ import { Dstore } from './dstore-api';
14
14
 
15
- process.env.GCLOUD_PROJECT = 'project-id' // Set the datastore project Id globally
16
- let emulator
15
+ process.env.GCLOUD_PROJECT = 'project-id'; // Set the datastore project Id globally
16
+ let emulator;
17
17
 
18
18
  function getDstore() {
19
- return new Dstore(new Datastore({ namespace: 'test', projectId: process.env.GCLOUD_PROJECT }))
19
+ return new Dstore(new Datastore({ namespace: 'test', projectId: process.env.GCLOUD_PROJECT }));
20
20
  }
21
21
 
22
22
  test('keySerialize', async () => {
23
- const kvStore = getDstore()
24
- assert.deepEqual(kvStore.key(['testYodel', 123]).path, ['testYodel', 123])
23
+ const kvStore = getDstore();
24
+ assert.deepEqual(kvStore.key(['testYodel', 123]).path, ['testYodel', 123]);
25
25
  assert.deepEqual(JSON.parse(JSON.stringify(kvStore.key(['testYodel', 123]))), {
26
26
  id: 123 as any, // typing in inconclusive here
27
27
  kind: 'testYodel',
28
28
  namespace: 'test',
29
29
  path: ['testYodel', 123],
30
- } as any)
31
- const ser = kvStore.keySerialize(kvStore.key(['testYodel', 123]))
32
- expect(ser).toMatchInlineSnapshot('"agByDwsSCXRlc3RZb2RlbBh7DKIBBHRlc3Q"')
30
+ } as any);
31
+ const ser = kvStore.keySerialize(kvStore.key(['testYodel', 123]));
32
+ expect(ser).toMatchInlineSnapshot('"agByDwsSCXRlc3RZb2RlbBh7DKIBBHRlc3Q"');
33
33
  expect(JSON.parse(JSON.stringify(kvStore.keyFromSerialized(ser)))).toMatchInlineSnapshot(`
34
34
  {
35
35
  "id": "123",
@@ -40,18 +40,18 @@ test('keySerialize', async () => {
40
40
  "123",
41
41
  ],
42
42
  }
43
- `)
44
- })
43
+ `);
44
+ });
45
45
  test('keyFromLegacyUrlsafe', async () => {
46
- const kvStore = getDstore()
47
- const ser = await kvStore.datastore.keyToLegacyUrlSafe(kvStore.key(['testYodel', 123]))
46
+ const kvStore = getDstore();
47
+ const ser = await kvStore.datastore.keyToLegacyUrlSafe(kvStore.key(['testYodel', 123]));
48
48
  expect(ser).toMatchInlineSnapshot(`
49
49
  [
50
50
  "agpwcm9qZWN0LWlkcg8LEgl0ZXN0WW9kZWwYewyiAQR0ZXN0",
51
51
  ]
52
- `)
52
+ `);
53
53
 
54
- const key = kvStore.datastore.keyFromLegacyUrlsafe(ser[0])
54
+ const key = kvStore.datastore.keyFromLegacyUrlsafe(ser[0]);
55
55
  expect(key).toMatchInlineSnapshot(`
56
56
  Key {
57
57
  "id": "123",
@@ -62,40 +62,40 @@ test('keyFromLegacyUrlsafe', async () => {
62
62
  "123",
63
63
  ],
64
64
  }
65
- `)
66
- expect(kvStore.datastore.isKey(key)).toBeTruthy()
67
- })
65
+ `);
66
+ expect(kvStore.datastore.isKey(key)).toBeTruthy();
67
+ });
68
68
 
69
69
  describe('Allocation', () => {
70
70
  test('allocateIds', async () => {
71
- const kvStore = getDstore()
72
- const keys = await kvStore.datastore.allocateIds(kvStore.datastore.key(['testYodel']), 2)
73
- expect(Array.isArray(keys)).toBeTruthy()
74
- expect(keys[0].length).toBe(2)
75
- expect(keys[0][0].kind).toBe('testYodel')
76
- expect(keys[0][0].id).toMatch(/\d+/)
77
- expect(keys?.[1]?.keys?.length).toBe(2)
78
- expect(keys[1].keys[0].partitionId.namespaceId).toMatchInlineSnapshot('"test"')
79
- expect(keys[1].keys[0].path[0].idType).toMatchInlineSnapshot('"id"')
80
- expect(keys[1].keys[0].path[0].kind).toMatchInlineSnapshot('"testYodel"')
81
- })
71
+ const kvStore = getDstore();
72
+ const keys = await kvStore.datastore.allocateIds(kvStore.datastore.key(['testYodel']), 2);
73
+ expect(Array.isArray(keys)).toBeTruthy();
74
+ expect(keys[0].length).toBe(2);
75
+ expect(keys[0][0].kind).toBe('testYodel');
76
+ expect(keys[0][0].id).toMatch(/\d+/);
77
+ expect(keys?.[1]?.keys?.length).toBe(2);
78
+ expect(keys[1].keys[0].partitionId.namespaceId).toMatchInlineSnapshot('"test"');
79
+ expect(keys[1].keys[0].path[0].idType).toMatchInlineSnapshot('"id"');
80
+ expect(keys[1].keys[0].path[0].kind).toMatchInlineSnapshot('"testYodel"');
81
+ });
82
82
 
83
83
  test('allocateOneId', async () => {
84
- const kvStore = getDstore()
85
- const id = await kvStore.allocateOneId()
86
- expect(id).toMatch(/\d+/)
87
- })
88
- })
84
+ const kvStore = getDstore();
85
+ const id = await kvStore.allocateOneId();
86
+ expect(id).toMatch(/\d+/);
87
+ });
88
+ });
89
89
 
90
90
  describe('Read', () => {
91
91
  test('get num_id', async () => {
92
- const kvStore = getDstore()
93
- const entity = { key: kvStore.key(['testYodel', 2]), data: { foo: 'bar' } }
92
+ const kvStore = getDstore();
93
+ const entity = { key: kvStore.key(['testYodel', 2]), data: { foo: 'bar' } };
94
94
  const entity2 = {
95
95
  key: kvStore.key(['testYodel', 3]),
96
96
  data: { foo: 'bar' },
97
- }
98
- const commitResponse = await kvStore.save([entity, entity2])
97
+ };
98
+ const commitResponse = await kvStore.save([entity, entity2]);
99
99
  // expect(isNumber(commitResponse?.[0]?.indexUpdates)).toBeTruthy();
100
100
  // expect(commitResponse).toMatchInlineSnapshot(`
101
101
  // Array [
@@ -137,22 +137,22 @@ describe('Read', () => {
137
137
  ],
138
138
  },
139
139
  }
140
- `)
140
+ `);
141
141
 
142
- const result = await kvStore.get(entity.key)
142
+ const result = await kvStore.get(entity.key);
143
143
  // get returns a single Entity
144
- expect(Array.isArray(result)).toBeFalsy()
145
- expect(result).toMatchInlineSnapshot('null')
144
+ expect(Array.isArray(result)).toBeFalsy();
145
+ expect(result).toMatchInlineSnapshot('null');
146
146
  // expect(kvStore.readKey(result)).toBeInstanceOf(Key);
147
147
 
148
- const result2 = await kvStore.getMulti([entity.key])
148
+ const result2 = await kvStore.getMulti([entity.key]);
149
149
  // getMulti returns a Array even for single keys
150
150
  expect(result2).toMatchInlineSnapshot(`
151
151
  [
152
152
  null,
153
153
  ]
154
- `)
155
- const result3 = await kvStore.getMulti([entity.key, kvStore.key(['testYodel', 3])])
154
+ `);
155
+ const result3 = await kvStore.getMulti([entity.key, kvStore.key(['testYodel', 3])]);
156
156
  // getMulti returns a Array with multiple keys
157
157
  // expect(Array.isArray(result)).toBeTruthy();
158
158
  expect(result3).toMatchInlineSnapshot(`
@@ -160,8 +160,8 @@ describe('Read', () => {
160
160
  null,
161
161
  null,
162
162
  ]
163
- `)
164
- const result4 = await kvStore.getMulti([entity.key, entity.key])
163
+ `);
164
+ const result4 = await kvStore.getMulti([entity.key, entity.key]);
165
165
  // getMulti returns a Array but collapses duplicate keys
166
166
  // expect(Array.isArray(result)).toBeTruthy();
167
167
  // Firestore in Datastore returns the entity once
@@ -172,9 +172,9 @@ describe('Read', () => {
172
172
  null,
173
173
  null,
174
174
  ]
175
- `)
175
+ `);
176
176
 
177
- const result5 = await kvStore.getMulti([entity.key, kvStore.key(['YodelNotThere', 3])])
177
+ const result5 = await kvStore.getMulti([entity.key, kvStore.key(['YodelNotThere', 3])]);
178
178
  // getMulti returns a Array but omits unknown keys
179
179
  // expect(Array.isArray(result)).toBeTruthy();
180
180
  expect(result5).toMatchInlineSnapshot(`
@@ -182,38 +182,38 @@ describe('Read', () => {
182
182
  null,
183
183
  null,
184
184
  ]
185
- `)
186
- const result6 = await kvStore.getMulti([])
185
+ `);
186
+ const result6 = await kvStore.getMulti([]);
187
187
  // getMulti returns a empty Array for an empty array
188
188
  // expect(Array.isArray(result)).toBeTruthy();
189
- expect(result6).toMatchInlineSnapshot('[]')
190
- })
189
+ expect(result6).toMatchInlineSnapshot('[]');
190
+ });
191
191
 
192
192
  test('get name', async (t) => {
193
- const kvStore = getDstore()
193
+ const kvStore = getDstore();
194
194
  const entity = {
195
195
  key: kvStore.key(['testYodel', 'two']),
196
196
  data: { foo: 'bar' },
197
- }
198
- await kvStore.save([entity])
199
- const result = await kvStore.get(entity.key)
200
- expect(result?._keyStr).toMatchInlineSnapshot('"agByEgsSCXRlc3RZb2RlbCIDdHdvDKIBBHRlc3Q"')
201
- expect(result?.foo).toBe('bar')
202
- })
203
- })
197
+ };
198
+ await kvStore.save([entity]);
199
+ const result = await kvStore.get(entity.key);
200
+ expect(result?._keyStr).toMatchInlineSnapshot('"agByEgsSCXRlc3RZb2RlbCIDdHdvDKIBBHRlc3Q"');
201
+ expect(result?.foo).toBe('bar');
202
+ });
203
+ });
204
204
  describe('query', async () => {
205
205
  test.skip('raw', async () => {
206
- const kvStore = getDstore()
206
+ const kvStore = getDstore();
207
207
  const entity = {
208
208
  key: kvStore.key(['testYodel', '3']),
209
209
  data: { foo: 'bar', baz: 'baz' },
210
- }
210
+ };
211
211
 
212
- await kvStore.save([entity])
213
- const query = kvStore.datastore.createQuery('testYodel')
214
- query.limit(1)
215
- const [entities, runQueryInfo] = await kvStore.datastore.runQuery(query)
216
- expect(entities.length).toBe(1)
212
+ await kvStore.save([entity]);
213
+ const query = kvStore.datastore.createQuery('testYodel');
214
+ query.limit(1);
215
+ const [entities, runQueryInfo] = await kvStore.datastore.runQuery(query);
216
+ expect(entities.length).toBe(1);
217
217
  expect(entities).toMatchInlineSnapshot(`
218
218
  [
219
219
  {
@@ -229,17 +229,17 @@ describe('query', async () => {
229
229
  },
230
230
  },
231
231
  ]
232
- `)
233
- })
232
+ `);
233
+ });
234
234
 
235
235
  test('query', async () => {
236
- const kvStore = getDstore()
236
+ const kvStore = getDstore();
237
237
  const entity = {
238
238
  key: kvStore.key(['testYodel', '3']),
239
239
  data: { foo: 'bar', baz: 'baz' },
240
- }
240
+ };
241
241
 
242
- const saveResult = await kvStore.save([entity])
242
+ const saveResult = await kvStore.save([entity]);
243
243
  expect(saveResult).toMatchInlineSnapshot(`
244
244
  [
245
245
  {
@@ -261,7 +261,7 @@ describe('query', async () => {
261
261
  ],
262
262
  },
263
263
  ]
264
- `)
264
+ `);
265
265
  expect(await kvStore.get(entity.key)).toMatchInlineSnapshot(`
266
266
  {
267
267
  "_keyStr": "agByEAsSCXRlc3RZb2RlbCIBMwyiAQR0ZXN0",
@@ -277,14 +277,14 @@ describe('query', async () => {
277
277
  ],
278
278
  },
279
279
  }
280
- `)
280
+ `);
281
281
 
282
282
  // Give Datastore time to become consistent
283
- do {} while ((await kvStore.get(entity.key)) === null)
283
+ do {} while ((await kvStore.get(entity.key)) === null);
284
284
 
285
- const query = kvStore.createQuery('testYodel')
286
- query.limit(1)
287
- const [entities, runQueryInfo] = await kvStore.runQuery(query)
285
+ const query = kvStore.createQuery('testYodel');
286
+ query.limit(1);
287
+ const [entities, runQueryInfo] = await kvStore.runQuery(query);
288
288
  // expect(entities.length).toBe(1)
289
289
  expect(entities).toMatchInlineSnapshot(`
290
290
  [
@@ -303,19 +303,19 @@ describe('query', async () => {
303
303
  },
304
304
  },
305
305
  ]
306
- `)
306
+ `);
307
307
  expect(runQueryInfo).toMatchInlineSnapshot(`
308
308
  {
309
309
  "moreResults": "MORE_RESULTS_AFTER_LIMIT",
310
310
  }
311
- `)
312
- expect(entities?.[0]?.foo).toBe('bar')
313
- expect(entities?.[0]?.[Datastore.KEY]?.kind).toBe('testYodel')
314
- expect(runQueryInfo?.moreResults).toBe('MORE_RESULTS_AFTER_LIMIT')
311
+ `);
312
+ expect(entities?.[0]?.foo).toBe('bar');
313
+ expect(entities?.[0]?.[Datastore.KEY]?.kind).toBe('testYodel');
314
+ expect(runQueryInfo?.moreResults).toBe('MORE_RESULTS_AFTER_LIMIT');
315
315
 
316
316
  // modern interface
317
- const [result2] = await kvStore.query('testYodel', [], 1, [], ['baz'])
318
- expect(result2.length).toBe(1)
317
+ const [result2] = await kvStore.query('testYodel', [], 1, [], ['baz']);
318
+ expect(result2.length).toBe(1);
319
319
  // foo is removed by selection
320
320
  expect(JSON.parse(JSON.stringify(result2?.[0]))).toMatchInlineSnapshot(`
321
321
  {
@@ -323,92 +323,92 @@ describe('query', async () => {
323
323
  "baz": "baz",
324
324
  "foo": "bar",
325
325
  }
326
- `)
326
+ `);
327
327
 
328
- const key = kvStore.readKey(result2?.[0])
329
- expect(key.id).toBe(entity.key.id)
330
- })
331
- })
328
+ const key = kvStore.readKey(result2?.[0]);
329
+ expect(key.id).toBe(entity.key.id);
330
+ });
331
+ });
332
332
 
333
333
  test('set', async () => {
334
334
  // expect.assertions(2);
335
- const kvStore = getDstore()
335
+ const kvStore = getDstore();
336
336
  const result = await kvStore.set(kvStore.key(['testYodel', '5e7']), {
337
337
  foo: 'bar',
338
- })
339
- expect(result.name).toBe('5e7')
340
- expect(result.kind).toBe('testYodel')
338
+ });
339
+ expect(result.name).toBe('5e7');
340
+ expect(result.kind).toBe('testYodel');
341
341
 
342
342
  // autogenerate key
343
- const result2 = await kvStore.set(kvStore.key(['testYodel']), { foo: 'bar' })
344
- expect(result2.kind).toBe('testYodel')
345
- })
343
+ const result2 = await kvStore.set(kvStore.key(['testYodel']), { foo: 'bar' });
344
+ expect(result2.kind).toBe('testYodel');
345
+ });
346
346
 
347
347
  test('save / upsert', async () => {
348
348
  // expect.assertions(2);
349
- const kvStore = getDstore()
349
+ const kvStore = getDstore();
350
350
  const entity = {
351
351
  key: kvStore.key(['testYodel', 3]),
352
352
  data: { foo: 'bar' } as any,
353
- }
354
- const result = await kvStore.save([entity])
353
+ };
354
+ const result = await kvStore.save([entity]);
355
355
  // const result2 = await kvStore.upsert([entity]);
356
- expect(result?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false)
357
- expect(entity.data._keyStr).toMatchInlineSnapshot('"agByDwsSCXRlc3RZb2RlbBgDDKIBBHRlc3Q"')
358
- expect(entity.data.foo).toBe('bar')
359
- expect(entity.data[Datastore.KEY].kind).toBe('testYodel')
360
- })
356
+ expect(result?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false);
357
+ expect(entity.data._keyStr).toMatchInlineSnapshot('"agByDwsSCXRlc3RZb2RlbBgDDKIBBHRlc3Q"');
358
+ expect(entity.data.foo).toBe('bar');
359
+ expect(entity.data[Datastore.KEY].kind).toBe('testYodel');
360
+ });
361
361
 
362
362
  test('update', async (t) => {
363
363
  // expect.assertions(3);
364
- const kvStore = getDstore()
365
- const keyName = `4insert${Math.random()}`
364
+ const kvStore = getDstore();
365
+ const keyName = `4insert${Math.random()}`;
366
366
  const entity = {
367
367
  key: kvStore.key(['testYodel', keyName]),
368
368
  data: { foo: 'bar' },
369
- }
369
+ };
370
370
  // const request = kvStore.update([entity]);
371
371
  // await expect(request).rejects.toThrowError(Error);
372
372
 
373
- await kvStore.save([entity])
374
- const result = await kvStore.update([entity])
375
- expect(result?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false)
376
- expect(result?.[0]?.mutationResults?.[0]?.key).toBe(null)
373
+ await kvStore.save([entity]);
374
+ const result = await kvStore.update([entity]);
375
+ expect(result?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false);
376
+ expect(result?.[0]?.mutationResults?.[0]?.key).toBe(null);
377
377
  // expect(result?.[0]?.indexUpdates).toBe(2);
378
- })
378
+ });
379
379
 
380
380
  test('insert / delete', async (t) => {
381
381
  // expect.assertions(2);
382
- const kvStore = getDstore()
383
- const testkey = kvStore.key(['testYodel', 4])
384
- await kvStore.delete([testkey])
382
+ const kvStore = getDstore();
383
+ const testkey = kvStore.key(['testYodel', 4]);
384
+ await kvStore.delete([testkey]);
385
385
  const entity = {
386
386
  key: testkey,
387
387
  data: { foo: 'bar' } as any,
388
- }
389
- const result = await kvStore.insert([entity])
388
+ };
389
+ const result = await kvStore.insert([entity]);
390
390
 
391
- expect(result?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false)
392
- expect(result?.[0]?.mutationResults?.[0]?.version).toMatch(/\d+/)
393
- expect(entity.data.foo).toBe('bar')
394
- expect(entity.key.path[0]).toBe('testYodel')
391
+ expect(result?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false);
392
+ expect(result?.[0]?.mutationResults?.[0]?.version).toMatch(/\d+/);
393
+ expect(entity.data.foo).toBe('bar');
394
+ expect(entity.key.path[0]).toBe('testYodel');
395
395
  // expect(result?.[0]?.indexUpdates).toBe(3);
396
396
 
397
- const result2 = await kvStore.delete([entity.key])
398
- expect(result2?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false)
399
- })
397
+ const result2 = await kvStore.delete([entity.key]);
398
+ expect(result2?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false);
399
+ });
400
400
 
401
401
  test('exception', async () => {
402
402
  // expect.assertions(2);
403
- const kvStore = getDstore()
403
+ const kvStore = getDstore();
404
404
  try {
405
405
  const result = await kvStore.set(kvStore.key(['testYodel', NaN]), {
406
406
  foo: 'bar',
407
- })
407
+ });
408
408
  } catch (e) {
409
- expect(e.stack).toMatch(/Dstore\.set/)
409
+ expect(e.stack).toMatch(/Dstore\.set/);
410
410
  }
411
- })
411
+ });
412
412
  // describe("Transactions", () => {
413
413
  // it("simple", async () => {
414
414
  // expect.assertions(2);