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.4.2 to 9.5.0

27

dist/commondao/common.dao.d.ts
/// <reference types="node" />
import { Transform } from 'node:stream';
import { AnyObject, AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, UnixTimestampMillisNumber, Unsaved, ZodSchema } from '@naturalcycles/js-lib';
import { AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, UnixTimestampMillisNumber, Unsaved, ZodSchema } from '@naturalcycles/js-lib';
import { AjvSchema, ObjectSchema, ReadableTyped } from '@naturalcycles/nodejs-lib';

@@ -15,5 +15,5 @@ import { CommonDBTransactionOptions, DBModelType, DBPatch, DBTransaction, RunQueryResult } from '../db.model';

*/
export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, TM extends AnyObject = BM> {
cfg: CommonDaoCfg<BM, DBM, TM>;
constructor(cfg: CommonDaoCfg<BM, DBM, TM>);
export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
cfg: CommonDaoCfg<BM, DBM>;
constructor(cfg: CommonDaoCfg<BM, DBM>);
create(part?: Partial<BM>, opt?: CommonDaoOptions): BM;

@@ -26,4 +26,2 @@ getById(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;

getByIdAsDBM(id?: string | null, opt?: CommonDaoOptions): Promise<DBM | null>;
getByIdAsTM(id: undefined | null, opt?: CommonDaoOptions): Promise<null>;
getByIdAsTM(id?: string | null, opt?: CommonDaoOptions): Promise<TM | null>;
getByIds(ids: string[], opt?: CommonDaoOptions): Promise<BM[]>;

@@ -49,3 +47,3 @@ getByIdsAsDBM(ids: string[], opt?: CommonDaoOptions): Promise<DBM[]>;

*/
query(table?: string): RunnableDBQuery<BM, DBM, TM>;
query(table?: string): RunnableDBQuery<BM, DBM>;
runQuery(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<BM[]>;

@@ -62,4 +60,2 @@ runQuerySingleColumn<T = any>(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<T[]>;

runQueryExtendedAsDBM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<DBM>>;
runQueryAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<TM[]>;
runQueryExtendedAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<TM>>;
runQueryCount(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<number>;

@@ -166,5 +162,2 @@ streamQueryForEach(q: DBQuery<DBM>, mapper: AsyncMapper<BM, void>, opt?: CommonDaoStreamForEachOptions<BM>): Promise<void>;

anyToDBMs(entities: DBM[], opt?: CommonDaoOptions): DBM[];
bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined;
bmToTM(bm?: BM, opt?: CommonDaoOptions): TM;
bmsToTM(bms: BM[], opt?: CommonDaoOptions): TM[];
/**

@@ -176,3 +169,3 @@ * Returns *converted value*.

*/
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, modelType?: DBModelType, opt?: CommonDaoOptions): any;
getTableSchema(): Promise<JsonSchemaRootObject<DBM>>;

@@ -208,8 +201,8 @@ createTable(schema: JsonSchemaObject<DBM>, opt?: CommonDaoCreateOptions): Promise<void>;

rollback(): Promise<void>;
getById<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM, any>, id?: string | null, opt?: CommonDaoOptions): Promise<BM | null>;
getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM, any>, ids: string[], opt?: CommonDaoOptions): Promise<BM[]>;
save<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM, any>, bm: Unsaved<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM>;
saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM, any>, bms: Unsaved<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM[]>;
getById<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM>, id?: string | null, opt?: CommonDaoOptions): Promise<BM | null>;
getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM>, ids: string[], opt?: CommonDaoOptions): Promise<BM[]>;
save<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM>, bm: Unsaved<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM>;
saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>(dao: CommonDao<BM, DBM>, bms: Unsaved<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM[]>;
deleteById(dao: CommonDao<any>, id?: string | null, opt?: CommonDaoOptions): Promise<number>;
deleteByIds(dao: CommonDao<any>, ids: string[], opt?: CommonDaoOptions): Promise<number>;
}

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

