datastore-api 4.0.0 → 6.0.0

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.
@@ -5,39 +5,39 @@
5
5
  * Copyright (c) 2021, 2023 Dr. Maximilian Dornseif
6
6
  */
7
7
  // @ts-nocheck
8
- import { Datastore, Key } from '@google-cloud/datastore'
9
- import Emulator from 'google-datastore-emulator'
10
- import { afterAll, assert, beforeAll, describe, expect, test } from 'vitest'
8
+ import { Datastore, Key } from '@google-cloud/datastore';
9
+ import Emulator from 'google-datastore-emulator';
10
+ import { afterAll, assert, beforeAll, describe, expect, test } from 'vitest';
11
11
 
12
- import { Dstore } from './dstore-api'
12
+ import { Dstore } from './dstore-api';
13
13
 
14
- process.env.GCLOUD_PROJECT = 'project-id' // Set the datastore project Id globally
15
- let emulator
14
+ process.env.GCLOUD_PROJECT = 'project-id'; // Set the datastore project Id globally
15
+ let emulator;
16
16
 
17
17
  beforeAll(async () => {
18
- emulator = new Emulator({ debug: false })
19
- await emulator.start()
20
- })
18
+ emulator = new Emulator({ debug: false });
19
+ await emulator.start();
20
+ });
21
21
 
22
22
  afterAll(async () => {
23
- await emulator.stop()
24
- })
23
+ await emulator.stop();
24
+ });
25
25
 
26
26
  function getDstore() {
27
- return new Dstore(new Datastore({ namespace: 'test', projectId: process.env.GCLOUD_PROJECT }))
27
+ return new Dstore(new Datastore({ namespace: 'test', projectId: process.env.GCLOUD_PROJECT }));
28
28
  }
29
29
 
30
30
  test('keySerialize', async () => {
31
- const kvStore = getDstore()
32
- assert.deepEqual(kvStore.key(['testYodel', 123]).path, ['testYodel', 123])
31
+ const kvStore = getDstore();
32
+ assert.deepEqual(kvStore.key(['testYodel', 123]).path, ['testYodel', 123]);
33
33
  assert.deepEqual(JSON.parse(JSON.stringify(kvStore.key(['testYodel', 123]))), {
34
34
  id: 123 as any, // typing in inconclusive here
35
35
  kind: 'testYodel',
36
36
  namespace: 'test',
37
37
  path: ['testYodel', 123],
38
- } as any)
39
- const ser = kvStore.keySerialize(kvStore.key(['testYodel', 123]))
40
- expect(ser).toMatchInlineSnapshot('"agByDwsSCXRlc3RZb2RlbBh7DKIBBHRlc3Q"')
38
+ } as any);
39
+ const ser = kvStore.keySerialize(kvStore.key(['testYodel', 123]));
40
+ expect(ser).toMatchInlineSnapshot('"agByDwsSCXRlc3RZb2RlbBh7DKIBBHRlc3Q"');
41
41
  expect(JSON.parse(JSON.stringify(kvStore.keyFromSerialized(ser)))).toMatchInlineSnapshot(`
42
42
  {
43
43
  "id": "123",
@@ -48,39 +48,39 @@ test('keySerialize', async () => {
48
48
  "123",
49
49
  ],
50
50
  }
51
- `)
52
- })
51
+ `);
52
+ });
53
53
 
54
54
  describe('Allocation', () => {
55
55
  test('allocateIds', async () => {
56
- const kvStore = getDstore()
57
- const keys = await kvStore.datastore.allocateIds(kvStore.datastore.key(['testYodel']), 2)
58
- expect(Array.isArray(keys)).toBeTruthy()
59
- expect(keys[0].length).toBe(2)
60
- expect(keys[0][0].kind).toBe('testYodel')
61
- expect(keys[0][0].id).toMatch(/\d+/)
62
- expect(keys?.[1]?.keys?.length).toBe(2)
63
- expect(keys[1].keys[0].partitionId.namespaceId).toMatchInlineSnapshot('"test"')
64
- expect(keys[1].keys[0].path[0].idType).toMatchInlineSnapshot('"id"')
65
- expect(keys[1].keys[0].path[0].kind).toMatchInlineSnapshot('"testYodel"')
66
- })
56
+ const kvStore = getDstore();
57
+ const keys = await kvStore.datastore.allocateIds(kvStore.datastore.key(['testYodel']), 2);
58
+ expect(Array.isArray(keys)).toBeTruthy();
59
+ expect(keys[0].length).toBe(2);
60
+ expect(keys[0][0].kind).toBe('testYodel');
61
+ expect(keys[0][0].id).toMatch(/\d+/);
62
+ expect(keys?.[1]?.keys?.length).toBe(2);
63
+ expect(keys[1].keys[0].partitionId.namespaceId).toMatchInlineSnapshot('"test"');
64
+ expect(keys[1].keys[0].path[0].idType).toMatchInlineSnapshot('"id"');
65
+ expect(keys[1].keys[0].path[0].kind).toMatchInlineSnapshot('"testYodel"');
66
+ });
67
67
 
68
68
  test('allocateOneId', async () => {
69
- const kvStore = getDstore()
70
- const id = await kvStore.allocateOneId()
71
- expect(id).toMatch(/\d+/)
72
- })
73
- })
69
+ const kvStore = getDstore();
70
+ const id = await kvStore.allocateOneId();
71
+ expect(id).toMatch(/\d+/);
72
+ });
73
+ });
74
74
 
75
75
  describe('Read', () => {
76
76
  test('get num_id', async () => {
77
- const kvStore = getDstore()
78
- const entity = { key: kvStore.key(['testYodel', 2]), data: { foo: 'bar' } }
77
+ const kvStore = getDstore();
78
+ const entity = { key: kvStore.key(['testYodel', 2]), data: { foo: 'bar' } };
79
79
  const entity2 = {
80
80
  key: kvStore.key(['testYodel', 3]),
81
81
  data: { foo: 'bar' },
82
- }
83
- const commitResponse = await kvStore.save([entity, entity2])
82
+ };
83
+ const commitResponse = await kvStore.save([entity, entity2]);
84
84
  // expect(isNumber(commitResponse?.[0]?.indexUpdates)).toBeTruthy();
85
85
  // expect(commitResponse).toMatchInlineSnapshot(`
