@naturalcycles/db-lib 10.3.0 → 10.4.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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Transform } from 'node:stream';
|
|
2
2
|
import type { AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, StringMap, UnixTimestampMillis, Unsaved } from '@naturalcycles/js-lib';
|
|
3
|
-
import {
|
|
3
|
+
import { ZodType } from '@naturalcycles/js-lib/zod';
|
|
4
4
|
import type { ObjectSchema, ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
5
5
|
import { AjvSchema } from '@naturalcycles/nodejs-lib';
|
|
6
6
|
import type { CommonDBTransactionOptions, DBTransaction, RunQueryResult } from '../db.model.js';
|
|
@@ -160,7 +160,7 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
160
160
|
* Does NOT mutate the object.
|
|
161
161
|
* Validates (unless `skipValidation=true` passed).
|
|
162
162
|
*/
|
|
163
|
-
validateAndConvert<T>(obj: Partial<T>, schema: ObjectSchema<T> | AjvSchema<T> |
|
|
163
|
+
validateAndConvert<T>(obj: Partial<T>, schema: ObjectSchema<T> | AjvSchema<T> | ZodType<T> | undefined, op?: 'load' | 'save', // this is to skip validation if validateOnLoad/Save is false
|
|
164
164
|
opt?: CommonDaoOptions): any;
|
|
165
165
|
getTableSchema(): Promise<JsonSchemaRootObject<DBM>>;
|
|
166
166
|
createTable(schema: JsonSchemaObject<DBM>, opt?: CommonDaoCreateOptions): Promise<void>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { _assert, _deepCopy, _deepJsonEquals, _filterUndefinedValues, _isTruthy, _objectAssignExact, _passthroughPredicate, _since, _truncate, _typeCast, _uniqBy, AppError, ErrorMode, localTime, pMap, SKIP, } from '@naturalcycles/js-lib';
|
|
2
|
-
import {
|
|
2
|
+
import { ZodType, zSafeValidate } from '@naturalcycles/js-lib/zod';
|
|
3
3
|
import { _pipeline, AjvSchema, getValidationResult, stringId, transformChunk, transformLogProgress, transformMap, transformNoOp, writableVoid, } from '@naturalcycles/nodejs-lib';
|
|
4
4
|
import { DBLibError } from '../cnst.js';
|
|
5
5
|
import { RunnableDBQuery } from '../query/dbQuery.js';
|
|
@@ -953,7 +953,7 @@ export class CommonDao {
|
|
|
953
953
|
const objectName = table;
|
|
954
954
|
let error;
|
|
955
955
|
let convertedValue;
|
|
956
|
-
if (schema instanceof
|
|
956
|
+
if (schema instanceof ZodType) {
|
|
957
957
|
// Zod schema
|
|
958
958
|
const vr = zSafeValidate(obj, schema);
|
|
959
959
|
error = vr.error;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BaseDBEntity, CommonLogger, ErrorMode, NumberOfMilliseconds, Promisable, UnixTimestamp } from '@naturalcycles/js-lib';
|
|
2
|
-
import type {
|
|
2
|
+
import type { ZodType, ZodValidationError } from '@naturalcycles/js-lib/zod';
|
|
3
3
|
import type { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchema, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib';
|
|
4
4
|
import type { CommonDB } from '../common.db.js';
|
|
5
5
|
import type { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model.js';
|
|
@@ -74,7 +74,7 @@ export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntit
|
|
|
74
74
|
* Return original `err` to pass the error through (will be thrown in CommonDao).
|
|
75
75
|
* Return modified/new `Error` if needed.
|
|
76
76
|
*/
|
|
77
|
-
onValidationError: (err: JoiValidationError | AjvValidationError |
|
|
77
|
+
onValidationError: (err: JoiValidationError | AjvValidationError | ZodValidationError) => Error | false;
|
|
78
78
|
}
|
|
79
79
|
export declare enum CommonDaoLogLevel {
|
|
80
80
|
/**
|
|
@@ -100,7 +100,7 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
100
100
|
/**
|
|
101
101
|
* Joi, AjvSchema or ZodSchema is supported.
|
|
102
102
|
*/
|
|
103
|
-
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> |
|
|
103
|
+
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodType<BM>;
|
|
104
104
|
excludeFromIndexes?: (keyof DBM)[];
|
|
105
105
|
/**
|
|
106
106
|
* Defaults to true.
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
Promisable,
|
|
7
7
|
UnixTimestamp,
|
|
8
8
|
} from '@naturalcycles/js-lib'
|
|
9
|
-
import type {
|
|
9
|
+
import type { ZodType, ZodValidationError } from '@naturalcycles/js-lib/zod'
|
|
10
10
|
import type {
|
|
11
11
|
AjvSchema,
|
|
12
12
|
AjvValidationError,
|
|
@@ -97,7 +97,9 @@ export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntit
|
|
|
97
97
|
* Return original `err` to pass the error through (will be thrown in CommonDao).
|
|
98
98
|
* Return modified/new `Error` if needed.
|
|
99
99
|
*/
|
|
100
|
-
onValidationError: (
|
|
100
|
+
onValidationError: (
|
|
101
|
+
err: JoiValidationError | AjvValidationError | ZodValidationError,
|
|
102
|
+
) => Error | false
|
|
101
103
|
}
|
|
102
104
|
|
|
103
105
|
export enum CommonDaoLogLevel {
|
|
@@ -130,7 +132,7 @@ export interface CommonDaoCfg<
|
|
|
130
132
|
/**
|
|
131
133
|
* Joi, AjvSchema or ZodSchema is supported.
|
|
132
134
|
*/
|
|
133
|
-
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> |
|
|
135
|
+
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodType<BM>
|
|
134
136
|
|
|
135
137
|
excludeFromIndexes?: (keyof DBM)[]
|
|
136
138
|
|
|
@@ -29,7 +29,7 @@ import {
|
|
|
29
29
|
SKIP,
|
|
30
30
|
} from '@naturalcycles/js-lib'
|
|
31
31
|
import type { ZodValidationError } from '@naturalcycles/js-lib/zod'
|
|
32
|
-
import {
|
|
32
|
+
import { ZodType, zSafeValidate } from '@naturalcycles/js-lib/zod'
|
|
33
33
|
import type {
|
|
34
34
|
AjvValidationError,
|
|
35
35
|
JoiValidationError,
|
|
@@ -1216,7 +1216,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
1216
1216
|
*/
|
|
1217
1217
|
validateAndConvert<T>(
|
|
1218
1218
|
obj: Partial<T>,
|
|
1219
|
-
schema: ObjectSchema<T> | AjvSchema<T> |
|
|
1219
|
+
schema: ObjectSchema<T> | AjvSchema<T> | ZodType<T> | undefined,
|
|
1220
1220
|
op?: 'load' | 'save', // this is to skip validation if validateOnLoad/Save is false
|
|
1221
1221
|
opt: CommonDaoOptions = {},
|
|
1222
1222
|
): any {
|
|
@@ -1245,10 +1245,10 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
1245
1245
|
const table = opt.table || this.cfg.table
|
|
1246
1246
|
const objectName = table
|
|
1247
1247
|
|
|
1248
|
-
let error: JoiValidationError | AjvValidationError | ZodValidationError
|
|
1248
|
+
let error: JoiValidationError | AjvValidationError | ZodValidationError | undefined
|
|
1249
1249
|
let convertedValue: any
|
|
1250
1250
|
|
|
1251
|
-
if (schema instanceof
|
|
1251
|
+
if (schema instanceof ZodType) {
|
|
1252
1252
|
// Zod schema
|
|
1253
1253
|
const vr = zSafeValidate(obj as T, schema)
|
|
1254
1254
|
error = vr.error
|