@naturalcycles/db-lib 8.14.0 → 8.16.2
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/CHANGELOG.md +28 -0
- package/dist/adapter/cachedb/cache.db.d.ts +2 -2
- package/dist/adapter/file/file.db.d.ts +2 -2
- package/dist/adapter/inmemory/inMemory.db.d.ts +2 -2
- package/dist/base.common.db.d.ts +2 -2
- package/dist/common.db.d.ts +8 -2
- package/dist/commondao/common.dao.d.ts +2 -2
- package/dist/commondao/common.dao.js +4 -2
- package/dist/testing/dbTest.js +1 -1
- package/package.json +1 -1
- package/src/adapter/cachedb/cache.db.ts +2 -2
- package/src/adapter/file/file.db.ts +3 -3
- package/src/adapter/inmemory/inMemory.db.ts +5 -2
- package/src/base.common.db.ts +4 -2
- package/src/common.db.ts +8 -2
- package/src/commondao/common.dao.ts +6 -3
- package/src/testing/dbTest.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
## [8.16.2](https://github.com/NaturalCycles/db-lib/compare/v8.16.1...v8.16.2) (2021-10-04)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* unnecessary await ([11282ba](https://github.com/NaturalCycles/db-lib/commit/11282ba44982c174b1b9eeb052c911c30ee22076))
|
|
7
|
+
|
|
8
|
+
## [8.16.1](https://github.com/NaturalCycles/db-lib/compare/v8.16.0...v8.16.1) (2021-10-04)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* use JsonSchemaRootObject type ([f938b62](https://github.com/NaturalCycles/db-lib/commit/f938b624e9fee61732efb4dc2dd53074dc16977b))
|
|
14
|
+
|
|
15
|
+
# [8.16.0](https://github.com/NaturalCycles/db-lib/compare/v8.15.0...v8.16.0) (2021-10-04)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* getTableSchema to enforce non-optional $id ([a147627](https://github.com/NaturalCycles/db-lib/commit/a147627d18c3323b63de7f617068c9b3e2517c10))
|
|
21
|
+
|
|
22
|
+
# [8.15.0](https://github.com/NaturalCycles/db-lib/compare/v8.14.0...v8.15.0) (2021-10-04)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* dbTest to ensure `${table}.schema.json` format ([eacf18f](https://github.com/NaturalCycles/db-lib/commit/eacf18fdd7197f18e15af89f19f786556a7d3219))
|
|
28
|
+
|
|
1
29
|
# [8.14.0](https://github.com/NaturalCycles/db-lib/compare/v8.13.0...v8.14.0) (2021-10-04)
|
|
2
30
|
|
|
3
31
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Readable } from 'stream';
|
|
3
|
-
import { JsonSchemaObject } from '@naturalcycles/js-lib';
|
|
3
|
+
import { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib';
|
|
4
4
|
import { IDebugger } from '@naturalcycles/nodejs-lib';
|
|
5
5
|
import { BaseCommonDB } from '../../base.common.db';
|
|
6
6
|
import { CommonDB } from '../../common.db';
|
|
@@ -22,7 +22,7 @@ export declare class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
22
22
|
* Resets InMemory DB data
|
|
23
23
|
*/
|
|
24
24
|
getTables(): Promise<string[]>;
|
|
25
|
-
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<
|
|
25
|
+
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
26
26
|
createTable(table: string, schema: JsonSchemaObject, opt?: CacheDBCreateOptions): Promise<void>;
|
|
27
27
|
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: CacheDBOptions): Promise<ROW[]>;
|
|
28
28
|
deleteByIds(table: string, ids: string[], opt?: CacheDBOptions): Promise<number>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { JsonSchemaRootObject } from '@naturalcycles/js-lib';
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { BaseCommonDB, DBSaveBatchOperation, ObjectWithId } from '../..';
|
|
4
4
|
import { CommonDB } from '../../common.db';
|
|
@@ -32,7 +32,7 @@ export declare class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
32
32
|
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, opt?: CommonDBStreamOptions): ReadableTyped<ROW>;
|
|
33
33
|
deleteByIds<ROW extends ObjectWithId>(table: string, ids: string[], _opt?: CommonDBOptions): Promise<number>;
|
|
34
34
|
deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
|
|
35
|
-
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<
|
|
35
|
+
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
36
36
|
loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]>;
|
|
37
37
|
saveFile<ROW extends ObjectWithId>(table: string, _rows: ROW[]): Promise<void>;
|
|
38
38
|
saveFiles(ops: DBSaveBatchOperation[]): Promise<void>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonSchemaObject, StringMap } from '@naturalcycles/js-lib';
|
|
1
|
+
import { JsonSchemaObject, StringMap, JsonSchemaRootObject } from '@naturalcycles/js-lib';
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDB, DBTransaction, ObjectWithId } from '../..';
|
|
4
4
|
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, RunQueryResult } from '../../db.model';
|
|
@@ -46,7 +46,7 @@ export declare class InMemoryDB implements CommonDB {
|
|
|
46
46
|
*/
|
|
47
47
|
resetCache(_table?: string): Promise<void>;
|
|
48
48
|
getTables(): Promise<string[]>;
|
|
49
|
-
getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<
|
|
49
|
+
getTableSchema<ROW extends ObjectWithId>(_table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
50
50
|
createTable(_table: string, _schema: JsonSchemaObject, opt?: CommonDBCreateOptions): Promise<void>;
|
|
51
51
|
getByIds<ROW extends ObjectWithId>(_table: string, ids: string[], _opt?: CommonDBOptions): Promise<ROW[]>;
|
|
52
52
|
saveBatch<ROW extends ObjectWithId>(_table: string, rows: ROW[], _opt?: CommonDBSaveOptions): Promise<void>;
|
package/dist/base.common.db.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonSchemaObject } from '@naturalcycles/js-lib';
|
|
1
|
+
import { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib';
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDB } from './common.db';
|
|
4
4
|
import { CommonDBSaveOptions, ObjectWithId, RunQueryResult } from './db.model';
|
|
@@ -11,7 +11,7 @@ import { DBTransaction } from './transaction/dbTransaction';
|
|
|
11
11
|
export declare class BaseCommonDB implements CommonDB {
|
|
12
12
|
ping(): Promise<void>;
|
|
13
13
|
getTables(): Promise<string[]>;
|
|
14
|
-
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<
|
|
14
|
+
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
15
15
|
createTable(_table: string, _schema: JsonSchemaObject): Promise<void>;
|
|
16
16
|
deleteByIds(_table: string, _ids: string[]): Promise<number>;
|
|
17
17
|
deleteByQuery<ROW extends ObjectWithId>(_q: DBQuery<ROW>): Promise<number>;
|
package/dist/common.db.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonSchemaObject } from '@naturalcycles/js-lib';
|
|
1
|
+
import { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib';
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, ObjectWithId, RunQueryResult } from './db.model';
|
|
4
4
|
import { DBQuery } from './query/dbQuery';
|
|
@@ -15,7 +15,13 @@ export interface CommonDB {
|
|
|
15
15
|
* Return all tables (table names) available in this DB.
|
|
16
16
|
*/
|
|
17
17
|
getTables(): Promise<string[]>;
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* $id of the schema SHOULD be like this:
|
|
20
|
+
* `${tableName}.schema.json`
|
|
21
|
+
*
|
|
22
|
+
* This is important for the code to rely on it, and it's verified by dbTest
|
|
23
|
+
*/
|
|
24
|
+
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
19
25
|
/**
|
|
20
26
|
* Will do like `create table ...` for mysql.
|
|
21
27
|
* Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AsyncMapper, JsonSchemaObject } from '@naturalcycles/js-lib';
|
|
1
|
+
import { AsyncMapper, JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib';
|
|
2
2
|
import { AjvSchema, ObjectSchemaTyped, ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { DBModelType, ObjectWithId, RunQueryResult, Saved } from '../db.model';
|
|
4
4
|
import { DBQuery, RunnableDBQuery } from '../query/dbQuery';
|
|
@@ -125,7 +125,7 @@ export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends Obj
|
|
|
125
125
|
* Does NOT mutate the object.
|
|
126
126
|
*/
|
|
127
127
|
validateAndConvert<IN, OUT = IN>(obj: IN, schema?: ObjectSchemaTyped<IN> | AjvSchema<IN>, modelType?: DBModelType, opt?: CommonDaoOptions): OUT;
|
|
128
|
-
getTableSchema(): Promise<
|
|
128
|
+
getTableSchema(): Promise<JsonSchemaRootObject<DBM>>;
|
|
129
129
|
createTable(schema: JsonSchemaObject<DBM>, opt?: CommonDaoCreateOptions): Promise<void>;
|
|
130
130
|
/**
|
|
131
131
|
* Proxy to this.cfg.db.ping
|
|
@@ -548,7 +548,8 @@ class CommonDao {
|
|
|
548
548
|
// DBM > BM
|
|
549
549
|
const bm = await this.cfg.hooks.beforeDBMToBM(dbm);
|
|
550
550
|
// Validate/convert BM
|
|
551
|
-
return
|
|
551
|
+
// eslint-disable-next-line @typescript-eslint/return-await
|
|
552
|
+
return this.validateAndConvert(bm, this.cfg.bmSchema, db_model_1.DBModelType.BM, opt);
|
|
552
553
|
}
|
|
553
554
|
async dbmsToBM(dbms, opt = {}) {
|
|
554
555
|
return await (0, js_lib_1.pMap)(dbms, async (dbm) => await this.dbmToBM(dbm, opt));
|
|
@@ -565,7 +566,8 @@ class CommonDao {
|
|
|
565
566
|
// BM > DBM
|
|
566
567
|
const dbm = { ...(await this.cfg.hooks.beforeBMToDBM(bm)) };
|
|
567
568
|
// Validate/convert DBM
|
|
568
|
-
return
|
|
569
|
+
// eslint-disable-next-line @typescript-eslint/return-await
|
|
570
|
+
return this.validateAndConvert(dbm, this.cfg.dbmSchema, db_model_1.DBModelType.DBM, opt);
|
|
569
571
|
}
|
|
570
572
|
async bmsToDBM(bms, opt = {}) {
|
|
571
573
|
// try/catch?
|
package/dist/testing/dbTest.js
CHANGED
|
@@ -130,7 +130,7 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
|
|
|
130
130
|
await (0, js_lib_1.pMap)(tables, async (table) => {
|
|
131
131
|
const schema = await db.getTableSchema(table);
|
|
132
132
|
console.log(schema);
|
|
133
|
-
expect(schema).
|
|
133
|
+
expect(schema.$id).toBe(`${table}.schema.json`);
|
|
134
134
|
});
|
|
135
135
|
}
|
|
136
136
|
});
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Readable } from 'stream'
|
|
2
|
-
import { JsonSchemaObject } from '@naturalcycles/js-lib'
|
|
2
|
+
import { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib'
|
|
3
3
|
import { Debug, IDebugger } from '@naturalcycles/nodejs-lib'
|
|
4
4
|
import { BaseCommonDB } from '../../base.common.db'
|
|
5
5
|
import { CommonDB } from '../../common.db'
|
|
@@ -45,7 +45,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
45
45
|
|
|
46
46
|
override async getTableSchema<ROW extends ObjectWithId>(
|
|
47
47
|
table: string,
|
|
48
|
-
): Promise<
|
|
48
|
+
): Promise<JsonSchemaRootObject<ROW>> {
|
|
49
49
|
return await this.cfg.downstreamDB.getTableSchema<ROW>(table)
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
2
|
generateJsonSchemaFromData,
|
|
3
|
-
JsonSchemaObject,
|
|
4
3
|
pMap,
|
|
5
4
|
StringMap,
|
|
6
5
|
_by,
|
|
@@ -10,6 +9,7 @@ import {
|
|
|
10
9
|
_sortObjectDeep,
|
|
11
10
|
_stringMapValues,
|
|
12
11
|
_uniq,
|
|
12
|
+
JsonSchemaRootObject,
|
|
13
13
|
} from '@naturalcycles/js-lib'
|
|
14
14
|
import { Debug, readableCreate, ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
15
15
|
import { dimGrey } from '@naturalcycles/nodejs-lib/dist/colors'
|
|
@@ -213,12 +213,12 @@ export class FileDB extends BaseCommonDB implements CommonDB {
|
|
|
213
213
|
|
|
214
214
|
override async getTableSchema<ROW extends ObjectWithId>(
|
|
215
215
|
table: string,
|
|
216
|
-
): Promise<
|
|
216
|
+
): Promise<JsonSchemaRootObject<ROW>> {
|
|
217
217
|
const rows = await this.loadFile(table)
|
|
218
218
|
return {
|
|
219
219
|
...generateJsonSchemaFromData(rows),
|
|
220
220
|
$id: `${table}.schema.json`,
|
|
221
|
-
}
|
|
221
|
+
}
|
|
222
222
|
}
|
|
223
223
|
|
|
224
224
|
// wrapper, to handle logging
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
_by,
|
|
9
9
|
_since,
|
|
10
10
|
_sortObjectDeep,
|
|
11
|
+
JsonSchemaRootObject,
|
|
11
12
|
} from '@naturalcycles/js-lib'
|
|
12
13
|
import {
|
|
13
14
|
bufferReviver,
|
|
@@ -111,12 +112,14 @@ export class InMemoryDB implements CommonDB {
|
|
|
111
112
|
return Object.keys(this.data).filter(t => t.startsWith(this.cfg.tablesPrefix))
|
|
112
113
|
}
|
|
113
114
|
|
|
114
|
-
async getTableSchema<ROW extends ObjectWithId>(
|
|
115
|
+
async getTableSchema<ROW extends ObjectWithId>(
|
|
116
|
+
_table: string,
|
|
117
|
+
): Promise<JsonSchemaRootObject<ROW>> {
|
|
115
118
|
const table = this.cfg.tablesPrefix + _table
|
|
116
119
|
return {
|
|
117
120
|
...generateJsonSchemaFromData(Object.values(this.data[table] || {})),
|
|
118
121
|
$id: `${table}.schema.json`,
|
|
119
|
-
}
|
|
122
|
+
}
|
|
120
123
|
}
|
|
121
124
|
|
|
122
125
|
async createTable(
|
package/src/base.common.db.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Readable } from 'stream'
|
|
2
|
-
import { JsonSchemaObject } from '@naturalcycles/js-lib'
|
|
2
|
+
import { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib'
|
|
3
3
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
4
4
|
import { CommonDB } from './common.db'
|
|
5
5
|
import { CommonDBSaveOptions, ObjectWithId, RunQueryResult } from './db.model'
|
|
@@ -18,7 +18,9 @@ export class BaseCommonDB implements CommonDB {
|
|
|
18
18
|
return []
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
async getTableSchema<ROW extends ObjectWithId>(
|
|
21
|
+
async getTableSchema<ROW extends ObjectWithId>(
|
|
22
|
+
table: string,
|
|
23
|
+
): Promise<JsonSchemaRootObject<ROW>> {
|
|
22
24
|
return {
|
|
23
25
|
$id: `${table}.schema.json`,
|
|
24
26
|
type: 'object',
|
package/src/common.db.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonSchemaObject } from '@naturalcycles/js-lib'
|
|
1
|
+
import { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib'
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
3
3
|
import {
|
|
4
4
|
CommonDBCreateOptions,
|
|
@@ -25,7 +25,13 @@ export interface CommonDB {
|
|
|
25
25
|
*/
|
|
26
26
|
getTables(): Promise<string[]>
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
/**
|
|
29
|
+
* $id of the schema SHOULD be like this:
|
|
30
|
+
* `${tableName}.schema.json`
|
|
31
|
+
*
|
|
32
|
+
* This is important for the code to rely on it, and it's verified by dbTest
|
|
33
|
+
*/
|
|
34
|
+
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>
|
|
29
35
|
|
|
30
36
|
/**
|
|
31
37
|
* Will do like `create table ...` for mysql.
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
_since,
|
|
9
9
|
_truncate,
|
|
10
10
|
pMap,
|
|
11
|
+
JsonSchemaRootObject,
|
|
11
12
|
} from '@naturalcycles/js-lib'
|
|
12
13
|
import {
|
|
13
14
|
AjvSchema,
|
|
@@ -723,7 +724,8 @@ export class CommonDao<
|
|
|
723
724
|
const bm = await this.cfg.hooks!.beforeDBMToBM!(dbm)
|
|
724
725
|
|
|
725
726
|
// Validate/convert BM
|
|
726
|
-
return
|
|
727
|
+
// eslint-disable-next-line @typescript-eslint/return-await
|
|
728
|
+
return this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
|
|
727
729
|
}
|
|
728
730
|
|
|
729
731
|
async dbmsToBM(dbms: DBM[], opt: CommonDaoOptions = {}): Promise<Saved<BM>[]> {
|
|
@@ -751,7 +753,8 @@ export class CommonDao<
|
|
|
751
753
|
const dbm = { ...(await this.cfg.hooks!.beforeBMToDBM!(bm)) }
|
|
752
754
|
|
|
753
755
|
// Validate/convert DBM
|
|
754
|
-
return
|
|
756
|
+
// eslint-disable-next-line @typescript-eslint/return-await
|
|
757
|
+
return this.validateAndConvert(dbm, this.cfg.dbmSchema, DBModelType.DBM, opt)
|
|
755
758
|
}
|
|
756
759
|
|
|
757
760
|
async bmsToDBM(bms: BM[], opt: CommonDaoOptions = {}): Promise<DBM[]> {
|
|
@@ -884,7 +887,7 @@ export class CommonDao<
|
|
|
884
887
|
return convertedValue
|
|
885
888
|
}
|
|
886
889
|
|
|
887
|
-
async getTableSchema(): Promise<
|
|
890
|
+
async getTableSchema(): Promise<JsonSchemaRootObject<DBM>> {
|
|
888
891
|
return await this.cfg.db.getTableSchema<DBM>(this.cfg.table)
|
|
889
892
|
}
|
|
890
893
|
|
package/src/testing/dbTest.ts
CHANGED