imodel 0.18.0 → 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 CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * imodel v0.18.0
2
+ * imodel v0.19.0
3
3
  * (c) 2019-2026 undefined
4
4
  * @license undefined
5
5
  */
@@ -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} [primary]
2121
+ * @param {...any | any[]} primaries
2122
2122
  * @returns {this}
2123
2123
  */
2124
- primary(primary?: any, ...args: any[]): this;
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.18.0
2
+ * imodel v0.19.0
3
3
  * (c) 2019-2026 undefined
4
4
  * @license undefined
5
5
  */
@@ -4855,7 +4855,20 @@ function coefficient(...value) {
4855
4855
  };
4856
4856
  }
4857
4857
 
4858
- /** @import { Fields, Options, Queryable, Select, TableConnect, VirtualTable } from './types' */
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)).map(([v]) => v);
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} [primary]
5342
+ * @param {...any | any[]} primaries
5333
5343
  * @returns {this}
5334
5344
  */
5335
- primary(primary) {
5336
- if (this.#primary) {
5337
- return this;
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
- const m = this.where(field, '=', primary);
5349
- m.#primary = primary;
5350
- return m;
5355
+ const [[field]] = primaryFields;
5356
+ return this.where(field, 'in', primary);
5351
5357
  }
5352
- if (typeof primary === 'object') {
5353
- let m = this;
5354
- for (const p of primaryFields) {
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
- m.#primary = primary;
5358
- return m;
5359
- }
5360
- const values = `${primary}`.split('$');
5361
- if (values.length !== primaryFields.length) ;
5362
- let m = this;
5363
- for (let i = 0; i < primaryFields.length; i++) {
5364
- m = m.where(primaryFields[i], '=', values[i]);
5365
- }
5366
- m.#primary = primary;
5367
- return m;
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
  *
@@ -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
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * imodel v0.18.0
2
+ * imodel v0.19.0
3
3
  * (c) 2019-2026 undefined
4
4
  * @license undefined
5
5
  */
package/migrate.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * imodel v0.18.0
2
+ * imodel v0.19.0
3
3
  * (c) 2019-2026 undefined
4
4
  * @license undefined
5
5
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imodel",
3
- "version": "0.18.0",
3
+ "version": "0.19.0",
4
4
  "main": "index.mjs",
5
5
  "type": "module",
6
6
  "repository": {