imodel 0.19.2 → 0.19.3

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.19.2
2
+ * imodel v0.19.3
3
3
  * (c) 2019-2026 undefined
4
4
  * @license undefined
5
5
  */
@@ -508,10 +508,126 @@ type FieldDecorator<T> = (val: {
508
508
  }, ctx: ClassAccessorDecoratorContext) => any;
509
509
  type MethodDecorator<T extends Function> = (val: T, ctx: ClassMethodDecoratorContext<any, T>) => any;
510
510
 
511
+ declare class ConnectionError extends Error {
512
+ /**
513
+ * @param {string} code - 自定义通用错误码 (例如 constraint:unique)
514
+ * @param {string} message - 原始错误信息或自定义信息
515
+ */
516
+ constructor(code: string, message: string);
517
+ /** @type {string} */
518
+ code: string;
519
+ }
520
+ /**
521
+ * 非空约束违规
522
+ * 对应 PG 23502 / MySQL 1048
523
+ * 场景:插入数据时,必填字段为空
524
+ */
525
+ declare class NotNullViolationError extends ConnectionError {
526
+ /** @type {string} */
527
+ table: string;
528
+ /** @type {string} */
529
+ column: string;
530
+ }
531
+ /**
532
+ * 唯一性约束违规
533
+ * 对应 PG 23505 / MySQL 1062
534
+ * 支持单字段和多字段组合冲突
535
+ */
536
+ declare class UniqueViolationError extends ConnectionError {
537
+ /**
538
+ * @param {string} table - 表名
539
+ * @param {string | string[]} columns - 冲突的列名(可以是字符串或字符串数组)
540
+ * @param {any} [values] - 导致冲突的值(可选,多字段时为对象或数组)
541
+ */
542
+ constructor(table: string, columns: string | string[], values?: any);
543
+ /** @type {string} */
544
+ table: string;
545
+ /** @type {string[]} */
546
+ columns: string[];
547
+ /** @type {any} */
548
+ values: any;
549
+ }
550
+ /**
551
+ * 外键约束违规
552
+ * 对应 PG 23503 / MySQL 1452
553
+ * 场景:插入了一个不存在的 user_id
554
+ */
555
+ declare class ForeignKeyViolationError extends ConnectionError {
556
+ /**
557
+ * @param {string} table - 当前操作的表
558
+ * @param {string | string[]} columns - 当前操作的外键列
559
+ * @param {string} [referencedTable] - 被引用的父表
560
+ */
561
+ constructor(table: string, columns: string | string[], referencedTable?: string);
562
+ /** @type {string} */
563
+ table: string;
564
+ /** @type {string[]} */
565
+ columns: string[];
566
+ /** @type {string} */
567
+ referencedTable: string;
568
+ }
569
+ /**
570
+ * 检查约束违规
571
+ * 对应 PG 23514
572
+ * 场景:年龄字段要求 > 0,但插入了 -5
573
+ */
574
+ declare class CheckViolationError extends ConnectionError {
575
+ /**
576
+ * @param {string} table - 当前操作的表
577
+ * @param {string} [constraint] - 当前操作的外键列
578
+ */
579
+ constructor(table: string, constraint?: string);
580
+ /** @type {string} */
581
+ table: string;
582
+ /** @type {string} */
583
+ constraint: string;
584
+ }
585
+ /**
586
+ * 无效的类型输入语法错误
587
+ * 对应 PG 22P02
588
+ * 场景:无效的 UUID、日期格式错误等
589
+ */
590
+ declare class InvalidInputSyntaxError extends ConnectionError {
591
+ /**
592
+ * @param {string} message - 目标数据类型
593
+ */
594
+ constructor(message: string);
595
+ }
596
+ /**
597
+ * 操作符不匹配错误
598
+ * 对应 PG 42883
599
+ * 场景:uuid = integer 等类型不兼容的操作
600
+ */
601
+ declare class OperatorMismatchError extends ConnectionError {
602
+ /**
603
+ * @param {string} message
604
+ */
605
+ constructor(message: string);
606
+ }
607
+
608
+ type errors_CheckViolationError = CheckViolationError;
609
+ declare const errors_CheckViolationError: typeof CheckViolationError;
610
+ type errors_ConnectionError = ConnectionError;
611
+ declare const errors_ConnectionError: typeof ConnectionError;
612
+ type errors_ForeignKeyViolationError = ForeignKeyViolationError;
613
+ declare const errors_ForeignKeyViolationError: typeof ForeignKeyViolationError;
614
+ type errors_InvalidInputSyntaxError = InvalidInputSyntaxError;
615
+ declare const errors_InvalidInputSyntaxError: typeof InvalidInputSyntaxError;
616
+ type errors_NotNullViolationError = NotNullViolationError;
617
+ declare const errors_NotNullViolationError: typeof NotNullViolationError;
618
+ type errors_OperatorMismatchError = OperatorMismatchError;
619
+ declare const errors_OperatorMismatchError: typeof OperatorMismatchError;
620
+ type errors_UniqueViolationError = UniqueViolationError;
621
+ declare const errors_UniqueViolationError: typeof UniqueViolationError;
622
+ declare namespace errors {
623
+ export { errors_CheckViolationError as CheckViolationError, errors_ConnectionError as ConnectionError, errors_ForeignKeyViolationError as ForeignKeyViolationError, errors_InvalidInputSyntaxError as InvalidInputSyntaxError, errors_NotNullViolationError as NotNullViolationError, errors_OperatorMismatchError as OperatorMismatchError, errors_UniqueViolationError as UniqueViolationError };
624
+ }
625
+
511
626
  interface Environment<T extends object> {
512
627
  transaction: T | null;
513
628
  isDevelopment: boolean;
514
629
  values: typeof values;
630
+ errors: typeof errors;
515
631
  }