beforeBMToDBM: bm => bm,
beforeBMToTM: bm => bm,
anonymize: dbm => dbm,

@@ -103,21 +102,2 @@ onValidationError: err => err,

}
async getByIdAsTM(id, opt = {}) {
if (!id)
return null;
const op = `getByIdAsTM(${id})`;
const table = opt.table || this.cfg.table;
const started = this.logStarted(op, table);
let [dbm] = await (opt.tx || this.cfg.db).getByIds(table, [id]);
if (dbm && !opt.raw && this.cfg.hooks.afterLoad) {
dbm = (await this.cfg.hooks.afterLoad(dbm)) || undefined;
}
if (opt.raw) {
this.logResult(started, op, dbm, table);
return dbm || null;
}
const bm = await this.dbmToBM(dbm, opt);
const tm = this.bmToTM(bm, opt);
this.logResult(started, op, tm, table);
return tm || null;
}
async getByIds(ids, opt = {}) {

@@ -270,22 +250,2 @@ if (!ids.length)

}
async runQueryAsTM(q, opt) {
const { rows } = await this.runQueryExtendedAsTM(q, opt);
return rows;
}
async runQueryExtendedAsTM(q, opt = {}) {
q.table = opt.table || q.table;
const op = `runQueryAsTM(${q.pretty()})`;
const started = this.logStarted(op, q.table);
let { rows, ...queryResult } = await this.cfg.db.runQuery(q, opt);
if (!opt.raw && 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 tms = partialQuery || opt.raw ? rows : this.bmsToTM(await this.dbmsToBM(rows, opt), opt);
this.logResult(started, op, tms, q.table);
return {
rows: tms,
...queryResult,
};
}
async runQueryCount(q, opt = {}) {

@@ -928,18 +888,2 @@ q.table = opt.table || q.table;

}
bmToTM(bm, opt) {
if (bm === undefined)
return;
// optimization: 1 validation is enough
// Validate/convert BM
// bm gets assigned to the new reference
// bm = this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
// BM > TM
const tm = this.cfg.hooks.beforeBMToTM(bm);
// Validate/convert DBM
return this.validateAndConvert(tm, this.cfg.tmSchema, db_model_1.DBModelType.TM, opt);
}
bmsToTM(bms, opt = {}) {
// try/catch?
return bms.map(bm => this.bmToTM(bm, opt));
}
/**

@@ -946,0 +890,0 @@ * Returns *converted value*.

@@ -1,6 +0,6 @@

import { AnyObject, BaseDBEntity, CommonLogger, ErrorMode, Promisable, ZodError, ZodSchema } from '@naturalcycles/js-lib';
import { BaseDBEntity, CommonLogger, ErrorMode, Promisable, ZodError, ZodSchema } from '@naturalcycles/js-lib';
import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchema, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib';
import { CommonDB } from '../common.db';
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model';
export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity, TM> {
export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity> {
/**

@@ -43,3 +43,2 @@ * Allows to override the id generation function.

beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>;
beforeBMToTM: (bm: BM) => Partial<TM>;
/**

@@ -108,3 +107,3 @@ * Allows to access the DBM just after it has been loaded from the DB.

}
export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, TM extends AnyObject = BM> {
export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
db: CommonDB;

@@ -117,3 +116,2 @@ table: string;

bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodSchema<BM>;
tmSchema?: ObjectSchema<TM> | AjvSchema<TM> | ZodSchema<TM>;
excludeFromIndexes?: (keyof DBM)[];

@@ -147,3 +145,3 @@ /**

logStarted?: boolean;
hooks?: Partial<CommonDaoHooks<BM, DBM, TM>>;
hooks?: Partial<CommonDaoHooks<BM, DBM>>;
/**

@@ -150,0 +148,0 @@ * Defaults to true.

@@ -97,4 +97,3 @@ import { ObjectWithId } from '@naturalcycles/js-lib';

DBM = "DBM",
BM = "BM",
TM = "TM"
BM = "BM"
}

@@ -101,0 +100,0 @@ /**

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

DBModelType["BM"] = "BM";
DBModelType["TM"] = "TM";
})(DBModelType || (exports.DBModelType = DBModelType = {}));

@@ -16,0 +15,0 @@ /**

@@ -1,2 +0,2 @@

import { AsyncMapper, AnyObject, BaseDBEntity, ObjectWithId } from '@naturalcycles/js-lib';
import { AsyncMapper, BaseDBEntity, ObjectWithId } from '@naturalcycles/js-lib';
import { ReadableTyped } from '@naturalcycles/nodejs-lib';

@@ -87,15 +87,13 @@ import { CommonDaoOptions, CommonDaoStreamDeleteOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, DBPatch } from '..';

*/
export declare class RunnableDBQuery<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, TM extends AnyObject = BM> extends DBQuery<DBM> {
dao: CommonDao<BM, DBM, TM>;
export declare class RunnableDBQuery<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> extends DBQuery<DBM> {
dao: CommonDao<BM, DBM>;
/**
* Pass `table` to override table.
*/
constructor(dao: CommonDao<BM, DBM, TM>, table?: string);
constructor(dao: CommonDao<BM, DBM>, table?: string);
runQuery(opt?: CommonDaoOptions): Promise<BM[]>;
runQuerySingleColumn<T = any>(opt?: CommonDaoOptions): Promise<T[]>;
runQueryAsDBM(opt?: CommonDaoOptions): Promise<DBM[]>;
runQueryAsTM(opt?: CommonDaoOptions): Promise<TM[]>;
runQueryExtended(opt?: CommonDaoOptions): Promise<RunQueryResult<BM>>;
runQueryExtendedAsDBM(opt?: CommonDaoOptions): Promise<RunQueryResult<DBM>>;
runQueryExtendedAsTM(opt?: CommonDaoOptions): Promise<RunQueryResult<TM>>;
runQueryCount(opt?: CommonDaoOptions): Promise<number>;

@@ -102,0 +100,0 @@ updateByQuery(patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number>;

@@ -156,5 +156,2 @@ "use strict";

}
async runQueryAsTM(opt) {
return await this.dao.runQueryAsTM(this, opt);
}
async runQueryExtended(opt) {

@@ -166,5 +163,2 @@ return await this.dao.runQueryExtended(this, opt);

}
async runQueryExtendedAsTM(opt) {
return await this.dao.runQueryExtendedAsTM(this, opt);
}
async runQueryCount(opt) {

@@ -171,0 +165,0 @@ return await this.dao.runQueryCount(this, opt);

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

bmSchema: test_model_1.testItemBMSchema,
tmSchema: test_model_1.testItemTMSchema,
logStarted: true,

@@ -21,0 +20,0 @@ logLevel: __1.CommonDaoLogLevel.DATA_FULL,

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

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

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

import {
AnyObject,
BaseDBEntity,

@@ -21,3 +20,3 @@ CommonLogger,

export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity, TM> {
export interface CommonDaoHooks<BM extends BaseDBEntity, DBM extends BaseDBEntity> {
/**

@@ -65,3 +64,2 @@ * Allows to override the id generation function.

beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>
beforeBMToTM: (bm: BM) => Partial<TM>

@@ -136,7 +134,3 @@ /**

export interface CommonDaoCfg<
BM extends BaseDBEntity,
DBM extends BaseDBEntity = BM,
TM extends AnyObject = BM,
> {
export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
db: CommonDB

@@ -150,3 +144,2 @@ table: string

bmSchema?: ObjectSchema<BM> | AjvSchema<BM> | ZodSchema<BM>
tmSchema?: ObjectSchema<TM> | AjvSchema<TM> | ZodSchema<TM>

@@ -188,3 +181,3 @@ excludeFromIndexes?: (keyof DBM)[]

// Hooks are designed with inspiration from got/ky interface
hooks?: Partial<CommonDaoHooks<BM, DBM, TM>>
hooks?: Partial<CommonDaoHooks<BM, DBM>>

@@ -191,0 +184,0 @@ /**

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

_uniqBy,
AnyObject,
AppError,

@@ -80,8 +79,4 @@ AsyncMapper,

*/
export class CommonDao<
BM extends BaseDBEntity,
DBM extends BaseDBEntity = BM,
TM extends AnyObject = BM,
> {
constructor(public cfg: CommonDaoCfg<BM, DBM, TM>) {
export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
constructor(public cfg: CommonDaoCfg<BM, DBM>) {
this.cfg = {

@@ -104,7 +99,6 @@ // Default is to NOT log in AppEngine and in CI,

beforeBMToDBM: bm => bm as any,
beforeBMToTM: bm => bm as any,
anonymize: dbm => dbm,
onValidationError: err => err,
...cfg.hooks,
} satisfies Partial<CommonDaoHooks<BM, DBM, TM>>,
} satisfies Partial<CommonDaoHooks<BM, DBM>>,
}

@@ -184,24 +178,2 @@

async getByIdAsTM(id: undefined | null, opt?: CommonDaoOptions): Promise<null>
async getByIdAsTM(id?: string | null, opt?: CommonDaoOptions): Promise<TM | null>
async getByIdAsTM(id?: string | null, opt: CommonDaoOptions = {}): Promise<TM | null> {
if (!id) return null
const op = `getByIdAsTM(${id})`
const table = opt.table || this.cfg.table
const started = this.logStarted(op, table)
let [dbm] = await (opt.tx || this.cfg.db).getByIds<DBM>(table, [id])
if (dbm && !opt.raw && this.cfg.hooks!.afterLoad) {
dbm = (await this.cfg.hooks!.afterLoad(dbm)) || undefined
}
if (opt.raw) {
this.logResult(started, op, dbm, table)
return (dbm as any) || null
}
const bm = await this.dbmToBM(dbm, opt)
const tm = this.bmToTM(bm, opt)
this.logResult(started, op, tm, table)
return tm || null
}
async getByIds(ids: string[], opt: CommonDaoOptions = {}): Promise<BM[]> {

@@ -314,4 +286,4 @@ if (!ids.length) return []

*/
query(table?: string): RunnableDBQuery<BM, DBM, TM> {
return new RunnableDBQuery<BM, DBM, TM>(this, table)
query(table?: string): RunnableDBQuery<BM, DBM> {
return new RunnableDBQuery<BM, DBM>(this, table)
}

@@ -393,31 +365,2 @@

async runQueryAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<TM[]> {
const { rows } = await this.runQueryExtendedAsTM(q, opt)
return rows
}
async runQueryExtendedAsTM(
q: DBQuery<DBM>,
opt: CommonDaoOptions = {},
): Promise<RunQueryResult<TM>> {
q.table = opt.table || q.table
const op = `runQueryAsTM(${q.pretty()})`
const started = this.logStarted(op, q.table)
let { rows, ...queryResult } = await this.cfg.db.runQuery<DBM>(q, opt)
if (!opt.raw && this.cfg.hooks!.afterLoad && rows.length) {
rows = (await pMap(rows, async dbm => await this.cfg.hooks!.afterLoad!(dbm))).filter(
_isTruthy,
)
}
const partialQuery = !!q._selectedFieldNames
const tms =
partialQuery || opt.raw ? (rows as any[]) : this.bmsToTM(await this.dbmsToBM(rows, opt), opt)
this.logResult(started, op, tms, q.table)
return {
rows: tms,
...queryResult,
}
}
async runQueryCount(q: DBQuery<DBM>, opt: CommonDaoOptions = {}): Promise<number> {

@@ -1248,24 +1191,2 @@ q.table = opt.table || q.table

bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined
bmToTM(bm?: BM, opt?: CommonDaoOptions): TM
bmToTM(bm?: BM, opt?: CommonDaoOptions): TM | undefined {
if (bm === undefined) return
// optimization: 1 validation is enough
// Validate/convert BM
// bm gets assigned to the new reference
// bm = this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
// BM > TM
const tm = this.cfg.hooks!.beforeBMToTM!(bm)
// Validate/convert DBM
return this.validateAndConvert(tm, this.cfg.tmSchema, DBModelType.TM, opt)
}
bmsToTM(bms: BM[], opt: CommonDaoOptions = {}): TM[] {
// try/catch?
return bms.map(bm => this.bmToTM(bm, opt))
}
/**

@@ -1280,3 +1201,3 @@ * Returns *converted value*.

schema: ObjectSchema<T> | AjvSchema<T> | ZodSchema<T> | undefined,
modelType: DBModelType,
modelType?: DBModelType,
opt: CommonDaoOptions = {},

@@ -1471,3 +1392,3 @@ ): any {

async getById<BM extends BaseDBEntity, DBM extends BaseDBEntity>(
dao: CommonDao<BM, DBM, any>,
dao: CommonDao<BM, DBM>,
id?: string | null,

@@ -1480,3 +1401,3 @@ opt?: CommonDaoOptions,

async getByIds<BM extends BaseDBEntity, DBM extends BaseDBEntity>(
dao: CommonDao<BM, DBM, any>,
dao: CommonDao<BM, DBM>,
ids: string[],

@@ -1503,3 +1424,3 @@ opt?: CommonDaoOptions,

async save<BM extends BaseDBEntity, DBM extends BaseDBEntity>(
dao: CommonDao<BM, DBM, any>,
dao: CommonDao<BM, DBM>,
bm: Unsaved<BM>,

@@ -1512,3 +1433,3 @@ opt?: CommonDaoSaveBatchOptions<DBM>,

async saveBatch<BM extends BaseDBEntity, DBM extends BaseDBEntity>(
dao: CommonDao<BM, DBM, any>,
dao: CommonDao<BM, DBM>,
bms: Unsaved<BM>[],

@@ -1515,0 +1436,0 @@ opt?: CommonDaoSaveBatchOptions<DBM>,

@@ -115,3 +115,2 @@ import { ObjectWithId } from '@naturalcycles/js-lib'

BM = 'BM',
TM = 'TM',
}

@@ -118,0 +117,0 @@

import {
AsyncMapper,
_truncate,
AnyObject,
_objectAssign,

@@ -241,3 +240,2 @@ BaseDBEntity,

DBM extends BaseDBEntity = BM,
TM extends AnyObject = BM,
> extends DBQuery<DBM> {

@@ -248,3 +246,3 @@ /**

constructor(
public dao: CommonDao<BM, DBM, TM>,
public dao: CommonDao<BM, DBM>,
table?: string,

@@ -267,6 +265,2 @@ ) {

async runQueryAsTM(opt?: CommonDaoOptions): Promise<TM[]> {
return await this.dao.runQueryAsTM(this, opt)
}
async runQueryExtended(opt?: CommonDaoOptions): Promise<RunQueryResult<BM>> {

@@ -280,6 +274,2 @@ return await this.dao.runQueryExtended(this, opt)

async runQueryExtendedAsTM(opt?: CommonDaoOptions): Promise<RunQueryResult<TM>> {
return await this.dao.runQueryExtendedAsTM(this, opt)
}
async runQueryCount(opt?: CommonDaoOptions): Promise<number> {

@@ -286,0 +276,0 @@ return await this.dao.runQueryCount(this, opt)

@@ -12,3 +12,2 @@ import { Readable } from 'node:stream'

testItemDBMSchema,
testItemTMSchema,
TEST_TABLE,

@@ -27,3 +26,2 @@ createTestItemBM,

bmSchema: testItemBMSchema,
tmSchema: testItemTMSchema,
logStarted: true,

@@ -30,0 +28,0 @@ logLevel: CommonDaoLogLevel.DATA_FULL,

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