86
86
  // Array [
@@ -122,22 +122,22 @@ describe('Read', () => {
122
122
  ],
123
123
  },
124
124
  }
125
- `)
125
+ `);
126
126
 
127
- const result = await kvStore.get(entity.key)
127
+ const result = await kvStore.get(entity.key);
128
128
  // get returns a single Entity
129
- expect(Array.isArray(result)).toBeFalsy()
130
- expect(result).toMatchInlineSnapshot('null')
129
+ expect(Array.isArray(result)).toBeFalsy();
130
+ expect(result).toMatchInlineSnapshot('null');
131
131
  // expect(kvStore.readKey(result)).toBeInstanceOf(Key);
132
132
 
133
- const result2 = await kvStore.getMulti([entity.key])
133
+ const result2 = await kvStore.getMulti([entity.key]);
134
134
  // getMulti returns a Array even for single keys
135
135
  expect(result2).toMatchInlineSnapshot(`
136
136
  [
137
137
  null,
138
138
  ]
139
- `)
140
- const result3 = await kvStore.getMulti([entity.key, kvStore.key(['testYodel', 3])])
139
+ `);
140
+ const result3 = await kvStore.getMulti([entity.key, kvStore.key(['testYodel', 3])]);
141
141
  // getMulti returns a Array with multiple keys
142
142
  // expect(Array.isArray(result)).toBeTruthy();
143
143
  expect(result3).toMatchInlineSnapshot(`
@@ -145,8 +145,8 @@ describe('Read', () => {
145
145
  null,
146
146
  null,
147
147
  ]
148
- `)
149
- const result4 = await kvStore.getMulti([entity.key, entity.key])
148
+ `);
149
+ const result4 = await kvStore.getMulti([entity.key, entity.key]);
150
150
  // getMulti returns a Array but collapses duplicate keys
151
151
  // expect(Array.isArray(result)).toBeTruthy();
152
152
  // Firestore in Datastore returns the entity once
@@ -157,9 +157,9 @@ describe('Read', () => {
157
157
  null,
158
158
  null,
159
159
  ]
160
- `)
160
+ `);
161
161
 
