imodel 0.2.0 → 0.3.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.2.0
2
+ * imodel v0.3.1
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
 
@@ -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
  /** 时间类型 */
@@ -630,16 +620,12 @@ type FieldSpecific = FieldSpecificValue | {
630
620
  computed?(): PromiseLike<FieldSpecificValue> | FieldSpecificValue;
631
621
  };
632
622
  interface FieldDefineOption {
633
- /** 精确度 */
634
- precision?: number;
635
623
  /** 缩放比 */
636
624
  scale?: number;
637
625
  /** 占用空间大小 */
638
626
  size?: number;
639
627
  /** 是否为主键 */
640
- primary?: number | boolean;
641
- /** 序列字段 */
642
- serial?: boolean;
628
+ primary?: number;
643
629
  /** 唯一索引? */
644
630
  unique?: boolean;
645
631
  /** 索引信息 */
@@ -693,11 +679,11 @@ interface IndexInfo extends IndexOptions {
693
679
  interface Index extends IndexInfo {
694
680
  name: string;
695
681
  }
696
- interface TableDefine<T extends Fields = Fields> {
682
+ interface TableDefine<T extends Fields = Fields, TT extends string | VirtualTable | void = string | VirtualTable | void> {
697
683
  /** 字段信息 */
698
684
  readonly fields: T;
699
685
  /** 表名 */
700
- readonly table?: string | VirtualTable;
686
+ readonly table: TT;
701
687
  /** 是否启用软删除 */
702
688
  readonly pseudo?: string;
703
689
  /** 索引信息 */
@@ -812,6 +798,27 @@ interface MatchArg extends Omit<TableDefine<Fields<keyof FieldType>>, 'table'> {
812
798
  where?: WhereValue[];
813
799
  }
814
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
+ }
815
822
  interface IConnection<E extends object = object> {
816
823
  /** 数据库类型 */
817
824
  readonly dbType: string;
@@ -847,6 +854,8 @@ interface IConnection<E extends object = object> {
847
854
  dropView(environment: Environment<E>, name: string): PromiseLike<number>;
848
855
  createMaterializedView(environment: Environment<E>, name: string, q: SelectArg): PromiseLike<number>;
849
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>;
850
859
  transaction<R>(environment: Environment<E>, fn: (t: E) => R | PromiseLike<R>): PromiseLike<R>;
851
860
  }
852
861
  interface Skip {
@@ -869,18 +878,13 @@ declare class Connection<E extends {} = {}> {
869
878
  *
870
879
  * @param {PromiseLike<IConnection<E>> | IConnection<E>} connection
871
880
  * @param {E?} [transaction]
872
- * @param {Record<string, string>?} [names]
873
881
  */
874
- 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);
875
883
  /**
876
884
  *
877
- * @param {object} [options]
878
- * @param {Record<string, string>?} [options.names]
879
885
  * @returns {Connection<E>}
880
886
  */
881
- clone({ names }?: {
882
- names?: Record<string, string> | null | undefined;
883
- }): Connection<E>;
887
+ clone(): Connection<E>;
884
888
  /** @return {Promise<string>} */
885
889
  dbType(): Promise<string>;
886
890
  /** @return {Promise<string>} */
@@ -1177,6 +1181,26 @@ declare class Connection<E extends {} = {}> {
1177
1181
  */
1178
1182
  dropMaterializedView(view: string): Promise<number>;
1179
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 {TableDefine | DBTable} table
1196
+ * @returns {Promise<void>}
1197
+ */
1198
+ syncTable(table: TableDefine | DBTable): Promise<void>;
1199
+ /**
1200
+ * @param {(TableDefine | DBTable)[]} tables
1201
+ * @returns {Promise<void>}
1202
+ */
1203
+ syncTables(tables: (TableDefine | DBTable)[]): Promise<void>;
1180
1204
  /**
1181
1205
  * @template T
1182
1206
  * @overload
@@ -1446,17 +1470,6 @@ declare function field$1<T extends keyof FieldType>(type: T, options: {
1446
1470
  [k: string]: any;
1447
1471
  } & FieldDefineOption): FieldDecorator<ToType<T, boolean, boolean>>;
1448
1472
 
1449
- /** @import { ToFieldType } from '../types' */
1450
- /** @import { FieldDecorator } from './index.mjs' */
1451
- /**
1452
- * @param {object} [options]
1453
- * @param {number?} [options.sort]
1454
- * @returns {FieldDecorator<ToFieldType<'id'>>}
1455
- */
1456
- declare function id({ sort }?: {
1457
- sort?: number | null | undefined;
1458
- }): FieldDecorator<ToFieldType<"id">>;
1459
-
1460
1473
  /** @import { FieldDecorator } from './index.mjs' */
1461
1474
  /**
1462
1475
  * @param {string} prop
@@ -1614,20 +1627,20 @@ declare function submodel<T extends Record<string, any>>(type: TableDefine, cons
1614
1627
  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[]>;
1615
1628
 
1616
1629
  /**
1617
- * @param {number} [precision]
1630
+ * @param {number} [size]
1618
1631
  * @returns {FieldDecorator<Date?>}
1619
1632
  */
1620
- declare function creating(precision?: number): FieldDecorator<Date | null>;
1633
+ declare function creating(size?: number): FieldDecorator<Date | null>;
1621
1634
  /**
1622
- * @param {number} [precision]
1635
+ * @param {number} [size]
1623
1636
  * @returns {FieldDecorator<Date?>}
1624
1637
  */
1625
- declare function updating(precision?: number): FieldDecorator<Date | null>;
1638
+ declare function updating(size?: number): FieldDecorator<Date | null>;
1626
1639
  /**
1627
- * @param {number} [precision]
1640
+ * @param {number} [size]
1628
1641
  * @returns {FieldDecorator<Date?>}
1629
1642
  */
1630
- declare function deleting(precision?: number): FieldDecorator<Date | null>;
1643
+ declare function deleting(size?: number): FieldDecorator<Date | null>;
1631
1644
 
1632
1645
  type ClassDecorator = (val: Function, ctx: ClassDecoratorContext) => any;
1633
1646
  type FieldDecorator<T> = (val: {
@@ -1636,7 +1649,7 @@ type FieldDecorator<T> = (val: {
1636
1649
  }, ctx: ClassAccessorDecoratorContext) => any;
1637
1650
 
1638
1651
  type QueryOptions<T extends Fields, TB extends unknown = any> = {
1639
- table: string;
1652
+ table?: string | VirtualTable<object> | undefined;
1640
1653
  fields: T;
1641
1654
  pseudo?: string | undefined;
1642
1655
  build?: ((a: object, b?: object | boolean) => TB) | null | undefined;
@@ -1645,7 +1658,7 @@ type QueryOptions<T extends Fields, TB extends unknown = any> = {
1645
1658
  * @template {Fields} T
1646
1659
  * @template {object} [TB=object]
1647
1660
  * @typedef {object} QueryOptions
1648
- * @property {string} table
1661
+ * @property {string | VirtualTable} [table]
1649
1662
  * @property {T} fields
1650
1663
  * @property {string} [pseudo]
1651
1664
  * @property {((a: object, b?: object | boolean) => TB)?} [build]
@@ -1670,8 +1683,8 @@ declare class Query<T extends Fields, TB extends unknown = any> implements Query
1670
1683
  * @param {QueryOptions<T, TB> | Query<T, TB>} options
1671
1684
  */
1672
1685
  constructor(options: QueryOptions<T, TB> | Query<T, TB>);
1673
- /** @readonly @type {string} */
1674
- readonly table: string;
1686
+ /** @readonly @type {string | VirtualTable | undefined} */
1687
+ readonly table: string | VirtualTable | undefined;
1675
1688
  /** @readonly @type {T} */
1676
1689
  readonly fields: T;
1677
1690
  /** @readonly @type {string} */
@@ -1831,6 +1844,23 @@ declare class Query<T extends Fields, TB extends unknown = any> implements Query
1831
1844
  #private;
1832
1845
  }
1833
1846
 
1847
+ /** @import { Fields, IndexInfo, FieldDefine } from './types' */
1848
+ /**
1849
+ *
1850
+ * @param {Fields} fields
1851
+ * @returns
1852
+ */
1853
+ declare function getPrimaryFields(fields: Fields): string[];
1854
+ /**
1855
+ * @param {string[]} fields
1856
+ * @param {Record<string, any>} data
1857
+ */
1858
+ declare function toPrimary(fields: string[], data: Record<string, any>): string; /**
1859
+ * @param {{fields: Fields}} model
1860
+ * @param {Record<string, any> | Record<string, any>[]} documents
1861
+ */
1862
+ declare function toPrimaries(model: any, documents: any): string | string[];
1863
+
1834
1864
  /**
1835
1865
  * @implements {Save}
1836
1866
  * @implements {Destroy}
@@ -1895,6 +1925,7 @@ declare class Model implements Save, Destroy, PseudoDestroy {
1895
1925
  [x: string]: any;
1896
1926
  };
1897
1927
  get $fieldChanged(): boolean;
1928
+ $primary(): string;
1898
1929
  /**
1899
1930
  * @overload
1900
1931
  * @param {Record<string, any>} key
@@ -2187,4 +2218,4 @@ declare function field<T extends MainFieldType>(type: T, options: {
2187
2218
  declare function setDevelopment(d?: boolean): void;
2188
2219
  declare let isDevelopment: boolean;
2189
2220
 
2190
- 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 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, 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 };
2221
+ 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 };