@naturalcycles/datastore-lib 4.17.0 → 4.18.1
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.
- package/dist/datastore.db.d.ts +3 -3
- package/dist/datastore.db.js +11 -12
- package/package.json +3 -3
- package/src/datastore.db.ts +31 -42
package/dist/datastore.db.d.ts
CHANGED
|
@@ -2,9 +2,9 @@ import type { Key, Transaction } from '@google-cloud/datastore';
|
|
|
2
2
|
import { Datastore } from '@google-cloud/datastore';
|
|
3
3
|
import type { CommonDB, CommonDBOptions, CommonDBReadOptions, CommonDBSaveOptions, CommonDBSupport, CommonDBTransactionOptions, DBQuery, DBTransaction, DBTransactionFn, RunQueryResult } from '@naturalcycles/db-lib';
|
|
4
4
|
import { BaseCommonDB } from '@naturalcycles/db-lib';
|
|
5
|
-
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
|
|
6
5
|
import type { CommonLogger } from '@naturalcycles/js-lib/log';
|
|
7
6
|
import { type ObjectWithId, type StringMap } from '@naturalcycles/js-lib/types';
|
|
7
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
8
8
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
9
9
|
import type { DatastoreDBCfg, DatastoreDBOptions, DatastoreDBReadOptions, DatastoreDBSaveOptions, DatastoreDBStreamOptions, DatastorePropertyStats, DatastoreStats } from './datastore.model.js';
|
|
10
10
|
/**
|
|
@@ -57,9 +57,9 @@ export declare class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
57
57
|
key(ds: Datastore, kind: string, id: string): Key;
|
|
58
58
|
private getDsKey;
|
|
59
59
|
private getIdFromKey;
|
|
60
|
-
createTable<ROW extends ObjectWithId>(_table: string, _schema:
|
|
60
|
+
createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchema<ROW>): Promise<void>;
|
|
61
61
|
getTables(): Promise<string[]>;
|
|
62
|
-
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<
|
|
62
|
+
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchema<ROW>>;
|
|
63
63
|
private getPRetryOptions;
|
|
64
64
|
/**
|
|
65
65
|
* Silently rollback the transaction.
|
package/dist/datastore.db.js
CHANGED
|
@@ -200,18 +200,17 @@ export class DatastoreDB extends BaseCommonDB {
|
|
|
200
200
|
};
|
|
201
201
|
}
|
|
202
202
|
streamQuery(dbQuery, _opt) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}).mapSync(r => this.mapId(r));
|
|
203
|
+
const ds = this.ds();
|
|
204
|
+
const q = dbQueryToDatastoreQuery(dbQuery, ds.createQuery(dbQuery.table));
|
|
205
|
+
const opt = {
|
|
206
|
+
logger: this.cfg.logger,
|
|
207
|
+
...this.cfg.streamOptions,
|
|
208
|
+
..._opt,
|
|
209
|
+
};
|
|
210
|
+
const readable = opt.experimentalCursorStream
|
|
211
|
+
? new DatastoreStreamReadable(q, opt)
|
|
212
|
+
: ds.runQueryStream(q, this.getRunQueryOptions(opt));
|
|
213
|
+
return Pipeline.from(readable).mapSync(r => this.mapId(r));
|
|
215
214
|
}
|
|
216
215
|
// https://github.com/GoogleCloudPlatform/nodejs-getting-started/blob/master/2-structured-data/books/model-datastore.js
|
|
217
216
|
/**
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/datastore-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.18.1",
|
|
5
5
|
"description": "Opinionated library to work with Google Datastore, implements CommonDB",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@google-cloud/datastore": "^10",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"@types/node": "^24",
|
|
14
|
-
"@naturalcycles/dev-lib": "20.
|
|
14
|
+
"@naturalcycles/dev-lib": "20.11.0"
|
|
15
15
|
},
|
|
16
16
|
"exports": {
|
|
17
17
|
".": "./dist/index.js"
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"main": "dist/index.js",
|
|
28
28
|
"types": "dist/index.d.ts",
|
|
29
29
|
"engines": {
|
|
30
|
-
"node": ">=
|
|
30
|
+
"node": ">=24.10.0"
|
|
31
31
|
},
|
|
32
32
|
"publishConfig": {
|
|
33
33
|
"access": "public"
|
package/src/datastore.db.ts
CHANGED
|
@@ -20,15 +20,6 @@ import { _chunk } from '@naturalcycles/js-lib/array/array.util.js'
|
|
|
20
20
|
import { _ms } from '@naturalcycles/js-lib/datetime/time.util.js'
|
|
21
21
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
22
22
|
import { _errorDataAppend, TimeoutError } from '@naturalcycles/js-lib/error/error.util.js'
|
|
23
|
-
import type {
|
|
24
|
-
JsonSchemaAny,
|
|
25
|
-
JsonSchemaBoolean,
|
|
26
|
-
JsonSchemaNull,
|
|
27
|
-
JsonSchemaNumber,
|
|
28
|
-
JsonSchemaObject,
|
|
29
|
-
JsonSchemaRootObject,
|
|
30
|
-
JsonSchemaString,
|
|
31
|
-
} from '@naturalcycles/js-lib/json-schema'
|
|
32
23
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
33
24
|
import { _omit } from '@naturalcycles/js-lib/object/object.util.js'
|
|
34
25
|
import type { PRetryOptions } from '@naturalcycles/js-lib/promise'
|
|
@@ -41,6 +32,7 @@ import {
|
|
|
41
32
|
type ObjectWithId,
|
|
42
33
|
type StringMap,
|
|
43
34
|
} from '@naturalcycles/js-lib/types'
|
|
35
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
44
36
|
import { boldWhite } from '@naturalcycles/nodejs-lib/colors'
|
|
45
37
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
46
38
|
import type {
|
|
@@ -310,21 +302,20 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
310
302
|
dbQuery: DBQuery<ROW>,
|
|
311
303
|
_opt?: DatastoreDBStreamOptions,
|
|
312
304
|
): Pipeline<ROW> {
|
|
313
|
-
|
|
314
|
-
|
|
305
|
+
const ds = this.ds()
|
|
306
|
+
const q = dbQueryToDatastoreQuery(dbQuery, ds.createQuery(dbQuery.table))
|
|
315
307
|
|
|
316
|
-
|
|
308
|
+
const opt = {
|
|
309
|
+
logger: this.cfg.logger,
|
|
310
|
+
...this.cfg.streamOptions,
|
|
311
|
+
..._opt,
|
|
312
|
+
}
|
|
317
313
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
..._opt,
|
|
322
|
-
}
|
|
314
|
+
const readable = opt.experimentalCursorStream
|
|
315
|
+
? new DatastoreStreamReadable<ROW>(q, opt)
|
|
316
|
+
: ds.runQueryStream(q, this.getRunQueryOptions(opt))
|
|
323
317
|
|
|
324
|
-
|
|
325
|
-
? new DatastoreStreamReadable<ROW>(q, opt)
|
|
326
|
-
: ds.runQueryStream(q, this.getRunQueryOptions(opt))
|
|
327
|
-
}).mapSync(r => this.mapId<ROW>(r))
|
|
318
|
+
return Pipeline.from<ROW>(readable).mapSync(r => this.mapId<ROW>(r))
|
|
328
319
|
}
|
|
329
320
|
|
|
330
321
|
// https://github.com/GoogleCloudPlatform/nodejs-getting-started/blob/master/2-structured-data/books/model-datastore.js
|
|
@@ -585,7 +576,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
585
576
|
|
|
586
577
|
override async createTable<ROW extends ObjectWithId>(
|
|
587
578
|
_table: string,
|
|
588
|
-
_schema:
|
|
579
|
+
_schema: JsonSchema<ROW>,
|
|
589
580
|
): Promise<void> {}
|
|
590
581
|
|
|
591
582
|
override async getTables(): Promise<string[]> {
|
|
@@ -594,12 +585,10 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
594
585
|
return statsArray.map(stats => stats.kind_name).filter(table => table && !table.startsWith('_'))
|
|
595
586
|
}
|
|
596
587
|
|
|
597
|
-
override async getTableSchema<ROW extends ObjectWithId>(
|
|
598
|
-
table: string,
|
|
599
|
-
): Promise<JsonSchemaRootObject<ROW>> {
|
|
588
|
+
override async getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchema<ROW>> {
|
|
600
589
|
const stats = await this.getTableProperties(table)
|
|
601
590
|
|
|
602
|
-
const s:
|
|
591
|
+
const s: JsonSchema<ROW> = {
|
|
603
592
|
$id: `${table}.schema.json`,
|
|
604
593
|
type: 'object',
|
|
605
594
|
properties: {
|
|
@@ -616,40 +605,40 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
|
|
|
616
605
|
const name = stats.property_name as keyof ROW
|
|
617
606
|
|
|
618
607
|
if (dtype === DatastoreType.Blob) {
|
|
619
|
-
s.properties[name] = {
|
|
608
|
+
s.properties![name] = {
|
|
620
609
|
instanceof: 'Buffer',
|
|
621
|
-
} as
|
|
610
|
+
} as JsonSchema<any, ROW[typeof name]>
|
|
622
611
|
} else if (dtype === DatastoreType.Text || dtype === DatastoreType.String) {
|
|
623
|
-
s.properties[name] = {
|
|
612
|
+
s.properties![name] = {
|
|
624
613
|
type: 'string',
|
|
625
|
-
} as
|
|
614
|
+
} as JsonSchema<ROW[typeof name]>
|
|
626
615
|
} else if (dtype === DatastoreType.EmbeddedEntity) {
|
|
627
|
-
s.properties[name] = {
|
|
616
|
+
s.properties![name] = {
|
|
628
617
|
type: 'object',
|
|
629
618
|
additionalProperties: true,
|
|
630
|
-
properties: {},
|
|
619
|
+
properties: {} as any,
|
|
631
620
|
required: [],
|
|
632
|
-
} as
|
|
621
|
+
} as JsonSchema<ROW[typeof name]>
|
|
633
622
|
} else if (dtype === DatastoreType.Integer) {
|
|
634
|
-
s.properties[name] = {
|
|
623
|
+
s.properties![name] = {
|
|
635
624
|
type: 'integer',
|
|
636
|
-
} as
|
|
625
|
+
} as JsonSchema<ROW[typeof name]>
|
|
637
626
|
} else if (dtype === DatastoreType.Float) {
|
|
638
|
-
s.properties[name] = {
|
|
627
|
+
s.properties![name] = {
|
|
639
628
|
type: 'number',
|
|
640
|
-
} as
|
|
629
|
+
} as JsonSchema<ROW[typeof name]>
|
|
641
630
|
} else if (dtype === DatastoreType.Boolean) {
|
|
642
|
-
s.properties[name] = {
|
|
631
|
+
s.properties![name] = {
|
|
643
632
|
type: 'boolean',
|
|
644
|
-
} as
|
|
633
|
+
} as JsonSchema<ROW[typeof name]>
|
|
645
634
|
} else if (dtype === DatastoreType.DATE_TIME) {
|
|
646
635
|
// Don't know how to map it properly
|
|
647
|
-
s.properties[name] = {} as
|
|
636
|
+
s.properties![name] = {} as JsonSchema<any>
|
|
648
637
|
} else if (dtype === DatastoreType.NULL) {
|
|
649
638
|
// check, maybe we can just skip this type and do nothing?
|
|
650
|
-
s.properties[name] ||= {
|
|
639
|
+
s.properties![name] ||= {
|
|
651
640
|
type: 'null',
|
|
652
|
-
} as
|
|
641
|
+
} as JsonSchema<ROW[typeof name]>
|
|
653
642
|
} else {
|
|
654
643
|
throw new Error(
|
|
655
644
|
`Unknown Datastore Type '${stats.property_type}' for ${table}.${name as string}`,
|