162
- const result5 = await kvStore.getMulti([entity.key, kvStore.key(['YodelNotThere', 3])])
162
+ const result5 = await kvStore.getMulti([entity.key, kvStore.key(['YodelNotThere', 3])]);
163
163
  // getMulti returns a Array but omits unknown keys
164
164
  // expect(Array.isArray(result)).toBeTruthy();
165
165
  expect(result5).toMatchInlineSnapshot(`
@@ -167,39 +167,39 @@ describe('Read', () => {
167
167
  null,
168
168
  null,
169
169
  ]
170
- `)
171
- const result6 = await kvStore.getMulti([])
170
+ `);
171
+ const result6 = await kvStore.getMulti([]);
172
172
  // getMulti returns a empty Array for an empty array
173
173
  // expect(Array.isArray(result)).toBeTruthy();
174
- expect(result6).toMatchInlineSnapshot('[]')
175
- })
176
- })
174
+ expect(result6).toMatchInlineSnapshot('[]');
175
+ });
176
+ });
177
177
 
178
178
  test('get name', async (t) => {
179
- const kvStore = getDstore()
179
+ const kvStore = getDstore();
180
180
  const entity = {
181
181
  key: kvStore.key(['testYodel', 'two']),
182
182
  data: { foo: 'bar' },
183
- }
184
- await kvStore.save([entity])
185
- const result = await kvStore.get(entity.key)
186
- expect(result?._keyStr).toMatchInlineSnapshot('"agByEgsSCXRlc3RZb2RlbCIDdHdvDKIBBHRlc3Q"')
187
- expect(result?.foo).toBe('bar')
188
- })
183
+ };
184
+ await kvStore.save([entity]);
185
+ const result = await kvStore.get(entity.key);
186
+ expect(result?._keyStr).toMatchInlineSnapshot('"agByEgsSCXRlc3RZb2RlbCIDdHdvDKIBBHRlc3Q"');
187
+ expect(result?.foo).toBe('bar');
188
+ });
189
189
 
190
190
  describe('query', async () => {
191
191
  test('raw', async () => {
192
- const kvStore = getDstore()
192
+ const kvStore = getDstore();
193
193
  const entity = {
194
194
  key: kvStore.key(['testYodel', '3']),
195
195
  data: { foo: 'bar', baz: 'baz' },
196
- }
196
+ };
197
197
 
198
- await kvStore.save([entity])
199
- const query = kvStore.datastore.createQuery('testYodel')
200
- query.limit(1)
201
- const [entities, runQueryInfo] = await kvStore.datastore.runQuery(query)
202
- expect(entities.length).toBe(1)
198
+ await kvStore.save([entity]);
199
+ const query = kvStore.datastore.createQuery('testYodel');
200
+ query.limit(1);
201
+ const [entities, runQueryInfo] = await kvStore.datastore.runQuery(query);
202
+ expect(entities.length).toBe(1);
203
203
  expect(entities).toMatchInlineSnapshot(`
204
204
  [
205
205
  {
@@ -215,17 +215,17 @@ describe('query', async () => {
215
215
  },
216
216
  },
217
217
  ]
218
- `)
219
- })
218
+ `);
219
+ });
220
220
 
221
221
  test('query', async () => {
222
- const kvStore = getDstore()
222
+ const kvStore = getDstore();
223
223
  const entity = {
224
224
  key: kvStore.key(['testYodel', '3']),
225
225
  data: { foo: 'bar', baz: 'baz' },
226
- }
226
+ };
227
227
 
228
- const saveResult = await kvStore.save([entity])
228
+ const saveResult = await kvStore.save([entity]);
229
229
  expect(saveResult).toMatchInlineSnapshot(`
230
230
  [
231
231
  {
@@ -242,7 +242,7 @@ describe('query', async () => {
242
242
  ],
243
243
  },
244
244
  ]
245
- `)
245
+ `);
246
246
  expect(await kvStore.get(entity.key)).toMatchInlineSnapshot(`
247
247
  {
248
248
  "_keyStr": "agByEAsSCXRlc3RZb2RlbCIBMwyiAQR0ZXN0",
@@ -258,13 +258,13 @@ describe('query', async () => {
258
258
  ],
259
259
  },
260
260
  }
261
- `)
261
+ `);
262
262
  // Give Datastore time to become consistent
