imodel 0.4.0 → 0.4.1

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.4.0
2
+ * imodel v0.4.1
3
3
  * (c) 2019-2025 undefined
4
4
  * @license undefined
5
5
  */
@@ -1841,6 +1841,33 @@ declare function toPrimary(fields: string[], data: Record<string, any>): string;
1841
1841
  */
1842
1842
  declare function toPrimaries(model: any, documents: any): string | string[];
1843
1843
 
1844
+ /**
1845
+ * @typedef {object} Scene.Define
1846
+ * @property {string} table
1847
+ * @property {[column: string, field?: string, value?: any][]} fields
1848
+ */
1849
+ /** @import {FieldDefine, Fields, TableDefine,} from './types/table' */
1850
+ declare class Scene {
1851
+ /**
1852
+ *
1853
+ * @param {Map<string, Scene.Define>} map
1854
+ */
1855
+ constructor(map: Map<string, Scene.Define>);
1856
+ /**
1857
+ * @template {TableDefine<Fields, string>} T
1858
+ * @param {T} table
1859
+ * @returns {T}
1860
+ */
1861
+ table<T extends TableDefine<Fields, string>>(table: T): T;
1862
+ #private;
1863
+ }
1864
+ declare namespace Scene {
1865
+ type Define = {
1866
+ table: string;
1867
+ fields: [column: string, field?: string, value?: any][];
1868
+ };
1869
+ }
1870
+
1844
1871
  /**
1845
1872
  * @implements {Save}
1846
1873
  * @implements {Destroy}
@@ -1868,6 +1895,8 @@ declare class Model implements Save, Destroy, PseudoDestroy {
1868
1895
  * @returns {T}
1869
1896
  */
1870
1897
  static bindFields<T extends typeof Model>(this: T, fields?: string[]): T;
1898
+ /** @type {Scene?} */
1899
+ static get scene(): Scene | null;
1871
1900
  /**
1872
1901
  * @template {typeof Model} T
1873
1902
  * @this {T}
@@ -1989,6 +2018,12 @@ declare class Model implements Save, Destroy, PseudoDestroy {
1989
2018
  * @returns {VoidReturn}
1990
2019
  */
1991
2020
  $_onAfterDelete(t: Connection, update: object | null): VoidReturn;
2021
+ /**
2022
+ * @template {TableDefine<Fields, string>} T
2023
+ * @param {T} table
2024
+ * @returns {T}
2025
+ */
2026
+ $table<T extends TableDefine<Fields, string>>(table: T): T;
1992
2027
  /**
1993
2028
  * @param {Connection} t
1994
2029
  * @param {(data: any, newData?: any) => Promise<object?>} run
@@ -2198,4 +2233,4 @@ declare function field<T extends MainFieldType>(type: T, options: {
2198
2233
  declare function setDevelopment(d?: boolean): void;
2199
2234
  declare let isDevelopment: boolean;
2200
2235
 
2201
- 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 IConnection, type Index, type IndexInfo, type IndexOptions, type Join, type JoinType, type MainFieldType, type MaybePromise, Model, type Options, PseudoDestroy, Query, type QueryOptions, type Queryable, Save, Select, SetValue, type Skip, Submodel, type Support, type TableDefine, type TableType, type ToFieldType, type ToType, type TransactionFn, type VirtualTable, Where, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, column, creating, decrement, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, submodel as model, multiply, now, primary, prop, pseudo, setDevelopment, sort, submodel, toNot, toPrimaries, toPrimary, undeleted, updating, uuid, values, withDeleted };
2236
+ 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 IConnection, type Index, type IndexInfo, type IndexOptions, type Join, type JoinType, type MainFieldType, type MaybePromise, Model, type Options, PseudoDestroy, Query, type QueryOptions, type Queryable, Save, Scene, Select, SetValue, type Skip, Submodel, type Support, type TableDefine, type TableType, type ToFieldType, type ToType, type TransactionFn, type VirtualTable, Where, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, column, creating, decrement, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, submodel as model, multiply, now, primary, prop, pseudo, setDevelopment, sort, submodel, toNot, toPrimaries, toPrimary, undeleted, updating, uuid, values, withDeleted };
package/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * imodel v0.4.0
2
+ * imodel v0.4.1
3
3
  * (c) 2019-2025 undefined
4
4
  * @license undefined
5
5
  */
@@ -4208,7 +4208,8 @@ function toPrimaries(model, documents) {
4208
4208
  }
4209
4209
 
4210
4210
  /* eslint-disable max-lines */
4211
- /** @import { Fields, IndexInfo, FieldDefine } from './types' */
4211
+ /** @import { Fields, IndexInfo, FieldDefine, TableDefine } from './types' */
4212
+ /** @import { Scene } from './Scene.mjs' */
4212
4213
  /**
4213
4214
  * @param {any} a
4214
4215
  * @param {any} b
@@ -5001,6 +5002,24 @@ class Model {
5001
5002
  }
5002
5003
  return this.#do(() => this.$_onBeforeDelete(t, update), () => run(this.#data), () => this.$_onAfterDelete(t, update));
5003
5004
  }
5005
+ /** @type {Scene?} */
5006
+ static get scene() {
5007
+ return null;
5008
+ }
5009
+ /**
5010
+ * @template {TableDefine<Fields, string>} T
5011
+ * @param {T} table
5012
+ * @returns {T}
5013
+ */
5014
+ $table(table) {
5015
+ const {
5016
+ scene
5017
+ } = this.#model;
5018
+ if (!scene) {
5019
+ return table;
5020
+ }
5021
+ return scene.table(table);
5022
+ }
5004
5023
  }
