Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@naturalcycles/db-lib

Package Overview
Dependencies
Maintainers
2
Versions
301
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@naturalcycles/db-lib - npm Package Compare versions

Comparing version 9.5.0 to 9.6.0

4

dist/commondao/common.dao.d.ts

@@ -5,3 +5,3 @@ /// <reference types="node" />

import { AjvSchema, ObjectSchema, ReadableTyped } from '@naturalcycles/nodejs-lib';
import { CommonDBTransactionOptions, DBModelType, DBPatch, DBTransaction, RunQueryResult } from '../db.model';
import { CommonDBTransactionOptions, DBPatch, DBTransaction, RunQueryResult } from '../db.model';
import { DBQuery, RunnableDBQuery } from '../query/dbQuery';

@@ -165,3 +165,3 @@ import { CommonDaoCfg, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoSaveBatchOptions, CommonDaoSaveOptions, CommonDaoStreamDeleteOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, CommonDaoStreamSaveOptions } from './common.dao.model';

*/
validateAndConvert<T>(obj: Partial<T>, schema: ObjectSchema<T> | AjvSchema<T> | ZodSchema<T> | undefined, modelType?: DBModelType, opt?: CommonDaoOptions): any;
validateAndConvert<T>(obj: Partial<T>, schema: ObjectSchema<T> | AjvSchema<T> | ZodSchema<T> | undefined, opt?: CommonDaoOptions): any;
getTableSchema(): Promise<JsonSchemaRootObject<DBM>>;

@@ -168,0 +168,0 @@ createTable(schema: JsonSchemaObject<DBM>, opt?: CommonDaoCreateOptions): Promise<void>;

@@ -7,3 +7,2 @@ "use strict";

const cnst_1 = require("../cnst");
const db_model_1 = require("../db.model");
const dbQuery_1 = require("../query/dbQuery");

@@ -37,5 +36,2 @@ const common_dao_model_1 = require("./common.dao.model");

beforeCreate: bm => bm,
beforeDBMValidate: dbm => dbm,
beforeDBMToBM: dbm => dbm,
beforeBMToDBM: bm => bm,
anonymize: dbm => dbm,

@@ -58,3 +54,3 @@ onValidationError: err => err,

this.assignIdCreatedUpdated(bm, opt);
return this.validateAndConvert(bm, this.cfg.bmSchema, db_model_1.DBModelType.BM, opt);
return this.validateAndConvert(bm, this.cfg.bmSchema, opt);
}