263
- do {} while ((await kvStore.get(entity.key)) === null)
263
+ do {} while ((await kvStore.get(entity.key)) === null);
264
264
 
265
- const query = kvStore.createQuery('testYodel')
266
- query.limit(1)
267
- const [entities, runQueryInfo] = await kvStore.runQuery(query)
265
+ const query = kvStore.createQuery('testYodel');
266
+ query.limit(1);
267
+ const [entities, runQueryInfo] = await kvStore.runQuery(query);
268
268
  // expect(entities.length).toBe(1)
269
269
  expect(entities).toMatchInlineSnapshot(`
270
270
  [
@@ -282,112 +282,112 @@ describe('query', async () => {
282
282
  },
283
283
  },
284
284
  ]
285
- `)
285
+ `);
286
286
  expect(runQueryInfo).toMatchInlineSnapshot(`
287
287
  {
288
288
  "endCursor": "CioSJGoKcHJvamVjdC1pZHIPCxIJdGVzdFlvZGVsGAIMogEEdGVzdBgAIAA=",
289
289
  "moreResults": "MORE_RESULTS_AFTER_LIMIT",
290
290
  }
291
- `)
292
- expect(entities?.[0]?.foo).toBe('bar')
293
- expect(entities?.[0]?.[Datastore.KEY]?.kind).toBe('testYodel')
294
- expect(runQueryInfo?.moreResults).toBe('MORE_RESULTS_AFTER_LIMIT')
291
+ `);
292
+ expect(entities?.[0]?.foo).toBe('bar');
293
+ expect(entities?.[0]?.[Datastore.KEY]?.kind).toBe('testYodel');
294
+ expect(runQueryInfo?.moreResults).toBe('MORE_RESULTS_AFTER_LIMIT');
295
295
 
296
296
  // modern interface
297
- const [result2] = await kvStore.query('testYodel', [], 1, [], ['baz'])
298
- expect(result2.length).toBe(1)
297
+ const [result2] = await kvStore.query('testYodel', [], 1, [], ['baz']);
298
+ expect(result2.length).toBe(1);
299
299
  // foo is removed by selection
