@naturalcycles/db-lib 10.30.0 → 10.31.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.
- package/dist/adapter/cachedb/cache.db.d.ts +3 -3
- package/dist/adapter/file/file.db.d.ts +2 -2
- package/dist/commondao/common.dao.d.ts +3 -3
- package/dist/commondao/common.dao.model.d.ts +1 -1
- package/dist/commondb/base.common.db.d.ts +3 -3
- package/dist/commondb/common.db.d.ts +4 -4
- package/dist/inmemory/inMemory.db.d.ts +3 -3
- package/dist/inmemory/inMemory.db.js +1 -1
- package/dist/testing/test.model.d.ts +1 -2
- package/dist/testing/test.model.js +8 -6
- package/package.json +2 -2
- package/src/adapter/cachedb/cache.db.ts +3 -5
- package/src/adapter/file/file.db.ts +3 -5
- package/src/commondao/common.dao.model.ts +1 -1
- package/src/commondao/common.dao.ts +3 -3
- package/src/commondb/base.common.db.ts +3 -5
- package/src/commondb/common.db.ts +4 -4
- package/src/inmemory/inMemory.db.ts +5 -10
- package/src/pipeline/dbPipelineRestore.ts +2 -2
- package/src/testing/test.model.ts +9 -8
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
|
|
2
1
|
import type { ObjectWithId } from '@naturalcycles/js-lib/types';
|
|
2
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
3
3
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
4
4
|
import { BaseCommonDB } from '../../commondb/base.common.db.js';
|
|
5
5
|
import type { CommonDB, CommonDBSupport } from '../../commondb/common.db.js';
|
|
@@ -21,8 +21,8 @@ export declare class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
21
21
|
* Resets InMemory DB data
|
|
22
22
|
*/
|
|
23
23
|
getTables(): Promise<string[]>;
|
|
24
|
-
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<
|
|
25
|
-
createTable<ROW extends ObjectWithId>(table: string, schema:
|
|
24
|
+
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchema<ROW>>;
|
|
25
|
+
createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchema<ROW>, opt?: CacheDBCreateOptions): Promise<void>;
|
|
26
26
|
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CacheDBSaveOptions<ROW>): Promise<ROW[]>;
|
|
27
27
|
saveBatch<ROW extends ObjectWithId>(table: string, rows: ROW[], opt?: CacheDBSaveOptions<ROW>): Promise<void>;
|
|
28
28
|
runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CacheDBSaveOptions<ROW>): Promise<RunQueryResult<ROW>>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
|
|
2
1
|
import { type ObjectWithId } from '@naturalcycles/js-lib/types';
|
|
2
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
3
3
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
4
4
|
import { BaseCommonDB } from '../../commondb/base.common.db.js';
|
|
5
5
|
import type { CommonDB, CommonDBSupport } from '../../commondb/common.db.js';
|
|
@@ -29,7 +29,7 @@ export declare class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
29
29
|
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBStreamOptions): Pipeline<ROW>;
|
|
30
30
|
deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
|
|
31
31
|
deleteByIds(table: string, ids: string[], _opt?: CommonDBOptions): Promise<number>;
|
|
32
|
-
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<
|
|
32
|
+
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchema<ROW>>;
|
|
33
33
|
loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]>;
|
|
34
34
|
saveFile<ROW extends ObjectWithId>(table: string, _rows: ROW[]): Promise<void>;
|
|
35
35
|
saveFiles<ROW extends ObjectWithId>(ops: DBSaveBatchOperation<ROW>[]): Promise<void>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Transform } from 'node:stream';
|
|
2
|
-
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
|
|
3
2
|
import { type BaseDBEntity, type NonNegativeInteger, type StringMap, type Unsaved } from '@naturalcycles/js-lib/types';
|
|
3
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
4
4
|
import { type Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
5
5
|
import type { CommonDBTransactionOptions, RunQueryResult } from '../db.model.js';
|
|
6
6
|
import type { DBQuery } from '../query/dbQuery.js';
|
|
@@ -138,8 +138,8 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
138
138
|
* Validates (unless `skipValidation=true` passed).
|
|
139
139
|
*/
|
|
140
140
|
private validateAndConvert;
|
|
141
|
-
getTableSchema(): Promise<
|
|
142
|
-
createTable(schema:
|
|
141
|
+
getTableSchema(): Promise<JsonSchema<any, DBM>>;
|
|
142
|
+
createTable(schema: JsonSchema<any, DBM>, opt?: CommonDaoCreateOptions): Promise<void>;
|
|
143
143
|
/**
|
|
144
144
|
* Proxy to this.cfg.db.ping
|
|
145
145
|
*/
|
|
@@ -89,7 +89,7 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
89
89
|
* It removes the knowledge from CommonDao about the validation library used
|
|
90
90
|
* and abstracts it away.
|
|
91
91
|
*/
|
|
92
|
-
validateBM?: ValidationFunction<BM, any>;
|
|
92
|
+
validateBM?: ValidationFunction<BM, BM, any>;
|
|
93
93
|
/**
|
|
94
94
|
* Used by e.g Datastore.
|
|
95
95
|
*/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
|
|
2
1
|
import type { ObjectWithId, StringMap } from '@naturalcycles/js-lib/types';
|
|
2
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
3
3
|
import type { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
4
4
|
import type { CommonDBOptions, CommonDBReadOptions, CommonDBSaveOptions, CommonDBTransactionOptions, DBTransaction, DBTransactionFn, RunQueryResult } from '../db.model.js';
|
|
5
5
|
import type { DBQuery } from '../query/dbQuery.js';
|
|
@@ -14,8 +14,8 @@ export declare class BaseCommonDB implements CommonDB {
|
|
|
14
14
|
support: CommonDBSupport;
|
|
15
15
|
ping(): Promise<void>;
|
|
16
16
|
getTables(): Promise<string[]>;
|
|
17
|
-
getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<
|
|
18
|
-
createTable<ROW extends ObjectWithId>(_table: string, _schema:
|
|
17
|
+
getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<JsonSchema<ROW>>;
|
|
18
|
+
createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchema<ROW>): Promise<void>;
|
|
19
19
|
getByIds<ROW extends ObjectWithId>(_table: string, _ids: string[]): Promise<ROW[]>;
|
|
20
20
|
deleteByQuery<ROW extends ObjectWithId>(_q: DBQuery<ROW>): Promise<number>;
|
|
21
21
|
patchByQuery<ROW extends ObjectWithId>(_q: DBQuery<ROW>, _patch: Partial<ROW>, _opt?: CommonDBOptions): Promise<number>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
|
|
2
1
|
import type { NonNegativeInteger, ObjectWithId, StringMap } from '@naturalcycles/js-lib/types';
|
|
2
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
3
3
|
import type { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
4
4
|
import type { CommonDBCreateOptions, CommonDBOptions, CommonDBReadOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBTransactionOptions, DBTransaction, DBTransactionFn, RunQueryResult } from '../db.model.js';
|
|
5
5
|
import type { DBQuery } from '../query/dbQuery.js';
|
|
@@ -13,7 +13,7 @@ export interface CommonDB {
|
|
|
13
13
|
*/
|
|
14
14
|
support: CommonDBSupport;
|
|
15
15
|
/**
|
|
16
|
-
* Checks that connection/credentials/etc is
|
|
16
|
+
* Checks that connection/credentials/etc is okay.
|
|
17
17
|
* Also acts as a "warmup request" for a DB.
|
|
18
18
|
* It SHOULD fail if DB setup is wrong (e.g on wrong credentials).
|
|
19
19
|
* It SHOULD succeed if e.g getByIds(['nonExistingKey']) doesn't throw.
|
|
@@ -29,12 +29,12 @@ export interface CommonDB {
|
|
|
29
29
|
*
|
|
30
30
|
* This is important for the code to rely on it, and it's verified by dbTest
|
|
31
31
|
*/
|
|
32
|
-
getTableSchema: <ROW extends ObjectWithId>(table: string) => Promise<
|
|
32
|
+
getTableSchema: <ROW extends ObjectWithId>(table: string) => Promise<JsonSchema<ROW>>;
|
|
33
33
|
/**
|
|
34
34
|
* Will do like `create table ...` for mysql.
|
|
35
35
|
* Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
|
|
36
36
|
*/
|
|
37
|
-
createTable: <ROW extends ObjectWithId>(table: string, schema:
|
|
37
|
+
createTable: <ROW extends ObjectWithId>(table: string, schema: JsonSchema<ROW>, opt?: CommonDBCreateOptions) => Promise<void>;
|
|
38
38
|
/**
|
|
39
39
|
* Order of items returned is not guaranteed to match order of ids.
|
|
40
40
|
* (Such limitation exists because Datastore doesn't support it).
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { type JsonSchemaObject, type JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
|
|
2
1
|
import type { CommonLogger } from '@naturalcycles/js-lib/log';
|
|
3
2
|
import { type AnyObjectWithId, type ObjectWithId, type StringMap } from '@naturalcycles/js-lib/types';
|
|
3
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
4
4
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
5
5
|
import type { CommonDB, CommonDBSupport } from '../commondb/common.db.js';
|
|
6
6
|
import { CommonDBType } from '../commondb/common.db.js';
|
|
@@ -47,8 +47,8 @@ export declare class InMemoryDB implements CommonDB {
|
|
|
47
47
|
*/
|
|
48
48
|
resetCache(_table?: string): Promise<void>;
|
|
49
49
|
getTables(): Promise<string[]>;
|
|
50
|
-
getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<
|
|
51
|
-
createTable<ROW extends ObjectWithId>(_table: string, _schema:
|
|
50
|
+
getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<JsonSchema<ROW>>;
|
|
51
|
+
createTable<ROW extends ObjectWithId>(_table: string, _schema: JsonSchema<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
|
|
52
52
|
getByIds<ROW extends ObjectWithId>(_table: string, ids: string[], _opt?: CommonDBOptions): Promise<ROW[]>;
|
|
53
53
|
multiGet<ROW extends ObjectWithId>(map: StringMap<string[]>, _opt?: CommonDBOptions): Promise<StringMap<ROW[]>>;
|
|
54
54
|
saveBatch<ROW extends ObjectWithId>(_table: string, rows: ROW[], opt?: CommonDBSaveOptions<ROW>): Promise<void>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { _isEmptyObject } from '@naturalcycles/js-lib';
|
|
2
2
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js';
|
|
3
|
-
import { generateJsonSchemaFromData
|
|
3
|
+
import { generateJsonSchemaFromData } from '@naturalcycles/js-lib/json-schema';
|
|
4
4
|
import { _deepCopy, _sortObjectDeep } from '@naturalcycles/js-lib/object';
|
|
5
5
|
import { _stringMapEntries, _stringMapValues, } from '@naturalcycles/js-lib/types';
|
|
6
6
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { JsonSchemaObject } from '@naturalcycles/js-lib/json-schema';
|
|
2
1
|
import type { BaseDBEntity } from '@naturalcycles/js-lib/types';
|
|
3
2
|
import { type ObjectSchema } from '@naturalcycles/nodejs-lib/joi';
|
|
4
3
|
export declare const TEST_TABLE = "TEST_TABLE";
|
|
@@ -21,7 +20,7 @@ export interface TestItemTM {
|
|
|
21
20
|
}
|
|
22
21
|
export declare const testItemBMSchema: ObjectSchema<TestItemBM>;
|
|
23
22
|
export declare const testItemTMSchema: ObjectSchema<TestItemTM>;
|
|
24
|
-
export declare const testItemBMJsonSchema:
|
|
23
|
+
export declare const testItemBMJsonSchema: any;
|
|
25
24
|
export declare function createTestItemDBM(num?: number): TestItemDBM;
|
|
26
25
|
export declare function createTestItemBM(num?: number): TestItemBM;
|
|
27
26
|
export declare function createTestItemsDBM(count?: number): TestItemDBM[];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { _range } from '@naturalcycles/js-lib/array/range.js';
|
|
2
|
-
import { j } from '@naturalcycles/
|
|
2
|
+
import { j } from '@naturalcycles/nodejs-lib/ajv';
|
|
3
3
|
import { baseDBEntitySchema, binarySchema, booleanSchema, numberSchema, objectSchema, stringSchema, } from '@naturalcycles/nodejs-lib/joi';
|
|
4
4
|
const MOCK_TS_2018_06_21 = 1529539200;
|
|
5
5
|
export const TEST_TABLE = 'TEST_TABLE';
|
|
@@ -19,18 +19,20 @@ export const testItemTMSchema = objectSchema({
|
|
|
19
19
|
even: booleanSchema.optional(),
|
|
20
20
|
});
|
|
21
21
|
export const testItemBMJsonSchema = j
|
|
22
|
-
.
|
|
22
|
+
.object({
|
|
23
23
|
// todo: figure out how to not copy-paste these 3 fields
|
|
24
24
|
id: j.string(), // todo: not strictly needed here
|
|
25
|
-
created: j.integer().unixTimestamp(),
|
|
26
|
-
updated: j.integer().unixTimestamp(),
|
|
25
|
+
created: j.number().integer().unixTimestamp(),
|
|
26
|
+
updated: j.number().integer().unixTimestamp(),
|
|
27
27
|
k1: j.string(),
|
|
28
|
-
k2: j.
|
|
28
|
+
k2: j.string().nullable().optional(),
|
|
29
29
|
k3: j.number().optional(),
|
|
30
30
|
even: j.boolean().optional(),
|
|
31
31
|
b1: j.buffer().optional(),
|
|
32
|
+
nested: j.object({ foo: j.number() }).optional(),
|
|
32
33
|
})
|
|
33
|
-
.
|
|
34
|
+
.dbEntity()
|
|
35
|
+
.isOfType()
|
|
34
36
|
.build();
|
|
35
37
|
export function createTestItemDBM(num = 1) {
|
|
36
38
|
return {
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/db-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "10.
|
|
4
|
+
"version": "10.31.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@naturalcycles/js-lib": "^15",
|
|
7
7
|
"@naturalcycles/nodejs-lib": "^15"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
|
-
"@naturalcycles/dev-lib": "
|
|
10
|
+
"@naturalcycles/dev-lib": "18.4.2"
|
|
11
11
|
},
|
|
12
12
|
"files": [
|
|
13
13
|
"dist",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { _isTruthy } from '@naturalcycles/js-lib'
|
|
2
|
-
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema'
|
|
3
2
|
import type { ObjectWithId, StringMap } from '@naturalcycles/js-lib/types'
|
|
3
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
4
4
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
5
5
|
import { BaseCommonDB } from '../../commondb/base.common.db.js'
|
|
6
6
|
import type { CommonDB, CommonDBSupport } from '../../commondb/common.db.js'
|
|
@@ -55,15 +55,13 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
55
55
|
return await this.cfg.downstreamDB.getTables()
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
override async getTableSchema<ROW extends ObjectWithId>(
|
|
59
|
-
table: string,
|
|
60
|
-
): Promise<JsonSchemaRootObject<ROW>> {
|
|
58
|
+
override async getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchema<ROW>> {
|
|
61
59
|
return await this.cfg.downstreamDB.getTableSchema<ROW>(table)
|
|
62
60
|
}
|
|
63
61
|
|
|
64
62
|
override async createTable<ROW extends ObjectWithId>(
|
|
65
63
|
table: string,
|
|
66
|
-
schema:
|
|
64
|
+
schema: JsonSchema<ROW>,
|
|
67
65
|
opt: CacheDBCreateOptions = {},
|
|
68
66
|
): Promise<void> {
|
|
69
67
|
if (!opt.onlyCache && !this.cfg.onlyCache) {
|
|
@@ -2,7 +2,6 @@ import { Readable } from 'node:stream'
|
|
|
2
2
|
import { _by, _sortBy } from '@naturalcycles/js-lib/array'
|
|
3
3
|
import { _since, localTime } from '@naturalcycles/js-lib/datetime'
|
|
4
4
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
5
|
-
import type { JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema'
|
|
6
5
|
import { generateJsonSchemaFromData } from '@naturalcycles/js-lib/json-schema'
|
|
7
6
|
import { _deepEquals, _filterUndefinedValues, _sortObjectDeep } from '@naturalcycles/js-lib/object'
|
|
8
7
|
import {
|
|
@@ -10,6 +9,7 @@ import {
|
|
|
10
9
|
type ObjectWithId,
|
|
11
10
|
type UnixTimestampMillis,
|
|
12
11
|
} from '@naturalcycles/js-lib/types'
|
|
12
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
13
13
|
import { dimGrey } from '@naturalcycles/nodejs-lib/colors'
|
|
14
14
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
15
15
|
import { BaseCommonDB } from '../../commondb/base.common.db.js'
|
|
@@ -174,14 +174,12 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
174
174
|
return deleted
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
-
override async getTableSchema<ROW extends ObjectWithId>(
|
|
178
|
-
table: string,
|
|
179
|
-
): Promise<JsonSchemaRootObject<ROW>> {
|
|
177
|
+
override async getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchema<ROW>> {
|
|
180
178
|
const rows = await this.loadFile(table)
|
|
181
179
|
return {
|
|
182
180
|
...generateJsonSchemaFromData(rows),
|
|
183
181
|
$id: `${table}.schema.json`,
|
|
184
|
-
}
|
|
182
|
+
} as any
|
|
185
183
|
}
|
|
186
184
|
|
|
187
185
|
// wrapper, to handle logging
|
|
@@ -109,7 +109,7 @@ export interface CommonDaoCfg<
|
|
|
109
109
|
* It removes the knowledge from CommonDao about the validation library used
|
|
110
110
|
* and abstracts it away.
|
|
111
111
|
*/
|
|
112
|
-
validateBM?: ValidationFunction<BM, any>
|
|
112
|
+
validateBM?: ValidationFunction<BM, BM, any>
|
|
113
113
|
|
|
114
114
|
/**
|
|
115
115
|
* Used by e.g Datastore.
|
|
@@ -3,7 +3,6 @@ import { _isTruthy } from '@naturalcycles/js-lib'
|
|
|
3
3
|
import { _uniqBy } from '@naturalcycles/js-lib/array/array.util.js'
|
|
4
4
|
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js'
|
|
5
5
|
import { _assert, ErrorMode } from '@naturalcycles/js-lib/error'
|
|
6
|
-
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema'
|
|
7
6
|
import { _deepJsonEquals } from '@naturalcycles/js-lib/object/deepEquals.js'
|
|
8
7
|
import {
|
|
9
8
|
_filterUndefinedValues,
|
|
@@ -22,6 +21,7 @@ import {
|
|
|
22
21
|
type Unsaved,
|
|
23
22
|
} from '@naturalcycles/js-lib/types'
|
|
24
23
|
import { stringId } from '@naturalcycles/nodejs-lib'
|
|
24
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
25
25
|
import {
|
|
26
26
|
type Pipeline,
|
|
27
27
|
transformChunk,
|
|
@@ -810,11 +810,11 @@ export class CommonDao<
|
|
|
810
810
|
return convertedValue
|
|
811
811
|
}
|
|
812
812
|
|
|
813
|
-
async getTableSchema(): Promise<
|
|
813
|
+
async getTableSchema(): Promise<JsonSchema<any, DBM>> {
|
|
814
814
|
return await this.cfg.db.getTableSchema<DBM>(this.cfg.table)
|
|
815
815
|
}
|
|
816
816
|
|
|
817
|
-
async createTable(schema:
|
|
817
|
+
async createTable(schema: JsonSchema<any, DBM>, opt?: CommonDaoCreateOptions): Promise<void> {
|
|
818
818
|
this.requireWriteAccess()
|
|
819
819
|
await this.cfg.db.createTable(this.cfg.table, schema, opt)
|
|
820
820
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema'
|
|
2
1
|
import type { ObjectWithId, StringMap } from '@naturalcycles/js-lib/types'
|
|
2
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
3
3
|
import type { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
4
4
|
import type {
|
|
5
5
|
CommonDBOptions,
|
|
@@ -32,15 +32,13 @@ export class BaseCommonDB implements CommonDB {
|
|
|
32
32
|
throw new Error('getTables is not implemented')
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
async getTableSchema<ROW extends ObjectWithId>(
|
|
36
|
-
_table: string,
|
|
37
|
-
): Promise<JsonSchemaRootObject<ROW>> {
|
|
35
|
+
async getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<JsonSchema<ROW>> {
|
|
38
36
|
throw new Error('getTableSchema is not implemented')
|
|
39
37
|
}
|
|
40
38
|
|
|
41
39
|
async createTable<ROW extends ObjectWithId>(
|
|
42
40
|
_table: string,
|
|
43
|
-
_schema:
|
|
41
|
+
_schema: JsonSchema<ROW>,
|
|
44
42
|
): Promise<void> {
|
|
45
43
|
// no-op
|
|
46
44
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema'
|
|
2
1
|
import type { NonNegativeInteger, ObjectWithId, StringMap } from '@naturalcycles/js-lib/types'
|
|
2
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
3
3
|
import type { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
4
4
|
import type {
|
|
5
5
|
CommonDBCreateOptions,
|
|
@@ -26,7 +26,7 @@ export interface CommonDB {
|
|
|
26
26
|
support: CommonDBSupport
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
|
-
* Checks that connection/credentials/etc is
|
|
29
|
+
* Checks that connection/credentials/etc is okay.
|
|
30
30
|
* Also acts as a "warmup request" for a DB.
|
|
31
31
|
* It SHOULD fail if DB setup is wrong (e.g on wrong credentials).
|
|
32
32
|
* It SHOULD succeed if e.g getByIds(['nonExistingKey']) doesn't throw.
|
|
@@ -44,7 +44,7 @@ export interface CommonDB {
|
|
|
44
44
|
*
|
|
45
45
|
* This is important for the code to rely on it, and it's verified by dbTest
|
|
46
46
|
*/
|
|
47
|
-
getTableSchema: <ROW extends ObjectWithId>(table: string) => Promise<
|
|
47
|
+
getTableSchema: <ROW extends ObjectWithId>(table: string) => Promise<JsonSchema<ROW>>
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
50
|
* Will do like `create table ...` for mysql.
|
|
@@ -52,7 +52,7 @@ export interface CommonDB {
|
|
|
52
52
|
*/
|
|
53
53
|
createTable: <ROW extends ObjectWithId>(
|
|
54
54
|
table: string,
|
|
55
|
-
schema:
|
|
55
|
+
schema: JsonSchema<ROW>,
|
|
56
56
|
opt?: CommonDBCreateOptions,
|
|
57
57
|
) => Promise<void>
|
|
58
58
|
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { _isEmptyObject } from '@naturalcycles/js-lib'
|
|
2
2
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
3
|
-
import {
|
|
4
|
-
generateJsonSchemaFromData,
|
|
5
|
-
type JsonSchemaObject,
|
|
6
|
-
type JsonSchemaRootObject,
|
|
7
|
-
} from '@naturalcycles/js-lib/json-schema'
|
|
3
|
+
import { generateJsonSchemaFromData } from '@naturalcycles/js-lib/json-schema'
|
|
8
4
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
9
5
|
import { _deepCopy, _sortObjectDeep } from '@naturalcycles/js-lib/object'
|
|
10
6
|
import {
|
|
@@ -14,6 +10,7 @@ import {
|
|
|
14
10
|
type ObjectWithId,
|
|
15
11
|
type StringMap,
|
|
16
12
|
} from '@naturalcycles/js-lib/types'
|
|
13
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
17
14
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
18
15
|
import { bufferReviver } from '@naturalcycles/nodejs-lib/stream/ndjson/transformJsonParse.js'
|
|
19
16
|
import type { CommonDB, CommonDBSupport } from '../commondb/common.db.js'
|
|
@@ -111,19 +108,17 @@ export class InMemoryDB implements CommonDB {
|
|
|
111
108
|
return Object.keys(this.data).filter(t => t.startsWith(this.cfg.tablesPrefix))
|
|
112
109
|
}
|
|
113
110
|
|
|
114
|
-
async getTableSchema<ROW extends ObjectWithId>(
|
|
115
|
-
_table: string,
|
|
116
|
-
): Promise<JsonSchemaRootObject<ROW>> {
|
|
111
|
+
async getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<JsonSchema<ROW>> {
|
|
117
112
|
const table = this.cfg.tablesPrefix + _table
|
|
118
113
|
return {
|
|
119
114
|
...generateJsonSchemaFromData(_stringMapValues(this.data[table] || {})),
|
|
120
115
|
$id: `${table}.schema.json`,
|
|
121
|
-
}
|
|
116
|
+
} as any
|
|
122
117
|
}
|
|
123
118
|
|
|
124
119
|
async createTable<ROW extends ObjectWithId>(
|
|
125
120
|
_table: string,
|
|
126
|
-
_schema:
|
|
121
|
+
_schema: JsonSchema<ROW>,
|
|
127
122
|
opt: CommonDBCreateOptions = {},
|
|
128
123
|
): Promise<void> {
|
|
129
124
|
const table = this.cfg.tablesPrefix + _table
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { _hb } from '@naturalcycles/js-lib'
|
|
2
2
|
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js'
|
|
3
3
|
import { ErrorMode } from '@naturalcycles/js-lib/error/errorMode.js'
|
|
4
|
-
import type { JsonSchemaObject } from '@naturalcycles/js-lib/json-schema'
|
|
5
4
|
import { _mapValues } from '@naturalcycles/js-lib/object'
|
|
6
5
|
import { pMap } from '@naturalcycles/js-lib/promise/pMap.js'
|
|
7
6
|
import type { AsyncMapper, BaseDBEntity, UnixTimestamp } from '@naturalcycles/js-lib/types'
|
|
8
7
|
import { _passthroughMapper } from '@naturalcycles/js-lib/types'
|
|
8
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
9
9
|
import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors'
|
|
10
10
|
import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
|
|
11
11
|
import {
|
|
@@ -166,7 +166,7 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
|
|
|
166
166
|
return
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
const schema = await fs2.readJsonAsync<
|
|
169
|
+
const schema = await fs2.readJsonAsync<JsonSchema<any>>(schemaFilePath)
|
|
170
170
|
await db.createTable(table, schema, { dropIfExists: true })
|
|
171
171
|
})
|
|
172
172
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { _range } from '@naturalcycles/js-lib/array/range.js'
|
|
2
|
-
import type { JsonSchemaObject } from '@naturalcycles/js-lib/json-schema'
|
|
3
|
-
import { j } from '@naturalcycles/js-lib/json-schema'
|
|
4
2
|
import type { BaseDBEntity, UnixTimestamp } from '@naturalcycles/js-lib/types'
|
|
3
|
+
import { j } from '@naturalcycles/nodejs-lib/ajv'
|
|
5
4
|
import {
|
|
6
5
|
baseDBEntitySchema,
|
|
7
6
|
binarySchema,
|
|
@@ -51,19 +50,21 @@ export const testItemTMSchema: ObjectSchema<TestItemTM> = objectSchema<TestItemT
|
|
|
51
50
|
even: booleanSchema.optional(),
|
|
52
51
|
})
|
|
53
52
|
|
|
54
|
-
export const testItemBMJsonSchema
|
|
55
|
-
.
|
|
53
|
+
export const testItemBMJsonSchema = j
|
|
54
|
+
.object({
|
|
56
55
|
// todo: figure out how to not copy-paste these 3 fields
|
|
57
56
|
id: j.string(), // todo: not strictly needed here
|
|
58
|
-
created: j.integer().unixTimestamp(),
|
|
59
|
-
updated: j.integer().unixTimestamp(),
|
|
57
|
+
created: j.number().integer().unixTimestamp(),
|
|
58
|
+
updated: j.number().integer().unixTimestamp(),
|
|
60
59
|
k1: j.string(),
|
|
61
|
-
k2: j.
|
|
60
|
+
k2: j.string().nullable().optional(),
|
|
62
61
|
k3: j.number().optional(),
|
|
63
62
|
even: j.boolean().optional(),
|
|
64
63
|
b1: j.buffer().optional(),
|
|
64
|
+
nested: j.object({ foo: j.number() }).optional(),
|
|
65
65
|
})
|
|
66
|
-
.
|
|
66
|
+
.dbEntity()
|
|
67
|
+
.isOfType<TestItemBM>()
|
|
67
68
|
.build()
|
|
68
69
|
|
|
69
70
|
export function createTestItemDBM(num = 1): TestItemDBM {
|