5005
5024
  /**
5006
5025
  * @param {string} key
@@ -5056,6 +5075,129 @@ defGetter('indexes', function () {
5056
5075
  return [];
5057
5076
  });
5058
5077
 
5078
+ /**
5079
+ * @typedef {object} Scene.Define
5080
+ * @property {string} table
5081
+ * @property {[column: string, field?: string, value?: any][]} fields
5082
+ */
5083
+ /** @import {FieldDefine, Fields, TableDefine,} from './types/table' */
5084
+ class Scene {
5085
+ /** @type {Map<string, Scene.Define>} */
5086
+ #map;
5087
+ /** @type {Map<typeof Model, typeof Model>} */
5088
+ #models = new Map();
5089
+ /**
5090
+ *
5091
+ * @param {Map<string, Scene.Define>} map
5092
+ */
5093
+ constructor(map) {
5094
+ this.#map = map;
5095
+ }
5096
+ /**
5097
+ *
5098
+ * @param {Fields} sceneFields
5099
+ * @param {Scene.Define} table
5100
+ * @returns
5101
+ */
5102
+ #cloneField(sceneFields, table) {
5103
+ const allFields = new Map(Object.entries(sceneFields));
5104
+ /** @type {Record<string, FieldDefine>} */
5105
+ const fields = {};
5106
+ for (const [column, field, value] of table.fields) {
5107
+ if (field) {
5108
+ const define = allFields.get(field);
5109
+ allFields.delete(field);
5110
+ if (!define) {
5111
+ throw new Error();
5112
+ }
5113
+ if (!define.type) {
5114
+ throw new Error();
5115
+ }
5116
+ if (typeof define.type !== 'string' && define.type.table) {
5117
+ throw new Error();
5118
+ }
5119
+ fields[field] = {
5120
+ ...define,
5121
+ column
5122
+ };
5123
+ }
5124
+ }
5125
+ for (const [name, define] of allFields) {
5126
+ if (!define.type) {
5127
+ fields[name] = {
5128
+ ...define
5129
+ };
5130
+ continue;
5131
+ }
5132
+ if (typeof define.type === 'string') {
5133
+ throw new Error();
5134
+ }
5135
+ if (typeof define.type === 'function') {
5136
+ fields[name] = {
5137
+ ...define,
5138
+ type: this.#getModel(define.type)
5139
+ };
5140
+ continue;
5141
+ }
5142
+ }
5143
+ return fields;
5144
+ }
5145
+ /**
5146
+ * @template {TableDefine<Fields, string>} T
5147
+ * @param {T} table
5148
+ * @returns {T}
5149
+ */
5150
+ #getModel(table, name = table.table) {
5151
+ const tableMap = this.#map.get(name);
5152
+ if (!tableMap) {
5153
+ throw new Error();
5154
+ }
5155
+ if (typeof table !== 'function') {
5156
+ return {
5157
+ ...table,
5158
+ table: tableMap.table,
5159
+ fields: this.#cloneField(table.fields, tableMap)
5160
+ // TODO: 索引?
5161
+ };
5162
+ }
5163
+ const models = this.#models;
5164
+ const ModelClass = /** @type {typeof Model} */table;
5165
+ const defined = models.get(ModelClass);
5166
+ // @ts-ignore
5167
+ if (defined) {
5168
+ return defined;
5169
+ }
5170
+ class M extends ModelClass {}
5171
+ models.set(table, M);
5172
+ Object.defineProperty(M, 'table', {
5173
+ value: tableMap.table,
5174
+ configurable: true,
5175
+ enumerable: true
5176
+ });
5177
+ Object.defineProperty(M, 'scene', {
5178
+ value: this,
5179
+ configurable: true,
5180
+ enumerable: true
5181
+ });
5182
+ Object.defineProperty(M, 'fields', {
5183
+ value: this.#cloneField(ModelClass.fields, tableMap),
5184
+ enumerable: true,
5185
+ configurable: true
5186
+ });
5187
+ // TODO: 索引?
5188
+ // @ts-ignore
5189
+ return M;
5190
+ }
5191
+ /**
5192
+ * @template {TableDefine<Fields, string>} T
5193
+ * @param {T} table
5194
+ * @returns {T}
5195
+ */
5196
+ table(table) {
5197
+ return this.#getModel(table);
5198
+ }
5199
+ }
5200
+
5059
5201
  /** @import { FieldDefine, FieldDefineOption, MainFieldType, ToType } from './types' */
5060
5202
  /**
5061
5203
  * @template {MainFieldType} T
@@ -5204,4 +5346,4 @@ function field(type, {
5204
5346
  };
5205
5347
  }
5206
5348
 
5207
- export { Build, Connection, Create, Destroy, Model, PseudoDestroy, Query, Save, Submodel, Where, column, creating, decrement, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, submodel as model, multiply, now, primary, prop, pseudo, setDevelopment, sort, submodel, toNot, toPrimaries, toPrimary, undeleted, updating, uuid, values, withDeleted };
5349
+ export { Build, Connection, Create, Destroy, Model, PseudoDestroy, Query, Save, Scene, Submodel, Where, column, creating, decrement, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, submodel as model, multiply, now, primary, prop, pseudo, setDevelopment, sort, submodel, toNot, toPrimaries, toPrimary, undeleted, updating, uuid, values, withDeleted };
package/migrate.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * imodel v0.4.0
2
+ * imodel v0.4.1
3
3
  * (c) 2019-2025 undefined
4
4
  * @license undefined
5
5
  */
package/migrate.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * imodel v0.4.0
2
+ * imodel v0.4.1
3
3
  * (c) 2019-2025 undefined
4
4
  * @license undefined
5
5
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imodel",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "main": "index.mjs",
5
5
  "type": "module",
6
6
  "repository": {