300
300
  expect(JSON.parse(JSON.stringify(result2?.[0]))).toMatchInlineSnapshot(`
301
301
  {
302
302
  "_keyStr": "agByEAsSCXRlc3RZb2RlbCIBMwyiAQR0ZXN0",
303
303
  "baz": "baz",
304
304
  }
305
- `)
305
+ `);
306
306
 
307
- const key = kvStore.readKey(result2?.[0])
308
- expect(key.id).toBe(entity.key.id)
309
- })
310
- })
307
+ const key = kvStore.readKey(result2?.[0]);
308
+ expect(key.id).toBe(entity.key.id);
309
+ });
310
+ });
311
311
 
312
312
  test('set', async () => {
313
313
  // expect.assertions(2);
314
- const kvStore = getDstore()
314
+ const kvStore = getDstore();
315
315
  const result = await kvStore.set(kvStore.key(['testYodel', '5e7']), {
316
316
  foo: 'bar',
317
- })
318
- expect(result.name).toBe('5e7')
319
- expect(result.kind).toBe('testYodel')
317
+ });
318
+ expect(result.name).toBe('5e7');
319
+ expect(result.kind).toBe('testYodel');
320
320
 
321
321
  // autogenerate key
322
- const result2 = await kvStore.set(kvStore.key(['testYodel']), { foo: 'bar' })
323
- expect(result2.kind).toBe('testYodel')
324
- })
322
+ const result2 = await kvStore.set(kvStore.key(['testYodel']), { foo: 'bar' });
323
+ expect(result2.kind).toBe('testYodel');
324
+ });
325
325
 
326
326
  test('save / upsert', async () => {
327
327
  // expect.assertions(2);
328
- const kvStore = getDstore()
328
+ const kvStore = getDstore();
329
329
  const entity = {
330
330
  key: kvStore.key(['testYodel', 3]),
331
331
  data: { foo: 'bar' } as any,
332
- }
333
- const result = await kvStore.save([entity])
332
+ };
333
+ const result = await kvStore.save([entity]);
334
334
  // const result2 = await kvStore.upsert([entity]);
335
- expect(result?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false)
336
- expect(entity.data._keyStr).toMatchInlineSnapshot('"agByDwsSCXRlc3RZb2RlbBgDDKIBBHRlc3Q"')
337
- expect(entity.data.foo).toBe('bar')
338
- expect(entity.data[Datastore.KEY].kind).toBe('testYodel')
339
- })
335
+ expect(result?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false);
336
+ expect(entity.data._keyStr).toMatchInlineSnapshot('"agByDwsSCXRlc3RZb2RlbBgDDKIBBHRlc3Q"');
337
+ expect(entity.data.foo).toBe('bar');
338
+ expect(entity.data[Datastore.KEY].kind).toBe('testYodel');
339
+ });
340
340
 
341
341
  test('update', async (t) => {
342
342
  // expect.assertions(3);
343
- const kvStore = getDstore()
344
- const keyName = `4insert${Math.random()}`
343
+ const kvStore = getDstore();
344
+ const keyName = `4insert${Math.random()}`;
345
345
  const entity = {
346
346
  key: kvStore.key(['testYodel', keyName]),
347
347
  data: { foo: 'bar' },
348
- }
348
+ };
349
349
  // const request = kvStore.update([entity]);
350
350
  // await expect(request).rejects.toThrowError(Error);
351
351
 
352
- await kvStore.save([entity])
353
- const result = await kvStore.update([entity])
354
- expect(result?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false)
355
- expect(result?.[0]?.mutationResults?.[0]?.key).toBe(null)
352
+ await kvStore.save([entity]);
353
+ const result = await kvStore.update([entity]);
354
+ expect(result?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false);
355
+ expect(result?.[0]?.mutationResults?.[0]?.key).toBe(null);
356
356
  // expect(result?.[0]?.indexUpdates).toBe(2);
357
- })
357
+ });
358
358
 
359
359
  test('insert / delete', async (t) => {
360
360
  // expect.assertions(2);
361
- const kvStore = getDstore()
362
- const testkey = kvStore.key(['testYodel', 4])
363
- await kvStore.delete([testkey])
361
+ const kvStore = getDstore();
362
+ const testkey = kvStore.key(['testYodel', 4]);
363
+ await kvStore.delete([testkey]);
364
364
  const entity = {
365
365
  key: testkey,
366
366
  data: { foo: 'bar' } as any,
367
- }
368
- const result = await kvStore.insert([entity])
367
+ };
368
+ const result = await kvStore.insert([entity]);
369
369
 
370
- expect(result?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false)
371
- expect(result?.[0]?.mutationResults?.[0]?.version).toMatch(/\d+/)
372
- expect(entity.data.foo).toBe('bar')
373
- expect(entity.key.path[0]).toBe('testYodel')
370
+ expect(result?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false);
371
+ expect(result?.[0]?.mutationResults?.[0]?.version).toMatch(/\d+/);
372
+ expect(entity.data.foo).toBe('bar');
373
+ expect(entity.key.path[0]).toBe('testYodel');
374
374
  // expect(result?.[0]?.indexUpdates).toBe(3);
375
375
 
376
- const result2 = await kvStore.delete([entity.key])
377
- expect(result2?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false)
378
- })
376
+ const result2 = await kvStore.delete([entity.key]);
377
+ expect(result2?.[0]?.mutationResults?.[0]?.conflictDetected).toBe(false);
378
+ });
379
379
 
380
380
  test('exception', async () => {
381
381
  // expect.assertions(2);
382
- const kvStore = getDstore()
382
+ const kvStore = getDstore();
383
383
  try {
384
384
  const result = await kvStore.set(kvStore.key(['testYodel', NaN]), {
385
385
  foo: 'bar',
386
- })
386
+ });
387
387
  } catch (e) {
388
- expect(e.stack).toMatch(/Dstore\.set/)
388
+ expect(e.stack).toMatch(/Dstore\.set/);
389
389
  }
390
- })
390
+ });
391
391
  // describe("Transactions", () => {
392
392
  // it("simple", async () => {
393
393
  // expect.assertions(2);