@@ -68,6 +64,6 @@ async getById(id, opt = {}) {

let dbm = (await (opt.tx || this.cfg.db).getByIds(table, [id]))[0];
if (dbm && !opt.raw && this.cfg.hooks.afterLoad) {
if (dbm && this.cfg.hooks.afterLoad) {
dbm = (await this.cfg.hooks.afterLoad(dbm)) || undefined;
}
const bm = opt.raw ? dbm : await this.dbmToBM(dbm, opt);
const bm = await this.dbmToBM(dbm, opt);
this.logResult(started, op, bm, table);

@@ -96,8 +92,6 @@ return bm || null;

let [dbm] = await (opt.tx || this.cfg.db).getByIds(table, [id]);
if (dbm && !opt.raw && this.cfg.hooks.afterLoad) {
if (dbm && this.cfg.hooks.afterLoad) {
dbm = (await this.cfg.hooks.afterLoad(dbm)) || undefined;
}
if (!opt.raw) {
dbm = this.anyToDBM(dbm, opt);
}
dbm = this.anyToDBM(dbm, opt);
this.logResult(started, op, dbm, table);

@@ -113,6 +107,6 @@ return dbm || null;

let dbms = await (opt.tx || this.cfg.db).getByIds(table, ids);
if (!opt.raw && this.cfg.hooks.afterLoad && dbms.length) {
if (this.cfg.hooks.afterLoad && dbms.length) {
dbms = (await (0, js_lib_1.pMap)(dbms, async (dbm) => await this.cfg.hooks.afterLoad(dbm))).filter(js_lib_1._isTruthy);
}
const bms = opt.raw ? dbms : await this.dbmsToBM(dbms, opt);
const bms = await this.dbmsToBM(dbms, opt);
this.logResult(started, op, bms, table);

@@ -128,3 +122,3 @@ return bms;

let dbms = await (opt.tx || this.cfg.db).getByIds(table, ids);
if (!opt.raw && this.cfg.hooks.afterLoad && dbms.length) {
if (this.cfg.hooks.afterLoad && dbms.length) {
dbms = (await (0, js_lib_1.pMap)(dbms, async (dbm) => await this.cfg.hooks.afterLoad(dbm))).filter(js_lib_1._isTruthy);

@@ -228,6 +222,6 @@ }

const partialQuery = !!q._selectedFieldNames;
if (!opt.raw && this.cfg.hooks.afterLoad && rows.length) {
if (this.cfg.hooks.afterLoad && rows.length) {
rows = (await (0, js_lib_1.pMap)(rows, async (dbm) => await this.cfg.hooks.afterLoad(dbm))).filter(js_lib_1._isTruthy);
}
const bms = partialQuery || opt.raw ? rows : await this.dbmsToBM(rows, opt);
const bms = partialQuery ? rows : await this.dbmsToBM(rows, opt);
this.logResult(started, op, bms, q.table);

@@ -248,7 +242,7 @@ return {

let { rows, ...queryResult } = await this.cfg.db.runQuery(q, opt);
if (!opt.raw && this.cfg.hooks.afterLoad && rows.length) {
if (this.cfg.hooks.afterLoad && rows.length) {
rows = (await (0, js_lib_1.pMap)(rows, async (dbm) => await this.cfg.hooks.afterLoad(dbm))).filter(js_lib_1._isTruthy);
}
const partialQuery = !!q._selectedFieldNames;
const dbms = partialQuery || opt.raw ? rows : this.anyToDBMs(rows, opt);
const dbms = partialQuery ? rows : this.anyToDBMs(rows, opt);
this.logResult(started, op, dbms, q.table);

@@ -280,3 +274,3 @@ return { rows: dbms, ...queryResult };

count++;
if (partialQuery || opt.raw)
if (partialQuery)
return dbm;

@@ -320,3 +314,3 @@ if (this.cfg.hooks.afterLoad) {

count++;
if (partialQuery || opt.raw)
if (partialQuery)
return dbm;

@@ -357,3 +351,3 @@ if (this.cfg.hooks.afterLoad) {

const stream = this.cfg.db.streamQuery(q, opt);
if (partialQuery || opt.raw)
if (partialQuery)
return stream;

@@ -389,3 +383,3 @@ return stream

const partialQuery = !!q._selectedFieldNames;
if (partialQuery || opt.raw)
if (partialQuery)
return stream;

@@ -485,3 +479,3 @@ return (stream

(0, js_lib_1._typeCast)(bm);
let dbm = await this.bmToDBM(bm, opt);
let dbm = await this.bmToDBM(bm, opt); // validates BM
if (this.cfg.hooks.beforeSave) {

@@ -613,10 +607,7 @@ dbm = (await this.cfg.hooks.beforeSave(dbm));

// will override/set `updated` field, unless opts.preserveUpdated is set
let row = dbm;
if (!opt.raw) {
const idWasGenerated = !dbm.id && this.cfg.generateId;
this.assignIdCreatedUpdated(dbm, opt); // mutates
row = this.anyToDBM(dbm, opt);
if (opt.ensureUniqueId && idWasGenerated)
await this.ensureUniqueId(table, row);
}
const idWasGenerated = !dbm.id && this.cfg.generateId;
this.assignIdCreatedUpdated(dbm, opt); // mutates
let row = this.anyToDBM(dbm, opt);
if (opt.ensureUniqueId && idWasGenerated)
await this.ensureUniqueId(table, row);
if (this.cfg.immutable && !opt.allowMutability && !opt.saveMethod) {

@@ -683,9 +674,6 @@ opt = { ...opt, saveMethod: 'insert' };

const table = opt.table || this.cfg.table;
let rows = dbms;
if (!opt.raw) {
dbms.forEach(dbm => this.assignIdCreatedUpdated(dbm, opt)); // mutates
rows = this.anyToDBMs(dbms, opt);
if (opt.ensureUniqueId)
throw new js_lib_1.AppError('ensureUniqueId is not supported in saveBatch');
}
dbms.forEach(dbm => this.assignIdCreatedUpdated(dbm, opt)); // mutates
let rows = this.anyToDBMs(dbms, opt);
if (opt.ensureUniqueId)
throw new js_lib_1.AppError('ensureUniqueId is not supported in saveBatch');
if (this.cfg.immutable && !opt.allowMutability && !opt.saveMethod) {

@@ -861,5 +849,11 @@ opt = { ...opt, saveMethod: 'insert' };

// DBM > BM
const bm = await this.cfg.hooks.beforeDBMToBM(dbm);
let bm;
if (this.cfg.hooks.beforeDBMToBM) {
bm = await this.cfg.hooks.beforeDBMToBM(dbm);
}
else {
bm = dbm;
}
// Validate/convert BM
return this.validateAndConvert(bm, this.cfg.bmSchema, db_model_1.DBModelType.BM, opt);
return this.validateAndConvert(bm, this.cfg.bmSchema, opt);
}

@@ -872,12 +866,17 @@ async dbmsToBM(dbms, opt = {}) {

return;
// optimization: no need to run the BM validation, since DBM will be validated anyway
// Validate/convert BM
// bm gets assigned to the new reference
// bm = this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
// should not do it on load, but only on save!
// this.assignIdCreatedUpdated(bm, opt)
// bm gets assigned to the new reference
bm = this.validateAndConvert(bm, this.cfg.bmSchema, opt);
// BM > DBM
const dbm = { ...(await this.cfg.hooks.beforeBMToDBM(bm)) };
let dbm;
if (this.cfg.hooks.beforeBMToDBM) {
dbm = { ...(await this.cfg.hooks.beforeBMToDBM(bm)) };
}
else {
dbm = bm;
}
// Validate/convert DBM
return this.validateAndConvert(dbm, this.cfg.dbmSchema, db_model_1.DBModelType.DBM, opt);
// return this.validateAndConvert(dbm, this.cfg.dbmSchema, DBModelType.DBM, opt)
return dbm;
}

@@ -894,2 +893,4 @@ async bmsToDBM(bms, opt = {}) {

dbm = { ...dbm, ...this.cfg.hooks.parseNaturalId(dbm.id) };
// todo: is this the right place?
// todo: is anyToDBM even needed?
if (opt.anonymize) {

@@ -899,3 +900,4 @@ dbm = this.cfg.hooks.anonymize(dbm);

// Validate/convert DBM
return this.validateAndConvert(dbm, this.cfg.dbmSchema, db_model_1.DBModelType.DBM, opt);
// return this.validateAndConvert(dbm, this.cfg.dbmSchema, DBModelType.DBM, opt)
return dbm;
}

@@ -911,6 +913,3 @@ anyToDBMs(entities, opt = {}) {

*/
validateAndConvert(obj, schema, modelType, opt = {}) {
// `raw` option completely bypasses any processing
if (opt.raw)
return obj;
validateAndConvert(obj, schema, opt = {}) {
// Kirill 2021-10-18: I realized that there's little reason to keep removing null values

@@ -925,12 +924,3 @@ // So, from now on we'll preserve them

// and they can be annoying with snapshot tests
if (this.cfg.filterNullishValues) {
obj = (0, js_lib_1._filterNullishValues)(obj);
}
else {
obj = (0, js_lib_1._filterUndefinedValues)(obj);
}
// Pre-validation hooks
if (modelType === db_model_1.DBModelType.DBM) {
obj = this.cfg.hooks.beforeDBMValidate(obj);
}
obj = (0, js_lib_1._filterUndefinedValues)(obj);
// Return as is if no schema is passed or if `skipConversion` is set

@@ -942,3 +932,3 @@ if (!schema || opt.skipConversion) {

const table = opt.table || this.cfg.table;
const objectName = table + (modelType || '');
const objectName = table;
let error;

@@ -945,0 +935,0 @@ let convertedValue;

@@ -31,12 +31,2 @@ import { BaseDBEntity, CommonLogger, ErrorMode, Promisable, ZodError, ZodSchema } from '@naturalcycles/js-lib';

beforeCreate: (bm: Partial<BM>) => Partial<BM>;
/**
* Called when loading things "as DBM" and validation is not skipped.
* When loading things as BM/TM - other hooks get involved instead:
* - beforeDBMToBM
* - beforeBMToTM
*
* TODO: maybe rename those to `validateAs(model)`
* as it only validates "final state", not intermediate
*/
beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM>;
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>;

@@ -113,3 +103,2 @@ beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>;

*/
dbmSchema?: ObjectSchema<DBM> | AjvSchema<DBM> | ZodSchema<DBM>;
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodSchema<BM>;

@@ -167,12 +156,2 @@ excludeFromIndexes?: (keyof DBM)[];

/**
* Default is false.
* If true - will run `_filterNullishValues` inside `validateAndConvert` function
* (instead of `_filterUndefinedValues`).
* This was the old db-lib behavior.
* This option allows to keep backwards-compatible behavior.
*
* @deprecated
*/
filterNullishValues?: boolean;
/**
* Defaults to false.

@@ -205,13 +184,4 @@ * If true - run patch operations (patch, patchById) in a Transaction.

/**
* If true - will SKIP ANY transformation/processing, will return DB objects as they are. Will also skip created/updated/id
* generation.
*
* Useful for performance/streaming/pipelines.
*
* @default false
*/
raw?: boolean;
/**
* @default false
*/
preserveUpdatedCreated?: boolean;

@@ -218,0 +188,0 @@ /**

@@ -16,3 +16,2 @@ "use strict";

db,
dbmSchema: test_model_1.testItemDBMSchema,
bmSchema: test_model_1.testItemBMSchema,

@@ -36,3 +35,3 @@ logStarted: true,

test('createTable, dropIfExists=true', async () => {
await dao.createTable(test_model_1.testItemDBMJsonSchema, { dropIfExists: true });
await dao.createTable(test_model_1.testItemBMJsonSchema, { dropIfExists: true });
});

@@ -39,0 +38,0 @@ }

@@ -23,3 +23,3 @@ "use strict";

test('createTable, dropIfExists=true', async () => {
await db.createTable(test_model_1.TEST_TABLE, test_model_1.testItemDBMJsonSchema, { dropIfExists: true });
await db.createTable(test_model_1.TEST_TABLE, test_model_1.testItemBMJsonSchema, { dropIfExists: true });
});

@@ -26,0 +26,0 @@ }

import { runCommonDaoTest } from './daoTest';
import { CommonDBImplementationQuirks, runCommonDBTest } from './dbTest';
import { runCommonKeyValueDBTest } from './keyValueDBTest';
import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TestItemBM, testItemBMJsonSchema, testItemBMSchema, TestItemDBM, testItemDBMJsonSchema, testItemDBMSchema, TestItemTM, testItemTMSchema, TEST_TABLE } from './test.model';
import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TestItemBM, testItemBMJsonSchema, testItemBMSchema, TestItemDBM, TestItemTM, testItemTMSchema, TEST_TABLE } from './test.model';
export type { TestItemDBM, TestItemBM, TestItemTM, CommonDBImplementationQuirks };
export { TEST_TABLE, createTestItemDBM, createTestItemBM, createTestItemsDBM, createTestItemsBM, testItemDBMSchema, testItemBMSchema, testItemTMSchema, testItemBMJsonSchema, testItemDBMJsonSchema, runCommonDBTest, runCommonDaoTest, runCommonKeyValueDBTest, };
export { TEST_TABLE, createTestItemDBM, createTestItemBM, createTestItemsDBM, createTestItemsBM, testItemBMSchema, testItemTMSchema, testItemBMJsonSchema, runCommonDBTest, runCommonDaoTest, runCommonKeyValueDBTest, };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runCommonKeyValueDBTest = exports.runCommonDaoTest = exports.runCommonDBTest = exports.testItemDBMJsonSchema = exports.testItemBMJsonSchema = exports.testItemTMSchema = exports.testItemBMSchema = exports.testItemDBMSchema = exports.createTestItemsBM = exports.createTestItemsDBM = exports.createTestItemBM = exports.createTestItemDBM = exports.TEST_TABLE = void 0;
exports.runCommonKeyValueDBTest = exports.runCommonDaoTest = exports.runCommonDBTest = exports.testItemBMJsonSchema = exports.testItemTMSchema = exports.testItemBMSchema = exports.createTestItemsBM = exports.createTestItemsDBM = exports.createTestItemBM = exports.createTestItemDBM = exports.TEST_TABLE = void 0;
const daoTest_1 = require("./daoTest");

@@ -17,5 +17,3 @@ Object.defineProperty(exports, "runCommonDaoTest", { enumerable: true, get: function () { return daoTest_1.runCommonDaoTest; } });

Object.defineProperty(exports, "testItemBMSchema", { enumerable: true, get: function () { return test_model_1.testItemBMSchema; } });
Object.defineProperty(exports, "testItemDBMJsonSchema", { enumerable: true, get: function () { return test_model_1.testItemDBMJsonSchema; } });
Object.defineProperty(exports, "testItemDBMSchema", { enumerable: true, get: function () { return test_model_1.testItemDBMSchema; } });
Object.defineProperty(exports, "testItemTMSchema", { enumerable: true, get: function () { return test_model_1.testItemTMSchema; } });
Object.defineProperty(exports, "TEST_TABLE", { enumerable: true, get: function () { return test_model_1.TEST_TABLE; } });

@@ -18,6 +18,4 @@ /// <reference types="node" />

export declare const testItemBMSchema: import("joi").ObjectSchema<TestItemBM>;
export declare const testItemDBMSchema: import("joi").ObjectSchema<TestItemDBM>;
export declare const testItemTMSchema: import("joi").ObjectSchema<TestItemTM>;
export declare const testItemBMJsonSchema: JsonSchemaObject<TestItemBM>;
export declare const testItemDBMJsonSchema: JsonSchemaObject<TestItemDBM>;
export declare function createTestItemDBM(num?: number): TestItemDBM;

@@ -24,0 +22,0 @@ export declare function createTestItemBM(num?: number): TestItemBM;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTestItemsBM = exports.createTestItemsDBM = exports.createTestItemBM = exports.createTestItemDBM = exports.testItemDBMJsonSchema = exports.testItemBMJsonSchema = exports.testItemTMSchema = exports.testItemDBMSchema = exports.testItemBMSchema = exports.TEST_TABLE = void 0;
exports.createTestItemsBM = exports.createTestItemsDBM = exports.createTestItemBM = exports.createTestItemDBM = exports.testItemBMJsonSchema = exports.testItemTMSchema = exports.testItemBMSchema = exports.TEST_TABLE = void 0;
const js_lib_1 = require("@naturalcycles/js-lib");

@@ -15,9 +15,2 @@ const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");

}).concat(nodejs_lib_1.baseDBEntitySchema);
exports.testItemDBMSchema = (0, nodejs_lib_1.objectSchema)({
k1: nodejs_lib_1.stringSchema,
k2: nodejs_lib_1.stringSchema.allow(null).optional(),
k3: nodejs_lib_1.numberSchema.optional(),
even: nodejs_lib_1.booleanSchema.optional(),
b1: nodejs_lib_1.binarySchema.optional(),
}).concat(nodejs_lib_1.baseDBEntitySchema);
exports.testItemTMSchema = (0, nodejs_lib_1.objectSchema)({

@@ -41,15 +34,2 @@ k1: nodejs_lib_1.stringSchema,

.build();
exports.testItemDBMJsonSchema = js_lib_1.jsonSchema
.rootObject({
// todo: figure out how to not copy-paste these 3 fields
id: js_lib_1.jsonSchema.string(),
created: js_lib_1.jsonSchema.unixTimestamp(),
updated: js_lib_1.jsonSchema.unixTimestamp(),
k1: js_lib_1.jsonSchema.string(),
k2: js_lib_1.jsonSchema.string().optional(),
k3: js_lib_1.jsonSchema.number().optional(),
even: js_lib_1.jsonSchema.boolean().optional(),
b1: js_lib_1.jsonSchema.buffer().optional(),
})
.build();
function createTestItemDBM(num = 1) {

@@ -56,0 +36,0 @@ return {

@@ -43,3 +43,3 @@ {

},
"version": "9.5.0",
"version": "9.6.0",
"description": "Lowest Common Denominator API to supported Databases",

@@ -46,0 +46,0 @@ "keywords": [

@@ -50,13 +50,2 @@ import {

/**
* Called when loading things "as DBM" and validation is not skipped.
* When loading things as BM/TM - other hooks get involved instead:
* - beforeDBMToBM
* - beforeBMToTM
*
* TODO: maybe rename those to `validateAs(model)`
* as it only validates "final state", not intermediate
*/
beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM>
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>

@@ -140,3 +129,2 @@ beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>

*/
dbmSchema?: ObjectSchema<DBM> | AjvSchema<DBM> | ZodSchema<DBM>
bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodSchema<BM>

@@ -207,13 +195,2 @@

/**
* Default is false.
* If true - will run `_filterNullishValues` inside `validateAndConvert` function
* (instead of `_filterUndefinedValues`).
* This was the old db-lib behavior.
* This option allows to keep backwards-compatible behavior.
*
* @deprecated
*/
filterNullishValues?: boolean
/**
* Defaults to false.

@@ -249,14 +226,4 @@ * If true - run patch operations (patch, patchById) in a Transaction.

/**
* If true - will SKIP ANY transformation/processing, will return DB objects as they are. Will also skip created/updated/id
* generation.
*
* Useful for performance/streaming/pipelines.
*
* @default false
*/
raw?: boolean
/**
* @default false
*/
preserveUpdatedCreated?: boolean

@@ -263,0 +230,0 @@

@@ -5,3 +5,2 @@ import { Transform } from 'node:stream'

_deepJsonEquals,
_filterNullishValues,
_filterUndefinedValues,

@@ -47,9 +46,3 @@ _isTruthy,

import { DBLibError } from '../cnst'
import {
CommonDBTransactionOptions,
DBModelType,
DBPatch,
DBTransaction,
RunQueryResult,
} from '../db.model'
import { CommonDBTransactionOptions, DBPatch, DBTransaction, RunQueryResult } from '../db.model'
import { DBQuery, RunnableDBQuery } from '../query/dbQuery'

@@ -96,5 +89,2 @@ import {

beforeCreate: bm => bm as BM,
beforeDBMValidate: dbm => dbm,
beforeDBMToBM: dbm => dbm as any,
beforeBMToDBM: bm => bm as any,
anonymize: dbm => dbm,

@@ -118,3 +108,3 @@ onValidationError: err => err,

this.assignIdCreatedUpdated(bm, opt)
return this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
return this.validateAndConvert(bm, this.cfg.bmSchema, opt)
}

@@ -132,7 +122,7 @@

let dbm = (await (opt.tx || this.cfg.db).getByIds<DBM>(table, [id]))[0]
if (dbm && !opt.raw && this.cfg.hooks!.afterLoad) {
if (dbm && this.cfg.hooks!.afterLoad) {
dbm = (await this.cfg.hooks!.afterLoad(dbm)) || undefined
}
const bm = opt.raw ? (dbm as any) : await this.dbmToBM(dbm, opt)
const bm = await this.dbmToBM(dbm, opt)
this.logResult(started, op, bm, table)

@@ -169,9 +159,7 @@ return bm || null

let [dbm] = await (opt.tx || this.cfg.db).getByIds<DBM>(table, [id])
if (dbm && !opt.raw && this.cfg.hooks!.afterLoad) {
if (dbm && this.cfg.hooks!.afterLoad) {
dbm = (await this.cfg.hooks!.afterLoad(dbm)) || undefined
}
if (!opt.raw) {
dbm = this.anyToDBM(dbm!, opt)
}
dbm = this.anyToDBM(dbm!, opt)
this.logResult(started, op, dbm, table)

@@ -187,3 +175,3 @@ return dbm || null

let dbms = await (opt.tx || this.cfg.db).getByIds<DBM>(table, ids)
if (!opt.raw && this.cfg.hooks!.afterLoad && dbms.length) {
if (this.cfg.hooks!.afterLoad && dbms.length) {
dbms = (await pMap(dbms, async dbm => await this.cfg.hooks!.afterLoad!(dbm))).filter(

@@ -194,3 +182,3 @@ _isTruthy,

const bms = opt.raw ? (dbms as any) : await this.dbmsToBM(dbms, opt)
const bms = await this.dbmsToBM(dbms, opt)
this.logResult(started, op, bms, table)

@@ -206,3 +194,3 @@ return bms

let dbms = await (opt.tx || this.cfg.db).getByIds<DBM>(table, ids)
if (!opt.raw && this.cfg.hooks!.afterLoad && dbms.length) {
if (this.cfg.hooks!.afterLoad && dbms.length) {
dbms = (await pMap(dbms, async dbm => await this.cfg.hooks!.afterLoad!(dbm))).filter(

@@ -330,3 +318,3 @@ _isTruthy,

const partialQuery = !!q._selectedFieldNames
if (!opt.raw && this.cfg.hooks!.afterLoad && rows.length) {
if (this.cfg.hooks!.afterLoad && rows.length) {
rows = (await pMap(rows, async dbm => await this.cfg.hooks!.afterLoad!(dbm))).filter(

@@ -337,3 +325,3 @@ _isTruthy,

const bms = partialQuery || opt.raw ? (rows as any[]) : await this.dbmsToBM(rows, opt)
const bms = partialQuery ? (rows as any[]) : await this.dbmsToBM(rows, opt)
this.logResult(started, op, bms, q.table)

@@ -359,3 +347,3 @@ return {

let { rows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
if (!opt.raw && this.cfg.hooks!.afterLoad && rows.length) {
if (this.cfg.hooks!.afterLoad && rows.length) {
rows = (await pMap(rows, async dbm => await this.cfg.hooks!.afterLoad!(dbm))).filter(

@@ -367,3 +355,3 @@ _isTruthy,

const partialQuery = !!q._selectedFieldNames
const dbms = partialQuery || opt.raw ? rows : this.anyToDBMs(rows, opt)
const dbms = partialQuery ? rows : this.anyToDBMs(rows, opt)
this.logResult(started, op, dbms, q.table)

@@ -404,3 +392,3 @@ return { rows: dbms, ...queryResult }

count++
if (partialQuery || opt.raw) return dbm as any
if (partialQuery) return dbm as any

@@ -455,3 +443,3 @@ if (this.cfg.hooks!.afterLoad) {

count++
if (partialQuery || opt.raw) return dbm
if (partialQuery) return dbm

@@ -498,3 +486,3 @@ if (this.cfg.hooks!.afterLoad) {

const stream = this.cfg.db.streamQuery<DBM>(q, opt)
if (partialQuery || opt.raw) return stream
if (partialQuery) return stream

@@ -537,3 +525,3 @@ return stream

const partialQuery = !!q._selectedFieldNames
if (partialQuery || opt.raw) return stream
if (partialQuery) return stream

@@ -661,3 +649,3 @@ return (

_typeCast<BM>(bm)
let dbm = await this.bmToDBM(bm, opt)
let dbm = await this.bmToDBM(bm, opt) // validates BM

@@ -821,9 +809,7 @@ if (this.cfg.hooks!.beforeSave) {

// will override/set `updated` field, unless opts.preserveUpdated is set
let row = dbm as DBM
if (!opt.raw) {
const idWasGenerated = !dbm.id && this.cfg.generateId
this.assignIdCreatedUpdated(dbm, opt) // mutates
row = this.anyToDBM(dbm, opt)
if (opt.ensureUniqueId && idWasGenerated) await this.ensureUniqueId(table, row)
}
const idWasGenerated = !dbm.id && this.cfg.generateId
this.assignIdCreatedUpdated(dbm, opt) // mutates
let row = this.anyToDBM(dbm, opt)
if (opt.ensureUniqueId && idWasGenerated) await this.ensureUniqueId(table, row)
if (this.cfg.immutable && !opt.allowMutability && !opt.saveMethod) {

@@ -907,8 +893,6 @@ opt = { ...opt, saveMethod: 'insert' }

const table = opt.table || this.cfg.table
let rows = dbms as DBM[]
if (!opt.raw) {
dbms.forEach(dbm => this.assignIdCreatedUpdated(dbm, opt)) // mutates
rows = this.anyToDBMs(dbms as DBM[], opt)
if (opt.ensureUniqueId) throw new AppError('ensureUniqueId is not supported in saveBatch')
}
dbms.forEach(dbm => this.assignIdCreatedUpdated(dbm, opt)) // mutates
let rows = this.anyToDBMs(dbms as DBM[], opt)
if (opt.ensureUniqueId) throw new AppError('ensureUniqueId is not supported in saveBatch')
if (this.cfg.immutable && !opt.allowMutability && !opt.saveMethod) {

@@ -1142,7 +1126,12 @@ opt = { ...opt, saveMethod: 'insert' }

// DBM > BM
const bm = await this.cfg.hooks!.beforeDBMToBM!(dbm)
let bm: Partial<BM>
if (this.cfg.hooks!.beforeDBMToBM) {
bm = await this.cfg.hooks!.beforeDBMToBM(dbm)
} else {
bm = dbm as any
}
// Validate/convert BM
return this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
return this.validateAndConvert(bm, this.cfg.bmSchema, opt)
}

@@ -1163,16 +1152,19 @@

// optimization: no need to run the BM validation, since DBM will be validated anyway
// Validate/convert BM
// bm gets assigned to the new reference
// bm = this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
// should not do it on load, but only on save!
// this.assignIdCreatedUpdated(bm, opt)
// bm gets assigned to the new reference
bm = this.validateAndConvert(bm, this.cfg.bmSchema, opt)
// BM > DBM
const dbm = { ...(await this.cfg.hooks!.beforeBMToDBM!(bm)) }
let dbm: DBM
if (this.cfg.hooks!.beforeBMToDBM) {
dbm = { ...((await this.cfg.hooks!.beforeBMToDBM(bm!)) as DBM) }
} else {
dbm = bm as any
}
// Validate/convert DBM
return this.validateAndConvert(dbm, this.cfg.dbmSchema, DBModelType.DBM, opt)
// return this.validateAndConvert(dbm, this.cfg.dbmSchema, DBModelType.DBM, opt)
return dbm
}

@@ -1195,2 +1187,4 @@

// todo: is this the right place?
// todo: is anyToDBM even needed?
if (opt.anonymize) {

@@ -1201,3 +1195,4 @@ dbm = this.cfg.hooks!.anonymize!(dbm)

// Validate/convert DBM
return this.validateAndConvert(dbm, this.cfg.dbmSchema, DBModelType.DBM, opt)
// return this.validateAndConvert(dbm, this.cfg.dbmSchema, DBModelType.DBM, opt)
return dbm
}

@@ -1218,8 +1213,4 @@

schema: ObjectSchema<T> | AjvSchema<T> | ZodSchema<T> | undefined,
modelType?: DBModelType,
opt: CommonDaoOptions = {},
): any {
// `raw` option completely bypasses any processing
if (opt.raw) return obj as any
// Kirill 2021-10-18: I realized that there's little reason to keep removing null values

@@ -1234,13 +1225,4 @@ // So, from now on we'll preserve them

// and they can be annoying with snapshot tests
if (this.cfg.filterNullishValues) {
obj = _filterNullishValues(obj)
} else {
obj = _filterUndefinedValues(obj)
}
obj = _filterUndefinedValues(obj)
// Pre-validation hooks
if (modelType === DBModelType.DBM) {
obj = this.cfg.hooks!.beforeDBMValidate!(obj as any) as T
}
// Return as is if no schema is passed or if `skipConversion` is set

@@ -1253,3 +1235,3 @@ if (!schema || opt.skipConversion) {

const table = opt.table || this.cfg.table
const objectName = table + (modelType || '')
const objectName = table

@@ -1256,0 +1238,0 @@ let error: JoiValidationError | AjvValidationError | ZodValidationError<T> | undefined

@@ -11,6 +11,5 @@ import { Readable } from 'node:stream'

testItemBMSchema,
testItemDBMSchema,
TEST_TABLE,
createTestItemBM,
testItemDBMJsonSchema,
testItemBMJsonSchema,
} from './test.model'

@@ -24,3 +23,2 @@ import { TestItemBM } from '.'

db,
dbmSchema: testItemDBMSchema,
bmSchema: testItemBMSchema,

@@ -48,3 +46,3 @@ logStarted: true,

test('createTable, dropIfExists=true', async () => {
await dao.createTable(testItemDBMJsonSchema, { dropIfExists: true })
await dao.createTable(testItemBMJsonSchema, { dropIfExists: true })
})

@@ -51,0 +49,0 @@ }

@@ -10,4 +10,4 @@ import { _filterObject, _pick, _sortBy, pMap } from '@naturalcycles/js-lib'

TEST_TABLE,
testItemBMJsonSchema,
TestItemDBM,
testItemDBMJsonSchema,
} from './test.model'

@@ -46,3 +46,3 @@ import { deepFreeze } from './test.util'

test('createTable, dropIfExists=true', async () => {
await db.createTable(TEST_TABLE, testItemDBMJsonSchema, { dropIfExists: true })
await db.createTable(TEST_TABLE, testItemBMJsonSchema, { dropIfExists: true })
})

@@ -49,0 +49,0 @@ }

@@ -13,4 +13,2 @@ import { runCommonDaoTest } from './daoTest'

TestItemDBM,
testItemDBMJsonSchema,
testItemDBMSchema,
TestItemTM,

@@ -29,7 +27,5 @@ testItemTMSchema,

createTestItemsBM,
testItemDBMSchema,
testItemBMSchema,
testItemTMSchema,
testItemBMJsonSchema,
testItemDBMJsonSchema,
runCommonDBTest,

@@ -36,0 +32,0 @@ runCommonDaoTest,

@@ -38,10 +38,2 @@ import { jsonSchema, _range, BaseDBEntity, JsonSchemaObject } from '@naturalcycles/js-lib'

export const testItemDBMSchema = objectSchema<TestItemDBM>({
k1: stringSchema,
k2: stringSchema.allow(null).optional(),
k3: numberSchema.optional(),
even: booleanSchema.optional(),
b1: binarySchema.optional(),
}).concat(baseDBEntitySchema as any)
export const testItemTMSchema = objectSchema<TestItemTM>({

@@ -67,16 +59,2 @@ k1: stringSchema,

export const testItemDBMJsonSchema: JsonSchemaObject<TestItemDBM> = jsonSchema
.rootObject<TestItemDBM>({
// todo: figure out how to not copy-paste these 3 fields
id: jsonSchema.string(),
created: jsonSchema.unixTimestamp(),
updated: jsonSchema.unixTimestamp(),
k1: jsonSchema.string(),
k2: jsonSchema.string().optional(),
k3: jsonSchema.number().optional(),
even: jsonSchema.boolean().optional(),
b1: jsonSchema.buffer().optional(),
})
.build()
export function createTestItemDBM(num = 1): TestItemDBM {

@@ -83,0 +61,0 @@ return {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc