imodel 0.1.0 → 0.3.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.1.0
2
+ * imodel v0.3.0
3
3
  * (c) 2019-2025 undefined
4
4
  * @license undefined
5
5
  */
@@ -18,8 +18,6 @@ declare namespace values {
18
18
  interface Environment<T extends object> {
19
19
  transaction: T | null;
20
20
  isDevelopment: boolean;
21
- names?: Record<string, string> | null;
22
- getName(table: string): string;
23
21
  values: typeof values;
24
22
  }
25
23
 
@@ -514,7 +512,7 @@ declare class Where {
514
512
  #private;
515
513
  }
516
514
 
517
- /** @typedef {SetValue.Increment | SetValue.Decrement | SetValue.Multiply | SetValue.Divide | string | number | bigint | boolean | null | typeof now} SetValue */
515
+ /** @typedef {SetValue.Increment | SetValue.Decrement | SetValue.Multiply | SetValue.Divide | string | number | bigint | boolean | null | typeof now | (string | number | bigint | boolean)[]} SetValue */
518
516
  /** @typedef {{[values.increment]: number | bigint}} SetValue.Increment */
519
517
  /** @typedef {{[values.decrement]: number | bigint}} SetValue.Decrement */
520
518
  /** @typedef {{[values.multiply]: number | bigint}} SetValue.Multiply */
@@ -540,7 +538,7 @@ declare function multiply(value: number | bigint): SetValue.Multiply;
540
538
  */
541
539
  declare function divide(value: number | bigint): SetValue.Divide;
542
540
 
543
- type SetValue = SetValue.Increment | SetValue.Decrement | SetValue.Multiply | SetValue.Divide | string | number | bigint | boolean | null | typeof now;
541
+ type SetValue = SetValue.Increment | SetValue.Decrement | SetValue.Multiply | SetValue.Divide | string | number | bigint | boolean | null | typeof now | (string | number | bigint | boolean)[];
544
542
  declare namespace SetValue {
545
543
  type Increment = {
546
544
  [increment$1]: number | bigint;
@@ -570,18 +568,9 @@ interface VirtualTable<E extends object = object> {
570
568
 
571
569
  /** 字段基本类型 */
572
570
  interface FieldType {
573
- /** 基本的 Id 类型,具体数据库实现由数据库决定 */
574
- id: string;
575
- /** 引用类型,具体数据库实现由数据库决定,需要与 id 类型一致 */
576
- rid: string;
577
- /** 弱引用类型,需要考虑跨数据库引擎实现 */
578
- ref: string;
579
571
  i16: number;
580
572
  i32: number;
581
573
  i64: bigint;
582
- u16: number;
583
- u32: number;
584
- u64: bigint;
585
574
  f32: number;
586
575
  f64: number;
587
576
  decimal: number;
@@ -595,12 +584,13 @@ interface FieldType {
595
584
  ip: string;
596
585
  ipv4: string;
597
586
  ipv6: string;
587
+ ipnet: string;
588
+ ipnetv4: string;
589
+ ipnetv6: string;
598
590
  mac48: string;
599
591
  mac64: string;
600
592
  /** 布尔型 */
601
593
  bool: boolean;
602
- /** 二进制类型 */
603
- binary: ArrayBuffer | ArrayBufferView;
604
594
  /** 日期类型 */
605
595
  date: Date;
606
596
  /** 时间类型 */
@@ -624,27 +614,28 @@ interface Constraint {
624
614
  }
625
615
  type MainFieldType = keyof FieldType | TableDefine;
626
616
  type ToFieldType<T> = T extends keyof FieldType ? FieldType[T] : unknown;
617
+ type FieldSpecificValue = (string | number | bigint | boolean)[] | string | number | bigint | boolean | null | typeof now;
618
+ type FieldSpecific = FieldSpecificValue | {
619
+ value: FieldSpecificValue;
620
+ computed?(): PromiseLike<FieldSpecificValue> | FieldSpecificValue;
621
+ };
627
622
  interface FieldDefineOption {
628
- /** 精确度 */
629
- precision?: number;
630
623
  /** 缩放比 */
631
624
  scale?: number;
632
625
  /** 占用空间大小 */
633
626
  size?: number;
634
627
  /** 是否为主键 */
635
- primary?: number | boolean;
636
- /** 序列字段 */
637
- serial?: boolean;
628
+ primary?: number;
638
629
  /** 唯一索引? */
639
630
  unique?: boolean;
640
631
  /** 索引信息 */
641
632
  index?: string | boolean;
642
- /** 创建时,作为时间戳 */
643
- timestampCreating?: boolean;
644
- /** 更新时,作为时间戳 */
645
- timestampUpdating?: boolean;
646
- /** 伪删除时,作为时间戳 */
647
- timestampDeleting?: boolean;
633
+ /** 创建时,自定义数据 */
634
+ creating?: FieldSpecific;
635
+ /** 更新时,自定义数据 */
636
+ updating?: FieldSpecific;
637
+ /** 伪删除时,自定义数据 */
638
+ deleting?: FieldSpecific;
648
639
  /** 是否为固定值 */
649
640
  immutable?: boolean;
650
641
  /** 文本查询 */
@@ -783,7 +774,7 @@ interface PseudoDestroy {
783
774
  }
784
775
  declare const Save: unique symbol;
785
776
  interface Save {
786
- [Save](connection: Connection, run: (data: any, newData?: any) => Promise<object | null>, table: TableDefine): PromiseLike<this | null>;
777
+ [Save](connection: Connection, run: (data: any, newData?: any) => Promise<object | null>, newData: Record<string, any> | null, table: TableDefine): PromiseLike<this | null>;
787
778
  }
788
779
  declare const Create: unique symbol;
789
780
  interface Create<T = unknown> {
@@ -807,6 +798,27 @@ interface MatchArg extends Omit<TableDefine<Fields<keyof FieldType>>, 'table'> {
807
798
  where?: WhereValue[];
808
799
  }
809
800
  type BuildValue<T extends TableDefine> = T extends Build<infer R> ? R : object;
801
+ interface DBField {
802
+ type: string;
803
+ size: number;
804
+ scale: number;
805
+ default?: any;
806
+ nullable: boolean;
807
+ array: boolean;
808
+ primary?: number;
809
+ }
810
+ interface DBIndex {
811
+ fields: string[];
812
+ includes: string[];
813
+ unique: boolean;
814
+ name?: string;
815
+ }
816
+ interface DBTable {
817
+ table: string;
818
+ fields: Record<string, DBField>;
819
+ indexes: DBIndex[];
820
+ primary?: string;
821
+ }
810
822
  interface IConnection<E extends object = object> {
811
823
  /** 数据库类型 */
812
824
  readonly dbType: string;
@@ -842,8 +854,14 @@ interface IConnection<E extends object = object> {
842
854
  dropView(environment: Environment<E>, name: string): PromiseLike<number>;
843
855
  createMaterializedView(environment: Environment<E>, name: string, q: SelectArg): PromiseLike<number>;
844
856
  dropMaterializedView(environment: Environment<E>, name: string): PromiseLike<number>;
857
+ loadTables(environment: Environment<E>, tables: string[]): PromiseLike<DBTable[]>;
858
+ syncTables(environment: Environment<E>, tables: DBTable[]): PromiseLike<void>;
845
859
  transaction<R>(environment: Environment<E>, fn: (t: E) => R | PromiseLike<R>): PromiseLike<R>;
846
860
  }
861
+ interface Skip {
862
+ immutable?: boolean;
863
+ specific?: boolean;
864
+ }
847
865
  interface TransactionFn {
848
866
  <T>(fn: (t: Connection) => PromiseLike<T> | T): Promise<T>;
849
867
  }
@@ -860,18 +878,13 @@ declare class Connection<E extends {} = {}> {
860
878
  *
861
879
  * @param {PromiseLike<IConnection<E>> | IConnection<E>} connection
862
880
  * @param {E?} [transaction]
863
- * @param {Record<string, string>?} [names]
864
881
  */
865
- constructor(connection: PromiseLike<IConnection<E>> | IConnection<E>, transaction?: E | null, names?: Record<string, string> | null);
882
+ constructor(connection: PromiseLike<IConnection<E>> | IConnection<E>, transaction?: E | null);
866
883
  /**
867
884
  *
868
- * @param {object} [options]
869
- * @param {Record<string, string>?} [options.names]
870
885
  * @returns {Connection<E>}
871
886
  */
872
- clone({ names }?: {
873
- names?: Record<string, string> | null | undefined;
874
- }): Connection<E>;
887
+ clone(): Connection<E>;
875
888
  /** @return {Promise<string>} */
876
889
  dbType(): Promise<string>;
877
890
  /** @return {Promise<string>} */
@@ -954,9 +967,10 @@ declare class Connection<E extends {} = {}> {
954
967
  * @template T
955
968
  * @param {TableDefine} model
956
969
  * @param {T} data
970
+ * @param {Record<string, any>?} [newData]
957
971
  * @returns {Promise<(T extends Save ? T : object)?>}
958
972
  */
959
- save<T>(model: TableDefine, data: T): Promise<(T extends Save ? T : object) | null>;
973
+ save<T>(model: TableDefine, data: T, newData?: Record<string, any> | null): Promise<(T extends Save ? T : object) | null>;
960
974
  /**
961
975
  *
962
976
  * @template T
@@ -970,7 +984,7 @@ declare class Connection<E extends {} = {}> {
970
984
  * @template {object} T
971
985
  * @param {TableDefine} tableDef
972
986
  * @param {T} data
973
- * @param {Record<string, SetValue>} value
987
+ * @param {Record<string, SetValue>} value
974
988
  * @returns {Promise<(T extends PseudoDestroy ? T : object)?>}
975
989
  */
976
990
  pseudoDestroy<T extends unknown>(tableDef: TableDefine, data: T, value: Record<string, SetValue>): Promise<(T extends PseudoDestroy ? T : object) | null>;
@@ -1018,29 +1032,29 @@ declare class Connection<E extends {} = {}> {
1018
1032
  * @param {TableDefine} tableDefine
1019
1033
  * @param {Record<string, any>} update
1020
1034
  * @param {WhereValue[]?} [where]
1021
- * @param {boolean} [skip]
1035
+ * @param {boolean | Skip} [skip]
1022
1036
  * @returns {Promise<number>}
1023
1037
  */
1024
- update({ table, fields }: TableDefine, update: Record<string, any>, where?: WhereValue[] | null, skip?: boolean): Promise<number>;
1038
+ update({ table, fields }: TableDefine, update: Record<string, any>, where?: WhereValue[] | null, skip?: boolean | Skip): Promise<number>;
1025
1039
  /**
1026
1040
  * @template {Fields<MainFieldType>} T
1027
1041
  * @param {TableDefine<T>} tableDefine
1028
1042
  * @param {Record<string, any>} update
1029
1043
  * @param {string[]?} [returning]
1030
1044
  * @param {WhereValue[]?} [where]
1031
- * @param {boolean} [skip]
1045
+ * @param {boolean | Skip} [skip]
1032
1046
  * @returns {Promise<any[]>}
1033
1047
  */
1034
- updateReturn<T extends Fields<MainFieldType>>({ table, fields }: TableDefine<T>, update: Record<string, any>, returning?: string[] | null, where?: WhereValue[] | null, skip?: boolean): Promise<any[]>;
1048
+ updateReturn<T extends Fields<MainFieldType>>({ table, fields }: TableDefine<T>, update: Record<string, any>, returning?: string[] | null, where?: WhereValue[] | null, skip?: boolean | Skip): Promise<any[]>;
1035
1049
  /**
1036
1050
  * @param {TableDefine} tableDefine
1037
1051
  * @param {string[]} pKeys
1038
1052
  * @param {string[]} setKeys
1039
1053
  * @param {Record<string, any>[]} list
1040
- * @param {boolean} [skip]
1054
+ * @param {boolean | Skip} [skip]
1041
1055
  * @returns {Promise<number>}
1042
1056
  */
1043
- updateMany({ table, fields }: TableDefine, pKeys: string[], setKeys: string[], list: Record<string, any>[], skip?: boolean): Promise<number>;
1057
+ updateMany({ table, fields }: TableDefine, pKeys: string[], setKeys: string[], list: Record<string, any>[], skip?: boolean | Skip): Promise<number>;
1044
1058
  /**
1045
1059
  * @template {Fields<MainFieldType>} T
1046
1060
  * @param {TableDefine<T>} tableDefine
@@ -1048,10 +1062,10 @@ declare class Connection<E extends {} = {}> {
1048
1062
  * @param {string[]} setKeys
1049
1063
  * @param {Record<string, any>[]} list
1050
1064
  * @param {string[]?} [returning]
1051
- * @param {boolean} [skip]
1065
+ * @param {boolean | Skip} [skip]
1052
1066
  * @returns {Promise<any[]>}
1053
1067
  */
1054
- updateManyReturn<T extends Fields<MainFieldType>>({ table, fields }: TableDefine<T>, pKeys: string[], setKeys: string[], list: Record<string, any>[], returning?: string[] | null, skip?: boolean): Promise<any[]>;
1068
+ updateManyReturn<T extends Fields<MainFieldType>>({ table, fields }: TableDefine<T>, pKeys: string[], setKeys: string[], list: Record<string, any>[], returning?: string[] | null, skip?: boolean | Skip): Promise<any[]>;
1055
1069
  /**
1056
1070
  * @param {TableDefine} tableDefine
1057
1071
  * @param {WhereValue[]} [where]
@@ -1167,6 +1181,21 @@ declare class Connection<E extends {} = {}> {
1167
1181
  */
1168
1182
  dropMaterializedView(view: string): Promise<number>;
1169
1183
  abort(): boolean;
1184
+ /**
1185
+ * @param {string} table
1186
+ * @returns {Promise<DBTable?>}
1187
+ */
1188
+ loadTable(table: string): Promise<DBTable | null>;
1189
+ /**
1190
+ * @param {string[]} tables
1191
+ * @returns {Promise<DBTable[]>}
1192
+ */
1193
+ loadTables(tables: string[]): Promise<DBTable[]>;
1194
+ /**
1195
+ * @param {DBTable[]} tables
1196
+ * @returns {Promise<void>}
1197
+ */
1198
+ syncTables(tables: DBTable[]): Promise<void>;
1170
1199
  /**
1171
1200
  * @template T
1172
1201
  * @overload
@@ -1436,17 +1465,6 @@ declare function field$1<T extends keyof FieldType>(type: T, options: {
1436
1465
  [k: string]: any;
1437
1466
  } & FieldDefineOption): FieldDecorator<ToType<T, boolean, boolean>>;
1438
1467
 
1439
- /** @import { ToFieldType } from '../types' */
1440
- /** @import { FieldDecorator } from './index.mjs' */
1441
- /**
1442
- * @param {object} [options]
1443
- * @param {number?} [options.sort]
1444
- * @returns {FieldDecorator<ToFieldType<'id'>>}
1445
- */
1446
- declare function id({ sort }?: {
1447
- sort?: number | null | undefined;
1448
- }): FieldDecorator<ToFieldType<"id">>;
1449
-
1450
1468
  /** @import { FieldDecorator } from './index.mjs' */
1451
1469
  /**
1452
1470
  * @param {string} prop
@@ -1604,20 +1622,20 @@ declare function submodel<T extends Record<string, any>>(type: TableDefine, cons
1604
1622
  declare function submodel<T extends Record<string, any>, A extends boolean = boolean>(type: TableDefine, constraints: Record<string, Constraint>, array: A, options?: FieldDefineOption | undefined): FieldDecorator<A extends false ? T : A extends true ? T[] : T | T[]>;
1605
1623
 
1606
1624
  /**
1607
- * @param {number} [precision]
1625
+ * @param {number} [size]
1608
1626
  * @returns {FieldDecorator<Date?>}
1609
1627
  */
1610
- declare function creating(precision?: number): FieldDecorator<Date | null>;
1628
+ declare function creating(size?: number): FieldDecorator<Date | null>;
1611
1629
  /**
1612
- * @param {number} [precision]
1630
+ * @param {number} [size]
1613
1631
  * @returns {FieldDecorator<Date?>}
1614
1632
  */
1615
- declare function updating(precision?: number): FieldDecorator<Date | null>;
1633
+ declare function updating(size?: number): FieldDecorator<Date | null>;
1616
1634
  /**
1617
- * @param {number} [precision]
1635
+ * @param {number} [size]
1618
1636
  * @returns {FieldDecorator<Date?>}
1619
1637
  */
1620
- declare function deleting(precision?: number): FieldDecorator<Date | null>;
1638
+ declare function deleting(size?: number): FieldDecorator<Date | null>;
1621
1639
 
1622
1640
  type ClassDecorator = (val: Function, ctx: ClassDecoratorContext) => any;
1623
1641
  type FieldDecorator<T> = (val: {
@@ -1626,7 +1644,7 @@ type FieldDecorator<T> = (val: {
1626
1644
  }, ctx: ClassAccessorDecoratorContext) => any;
1627
1645
 
1628
1646
  type QueryOptions<T extends Fields, TB extends unknown = any> = {
1629
- table: string;
1647
+ table?: string | VirtualTable<object> | undefined;
1630
1648
  fields: T;
1631
1649
  pseudo?: string | undefined;
1632
1650
  build?: ((a: object, b?: object | boolean) => TB) | null | undefined;
@@ -1635,7 +1653,7 @@ type QueryOptions<T extends Fields, TB extends unknown = any> = {
1635
1653
  * @template {Fields} T
1636
1654
  * @template {object} [TB=object]
1637
1655
  * @typedef {object} QueryOptions
1638
- * @property {string} table
1656
+ * @property {string | VirtualTable} [table]
1639
1657
  * @property {T} fields
1640
1658
  * @property {string} [pseudo]
1641
1659
  * @property {((a: object, b?: object | boolean) => TB)?} [build]
@@ -1660,8 +1678,8 @@ declare class Query<T extends Fields, TB extends unknown = any> implements Query
1660
1678
  * @param {QueryOptions<T, TB> | Query<T, TB>} options
1661
1679
  */
1662
1680
  constructor(options: QueryOptions<T, TB> | Query<T, TB>);
1663
- /** @readonly @type {string} */
1664
- readonly table: string;
1681
+ /** @readonly @type {string | VirtualTable | undefined} */
1682
+ readonly table: string | VirtualTable | undefined;
1665
1683
  /** @readonly @type {T} */
1666
1684
  readonly fields: T;
1667
1685
  /** @readonly @type {string} */
@@ -1821,6 +1839,23 @@ declare class Query<T extends Fields, TB extends unknown = any> implements Query
1821
1839
  #private;
1822
1840
  }
1823
1841
 
1842
+ /** @import { Fields, IndexInfo, FieldDefine } from './types' */
1843
+ /**
1844
+ *
1845
+ * @param {Fields} fields
1846
+ * @returns
1847
+ */
1848
+ declare function getPrimaryFields(fields: Fields): string[];
1849
+ /**
1850
+ * @param {string[]} fields
1851
+ * @param {Record<string, any>} data
1852
+ */
1853
+ declare function toPrimary(fields: string[], data: Record<string, any>): string; /**
1854
+ * @param {{fields: Fields}} model
1855
+ * @param {Record<string, any> | Record<string, any>[]} documents
1856
+ */
1857
+ declare function toPrimaries(model: any, documents: any): string | string[];
1858
+
1824
1859
  /**
1825
1860
  * @implements {Save}
1826
1861
  * @implements {Destroy}
@@ -1885,6 +1920,7 @@ declare class Model implements Save, Destroy, PseudoDestroy {
1885
1920
  [x: string]: any;
1886
1921
  };
1887
1922
  get $fieldChanged(): boolean;
1923
+ $primary(): string;
1888
1924
  /**
1889
1925
  * @overload
1890
1926
  * @param {Record<string, any>} key
@@ -1971,9 +2007,10 @@ declare class Model implements Save, Destroy, PseudoDestroy {
1971
2007
  /**
1972
2008
  * @param {Connection} t
1973
2009
  * @param {(data: any, newData?: any) => Promise<object?>} run
2010
+ * @param {Record<string, any>?} newData
1974
2011
  * @returns {Promise<this?>}
1975
2012
  */
1976
- [Save](t: Connection, run: (data: any, newData?: any) => Promise<object | null>): Promise<this | null>;
2013
+ [Save](t: Connection, run: (data: any, newData?: any) => Promise<object | null>, newData: Record<string, any> | null): Promise<this | null>;
1977
2014
  /**
1978
2015
  * @param {Connection} t
1979
2016
  * @param {(data: any) => Promise<object?>} run
@@ -2176,4 +2213,4 @@ declare function field<T extends MainFieldType>(type: T, options: {
2176
2213
  declare function setDevelopment(d?: boolean): void;
2177
2214
  declare let isDevelopment: boolean;
2178
2215
 
2179
- export { Build, type BuildValue, type ClassDecorator, type Column, type ColumnOptions, Connection, type Constraint, type CountArg, Create, Destroy, type Environment, type FieldDecorator, type FieldDefine, type FieldDefineOption, type FieldDefineType, type FieldType, type FieldTypeDefine, type FieldValue, type Fields, type FindRange, type GetName, type IConnection, type Index, type IndexInfo, type IndexOptions, type Join, type JoinType, type MainFieldType, type MatchArg, type MaybePromise, Model, type Options, PseudoDestroy, type Querier, Query, type QueryOptions, type Queryable, Save, Select, type SelectArg, SetValue, Submodel, type Support, type TableDefine, type TableType, type ToFieldType, type ToType, type TransactionFn, Where, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, creating, decrement, field as define, deleted, deleting, divide, field$1 as field, getPrimaryKeys, id, immutable, increment, index, isDevelopment, isPseudo, submodel as model, multiply, now, primary, prop, pseudo, setDevelopment, sort, submodel, toNot, undeleted, updating, values, withDeleted };
2216
+ export { Build, type BuildValue, type ClassDecorator, type Column, type ColumnOptions, Connection, type Constraint, type CountArg, Create, type DBField, 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 FindRange, type GetName, type IConnection, type Index, type IndexInfo, type IndexOptions, type Join, type JoinType, type MainFieldType, type MatchArg, type MaybePromise, Model, type Options, PseudoDestroy, type Querier, Query, type QueryOptions, type Queryable, Save, Select, type SelectArg, 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, 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, values, withDeleted };