imodel 0.17.2 → 0.19.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/index.d.mts +24 -8
- package/index.mjs +72 -51
- 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.
|
|
2
|
+
* imodel v0.19.0
|
|
3
3
|
* (c) 2019-2026 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
@@ -469,19 +469,19 @@ declare function deleting(size?: number): FieldDecorator<Date | null>;
|
|
|
469
469
|
|
|
470
470
|
/**
|
|
471
471
|
* @overload
|
|
472
|
-
* @param {Hooks} hook
|
|
472
|
+
* @param {Partial<Hooks>} hook
|
|
473
473
|
* @param {boolean} [approach] 插入位置是否为更靠近执行的位置
|
|
474
474
|
* @returns {ClassDecorator}
|
|
475
475
|
*/
|
|
476
|
-
declare function hooks(hook: Hooks
|
|
476
|
+
declare function hooks(hook: Partial<Hooks>, approach?: boolean | undefined): ClassDecorator;
|
|
477
477
|
/**
|
|
478
478
|
* @overload
|
|
479
479
|
* @param {Function} Model
|
|
480
|
-
* @param {Hooks} hook
|
|
480
|
+
* @param {Partial<Hooks>} hook
|
|
481
481
|
* @param {boolean} [approach] 插入位置是否为更靠近执行的位置
|
|
482
482
|
* @returns {void}
|
|
483
483
|
*/
|
|
484
|
-
declare function hooks(Model: Function, hook: Hooks
|
|
484
|
+
declare function hooks(Model: Function, hook: Partial<Hooks>, approach?: boolean | undefined): void;
|
|
485
485
|
/** @type {Hook<Hooks['where']>} */
|
|
486
486
|
declare const where: Hook<Hooks["where"]>;
|
|
487
487
|
/** @type {Hook<Hooks['beforeCreateMany']>} */
|
|
@@ -2118,10 +2118,10 @@ declare class Query<T extends Fields, TB extends unknown = any> extends Subquery
|
|
|
2118
2118
|
*/
|
|
2119
2119
|
contains(text?: string): this;
|
|
2120
2120
|
/**
|
|
2121
|
-
* @param {any
|
|
2121
|
+
* @param {...any | any[]} primaries
|
|
2122
2122
|
* @returns {this}
|
|
2123
2123
|
*/
|
|
2124
|
-
primary(
|
|
2124
|
+
primary(...primaries: (any | any[])[]): this;
|
|
2125
2125
|
/**
|
|
2126
2126
|
*
|
|
2127
2127
|
* @param {...string | string[]} groups
|
|
@@ -2180,6 +2180,15 @@ declare function bindHooks(hooks: Readonly<Partial<Hooks>>, that: any): Readonly
|
|
|
2180
2180
|
*/
|
|
2181
2181
|
/** @import {FieldDefine, Fields, TableDefine,} from './types/table' */
|
|
2182
2182
|
declare class Scene {
|
|
2183
|
+
/**
|
|
2184
|
+
* @template {TableDefine<Fields, string>} T
|
|
2185
|
+
* @param {{scene?: Scene} | Scene} source
|
|
2186
|
+
* @param {T} table
|
|
2187
|
+
* @returns {T}
|
|
2188
|
+
*/
|
|
2189
|
+
static table<T extends TableDefine<Fields, string>>(source: {
|
|
2190
|
+
scene?: Scene;
|
|
2191
|
+
} | Scene, table: T): T;
|
|
2183
2192
|
/**
|
|
2184
2193
|
*
|
|
2185
2194
|
* @param {Map<string, Scene.Define>} map
|
|
@@ -2564,6 +2573,13 @@ declare function field<T extends MainFieldType>(type: T, options: {
|
|
|
2564
2573
|
default?: ToType<T, boolean, boolean> | symbol;
|
|
2565
2574
|
} & FieldDefineOption): FieldDefine<T, boolean, boolean>;
|
|
2566
2575
|
|
|
2576
|
+
/**
|
|
2577
|
+
*
|
|
2578
|
+
* @param {any} v
|
|
2579
|
+
* @returns
|
|
2580
|
+
*/
|
|
2581
|
+
declare function toBool(v: any): boolean;
|
|
2582
|
+
|
|
2567
2583
|
/**
|
|
2568
2584
|
*
|
|
2569
2585
|
* @param {boolean} [d]
|
|
@@ -2571,4 +2587,4 @@ declare function field<T extends MainFieldType>(type: T, options: {
|
|
|
2571
2587
|
declare function setDevelopment(d?: boolean): void;
|
|
2572
2588
|
declare let isDevelopment: boolean;
|
|
2573
2589
|
|
|
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 };
|
|
2590
|
+
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, toBool, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, where, withDeleted };
|
package/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* imodel v0.
|
|
2
|
+
* imodel v0.19.0
|
|
3
3
|
* (c) 2019-2026 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
@@ -3963,14 +3963,14 @@ function buildHooks(Model) {
|
|
|
3963
3963
|
const isBeforeList = [['where', true], ['beforeCreateMany', true], ['afterCreateMany', false], ['beforeCreate', true], ['afterCreate', false], ['beforeUpdate', true], ['afterUpdate', false], ['beforeDelete', true], ['afterDelete', false]];
|
|
3964
3964
|
/**
|
|
3965
3965
|
* @overload
|
|
3966
|
-
* @param {Hooks} hook
|
|
3966
|
+
* @param {Partial<Hooks>} hook
|
|
3967
3967
|
* @param {boolean} [approach] 插入位置是否为更靠近执行的位置
|
|
3968
3968
|
* @returns {ClassDecorator}
|
|
3969
3969
|
*/
|
|
3970
3970
|
/**
|
|
3971
3971
|
* @overload
|
|
3972
3972
|
* @param {Function} Model
|
|
3973
|
-
* @param {Hooks} hook
|
|
3973
|
+
* @param {Partial<Hooks>} hook
|
|
3974
3974
|
* @param {boolean} [approach] 插入位置是否为更靠近执行的位置
|
|
3975
3975
|
* @returns {void}
|
|
3976
3976
|
*/
|
|
@@ -4855,7 +4855,20 @@ function coefficient(...value) {
|
|
|
4855
4855
|
};
|
|
4856
4856
|
}
|
|
4857
4857
|
|
|
4858
|
-
|
|
4858
|
+
const falseValues = new Set(['', '0', 'n', 'f', 'no', 'off', 'false', '否', '不', '假', '不是', '不行', '不可以']);
|
|
4859
|
+
/**
|
|
4860
|
+
*
|
|
4861
|
+
* @param {any} v
|
|
4862
|
+
* @returns
|
|
4863
|
+
*/
|
|
4864
|
+
function toBool(v) {
|
|
4865
|
+
if (typeof v !== 'string') {
|
|
4866
|
+
return Boolean(v);
|
|
4867
|
+
}
|
|
4868
|
+
return !falseValues.has(v.toLowerCase());
|
|
4869
|
+
}
|
|
4870
|
+
|
|
4871
|
+
/** @import { FieldDefine, Fields, Options, Queryable, Select, TableConnect, VirtualTable } from './types' */
|
|
4859
4872
|
/** @import { WhereValue, Wheres } from './Where.mjs' */
|
|
4860
4873
|
/**
|
|
4861
4874
|
*
|
|
@@ -4953,7 +4966,7 @@ class Query extends Subquery {
|
|
|
4953
4966
|
#options = {
|
|
4954
4967
|
where: []
|
|
4955
4968
|
};
|
|
4956
|
-
/** @type {string[]} 主键字段 */
|
|
4969
|
+
/** @type {[string, FieldDefine][]} 主键字段 */
|
|
4957
4970
|
#primaryFields = [];
|
|
4958
4971
|
get options() {
|
|
4959
4972
|
return this.#options;
|
|
@@ -4977,7 +4990,6 @@ class Query extends Subquery {
|
|
|
4977
4990
|
...options.#options
|
|
4978
4991
|
};
|
|
4979
4992
|
this.#primaryFields = options.#primaryFields;
|
|
4980
|
-
this.#primary = options.#primary;
|
|
4981
4993
|
} else {
|
|
4982
4994
|
const {
|
|
4983
4995
|
connect,
|
|
@@ -4991,7 +5003,7 @@ class Query extends Subquery {
|
|
|
4991
5003
|
this.fields = fields;
|
|
4992
5004
|
this.pseudo = typeof pseudo === 'string' ? pseudo : '';
|
|
4993
5005
|
this.#build = typeof build === 'function' ? build : null;
|
|
4994
|
-
this.#primaryFields = Object.entries(fields).filter(([, v]) => v.primary).sort(([, a], [, b]) => Number(a.primary) - Number(b.primary))
|
|
5006
|
+
this.#primaryFields = Object.entries(fields).filter(([, v]) => v.primary).sort(([, a], [, b]) => Number(a.primary) - Number(b.primary));
|
|
4995
5007
|
if (noBuild) {
|
|
4996
5008
|
this.#build = null;
|
|
4997
5009
|
} else if (typeof options[Build] === 'function') {
|
|
@@ -5326,17 +5338,13 @@ class Query extends Subquery {
|
|
|
5326
5338
|
this.#options.where = [...this.#options.where, ...matches];
|
|
5327
5339
|
return this;
|
|
5328
5340
|
}
|
|
5329
|
-
/** @type {string | number | bigint | object | undefined} */
|
|
5330
|
-
#primary;
|
|
5331
5341
|
/**
|
|
5332
|
-
* @param {any
|
|
5342
|
+
* @param {...any | any[]} primaries
|
|
5333
5343
|
* @returns {this}
|
|
5334
5344
|
*/
|
|
5335
|
-
primary(
|
|
5336
|
-
|
|
5337
|
-
|
|
5338
|
-
}
|
|
5339
|
-
if (!primary) {
|
|
5345
|
+
primary(...primaries) {
|
|
5346
|
+
const primary = primaries?.flat();
|
|
5347
|
+
if (!primary.length) {
|
|
5340
5348
|
return this;
|
|
5341
5349
|
}
|
|
5342
5350
|
const primaryFields = this.#primaryFields;
|
|
@@ -5344,27 +5352,26 @@ class Query extends Subquery {
|
|
|
5344
5352
|
return this;
|
|
5345
5353
|
}
|
|
5346
5354
|
if (primaryFields.length === 1) {
|
|
5347
|
-
const [field] = primaryFields;
|
|
5348
|
-
|
|
5349
|
-
m.#primary = primary;
|
|
5350
|
-
return m;
|
|
5355
|
+
const [[field]] = primaryFields;
|
|
5356
|
+
return this.where(field, 'in', primary);
|
|
5351
5357
|
}
|
|
5352
|
-
|
|
5353
|
-
|
|
5354
|
-
|
|
5355
|
-
m = m.where(p, '=', primary[p]);
|
|
5358
|
+
const list = primary.map(primary => {
|
|
5359
|
+
if (typeof primary === 'object') {
|
|
5360
|
+
return primaryFields.map(([p]) => primary[p]);
|
|
5356
5361
|
}
|
|
5357
|
-
|
|
5358
|
-
|
|
5359
|
-
|
|
5360
|
-
|
|
5361
|
-
|
|
5362
|
-
|
|
5363
|
-
|
|
5364
|
-
|
|
5365
|
-
|
|
5366
|
-
|
|
5367
|
-
|
|
5362
|
+
const values = `${primary}`.split('$');
|
|
5363
|
+
if (values.length !== primaryFields.length) ;
|
|
5364
|
+
return primaryFields.map(([, {
|
|
5365
|
+
type
|
|
5366
|
+
}], i) => {
|
|
5367
|
+
const value = values[i] ?? null;
|
|
5368
|
+
if (type !== 'bool' || value === null) {
|
|
5369
|
+
return value;
|
|
5370
|
+
}
|
|
5371
|
+
return toBool(value);
|
|
5372
|
+
});
|
|
5373
|
+
});
|
|
5374
|
+
return this.where(primaryFields.map(f => f[0]), 'in', list);
|
|
5368
5375
|
}
|
|
5369
5376
|
/**
|
|
5370
5377
|
*
|
|
@@ -5416,8 +5423,8 @@ function toPrimaries(model, documents) {
|
|
|
5416
5423
|
* @returns {Hooks['where'] | undefined}
|
|
5417
5424
|
*/
|
|
5418
5425
|
function mergeWhere(hooks) {
|
|
5419
|
-
if (
|
|
5420
|
-
return;
|
|
5426
|
+
if (hooks.length <= 1) {
|
|
5427
|
+
return hooks[0];
|
|
5421
5428
|
}
|
|
5422
5429
|
return async function (conn, model) {
|
|
5423
5430
|
let that;
|
|
@@ -5437,8 +5444,8 @@ function mergeWhere(hooks) {
|
|
|
5437
5444
|
* @returns {Hooks['beforeCreateMany'] | undefined}
|
|
5438
5445
|
*/
|
|
5439
5446
|
function mergeBeforeCreateMany(hooks) {
|
|
5440
|
-
if (
|
|
5441
|
-
return;
|
|
5447
|
+
if (hooks.length <= 1) {
|
|
5448
|
+
return hooks[0];
|
|
5442
5449
|
}
|
|
5443
5450
|
return async function (conn, model, record) {
|
|
5444
5451
|
let data = record;
|
|
@@ -5454,8 +5461,8 @@ function mergeBeforeCreateMany(hooks) {
|
|
|
5454
5461
|
* @returns {Hooks['beforeCreate'] | undefined}
|
|
5455
5462
|
*/
|
|
5456
5463
|
function mergeBeforeCreate(hooks) {
|
|
5457
|
-
if (
|
|
5458
|
-
return;
|
|
5464
|
+
if (hooks.length <= 1) {
|
|
5465
|
+
return hooks[0];
|
|
5459
5466
|
}
|
|
5460
5467
|
return async function (conn, model, record) {
|
|
5461
5468
|
let data = record;
|
|
@@ -5471,8 +5478,8 @@ function mergeBeforeCreate(hooks) {
|
|
|
5471
5478
|
* @returns {Hooks['beforeUpdate'] | undefined}
|
|
5472
5479
|
*/
|
|
5473
5480
|
function mergeBeforeUpdate(hooks) {
|
|
5474
|
-
if (
|
|
5475
|
-
return;
|
|
5481
|
+
if (hooks.length <= 1) {
|
|
5482
|
+
return hooks[0];
|
|
5476
5483
|
}
|
|
5477
5484
|
return async function (conn, model, record, set) {
|
|
5478
5485
|
let data = set;
|
|
@@ -5488,8 +5495,8 @@ function mergeBeforeUpdate(hooks) {
|
|
|
5488
5495
|
* @returns {((this: any, ...argv: T) => MaybePromise<void>) | undefined}
|
|
5489
5496
|
*/
|
|
5490
5497
|
function mergeRun(hooks) {
|
|
5491
|
-
if (
|
|
5492
|
-
return;
|
|
5498
|
+
if (hooks.length <= 1) {
|
|
5499
|
+
return hooks[0];
|
|
5493
5500
|
}
|
|
5494
5501
|
return async function (...p) {
|
|
5495
5502
|
for (const hook of hooks) {
|
|
@@ -5513,14 +5520,14 @@ function filterFunction(v) {
|
|
|
5513
5520
|
function mergeHooks(...hooks) {
|
|
5514
5521
|
const allHooks = hooks.flat();
|
|
5515
5522
|
return {
|
|
5516
|
-
where: mergeWhere(allHooks.map(v => v.where).filter(filterFunction)),
|
|
5517
|
-
beforeCreateMany: mergeBeforeCreateMany(allHooks.map(v => v.beforeCreateMany).filter(filterFunction)),
|
|
5523
|
+
where: mergeWhere(allHooks.map(v => v.where).filter(filterFunction).reverse()),
|
|
5524
|
+
beforeCreateMany: mergeBeforeCreateMany(allHooks.map(v => v.beforeCreateMany).filter(filterFunction).reverse()),
|
|
5518
5525
|
afterCreateMany: mergeRun(allHooks.map(v => v.afterCreateMany).filter(filterFunction)),
|
|
5519
|
-
beforeCreate: mergeBeforeCreate(allHooks.map(v => v.beforeCreate).filter(filterFunction)),
|
|
5526
|
+
beforeCreate: mergeBeforeCreate(allHooks.map(v => v.beforeCreate).filter(filterFunction).reverse()),
|
|
5520
5527
|
afterCreate: mergeRun(allHooks.map(v => v.afterCreate).filter(filterFunction)),
|
|
5521
|
-
beforeUpdate: mergeBeforeUpdate(allHooks.map(v => v.beforeUpdate).filter(filterFunction)),
|
|
5528
|
+
beforeUpdate: mergeBeforeUpdate(allHooks.map(v => v.beforeUpdate).filter(filterFunction).reverse()),
|
|
5522
5529
|
afterUpdate: mergeRun(allHooks.map(v => v.afterUpdate).filter(filterFunction)),
|
|
5523
|
-
beforeDelete: mergeRun(allHooks.map(v => v.beforeDelete).filter(filterFunction)),
|
|
5530
|
+
beforeDelete: mergeRun(allHooks.map(v => v.beforeDelete).filter(filterFunction).reverse()),
|
|
5524
5531
|
afterDelete: mergeRun(allHooks.map(v => v.afterDelete).filter(filterFunction))
|
|
5525
5532
|
};
|
|
5526
5533
|
}
|
|
@@ -6510,7 +6517,8 @@ class Scene {
|
|
|
6510
6517
|
return {
|
|
6511
6518
|
...table,
|
|
6512
6519
|
table: tableMap.table,
|
|
6513
|
-
fields: this.#cloneField(table.fields, tableMap)
|
|
6520
|
+
fields: this.#cloneField(table.fields, tableMap),
|
|
6521
|
+
scene: this
|
|
6514
6522
|
// TODO: 索引?
|
|
6515
6523
|
};
|
|
6516
6524
|
}
|
|
@@ -6550,6 +6558,19 @@ class Scene {
|
|
|
6550
6558
|
table(table) {
|
|
6551
6559
|
return this.#getModel(table);
|
|
6552
6560
|
}
|
|
6561
|
+
/**
|
|
6562
|
+
* @template {TableDefine<Fields, string>} T
|
|
6563
|
+
* @param {{scene?: Scene} | Scene} source
|
|
6564
|
+
* @param {T} table
|
|
6565
|
+
* @returns {T}
|
|
6566
|
+
*/
|
|
6567
|
+
static table(source, table) {
|
|
6568
|
+
const scene = source instanceof Scene ? source : source?.scene;
|
|
6569
|
+
if (!(scene instanceof Scene)) {
|
|
6570
|
+
return table;
|
|
6571
|
+
}
|
|
6572
|
+
return scene.table(table);
|
|
6573
|
+
}
|
|
6553
6574
|
}
|
|
6554
6575
|
|
|
6555
6576
|
/** @import { FieldDefine, FieldDefineOption, MainFieldType, ToType } from './types' */
|
|
@@ -6702,4 +6723,4 @@ function field(type, {
|
|
|
6702
6723
|
};
|
|
6703
6724
|
}
|
|
6704
6725
|
|
|
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 };
|
|
6726
|
+
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, toBool, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, where, withDeleted };
|
package/migrate.d.mts
CHANGED
package/migrate.mjs
CHANGED