imodel 0.17.1 → 0.17.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/index.d.mts +52 -17
- package/index.mjs +221 -6
- package/migrate.d.mts +1 -1
- package/migrate.mjs +1 -1
- package/package.json +1 -1
package/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* imodel v0.17.
|
|
2
|
+
* imodel v0.17.2
|
|
3
3
|
* (c) 2019-2026 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
@@ -467,6 +467,21 @@ declare function updating(size?: number): FieldDecorator<Date | null>;
|
|
|
467
467
|
*/
|
|
468
468
|
declare function deleting(size?: number): FieldDecorator<Date | null>;
|
|
469
469
|
|
|
470
|
+
/**
|
|
471
|
+
* @overload
|
|
472
|
+
* @param {Hooks} hook
|
|
473
|
+
* @param {boolean} [approach] 插入位置是否为更靠近执行的位置
|
|
474
|
+
* @returns {ClassDecorator}
|
|
475
|
+
*/
|
|
476
|
+
declare function hooks(hook: Hooks, approach?: boolean | undefined): ClassDecorator;
|
|
477
|
+
/**
|
|
478
|
+
* @overload
|
|
479
|
+
* @param {Function} Model
|
|
480
|
+
* @param {Hooks} hook
|
|
481
|
+
* @param {boolean} [approach] 插入位置是否为更靠近执行的位置
|
|
482
|
+
* @returns {void}
|
|
483
|
+
*/
|
|
484
|
+
declare function hooks(Model: Function, hook: Hooks, approach?: boolean | undefined): void;
|
|
470
485
|
/** @type {Hook<Hooks['where']>} */
|
|
471
486
|
declare const where: Hook<Hooks["where"]>;
|
|
472
487
|
/** @type {Hook<Hooks['beforeCreateMany']>} */
|
|
@@ -1206,15 +1221,15 @@ interface Index extends IndexInfo {
|
|
|
1206
1221
|
name: string;
|
|
1207
1222
|
}
|
|
1208
1223
|
interface Hooks<T extends Record<string, any> = Record<string, any>> {
|
|
1209
|
-
where(conn: Connection, model: TableDefine): PromiseLike<void | Where> | void;
|
|
1210
|
-
beforeCreateMany(conn: Connection, model: TableDefine, records: Record<string, any>[]): PromiseLike<void | Record<string, any>[]> | void | Record<string, any>[];
|
|
1211
|
-
afterCreateMany(conn: Connection, model: TableDefine, records: Record<string, any>[]): PromiseLike<void> | void;
|
|
1212
|
-
beforeCreate(conn: Connection, model: TableDefine, record: Record<string, any>): PromiseLike<void | Record<string, any>> | void | Record<string, any>;
|
|
1213
|
-
afterCreate(conn: Connection, model: TableDefine, record: Record<string, any>): PromiseLike<void> | void;
|
|
1214
|
-
beforeUpdate(conn: Connection, model: TableDefine, record: Record<string, any>, set: Record<string, any>): PromiseLike<void | Record<string, any>> | void | Record<string, any>;
|
|
1215
|
-
afterUpdate(conn: Connection, model: TableDefine, record: Record<string, any>, oldRecord: Record<string, any>): PromiseLike<void> | void;
|
|
1216
|
-
beforeDelete(conn: Connection, model: TableDefine, record: T, update: Record<string, any> | null): PromiseLike<void> | void;
|
|
1217
|
-
afterDelete(conn: Connection, model: TableDefine, record: T, update: Record<string, any> | null): PromiseLike<void> | void;
|
|
1224
|
+
where(this: unknown, conn: Connection, model: TableDefine): PromiseLike<void | Where> | void;
|
|
1225
|
+
beforeCreateMany(this: unknown, conn: Connection, model: TableDefine, records: Record<string, any>[]): PromiseLike<void | Record<string, any>[]> | void | Record<string, any>[];
|
|
1226
|
+
afterCreateMany(this: unknown, conn: Connection, model: TableDefine, records: Record<string, any>[]): PromiseLike<void> | void;
|
|
1227
|
+
beforeCreate(this: unknown, conn: Connection, model: TableDefine, record: Record<string, any>): PromiseLike<void | Record<string, any>> | void | Record<string, any>;
|
|
1228
|
+
afterCreate(this: unknown, conn: Connection, model: TableDefine, record: Record<string, any>): PromiseLike<void> | void;
|
|
1229
|
+
beforeUpdate(this: unknown, conn: Connection, model: TableDefine, record: Record<string, any>, set: Record<string, any>): PromiseLike<void | Record<string, any>> | void | Record<string, any>;
|
|
1230
|
+
afterUpdate(this: unknown, conn: Connection, model: TableDefine, record: Record<string, any>, oldRecord: Record<string, any>): PromiseLike<void> | void;
|
|
1231
|
+
beforeDelete(this: unknown, conn: Connection, model: TableDefine, record: T, update: Record<string, any> | null): PromiseLike<void> | void;
|
|
1232
|
+
afterDelete(this: unknown, conn: Connection, model: TableDefine, record: T, update: Record<string, any> | null): PromiseLike<void> | void;
|
|
1218
1233
|
}
|
|
1219
1234
|
interface TableDefine<T extends Fields = Fields, TT extends string | VirtualTable | undefined = string | VirtualTable | undefined> {
|
|
1220
1235
|
readonly connect?: TableConnect<any> | null;
|
|
@@ -1257,6 +1272,7 @@ interface Join<T extends TableDefine = TableDefine> {
|
|
|
1257
1272
|
};
|
|
1258
1273
|
}
|
|
1259
1274
|
interface Options {
|
|
1275
|
+
distinct?: boolean;
|
|
1260
1276
|
range?: FindRange;
|
|
1261
1277
|
select?: Record<string, Select> | null;
|
|
1262
1278
|
/** 排序配置,true 表示逆序 */
|
|
@@ -1465,25 +1481,25 @@ declare class Connection<E extends {} = {}> {
|
|
|
1465
1481
|
* @overload
|
|
1466
1482
|
* @param {T} model
|
|
1467
1483
|
* @param {object[]} list
|
|
1468
|
-
* @returns {Promise<
|
|
1484
|
+
* @returns {Promise<TableValue<T>[]>}
|
|
1469
1485
|
*/
|
|
1470
|
-
create<T extends TableDefine>(model: T, list: object[]): Promise<
|
|
1486
|
+
create<T extends TableDefine>(model: T, list: object[]): Promise<TableValue<T>[]>;
|
|
1471
1487
|
/**
|
|
1472
1488
|
* @template {TableDefine} T
|
|
1473
1489
|
* @overload
|
|
1474
1490
|
* @param {T} model
|
|
1475
1491
|
* @param {object} value
|
|
1476
|
-
* @returns {Promise<T extends Build<infer R> ? R :
|
|
1492
|
+
* @returns {Promise<T extends Build<infer R> ? R : TableValue<T>>}
|
|
1477
1493
|
*/
|
|
1478
|
-
create<T extends TableDefine>(model: T, value: object): Promise<T extends Build<infer R> ? R :
|
|
1494
|
+
create<T extends TableDefine>(model: T, value: object): Promise<T extends Build<infer R> ? R : TableValue<T>>;
|
|
1479
1495
|
/**
|
|
1480
1496
|
* @template {TableDefine} T
|
|
1481
1497
|
* @overload
|
|
1482
1498
|
* @param {T} model
|
|
1483
1499
|
* @param {object | object[]} value
|
|
1484
|
-
* @returns {Promise<
|
|
1500
|
+
* @returns {Promise<TableValue<T>[] | (T extends Build<infer R> ? R : TableValue<T>)>}
|
|
1485
1501
|
*/
|
|
1486
|
-
create<T extends TableDefine>(model: T, value: object | object[]): Promise<
|
|
1502
|
+
create<T extends TableDefine>(model: T, value: object | object[]): Promise<TableValue<T>[] | (T extends Build<infer R> ? R : TableValue<T>)>;
|
|
1487
1503
|
/**
|
|
1488
1504
|
*
|
|
1489
1505
|
* @template {TableDefine} T
|
|
@@ -2111,6 +2127,11 @@ declare class Query<T extends Fields, TB extends unknown = any> extends Subquery
|
|
|
2111
2127
|
* @param {...string | string[]} groups
|
|
2112
2128
|
*/
|
|
2113
2129
|
groupBy(...groups: (string | string[])[]): this;
|
|
2130
|
+
/**
|
|
2131
|
+
*
|
|
2132
|
+
* @param {boolean} [distinct]
|
|
2133
|
+
*/
|
|
2134
|
+
distinct(distinct?: boolean): this;
|
|
2114
2135
|
/**
|
|
2115
2136
|
*
|
|
2116
2137
|
* @param {object} a
|
|
@@ -2138,6 +2159,20 @@ declare function toPrimary(fields: string[], data: Record<string, any>): string;
|
|
|
2138
2159
|
*/
|
|
2139
2160
|
declare function toPrimaries(model: any, documents: any): string | string[];
|
|
2140
2161
|
|
|
2162
|
+
/**
|
|
2163
|
+
*
|
|
2164
|
+
* @param {...Readonly<Partial<Hooks>> | Readonly<Partial<Hooks>>[]} hooks
|
|
2165
|
+
* @returns {Readonly<Partial<Hooks>>}
|
|
2166
|
+
*/
|
|
2167
|
+
declare function mergeHooks(...hooks: (Readonly<Partial<Hooks>> | Readonly<Partial<Hooks>>[])[]): Readonly<Partial<Hooks>>;
|
|
2168
|
+
/**
|
|
2169
|
+
*
|
|
2170
|
+
* @param {Readonly<Partial<Hooks>>} hooks
|
|
2171
|
+
* @param {any} that
|
|
2172
|
+
* @returns {Readonly<Partial<Hooks>>}
|
|
2173
|
+
*/
|
|
2174
|
+
declare function bindHooks(hooks: Readonly<Partial<Hooks>>, that: any): Readonly<Partial<Hooks>>;
|
|
2175
|
+
|
|
2141
2176
|
/**
|
|
2142
2177
|
* @typedef {object} Scene.Define
|
|
2143
2178
|
* @property {string} table
|
|
@@ -2536,4 +2571,4 @@ declare function field<T extends MainFieldType>(type: T, options: {
|
|
|
2536
2571
|
declare function setDevelopment(d?: boolean): void;
|
|
2537
2572
|
declare let isDevelopment: boolean;
|
|
2538
2573
|
|
|
2539
|
-
export { Build, type ClassDecorator, type Column, type ColumnOptions, Connection, type Constraint, Create, type DBColumn, type DBIndex, type DBTable, Destroy, type Environment, type FieldDecorator, type FieldDefine, type FieldDefineOption, type FieldDefineType, type FieldSpecific, type FieldSpecificValue, type FieldType, type FieldTypeDefine, type FieldValue, type Fields, type FindArg, type FindRange, type GetName, type Hook, type Hooks, type IConnection, type Index, type IndexInfo, type IndexOptions, type JOIN, type Join, type JoinType, type MainFieldType, type MaybePromise, type MethodDecorator, Model, type Options, PseudoDestroy, Query, type QueryOptions, type Queryable, Save, Scene, Select, type SelectItem, SetValue, type Skip, Submodel, type Support, type TableConnect, type TableConnection, type TableDefine, type TableType, type TableValue, type ToFieldType, type ToType, type TransactionFn, type VirtualTable, Where, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, afterCreate, afterCreateMany, afterDelete, afterUpdate, beforeCreate, beforeCreateMany, beforeDelete, beforeUpdate, coefficient, column, creating, decrement, defaultValue, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, model, multiply, now, primary, prop, pseudo, setDevelopment, sort, model as submodel, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, where, withDeleted };
|
|
2574
|
+
export { Build, type ClassDecorator, type Column, type ColumnOptions, Connection, type Constraint, Create, type DBColumn, type DBIndex, type DBTable, Destroy, type Environment, type FieldDecorator, type FieldDefine, type FieldDefineOption, type FieldDefineType, type FieldSpecific, type FieldSpecificValue, type FieldType, type FieldTypeDefine, type FieldValue, type Fields, type FindArg, type FindRange, type GetName, type Hook, type Hooks, type IConnection, type Index, type IndexInfo, type IndexOptions, type JOIN, type Join, type JoinType, type MainFieldType, type MaybePromise, type MethodDecorator, Model, type Options, PseudoDestroy, Query, type QueryOptions, type Queryable, Save, Scene, Select, type SelectItem, SetValue, type Skip, Submodel, type Support, type TableConnect, type TableConnection, type TableDefine, type TableType, type TableValue, type ToFieldType, type ToType, type TransactionFn, type VirtualTable, Where, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, afterCreate, afterCreateMany, afterDelete, afterUpdate, beforeCreate, beforeCreateMany, beforeDelete, beforeUpdate, bindHooks, coefficient, column, creating, decrement, defaultValue, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, hooks, immutable, increment, index, isDevelopment, isPseudo, mergeHooks, model, multiply, now, primary, prop, pseudo, setDevelopment, sort, model as submodel, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, where, withDeleted };
|
package/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* imodel v0.17.
|
|
2
|
+
* imodel v0.17.2
|
|
3
3
|
* (c) 2019-2026 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
@@ -2499,6 +2499,7 @@ function toSelect(queryable) {
|
|
|
2499
2499
|
select,
|
|
2500
2500
|
sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
|
|
2501
2501
|
group: options.group || [],
|
|
2502
|
+
distinct: Boolean(options.distinct),
|
|
2502
2503
|
having: []
|
|
2503
2504
|
}];
|
|
2504
2505
|
return result;
|
|
@@ -2652,28 +2653,28 @@ class Connection {
|
|
|
2652
2653
|
* @overload
|
|
2653
2654
|
* @param {T} model
|
|
2654
2655
|
* @param {object[]} list
|
|
2655
|
-
* @returns {Promise<
|
|
2656
|
+
* @returns {Promise<TableValue<T>[]>}
|
|
2656
2657
|
*/
|
|
2657
2658
|
/**
|
|
2658
2659
|
* @template {TableDefine} T
|
|
2659
2660
|
* @overload
|
|
2660
2661
|
* @param {T} model
|
|
2661
2662
|
* @param {object} value
|
|
2662
|
-
* @returns {Promise<T extends Build<infer R> ? R :
|
|
2663
|
+
* @returns {Promise<T extends Build<infer R> ? R : TableValue<T>>}
|
|
2663
2664
|
*/
|
|
2664
2665
|
/**
|
|
2665
2666
|
* @template {TableDefine} T
|
|
2666
2667
|
* @overload
|
|
2667
2668
|
* @param {T} model
|
|
2668
2669
|
* @param {object | object[]} value
|
|
2669
|
-
* @returns {Promise<
|
|
2670
|
+
* @returns {Promise<TableValue<T>[] | (T extends Build<infer R> ? R : TableValue<T>)>}
|
|
2670
2671
|
*/
|
|
2671
2672
|
/**
|
|
2672
2673
|
* @template {TableDefine} T
|
|
2673
2674
|
* @param {T} model
|
|
2674
2675
|
* @param {object | object[]} p
|
|
2675
2676
|
* @param {Skip} [skip]
|
|
2676
|
-
* @returns {Promise<
|
|
2677
|
+
* @returns {Promise<TableValue<T>[] | (T extends Build<infer R> ? R : TableValue<T>)>}
|
|
2677
2678
|
*/
|
|
2678
2679
|
async create(model, p, skip) {
|
|
2679
2680
|
const {
|
|
@@ -2732,6 +2733,7 @@ class Connection {
|
|
|
2732
2733
|
}
|
|
2733
2734
|
return r;
|
|
2734
2735
|
}, model);
|
|
2736
|
+
// @ts-ignore
|
|
2735
2737
|
return isArray ? [result] : result;
|
|
2736
2738
|
}
|
|
2737
2739
|
/**
|
|
@@ -3957,6 +3959,76 @@ function buildHooks(Model) {
|
|
|
3957
3959
|
}
|
|
3958
3960
|
};
|
|
3959
3961
|
}
|
|
3962
|
+
/** @type {[keyof Hooks, boolean][]} */
|
|
3963
|
+
const isBeforeList = [['where', true], ['beforeCreateMany', true], ['afterCreateMany', false], ['beforeCreate', true], ['afterCreate', false], ['beforeUpdate', true], ['afterUpdate', false], ['beforeDelete', true], ['afterDelete', false]];
|
|
3964
|
+
/**
|
|
3965
|
+
* @overload
|
|
3966
|
+
* @param {Hooks} hook
|
|
3967
|
+
* @param {boolean} [approach] 插入位置是否为更靠近执行的位置
|
|
3968
|
+
* @returns {ClassDecorator}
|
|
3969
|
+
*/
|
|
3970
|
+
/**
|
|
3971
|
+
* @overload
|
|
3972
|
+
* @param {Function} Model
|
|
3973
|
+
* @param {Hooks} hook
|
|
3974
|
+
* @param {boolean} [approach] 插入位置是否为更靠近执行的位置
|
|
3975
|
+
* @returns {void}
|
|
3976
|
+
*/
|
|
3977
|
+
/**
|
|
3978
|
+
* @param {*[]} p
|
|
3979
|
+
* @returns {MethodDecorator<Hooks> | ClassDecorator | void}
|
|
3980
|
+
*/
|
|
3981
|
+
function hooks(...p) {
|
|
3982
|
+
let first = p.shift();
|
|
3983
|
+
/** @type {Function?} */
|
|
3984
|
+
const Model = typeof first === 'function' ? first : null;
|
|
3985
|
+
if (Model) {
|
|
3986
|
+
first = p.shift();
|
|
3987
|
+
}
|
|
3988
|
+
/** @type {Partial<Hooks>} */
|
|
3989
|
+
const hookFns = first;
|
|
3990
|
+
const approach = p.shift();
|
|
3991
|
+
if (approach && typeof approach !== 'boolean') {
|
|
3992
|
+
const call = Model ? `hooks(Model, hooks, approach?)` : `@hooks(hooks, approach?)`;
|
|
3993
|
+
throw new Error(`${call} 的 approach 参数只能是布尔型`);
|
|
3994
|
+
}
|
|
3995
|
+
function add(metadata) {
|
|
3996
|
+
for (const [key, isBefore] of isBeforeList) {
|
|
3997
|
+
const hook = hookFns[key];
|
|
3998
|
+
if (typeof hook !== 'function') {
|
|
3999
|
+
continue;
|
|
4000
|
+
}
|
|
4001
|
+
const insert = isBefore ? !approach : Boolean(approach);
|
|
4002
|
+
const metaKey = insert ? insertHookMetaKey : appendHookMetaKey;
|
|
4003
|
+
/** @type {Record<string, HookItem[]>} */
|
|
4004
|
+
const hooks = metadata[metaKey] ||= {};
|
|
4005
|
+
const list = hooks[key];
|
|
4006
|
+
const item = {
|
|
4007
|
+
static: true,
|
|
4008
|
+
hook
|
|
4009
|
+
};
|
|
4010
|
+
if (Array.isArray(list)) {
|
|
4011
|
+
if (insert) {
|
|
4012
|
+
list.unshift(item);
|
|
4013
|
+
} else {
|
|
4014
|
+
list.push(item);
|
|
4015
|
+
}
|
|
4016
|
+
} else {
|
|
4017
|
+
hooks[key] = [item];
|
|
4018
|
+
}
|
|
4019
|
+
}
|
|
4020
|
+
}
|
|
4021
|
+
if (Model) {
|
|
4022
|
+
return add(Model[Symbol.metadata] ||= {});
|
|
4023
|
+
}
|
|
4024
|
+
/** @type {ClassDecorator} */
|
|
4025
|
+
return (value, ctx) => {
|
|
4026
|
+
if (!ctx || ctx.kind !== 'class') {
|
|
4027
|
+
throw new Error('钩子属性只能是类(class)');
|
|
4028
|
+
}
|
|
4029
|
+
add(ctx.metadata);
|
|
4030
|
+
};
|
|
4031
|
+
}
|
|
3960
4032
|
|
|
3961
4033
|
const propsKey = Symbol();
|
|
3962
4034
|
/**
|
|
@@ -5302,6 +5374,14 @@ class Query extends Subquery {
|
|
|
5302
5374
|
this.#options.group = [this.#options.group || [], groups].flat(2);
|
|
5303
5375
|
return this;
|
|
5304
5376
|
}
|
|
5377
|
+
/**
|
|
5378
|
+
*
|
|
5379
|
+
* @param {boolean} [distinct]
|
|
5380
|
+
*/
|
|
5381
|
+
distinct(distinct = true) {
|
|
5382
|
+
this.#options.distinct = Boolean(distinct);
|
|
5383
|
+
return this;
|
|
5384
|
+
}
|
|
5305
5385
|
}
|
|
5306
5386
|
|
|
5307
5387
|
/** @import { Fields, IndexInfo, FieldDefine } from './types' */
|
|
@@ -5329,6 +5409,141 @@ function toPrimaries(model, documents) {
|
|
|
5329
5409
|
return Array.isArray(documents) ? documents.map(document => toPrimary(fields, document)) : toPrimary(fields, documents);
|
|
5330
5410
|
}
|
|
5331
5411
|
|
|
5412
|
+
/** @import { Hooks, MaybePromise } from './types' */
|
|
5413
|
+
/**
|
|
5414
|
+
*
|
|
5415
|
+
* @param {Hooks['where'][]} hooks
|
|
5416
|
+
* @returns {Hooks['where'] | undefined}
|
|
5417
|
+
*/
|
|
5418
|
+
function mergeWhere(hooks) {
|
|
5419
|
+
if (!hooks.length) {
|
|
5420
|
+
return;
|
|
5421
|
+
}
|
|
5422
|
+
return async function (conn, model) {
|
|
5423
|
+
let that;
|
|
5424
|
+
for (const hook of hooks) {
|
|
5425
|
+
const where = await hook.call(this, conn, model);
|
|
5426
|
+
if (!where) {
|
|
5427
|
+
continue;
|
|
5428
|
+
}
|
|
5429
|
+
that = (that || Where).and(where);
|
|
5430
|
+
}
|
|
5431
|
+
return that;
|
|
5432
|
+
};
|
|
5433
|
+
}
|
|
5434
|
+
/**
|
|
5435
|
+
*
|
|
5436
|
+
* @param {Hooks['beforeCreateMany'][]} hooks
|
|
5437
|
+
* @returns {Hooks['beforeCreateMany'] | undefined}
|
|
5438
|
+
*/
|
|
5439
|
+
function mergeBeforeCreateMany(hooks) {
|
|
5440
|
+
if (!hooks.length) {
|
|
5441
|
+
return;
|
|
5442
|
+
}
|
|
5443
|
+
return async function (conn, model, record) {
|
|
5444
|
+
let data = record;
|
|
5445
|
+
for (const hook of hooks) {
|
|
5446
|
+
data = (await hook.call(this, conn, model, data)) || data;
|
|
5447
|
+
}
|
|
5448
|
+
return data;
|
|
5449
|
+
};
|
|
5450
|
+
}
|
|
5451
|
+
/**
|
|
5452
|
+
*
|
|
5453
|
+
* @param {Hooks['beforeCreate'][]} hooks
|
|
5454
|
+
* @returns {Hooks['beforeCreate'] | undefined}
|
|
5455
|
+
*/
|
|
5456
|
+
function mergeBeforeCreate(hooks) {
|
|
5457
|
+
if (!hooks.length) {
|
|
5458
|
+
return;
|
|
5459
|
+
}
|
|
5460
|
+
return async function (conn, model, record) {
|
|
5461
|
+
let data = record;
|
|
5462
|
+
for (const hook of hooks) {
|
|
5463
|
+
data = (await hook.call(this, conn, model, data)) || data;
|
|
5464
|
+
}
|
|
5465
|
+
return data;
|
|
5466
|
+
};
|
|
5467
|
+
}
|
|
5468
|
+
/**
|
|
5469
|
+
*
|
|
5470
|
+
* @param {Hooks['beforeUpdate'][]} hooks
|
|
5471
|
+
* @returns {Hooks['beforeUpdate'] | undefined}
|
|
5472
|
+
*/
|
|
5473
|
+
function mergeBeforeUpdate(hooks) {
|
|
5474
|
+
if (!hooks.length) {
|
|
5475
|
+
return;
|
|
5476
|
+
}
|
|
5477
|
+
return async function (conn, model, record, set) {
|
|
5478
|
+
let data = set;
|
|
5479
|
+
for (const hook of hooks) {
|
|
5480
|
+
data = (await hook.call(this, conn, model, record, data)) || data;
|
|
5481
|
+
}
|
|
5482
|
+
return data;
|
|
5483
|
+
};
|
|
5484
|
+
}
|
|
5485
|
+
/**
|
|
5486
|
+
* @template {any[]} T
|
|
5487
|
+
* @param {((this: any, ...argv: T) => MaybePromise<void>)[]} hooks
|
|
5488
|
+
* @returns {((this: any, ...argv: T) => MaybePromise<void>) | undefined}
|
|
5489
|
+
*/
|
|
5490
|
+
function mergeRun(hooks) {
|
|
5491
|
+
if (!hooks.length) {
|
|
5492
|
+
return;
|
|
5493
|
+
}
|
|
5494
|
+
return async function (...p) {
|
|
5495
|
+
for (const hook of hooks) {
|
|
5496
|
+
await hook.apply(this, p);
|
|
5497
|
+
}
|
|
5498
|
+
};
|
|
5499
|
+
}
|
|
5500
|
+
/**
|
|
5501
|
+
* @template {(...p: any[]) => any} T
|
|
5502
|
+
* @param {T?} [v]
|
|
5503
|
+
* @returns {v is T}
|
|
5504
|
+
*/
|
|
5505
|
+
function filterFunction(v) {
|
|
5506
|
+
return typeof v === 'function';
|
|
5507
|
+
}
|
|
5508
|
+
/**
|
|
5509
|
+
*
|
|
5510
|
+
* @param {...Readonly<Partial<Hooks>> | Readonly<Partial<Hooks>>[]} hooks
|
|
5511
|
+
* @returns {Readonly<Partial<Hooks>>}
|
|
5512
|
+
*/
|
|
5513
|
+
function mergeHooks(...hooks) {
|
|
5514
|
+
const allHooks = hooks.flat();
|
|
5515
|
+
return {
|
|
5516
|
+
where: mergeWhere(allHooks.map(v => v.where).filter(filterFunction)),
|
|
5517
|
+
beforeCreateMany: mergeBeforeCreateMany(allHooks.map(v => v.beforeCreateMany).filter(filterFunction)),
|
|
5518
|
+
afterCreateMany: mergeRun(allHooks.map(v => v.afterCreateMany).filter(filterFunction)),
|
|
5519
|
+
beforeCreate: mergeBeforeCreate(allHooks.map(v => v.beforeCreate).filter(filterFunction)),
|
|
5520
|
+
afterCreate: mergeRun(allHooks.map(v => v.afterCreate).filter(filterFunction)),
|
|
5521
|
+
beforeUpdate: mergeBeforeUpdate(allHooks.map(v => v.beforeUpdate).filter(filterFunction)),
|
|
5522
|
+
afterUpdate: mergeRun(allHooks.map(v => v.afterUpdate).filter(filterFunction)),
|
|
5523
|
+
beforeDelete: mergeRun(allHooks.map(v => v.beforeDelete).filter(filterFunction)),
|
|
5524
|
+
afterDelete: mergeRun(allHooks.map(v => v.afterDelete).filter(filterFunction))
|
|
5525
|
+
};
|
|
5526
|
+
}
|
|
5527
|
+
/**
|
|
5528
|
+
*
|
|
5529
|
+
* @param {Readonly<Partial<Hooks>>} hooks
|
|
5530
|
+
* @param {any} that
|
|
5531
|
+
* @returns {Readonly<Partial<Hooks>>}
|
|
5532
|
+
*/
|
|
5533
|
+
function bindHooks(hooks, that) {
|
|
5534
|
+
return {
|
|
5535
|
+
where: hooks.where?.bind(that),
|
|
5536
|
+
beforeCreateMany: hooks.beforeCreateMany?.bind(that),
|
|
5537
|
+
afterCreateMany: hooks.afterCreateMany?.bind(that),
|
|
5538
|
+
beforeCreate: hooks.beforeCreate?.bind(that),
|
|
5539
|
+
afterCreate: hooks.afterCreate?.bind(that),
|
|
5540
|
+
beforeUpdate: hooks.beforeUpdate?.bind(that),
|
|
5541
|
+
afterUpdate: hooks.afterUpdate?.bind(that),
|
|
5542
|
+
beforeDelete: hooks.beforeDelete?.bind(that),
|
|
5543
|
+
afterDelete: hooks.afterDelete?.bind(that)
|
|
5544
|
+
};
|
|
5545
|
+
}
|
|
5546
|
+
|
|
5332
5547
|
/* eslint-disable max-lines */
|
|
5333
5548
|
/** @import { Fields, IndexInfo, FieldDefine, TableDefine } from './types' */
|
|
5334
5549
|
/** @import { Scene } from './Scene.mjs' */
|
|
@@ -6487,4 +6702,4 @@ function field(type, {
|
|
|
6487
6702
|
};
|
|
6488
6703
|
}
|
|
6489
6704
|
|
|
6490
|
-
export { Build, Connection, Create, Destroy, Model, PseudoDestroy, Query, Save, Scene, Submodel, Where, afterCreate, afterCreateMany, afterDelete, afterUpdate, beforeCreate, beforeCreateMany, beforeDelete, beforeUpdate, coefficient, column, creating, decrement, defaultValue, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, model, multiply, now, primary, prop, pseudo, setDevelopment, sort, model as submodel, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, where, withDeleted };
|
|
6705
|
+
export { Build, Connection, Create, Destroy, Model, PseudoDestroy, Query, Save, Scene, Submodel, Where, afterCreate, afterCreateMany, afterDelete, afterUpdate, beforeCreate, beforeCreateMany, beforeDelete, beforeUpdate, bindHooks, coefficient, column, creating, decrement, defaultValue, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, hooks, immutable, increment, index, isDevelopment, isPseudo, mergeHooks, model, multiply, now, primary, prop, pseudo, setDevelopment, sort, model as submodel, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, where, withDeleted };
|
package/migrate.d.mts
CHANGED
package/migrate.mjs
CHANGED