516
632
 
517
633
  /** 特性支持情况 */
@@ -2698,4 +2814,4 @@ declare function toBool(v: any): boolean;
2698
2814
  declare function setDevelopment(d?: boolean): void;
2699
2815
  declare let isDevelopment: boolean;
2700
2816
 
2701
- 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 LockType, 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 TransactionHandler, type TransactionHandlerArray, type VirtualTable, Where, type WhereExists, 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 };
2817
+ export { Build, CheckViolationError, type ClassDecorator, type Column, type ColumnOptions, Connection, ConnectionError, 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, ForeignKeyViolationError, type GetName, type Hook, type Hooks, type IConnection, type Index, type IndexInfo, type IndexOptions, InvalidInputSyntaxError, type JOIN, type Join, type JoinType, type LockType, type MainFieldType, type MaybePromise, type MethodDecorator, Model, NotNullViolationError, OperatorMismatchError, 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 TransactionHandler, type TransactionHandlerArray, UniqueViolationError, type VirtualTable, Where, type WhereExists, 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.19.2
2
+ * imodel v0.19.3
3
3
  * (c) 2019-2026 undefined
4
4
  * @license undefined
5
5
  */
@@ -804,6 +804,162 @@ var values = /*#__PURE__*/Object.freeze({
804
804
  uuid: uuid
805
805
  });
806
806
 
807
+ class ConnectionError extends Error {
808
+ /** @type {string} */
809
+
810
+ /**
811
+ * @param {string} code - 自定义通用错误码 (例如 constraint:unique)
812
+ * @param {string} message - 原始错误信息或自定义信息
813
+ */
814
+ constructor(code, message) {
815
+ super(message);
816
+ this.code = code;
817
+ }
818
+ }
819
+ /**
820
+ * 非空约束违规
821
+ * 对应 PG 23502 / MySQL 1048
822
+ * 场景:插入数据时,必填字段为空
823
+ */
824
+ class NotNullViolationError extends ConnectionError {
825
+ /** @type {string} */
826
+
827
+ /** @type {string} */
828
+
829
+ /**
830
+ * @param {string} table - 表名
831
+ * @param {string} column - 列名
832
+ */
833
+ constructor(table, column) {
834
+ const msg = `Column '${column}' in table '${table}' cannot be null`;
835
+ super('constraint:notNull', msg);
836
+ this.table = table;
837
+ this.column = column;
838
+ }
839
+ }
840
+ /**
841
+ * 唯一性约束违规
842
+ * 对应 PG 23505 / MySQL 1062
843
+ * 支持单字段和多字段组合冲突
844
+ */
845
+ class UniqueViolationError extends ConnectionError {
846
+ /** @type {string} */
847
+
848
+ /** @type {string[]} */
849
+
850
+ /** @type {any} */
851
+
852
+ /**
853
+ * @param {string} table - 表名
854
+ * @param {string | string[]} columns - 冲突的列名(可以是字符串或字符串数组)
855
+ * @param {any} [values] - 导致冲突的值(可选,多字段时为对象或数组)
856
+ */
857
+ constructor(table, columns, values = undefined) {
858
+ // 确保 columns 始终是数组,方便后续处理
859
+ const cols = Array.isArray(columns) ? columns : [columns];
860
+ let msg = `Duplicate value for ${cols.join(', ')} in table '${table}'`;
861
+ if (values !== undefined) {
862
+ msg += ` (values: ${JSON.stringify(values)})`;
863
+ }
864
+ super('constraint:unique', msg);
865
+ this.table = table;
866
+ this.columns = cols;
867
+ this.values = values;
868
+ }
869
+ }
870
+ /**
871
+ * 外键约束违规
872
+ * 对应 PG 23503 / MySQL 1452
873
+ * 场景:插入了一个不存在的 user_id
874
+ */
875
+ class ForeignKeyViolationError extends ConnectionError {
876
+ /** @type {string} */
877
+
878
+ /** @type {string[]} */
879
+
880
+ /** @type {string} */
881
+
882
+ /**
883
+ * @param {string} table - 当前操作的表
884
+ * @param {string | string[]} columns - 当前操作的外键列
885
+ * @param {string} [referencedTable] - 被引用的父表
886
+ */
887
+ constructor(table, columns, referencedTable) {
888
+ // 确保 columns 始终是数组,方便后续处理
889
+ const cols = Array.isArray(columns) ? columns : [columns];
890
+ let msg = `Foreign key constraint failed for column ${cols.join(', ')}`;
891
+ msg += ` in table '${table}'`;
892
+ if (referencedTable) {
893
+ msg += ` (references '${referencedTable}')`;
894
+ }
895
+ super('constraint:foreignKey', msg);
896
+ this.table = table;
897
+ this.columns = cols;
898
+ this.referencedTable = referencedTable || '';
899
+ }
900
+ }
901
+ /**
902
+ * 检查约束违规
903
+ * 对应 PG 23514
904
+ * 场景:年龄字段要求 > 0,但插入了 -5
905
+ */
906
+ class CheckViolationError extends ConnectionError {
907
+ /** @type {string} */
908
+
909
+ /** @type {string} */
910
+
911
+ /**
912
+ * @param {string} table - 当前操作的表
913
+ * @param {string} [constraint] - 当前操作的外键列
914
+ */
915
+ constructor(table, constraint) {
916
+ let msg = `Check constraint failed in table '${table}'`;
917
+ if (constraint) {
918
+ msg += ` (${constraint})`;
919
+ }
920
+ super('constraint:check', msg);
921
+ this.table = table;
922
+ this.constraint = constraint || '';
923
+ }
924
+ }
925
+ /**
926
+ * 无效的类型输入语法错误
927
+ * 对应 PG 22P02
928
+ * 场景:无效的 UUID、日期格式错误等
929
+ */
930
+ class InvalidInputSyntaxError extends ConnectionError {
931
+ /**
932
+ * @param {string} message - 目标数据类型
933
+ */
934
+ constructor(message) {
935
+ super('data:type:invalidInputSyntax', message);
936
+ }
937
+ }
938
+ /**
939
+ * 操作符不匹配错误
940
+ * 对应 PG 42883
941
+ * 场景:uuid = integer 等类型不兼容的操作
942
+ */
943
+ class OperatorMismatchError extends ConnectionError {
944
+ /**
945
+ * @param {string} message
946
+ */
947
+ constructor(message) {
948
+ super('syntax:operator:mismatch', message);
949
+ }
950
+ }
951
+
952
+ var errors = /*#__PURE__*/Object.freeze({
953
+ __proto__: null,
954
+ CheckViolationError: CheckViolationError,
955
+ ConnectionError: ConnectionError,
956
+ ForeignKeyViolationError: ForeignKeyViolationError,
957
+ InvalidInputSyntaxError: InvalidInputSyntaxError,
958
+ NotNullViolationError: NotNullViolationError,
959
+ OperatorMismatchError: OperatorMismatchError,
960
+ UniqueViolationError: UniqueViolationError
961
+ });
962
+
807
963
  /** @import { Options, Queryable } from '../types' */
808
964
  /**
809
965
  *
@@ -2638,6 +2794,13 @@ function toSelect(queryable, lockable) {
2638
2794
 
2639
2795
  /* eslint-disable max-lines */
2640
2796
  /* eslint-disable max-len */
2797
+ /** @import { Wheres } from '../Where.mjs' */
2798
+ /** @import { SetValue } from '../set.mjs' */
2799
+ /** @import { Environment, TableConnection, TableValue } from '../types' */
2800
+ /** @import { Fields, FieldType, FieldValue, IndexOptions, MainFieldType, TableDefine } from '../types/table' */
2801
+ /** @import { Queryable } from '../types/options' */
2802
+ /** @import { Column, ColumnOptions } from '../types/column' */
2803
+ /** @import { DBTable, IConnection, Skip, TableConnect, TableType, TransactionHandler } from '../types/connection' */
2641
2804
  /**
2642
2805
  *
2643
2806
  * @param {Fields<keyof FieldType>} fields
@@ -2696,7 +2859,8 @@ class Connection {
2696
2859
  this.#env = {
2697
2860
  isDevelopment,
2698
2861
  values,
2699
- transaction
2862
+ transaction,
2863
+ errors
2700
2864
  };
2701
2865
  }
2702
2866
  /**
@@ -3323,11 +3487,11 @@ class Connection {
3323
3487
  }, data, keys, conflictKeys, conflictSet) {
3324
3488
  const [columns, fieldColumns, baseFields] = toColumns(fields);
3325
3489
  const isArray = Array.isArray(data);
3326
- const insertValues = (isArray ? data : [data]
3490
+ const insertValues = await Promise.all((isArray ? data : [data]
3327
3491
  // @ts-ignore
3328
3492
  ).map(d => getInsertValue(d, columns, fieldColumns, {
3329
3493
  uncreatable: true
3330
- }));
3494
+ })));
3331
3495
  if (!insertValues.length) {
3332
3496
  throw new Error();
3333
3497
  }
@@ -6894,4 +7058,4 @@ function field(type, {
6894
7058
  };
6895
7059
  }
6896
7060
 
6897
- 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 };
7061
+ export { Build, CheckViolationError, Connection, ConnectionError, Create, Destroy, ForeignKeyViolationError, InvalidInputSyntaxError, Model, NotNullViolationError, OperatorMismatchError, PseudoDestroy, Query, Save, Scene, Submodel, UniqueViolationError, 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.19.2
2
+ * imodel v0.19.3
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.19.2
2
+ * imodel v0.19.3
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.19.2",
3
+ "version": "0.19.3",
4
4
  "main": "index.mjs",
5
5
  "type": "module",
6
6
  "repository": {