@naturalcycles/db-lib 10.39.0 → 10.40.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/testing/commonDBTest.js +2 -2
- package/dist/testing/commonDaoTest.js +4 -4
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/index.js +2 -2
- package/dist/testing/test.model.d.ts +2 -4
- package/dist/testing/test.model.js +5 -18
- package/dist/validation/index.d.ts +9 -9
- package/dist/validation/index.js +31 -24
- package/package.json +1 -1
- package/src/testing/commonDBTest.ts +2 -2
- package/src/testing/commonDaoTest.ts +4 -10
- package/src/testing/index.ts +0 -2
- package/src/testing/test.model.ts +15 -37
- package/src/validation/index.ts +61 -55
|
@@ -4,7 +4,7 @@ import { _deepFreeze, _filterObject, _pick } from '@naturalcycles/js-lib/object'
|
|
|
4
4
|
import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
|
|
5
5
|
import { CommonDBType } from '../commondb/common.db.js';
|
|
6
6
|
import { DBQuery } from '../query/dbQuery.js';
|
|
7
|
-
import { createTestItemDBM, createTestItemsDBM, TEST_TABLE,
|
|
7
|
+
import { createTestItemDBM, createTestItemsDBM, TEST_TABLE, testItemBMSchema, } from './test.model.js';
|
|
8
8
|
export async function runCommonDBTest(db, quirks = {}) {
|
|
9
9
|
// this is because vitest cannot be "required" from cjs
|
|
10
10
|
const { test, expect } = await import('vitest');
|
|
@@ -19,7 +19,7 @@ export async function runCommonDBTest(db, quirks = {}) {
|
|
|
19
19
|
// CREATE TABLE, DROP
|
|
20
20
|
if (support.createTable) {
|
|
21
21
|
test('createTable, dropIfExists=true', async () => {
|
|
22
|
-
await db.createTable(TEST_TABLE,
|
|
22
|
+
await db.createTable(TEST_TABLE, testItemBMSchema.build(), { dropIfExists: true });
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
if (support.queries) {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { _sortBy } from '@naturalcycles/js-lib/array/sort.js';
|
|
2
2
|
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js';
|
|
3
3
|
import { _deepCopy, _filterObject, _omit, _pick } from '@naturalcycles/js-lib/object';
|
|
4
|
-
import {
|
|
4
|
+
import { AjvSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
5
5
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
6
6
|
import { CommonDao } from '../commondao/common.dao.js';
|
|
7
7
|
import { DBQuery } from '../query/dbQuery.js';
|
|
8
|
-
import { createTestItemBM, createTestItemsBM, TEST_TABLE,
|
|
8
|
+
import { createTestItemBM, createTestItemsBM, TEST_TABLE, testItemBMSchema } from './test.model.js';
|
|
9
9
|
export async function runCommonDaoTest(db, quirks = {}) {
|
|
10
10
|
// this is because vitest cannot be "required" from cjs
|
|
11
11
|
const { test, expect } = await import('vitest');
|
|
@@ -13,7 +13,7 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
13
13
|
const dao = new CommonDao({
|
|
14
14
|
table: TEST_TABLE,
|
|
15
15
|
db,
|
|
16
|
-
validateBM:
|
|
16
|
+
validateBM: AjvSchema.create(testItemBMSchema).getValidationFunction(),
|
|
17
17
|
});
|
|
18
18
|
const items = createTestItemsBM(3);
|
|
19
19
|
const itemsClone = _deepCopy(items);
|
|
@@ -29,7 +29,7 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
29
29
|
// CREATE TABLE, DROP
|
|
30
30
|
if (support.createTable) {
|
|
31
31
|
test('createTable, dropIfExists=true', async () => {
|
|
32
|
-
await dao.createTable(
|
|
32
|
+
await dao.createTable(testItemBMSchema.build(), { dropIfExists: true });
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
35
|
if (support.queries) {
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -4,6 +4,6 @@ import { runCommonDBTest } from './commonDBTest.js';
|
|
|
4
4
|
import { runCommonKeyValueDaoTest } from './keyValueDaoTest.js';
|
|
5
5
|
import { runCommonKeyValueDBTest } from './keyValueDBTest.js';
|
|
6
6
|
import type { TestItemBM, TestItemDBM, TestItemTM } from './test.model.js';
|
|
7
|
-
import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TEST_TABLE,
|
|
7
|
+
import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TEST_TABLE, testItemBMSchema, testItemTMSchema } from './test.model.js';
|
|
8
8
|
export type { CommonDBImplementationQuirks, TestItemBM, TestItemDBM, TestItemTM };
|
|
9
|
-
export { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, runCommonDaoTest, runCommonDBTest, runCommonKeyValueDaoTest, runCommonKeyValueDBTest, TEST_TABLE,
|
|
9
|
+
export { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, runCommonDaoTest, runCommonDBTest, runCommonKeyValueDaoTest, runCommonKeyValueDBTest, TEST_TABLE, testItemBMSchema, testItemTMSchema, };
|
package/dist/testing/index.js
CHANGED
|
@@ -2,5 +2,5 @@ import { runCommonDaoTest } from './commonDaoTest.js';
|
|
|
2
2
|
import { runCommonDBTest } from './commonDBTest.js';
|
|
3
3
|
import { runCommonKeyValueDaoTest } from './keyValueDaoTest.js';
|
|
4
4
|
import { runCommonKeyValueDBTest } from './keyValueDBTest.js';
|
|
5
|
-
import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TEST_TABLE,
|
|
6
|
-
export { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, runCommonDaoTest, runCommonDBTest, runCommonKeyValueDaoTest, runCommonKeyValueDBTest, TEST_TABLE,
|
|
5
|
+
import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TEST_TABLE, testItemBMSchema, testItemTMSchema, } from './test.model.js';
|
|
6
|
+
export { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, runCommonDaoTest, runCommonDBTest, runCommonKeyValueDaoTest, runCommonKeyValueDBTest, TEST_TABLE, testItemBMSchema, testItemTMSchema, };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { BaseDBEntity } from '@naturalcycles/js-lib/types';
|
|
2
|
-
import { type ObjectSchema } from '@naturalcycles/nodejs-lib/joi';
|
|
3
2
|
export declare const TEST_TABLE = "TEST_TABLE";
|
|
4
3
|
export declare const TEST_TABLE_2 = "TEST_TABLE_2";
|
|
5
4
|
export interface TestItemBM extends BaseDBEntity {
|
|
@@ -18,9 +17,8 @@ export interface TestItemTM {
|
|
|
18
17
|
k1: string;
|
|
19
18
|
even?: boolean;
|
|
20
19
|
}
|
|
21
|
-
export declare const
|
|
22
|
-
export declare const
|
|
23
|
-
export declare const testItemBMJsonSchema: any;
|
|
20
|
+
export declare const testItemTMSchema: any;
|
|
21
|
+
export declare const testItemBMSchema: any;
|
|
24
22
|
export declare function createTestItemDBM(num?: number): TestItemDBM;
|
|
25
23
|
export declare function createTestItemBM(num?: number): TestItemBM;
|
|
26
24
|
export declare function createTestItemsDBM(count?: number): TestItemDBM[];
|
|
@@ -1,25 +1,13 @@
|
|
|
1
1
|
import { _range } from '@naturalcycles/js-lib/array/range.js';
|
|
2
2
|
import { j } from '@naturalcycles/nodejs-lib/ajv';
|
|
3
|
-
import { baseDBEntitySchema, binarySchema, booleanSchema, numberSchema, objectSchema, stringSchema, } from '@naturalcycles/nodejs-lib/joi';
|
|
4
3
|
const MOCK_TS_2018_06_21 = 1529539200;
|
|
5
4
|
export const TEST_TABLE = 'TEST_TABLE';
|
|
6
5
|
export const TEST_TABLE_2 = 'TEST_TABLE_2';
|
|
7
|
-
export const
|
|
8
|
-
k1:
|
|
9
|
-
|
|
10
|
-
k3: numberSchema.optional(),
|
|
11
|
-
even: booleanSchema.optional(),
|
|
12
|
-
b1: binarySchema.optional(),
|
|
13
|
-
nested: objectSchema({
|
|
14
|
-
foo: numberSchema,
|
|
15
|
-
}).optional(),
|
|
16
|
-
}).concat(baseDBEntitySchema);
|
|
17
|
-
export const testItemTMSchema = objectSchema({
|
|
18
|
-
k1: stringSchema,
|
|
19
|
-
even: booleanSchema.optional(),
|
|
6
|
+
export const testItemTMSchema = j.object({
|
|
7
|
+
k1: j.string(),
|
|
8
|
+
even: j.boolean().optional(),
|
|
20
9
|
});
|
|
21
|
-
export const
|
|
22
|
-
.dbEntity({
|
|
10
|
+
export const testItemBMSchema = j.object.dbEntity({
|
|
23
11
|
// todo: figure out how to not copy-paste these 3 fields
|
|
24
12
|
id: j.string(), // todo: not strictly needed here
|
|
25
13
|
created: j.number().integer().unixTimestamp(),
|
|
@@ -30,8 +18,7 @@ export const testItemBMJsonSchema = j.object
|
|
|
30
18
|
even: j.boolean().optional(),
|
|
31
19
|
b1: j.buffer().optional(),
|
|
32
20
|
nested: j.object.infer({ foo: j.number() }).optional(),
|
|
33
|
-
})
|
|
34
|
-
.build();
|
|
21
|
+
});
|
|
35
22
|
export function createTestItemDBM(num = 1) {
|
|
36
23
|
return {
|
|
37
24
|
id: `id${num}`,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import type {
|
|
4
|
-
export declare const commonDBOptionsSchema:
|
|
5
|
-
export declare const commonDBSaveOptionsSchema:
|
|
6
|
-
export declare const dbQueryFilterOperatorSchema:
|
|
7
|
-
export declare const dbQueryFilterSchema:
|
|
8
|
-
export declare const dbQueryOrderSchema:
|
|
9
|
-
export declare const dbQuerySchema:
|
|
1
|
+
import type { ObjectWithId } from '@naturalcycles/js-lib/types';
|
|
2
|
+
import { type JsonSchemaObjectBuilder } from '@naturalcycles/nodejs-lib/ajv';
|
|
3
|
+
import type { CommonDBOptions } from '../db.model.js';
|
|
4
|
+
export declare const commonDBOptionsSchema: () => JsonSchemaObjectBuilder<CommonDBOptions, CommonDBOptions>;
|
|
5
|
+
export declare const commonDBSaveOptionsSchema: <ROW extends ObjectWithId>() => any;
|
|
6
|
+
export declare const dbQueryFilterOperatorSchema: any;
|
|
7
|
+
export declare const dbQueryFilterSchema: <ROW extends ObjectWithId>() => any;
|
|
8
|
+
export declare const dbQueryOrderSchema: <ROW extends ObjectWithId>() => any;
|
|
9
|
+
export declare const dbQuerySchema: <ROW extends ObjectWithId>() => any;
|
package/dist/validation/index.js
CHANGED
|
@@ -1,29 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { dbQueryFilterOperatorValues } from '../query/dbQuery.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { j, JsonSchemaAnyBuilder, } from '@naturalcycles/nodejs-lib/ajv';
|
|
2
|
+
import { dbQueryFilterOperatorValues, } from '../query/dbQuery.js';
|
|
3
|
+
// oxlint-disable typescript/explicit-function-return-type
|
|
4
|
+
// DBTransaction schema - validates presence without deep validation
|
|
5
|
+
const dbTransactionSchema = j.object.any().castAs();
|
|
6
|
+
// Schema that accepts any value (string, number, boolean, object, array, null)
|
|
7
|
+
const anyValueSchema = new JsonSchemaAnyBuilder({});
|
|
8
|
+
export const commonDBOptionsSchema = () => j.object({
|
|
9
|
+
tx: dbTransactionSchema.optional(),
|
|
6
10
|
});
|
|
7
|
-
export const commonDBSaveOptionsSchema =
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
export const commonDBSaveOptionsSchema = () => j.object({
|
|
12
|
+
tx: dbTransactionSchema.optional(),
|
|
13
|
+
excludeFromIndexes: j.array(j.string().castAs()).optional(),
|
|
14
|
+
saveMethod: j.enum(['upsert', 'insert', 'update']).optional(),
|
|
15
|
+
assignGeneratedIds: j.boolean().optional(),
|
|
16
|
+
});
|
|
17
|
+
export const dbQueryFilterOperatorSchema = j.enum(dbQueryFilterOperatorValues);
|
|
18
|
+
export const dbQueryFilterSchema = () => j.object({
|
|
19
|
+
name: j.string().castAs(),
|
|
13
20
|
op: dbQueryFilterOperatorSchema,
|
|
14
|
-
val:
|
|
21
|
+
val: anyValueSchema,
|
|
15
22
|
});
|
|
16
|
-
export const dbQueryOrderSchema =
|
|
17
|
-
name:
|
|
18
|
-
descending:
|
|
23
|
+
export const dbQueryOrderSchema = () => j.object({
|
|
24
|
+
name: j.string().castAs(),
|
|
25
|
+
descending: j.boolean().optional(),
|
|
19
26
|
});
|
|
20
|
-
export const dbQuerySchema =
|
|
21
|
-
table:
|
|
22
|
-
_filters:
|
|
23
|
-
_limitValue:
|
|
24
|
-
_offsetValue:
|
|
25
|
-
_orders:
|
|
26
|
-
_startCursor:
|
|
27
|
-
_endCursor:
|
|
28
|
-
_selectedFieldNames:
|
|
27
|
+
export const dbQuerySchema = () => j.object.infer({
|
|
28
|
+
table: j.string(),
|
|
29
|
+
_filters: j.array(dbQueryFilterSchema()).optional(),
|
|
30
|
+
_limitValue: j.number().integer().min(0).optional(),
|
|
31
|
+
_offsetValue: j.number().integer().min(0).optional(),
|
|
32
|
+
_orders: j.array(dbQueryOrderSchema()).optional(),
|
|
33
|
+
_startCursor: j.string().optional(),
|
|
34
|
+
_endCursor: j.string().optional(),
|
|
35
|
+
_selectedFieldNames: j.array(j.string().castAs()).optional(),
|
|
29
36
|
});
|
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
createTestItemDBM,
|
|
11
11
|
createTestItemsDBM,
|
|
12
12
|
TEST_TABLE,
|
|
13
|
-
|
|
13
|
+
testItemBMSchema,
|
|
14
14
|
} from './test.model.js'
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -49,7 +49,7 @@ export async function runCommonDBTest(
|
|
|
49
49
|
// CREATE TABLE, DROP
|
|
50
50
|
if (support.createTable) {
|
|
51
51
|
test('createTable, dropIfExists=true', async () => {
|
|
52
|
-
await db.createTable(TEST_TABLE,
|
|
52
|
+
await db.createTable(TEST_TABLE, testItemBMSchema.build(), { dropIfExists: true })
|
|
53
53
|
})
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
import { _sortBy } from '@naturalcycles/js-lib/array/sort.js'
|
|
2
2
|
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js'
|
|
3
3
|
import { _deepCopy, _filterObject, _omit, _pick } from '@naturalcycles/js-lib/object'
|
|
4
|
-
import {
|
|
4
|
+
import { AjvSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
5
5
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
6
6
|
import { CommonDao } from '../commondao/common.dao.js'
|
|
7
7
|
import type { CommonDB } from '../commondb/common.db.js'
|
|
8
8
|
import { DBQuery } from '../query/dbQuery.js'
|
|
9
9
|
import type { CommonDBImplementationQuirks } from './commonDBTest.js'
|
|
10
10
|
import type { TestItemBM } from './test.model.js'
|
|
11
|
-
import {
|
|
12
|
-
createTestItemBM,
|
|
13
|
-
createTestItemsBM,
|
|
14
|
-
TEST_TABLE,
|
|
15
|
-
testItemBMJsonSchema,
|
|
16
|
-
testItemBMSchema,
|
|
17
|
-
} from './test.model.js'
|
|
11
|
+
import { createTestItemBM, createTestItemsBM, TEST_TABLE, testItemBMSchema } from './test.model.js'
|
|
18
12
|
|
|
19
13
|
export async function runCommonDaoTest(
|
|
20
14
|
db: CommonDB,
|
|
@@ -27,7 +21,7 @@ export async function runCommonDaoTest(
|
|
|
27
21
|
const dao = new CommonDao({
|
|
28
22
|
table: TEST_TABLE,
|
|
29
23
|
db,
|
|
30
|
-
validateBM:
|
|
24
|
+
validateBM: AjvSchema.create(testItemBMSchema).getValidationFunction(),
|
|
31
25
|
})
|
|
32
26
|
|
|
33
27
|
const items = createTestItemsBM(3)
|
|
@@ -47,7 +41,7 @@ export async function runCommonDaoTest(
|
|
|
47
41
|
// CREATE TABLE, DROP
|
|
48
42
|
if (support.createTable) {
|
|
49
43
|
test('createTable, dropIfExists=true', async () => {
|
|
50
|
-
await dao.createTable(
|
|
44
|
+
await dao.createTable(testItemBMSchema.build(), { dropIfExists: true })
|
|
51
45
|
})
|
|
52
46
|
}
|
|
53
47
|
|
package/src/testing/index.ts
CHANGED
|
@@ -10,7 +10,6 @@ import {
|
|
|
10
10
|
createTestItemsBM,
|
|
11
11
|
createTestItemsDBM,
|
|
12
12
|
TEST_TABLE,
|
|
13
|
-
testItemBMJsonSchema,
|
|
14
13
|
testItemBMSchema,
|
|
15
14
|
testItemTMSchema,
|
|
16
15
|
} from './test.model.js'
|
|
@@ -27,7 +26,6 @@ export {
|
|
|
27
26
|
runCommonKeyValueDaoTest,
|
|
28
27
|
runCommonKeyValueDBTest,
|
|
29
28
|
TEST_TABLE,
|
|
30
|
-
testItemBMJsonSchema,
|
|
31
29
|
testItemBMSchema,
|
|
32
30
|
testItemTMSchema,
|
|
33
31
|
}
|
|
@@ -1,15 +1,6 @@
|
|
|
1
1
|
import { _range } from '@naturalcycles/js-lib/array/range.js'
|
|
2
2
|
import type { BaseDBEntity, UnixTimestamp } from '@naturalcycles/js-lib/types'
|
|
3
3
|
import { j } from '@naturalcycles/nodejs-lib/ajv'
|
|
4
|
-
import {
|
|
5
|
-
baseDBEntitySchema,
|
|
6
|
-
binarySchema,
|
|
7
|
-
booleanSchema,
|
|
8
|
-
numberSchema,
|
|
9
|
-
type ObjectSchema,
|
|
10
|
-
objectSchema,
|
|
11
|
-
stringSchema,
|
|
12
|
-
} from '@naturalcycles/nodejs-lib/joi'
|
|
13
4
|
|
|
14
5
|
const MOCK_TS_2018_06_21 = 1529539200 as UnixTimestamp
|
|
15
6
|
|
|
@@ -34,36 +25,23 @@ export interface TestItemTM {
|
|
|
34
25
|
even?: boolean
|
|
35
26
|
}
|
|
36
27
|
|
|
37
|
-
export const
|
|
38
|
-
k1:
|
|
39
|
-
|
|
40
|
-
k3: numberSchema.optional(),
|
|
41
|
-
even: booleanSchema.optional(),
|
|
42
|
-
b1: binarySchema.optional(),
|
|
43
|
-
nested: objectSchema({
|
|
44
|
-
foo: numberSchema,
|
|
45
|
-
}).optional(),
|
|
46
|
-
}).concat(baseDBEntitySchema as any)
|
|
47
|
-
|
|
48
|
-
export const testItemTMSchema: ObjectSchema<TestItemTM> = objectSchema<TestItemTM>({
|
|
49
|
-
k1: stringSchema,
|
|
50
|
-
even: booleanSchema.optional(),
|
|
28
|
+
export const testItemTMSchema = j.object<TestItemTM>({
|
|
29
|
+
k1: j.string(),
|
|
30
|
+
even: j.boolean().optional(),
|
|
51
31
|
})
|
|
52
32
|
|
|
53
|
-
export const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
})
|
|
66
|
-
.build()
|
|
33
|
+
export const testItemBMSchema = j.object.dbEntity<TestItemBM>({
|
|
34
|
+
// todo: figure out how to not copy-paste these 3 fields
|
|
35
|
+
id: j.string(), // todo: not strictly needed here
|
|
36
|
+
created: j.number().integer().unixTimestamp(),
|
|
37
|
+
updated: j.number().integer().unixTimestamp(),
|
|
38
|
+
k1: j.string(),
|
|
39
|
+
k2: j.string().nullable().optional(),
|
|
40
|
+
k3: j.number().optional(),
|
|
41
|
+
even: j.boolean().optional(),
|
|
42
|
+
b1: j.buffer().optional(),
|
|
43
|
+
nested: j.object.infer({ foo: j.number() }).optional(),
|
|
44
|
+
})
|
|
67
45
|
|
|
68
46
|
export function createTestItemDBM(num = 1): TestItemDBM {
|
|
69
47
|
return {
|
package/src/validation/index.ts
CHANGED
|
@@ -1,57 +1,63 @@
|
|
|
1
|
+
import type { ObjectWithId } from '@naturalcycles/js-lib/types'
|
|
1
2
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from '@naturalcycles/nodejs-lib/joi'
|
|
12
|
-
import type { CommonDBOptions, CommonDBSaveOptions } from '../db.model.js'
|
|
13
|
-
import type {
|
|
14
|
-
DBQuery,
|
|
15
|
-
DBQueryFilter,
|
|
16
|
-
DBQueryFilterOperator,
|
|
17
|
-
DBQueryOrder,
|
|
3
|
+
j,
|
|
4
|
+
JsonSchemaAnyBuilder,
|
|
5
|
+
type JsonSchemaObjectBuilder,
|
|
6
|
+
} from '@naturalcycles/nodejs-lib/ajv'
|
|
7
|
+
import type { CommonDBOptions, CommonDBSaveOptions, DBTransaction } from '../db.model.js'
|
|
8
|
+
import {
|
|
9
|
+
type DBQueryFilter,
|
|
10
|
+
dbQueryFilterOperatorValues,
|
|
11
|
+
type DBQueryOrder,
|
|
18
12
|
} from '../query/dbQuery.js'
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
})
|
|
13
|
+
|
|
14
|
+
// oxlint-disable typescript/explicit-function-return-type
|
|
15
|
+
|
|
16
|
+
// DBTransaction schema - validates presence without deep validation
|
|
17
|
+
const dbTransactionSchema = j.object.any().castAs<DBTransaction>()
|
|
18
|
+
|
|
19
|
+
// Schema that accepts any value (string, number, boolean, object, array, null)
|
|
20
|
+
const anyValueSchema = new JsonSchemaAnyBuilder<any, any, false>({})
|
|
21
|
+
|
|
22
|
+
export const commonDBOptionsSchema = (): JsonSchemaObjectBuilder<
|
|
23
|
+
CommonDBOptions,
|
|
24
|
+
CommonDBOptions
|
|
25
|
+
> =>
|
|
26
|
+
j.object<CommonDBOptions>({
|
|
27
|
+
tx: dbTransactionSchema.optional(),
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
export const commonDBSaveOptionsSchema = <ROW extends ObjectWithId>() =>
|
|
31
|
+
j.object<CommonDBSaveOptions<ROW>>({
|
|
32
|
+
tx: dbTransactionSchema.optional(),
|
|
33
|
+
excludeFromIndexes: j.array(j.string().castAs<keyof ROW>()).optional(),
|
|
34
|
+
saveMethod: j.enum(['upsert', 'insert', 'update'] as const).optional(),
|
|
35
|
+
assignGeneratedIds: j.boolean().optional(),
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
export const dbQueryFilterOperatorSchema = j.enum(dbQueryFilterOperatorValues)
|
|
39
|
+
|
|
40
|
+
export const dbQueryFilterSchema = <ROW extends ObjectWithId>() =>
|
|
41
|
+
j.object<DBQueryFilter<ROW>>({
|
|
42
|
+
name: j.string().castAs<keyof ROW>(),
|
|
43
|
+
op: dbQueryFilterOperatorSchema,
|
|
44
|
+
val: anyValueSchema,
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
export const dbQueryOrderSchema = <ROW extends ObjectWithId>() =>
|
|
48
|
+
j.object<DBQueryOrder<ROW>>({
|
|
49
|
+
name: j.string().castAs<keyof ROW>(),
|
|
50
|
+
descending: j.boolean().optional(),
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
export const dbQuerySchema = <ROW extends ObjectWithId>() =>
|
|
54
|
+
j.object.infer({
|
|
55
|
+
table: j.string(),
|
|
56
|
+
_filters: j.array(dbQueryFilterSchema<ROW>()).optional(),
|
|
57
|
+
_limitValue: j.number().integer().min(0).optional(),
|
|
58
|
+
_offsetValue: j.number().integer().min(0).optional(),
|
|
59
|
+
_orders: j.array(dbQueryOrderSchema<ROW>()).optional(),
|
|
60
|
+
_startCursor: j.string().optional(),
|
|
61
|
+
_endCursor: j.string().optional(),
|
|
62
|
+
_selectedFieldNames: j.array(j.string().castAs<keyof ROW>()).optional(),
|
|
63
|
+
})
|