orange-orm 5.2.2 → 5.2.3-beta2

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/README.md CHANGED
@@ -2058,6 +2058,7 @@ export default map;
2058
2058
 
2059
2059
  <details><summary><strong>Validation</strong></summary>
2060
2060
  <p>In the previous sections you have already seen the <strong><i>notNull()</i></strong> validator being used on some columns. This will not only generate correct typescript mapping, but also throw an error if value is set to null or undefined. However, sometimes we do not want the notNull-validator to be run on inserts. Typically, when we have an autoincremental key or server generated uuid, it does not make sense to check for null on insert. This is where <strong><i>notNullExceptInsert()</strong></i> comes to rescue. You can also create your own custom validator as shown below. The last kind of validator, is the <a href="https://ajv.js.org/json-schema.html">ajv JSON schema validator</a>. This can be used on json columns as well as any other column type.</p>
2061
+ <p>Custom validators receive <code>value</code> and a metadata object with <code>table</code>, <code>column</code>, <code>property</code>, and <code>isInsert</code>.</p>
2061
2062
 
2062
2063
  <sub>📄 map.ts</sub>
2063
2064
  ```ts
@@ -2075,9 +2076,9 @@ let petSchema = {
2075
2076
  }
2076
2077
  };
2077
2078
 
2078
- function validateName(value?: string) {
2079
+ function validateName(value?: string, meta: { table: string; column: string; property: string; isInsert: boolean }) {
2079
2080
  if (value && value.length > 10)
2080
- throw new Error('Length cannot exceed 10 characters');
2081
+ throw new Error(`Length cannot exceed 10 characters in ${meta.table}.${meta.column}`);
2081
2082
  }
2082
2083
 
2083
2084
  const map = orange.map(x => ({
@@ -2110,9 +2111,9 @@ let petSchema = {
2110
2111
  }
2111
2112
  };
2112
2113
 
2113
- function validateName(value) {
2114
+ function validateName(value, meta) {
2114
2115
  if (value && value.length > 10)
2115
- throw new Error('Length cannot exceed 10 characters');
2116
+ throw new Error(`Length cannot exceed 10 characters in ${meta.table}.${meta.column}`);
2116
2117
  }
2117
2118
 
2118
2119
  const map = orange.map(x => ({
package/SKILL.md CHANGED
@@ -6,6 +6,10 @@
6
6
  > Supports: PostgreSQL, SQLite, MySQL, MS SQL, Oracle, SAP ASE, PGlite, Cloudflare D1.
7
7
  > Works in the browser via Express/Hono adapters.
8
8
 
9
+ ## Repository maintenance note
10
+
11
+ - Treat `dist/index.mjs` and `dist/index.browser.mjs` as generated build output from the build command, not as source files to review or edit directly unless the task explicitly targets build artifacts.
12
+
9
13
  ---
10
14
 
11
15
  ## Table of Contents
@@ -6391,8 +6391,8 @@ function requireColumn () {
6391
6391
 
6392
6392
  c.notNullExceptInsert = function() {
6393
6393
  column._notNullExceptInsert = true;
6394
- function validate(value, _row, isInsert) {
6395
- if (isInsert)
6394
+ function validate(value, meta) {
6395
+ if (meta.isInsert)
6396
6396
  return;
6397
6397
  if (value === undefined || value === null)
6398
6398
  throw new Error(`Column ${column.alias} cannot be null or undefined`);
@@ -6406,12 +6406,12 @@ function requireColumn () {
6406
6406
  if (previousValue)
6407
6407
  column.validate = nestedValidate;
6408
6408
  else
6409
- column.validate = value;
6409
+ column.validate = invokeValidate;
6410
6410
 
6411
6411
  function nestedValidate() {
6412
6412
  try {
6413
6413
  previousValue.apply(null, arguments);
6414
- value.apply(null, arguments);
6414
+ invokeValidate.apply(null, arguments);
6415
6415
  }
6416
6416
  catch (e) {
6417
6417
  const error = new Error(e.message || e);
@@ -6420,6 +6420,10 @@ function requireColumn () {
6420
6420
  throw error;
6421
6421
  }
6422
6422
  }
6423
+
6424
+ function invokeValidate(inputValue, meta) {
6425
+ value(inputValue, meta);
6426
+ }
6423
6427
  return c;
6424
6428
  };
6425
6429
 
@@ -8609,7 +8613,7 @@ function requireNewDecodeDbRow () {
8609
8613
  value = purify(value);
8610
8614
  this._dbRow[key] = value;
8611
8615
  if (column.validate)
8612
- column.validate(value, this._dbRow);
8616
+ column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias, isInsert: false });
8613
8617
  updateField(this._context, table, column, this);
8614
8618
  let emit = this._emitColumnChanged[name];
8615
8619
  if (emit)
@@ -8765,7 +8769,7 @@ function requireNewDecodeDbRow () {
8765
8769
  if (row[key] !== undefined && !isInsert)
8766
8770
  row[key] = columns[i].decode(context, row[key]);
8767
8771
  if (shouldValidate && columns[i].validate)
8768
- columns[i].validate(row[key], row, isInsert);
8772
+ columns[i].validate(row[key], { table: table._dbName, column: columns[i]._dbName, property: columns[i].alias, isInsert });
8769
8773
  }
8770
8774
  let target = new Row(context, row);
8771
8775
  const p = new Proxy(target, {
package/dist/index.mjs CHANGED
@@ -6392,8 +6392,8 @@ function requireColumn () {
6392
6392
 
6393
6393
  c.notNullExceptInsert = function() {
6394
6394
  column._notNullExceptInsert = true;
6395
- function validate(value, _row, isInsert) {
6396
- if (isInsert)
6395
+ function validate(value, meta) {
6396
+ if (meta.isInsert)
6397
6397
  return;
6398
6398
  if (value === undefined || value === null)
6399
6399
  throw new Error(`Column ${column.alias} cannot be null or undefined`);
@@ -6407,12 +6407,12 @@ function requireColumn () {
6407
6407
  if (previousValue)
6408
6408
  column.validate = nestedValidate;
6409
6409
  else
6410
- column.validate = value;
6410
+ column.validate = invokeValidate;
6411
6411
 
6412
6412
  function nestedValidate() {
6413
6413
  try {
6414
6414
  previousValue.apply(null, arguments);
6415
- value.apply(null, arguments);
6415
+ invokeValidate.apply(null, arguments);
6416
6416
  }
6417
6417
  catch (e) {
6418
6418
  const error = new Error(e.message || e);
@@ -6421,6 +6421,10 @@ function requireColumn () {
6421
6421
  throw error;
6422
6422
  }
6423
6423
  }
6424
+
6425
+ function invokeValidate(inputValue, meta) {
6426
+ value(inputValue, meta);
6427
+ }
6424
6428
  return c;
6425
6429
  };
6426
6430
 
@@ -8610,7 +8614,7 @@ function requireNewDecodeDbRow () {
8610
8614
  value = purify(value);
8611
8615
  this._dbRow[key] = value;
8612
8616
  if (column.validate)
8613
- column.validate(value, this._dbRow);
8617
+ column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias, isInsert: false });
8614
8618
  updateField(this._context, table, column, this);
8615
8619
  let emit = this._emitColumnChanged[name];
8616
8620
  if (emit)
@@ -8766,7 +8770,7 @@ function requireNewDecodeDbRow () {
8766
8770
  if (row[key] !== undefined && !isInsert)
8767
8771
  row[key] = columns[i].decode(context, row[key]);
8768
8772
  if (shouldValidate && columns[i].validate)
8769
- columns[i].validate(row[key], row, isInsert);
8773
+ columns[i].validate(row[key], { table: table._dbName, column: columns[i]._dbName, property: columns[i].alias, isInsert });
8770
8774
  }
8771
8775
  let target = new Row(context, row);
8772
8776
  const p = new Proxy(target, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orange-orm",
3
- "version": "5.2.2",
3
+ "version": "5.2.3-beta2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/index.d.ts CHANGED
@@ -10,6 +10,12 @@ import type { Filter as MapFilter, RawFilter as MapRawFilter, Pool as MapPool, P
10
10
  declare function r(config: r.Config): unknown;
11
11
 
12
12
  declare namespace r {
13
+ export interface ValidationMeta {
14
+ table: string;
15
+ column: string;
16
+ property: string;
17
+ isInsert: boolean;
18
+ }
13
19
 
14
20
  function table(name: string): Table;
15
21
  function close(): Promise<void>;
@@ -94,7 +100,7 @@ declare namespace r {
94
100
  date(): DateColumnDef;
95
101
  }
96
102
  export interface DateColumnDef {
97
- validate(validator: (value?: Date | string, row?: object) => void): DateColumnDef;
103
+ validate(validator: (value?: Date | string, meta: ValidationMeta) => void): DateColumnDef;
98
104
  notNull(): DateColumnNotNullDef;
99
105
  JSONSchema(schema: object, options?: Options): DateColumnDef;
100
106
  serializable(value: boolean): DateColumnDef;
@@ -104,7 +110,7 @@ declare namespace r {
104
110
  }
105
111
 
106
112
  export interface DateColumnNotNullDef {
107
- validate(validator: (value: Date | string, row?: object) => void): DateColumnNotNullDef;
113
+ validate(validator: (value: Date | string, meta: ValidationMeta) => void): DateColumnNotNullDef;
108
114
  JSONSchema(schema: object, options?: Options): DateColumnNotNullDef;
109
115
  serializable(value: boolean): DateColumnNotNullDef;
110
116
  as(dbName: string): DateColumnNotNullDef;
@@ -113,7 +119,7 @@ declare namespace r {
113
119
  }
114
120
 
115
121
  export interface BinaryColumnDef {
116
- validate(validator: (value?: Buffer | string, row?: object) => void): BinaryColumnDef;
122
+ validate(validator: (value?: Buffer | string, meta: ValidationMeta) => void): BinaryColumnDef;
117
123
  notNull(): BinaryColumnNotNullDef;
118
124
  JSONSchema(schema: object, options?: Options): BinaryColumnDef;
119
125
  serializable(value: boolean): BinaryColumnDef;
@@ -123,7 +129,7 @@ declare namespace r {
123
129
  }
124
130
 
125
131
  export interface BinaryColumnNotNullDef {
126
- validate(validator: (value: Buffer | string, row?: object) => void): BinaryColumnNotNullDef;
132
+ validate(validator: (value: Buffer | string, meta: ValidationMeta) => void): BinaryColumnNotNullDef;
127
133
  JSONSchema(schema: object, options?: Options): BinaryColumnNotNullDef;
128
134
  serializable(value: boolean): BinaryColumnNotNullDef;
129
135
  as(dbName: string): BinaryColumnNotNullDef;
@@ -132,7 +138,7 @@ declare namespace r {
132
138
  }
133
139
 
134
140
  export interface ColumnOf<T> {
135
- validate(validator: (value?: T, row?: object) => void): ColumnOf<T>;
141
+ validate(validator: (value?: T, meta: ValidationMeta) => void): ColumnOf<T>;
136
142
  notNull(): ColumnNotNullOf<T>;
137
143
  JSONSchema(schema: object, options?: Options): ColumnOf<T>;
138
144
  serializable(value: boolean): ColumnOf<T>;
@@ -142,7 +148,7 @@ declare namespace r {
142
148
  }
143
149
 
144
150
  export interface ColumnNotNullOf<T> {
145
- validate(validator: (value: T, row?: object) => void): ColumnNotNullOf<T>;
151
+ validate(validator: (value: T, meta: ValidationMeta) => void): ColumnNotNullOf<T>;
146
152
  notNull(): ColumnNotNullOf<T>;
147
153
  JSONSchema(schema: object, options?: Options): ColumnNotNullOf<T>;
148
154
  serializable(value: boolean): ColumnNotNullOf<T>;
package/src/map.d.ts CHANGED
@@ -636,6 +636,18 @@ type JsonOf<T> = {
636
636
  type: T;
637
637
  };
638
638
 
639
+ interface ValidationMeta {
640
+ table: string;
641
+ column: string;
642
+ property: string;
643
+ isInsert: boolean;
644
+ }
645
+
646
+ type OptionalOnNullable<M, T> = M extends NotNull ? T : T | null | undefined;
647
+ type ValidateMethod<TValue, TReturn> = {
648
+ validate(validator: (value: TValue, meta: ValidationMeta) => void): TReturn;
649
+ };
650
+
639
651
  interface ColumnType<M> {
640
652
  string(): StringColumnTypeDef<M & StringColumnSymbol>;
641
653
  uuid(): UuidColumnTypeDef<M & UuidColumnSymbol>;
@@ -650,94 +662,15 @@ interface ColumnType<M> {
650
662
  jsonOf<T>(helper: T): JSONColumnTypeDef<M & JSONColumnSymbol & JsonOf<T>>;
651
663
  }
652
664
 
653
- type UuidValidator<M> = M extends NotNull
654
- ? {
655
- validate(validator: (value: string) => void): UuidColumnTypeDef<M>;
656
- }
657
- : {
658
- validate(
659
- validator: (value?: string | null) => void
660
- ): UuidColumnTypeDef<M>;
661
- };
662
- type StringValidator<M> = M extends NotNull
663
- ? {
664
- validate(validator: (value: string) => void): StringColumnTypeDef<M>;
665
- }
666
- : {
667
- validate(
668
- validator: (value?: string | null) => void
669
- ): StringColumnTypeDef<M>;
670
- };
671
- type NumericValidator<M> = M extends NotNull
672
- ? {
673
- validate(validator: (value: number) => void): NumericColumnTypeDef<M>;
674
- }
675
- : {
676
- validate(
677
- validator: (value?: number | null) => void
678
- ): NumericColumnTypeDef<M>;
679
- };
680
- type BigIntValidator<M> = M extends NotNull
681
- ? {
682
- validate(validator: (value: bigint) => void): BigIntColumnTypeDef<M>;
683
- }
684
- : {
685
- validate(
686
- validator: (value?: bigint | null) => void
687
- ): BigIntColumnTypeDef<M>;
688
- };
689
- type BinaryValidator<M> = M extends NotNull
690
- ? {
691
- validate(validator: (value: string) => void): BinaryColumnTypeDef<M>;
692
- }
693
- : {
694
- validate(
695
- validator: (value?: string | null) => void
696
- ): BinaryColumnTypeDef<M>;
697
- };
698
- type BooleanValidator<M> = M extends NotNull
699
- ? {
700
- validate(validator: (value: boolean) => void): BooleanColumnTypeDef<M>;
701
- }
702
- : {
703
- validate(
704
- validator: (value?: boolean | null) => void
705
- ): BooleanColumnTypeDef<M>;
706
- };
707
- type JSONValidator<M> = M extends NotNull
708
- ? {
709
- validate(
710
- validator: (value: ToJsonType<M>) => void
711
- ): JSONColumnTypeDef<M>;
712
- }
713
- : {
714
- validate(
715
- validator: (value?: ToJsonType<M> | null) => void
716
- ): JSONColumnTypeDef<M>;
717
- };
718
- type DateValidator<M> = M extends NotNull
719
- ? {
720
- validate(
721
- validator: (value: string | Date) => void
722
- ): DateColumnTypeDef<M>;
723
- }
724
- : {
725
- validate(
726
- validator: (value?: string | Date | null) => void
727
- ): DateColumnTypeDef<M>;
728
- };
729
-
730
- type DateWithTimeZoneValidator<M> = M extends NotNull
731
- ? {
732
- validate(
733
- validator: (value: string | Date) => void
734
- ): DateWithTimeZoneColumnTypeDef<M>;
735
- }
736
- : {
737
- validate(
738
- validator: (value?: string | Date | null) => void
739
- ): DateWithTimeZoneColumnTypeDef<M>;
740
- };
665
+ type UuidValidator<M> = ValidateMethod<OptionalOnNullable<M, string>, UuidColumnTypeDef<M>>;
666
+ type StringValidator<M> = ValidateMethod<OptionalOnNullable<M, string>, StringColumnTypeDef<M>>;
667
+ type NumericValidator<M> = ValidateMethod<OptionalOnNullable<M, number>, NumericColumnTypeDef<M>>;
668
+ type BigIntValidator<M> = ValidateMethod<OptionalOnNullable<M, bigint>, BigIntColumnTypeDef<M>>;
669
+ type BinaryValidator<M> = ValidateMethod<OptionalOnNullable<M, string>, BinaryColumnTypeDef<M>>;
670
+ type BooleanValidator<M> = ValidateMethod<OptionalOnNullable<M, boolean>, BooleanColumnTypeDef<M>>;
671
+ type JSONValidator<M> = ValidateMethod<OptionalOnNullable<M, ToJsonType<M>>, JSONColumnTypeDef<M>>;
672
+ type DateValidator<M> = ValidateMethod<OptionalOnNullable<M, string | Date>, DateColumnTypeDef<M>>;
673
+ type DateWithTimeZoneValidator<M> = ValidateMethod<OptionalOnNullable<M, string | Date>, DateWithTimeZoneColumnTypeDef<M>>;
741
674
 
742
675
  type StringColumnTypeDef<M> = StringValidator<M> & {
743
676
  enum<const V extends readonly string[]>(values: V): StringColumnTypeDef<M & EnumOf<V[number]>> & EnumOf<V[number]>;
@@ -856,7 +789,7 @@ type DateColumnTypeDef<M> = DateValidator<M> & {
856
789
  } & ColumnTypeOf<DateColumnType<M>> &
857
790
  M;
858
791
 
859
- type DateWithTimeZoneColumnTypeDef<M> = DateValidator<M> & {
792
+ type DateWithTimeZoneColumnTypeDef<M> = DateWithTimeZoneValidator<M> & {
860
793
  enum<const V extends Record<string, string | Date>>(values: V): DateWithTimeZoneColumnTypeDef<M & EnumOf<V[keyof V]>> & EnumOf<V[keyof V]>;
861
794
  enum<TEnum>(values: Record<string, TEnum>): DateWithTimeZoneColumnTypeDef<M & EnumOf<TEnum>> & EnumOf<TEnum>;
862
795
  enum<E extends string | Date>(values: readonly E[]): DateWithTimeZoneColumnTypeDef<M & EnumOf<E>> & EnumOf<E>;
@@ -134,8 +134,8 @@ function defineColumn(column, table) {
134
134
 
135
135
  c.notNullExceptInsert = function() {
136
136
  column._notNullExceptInsert = true;
137
- function validate(value, _row, isInsert) {
138
- if (isInsert)
137
+ function validate(value, meta) {
138
+ if (meta.isInsert)
139
139
  return;
140
140
  if (value === undefined || value === null)
141
141
  throw new Error(`Column ${column.alias} cannot be null or undefined`);
@@ -149,12 +149,12 @@ function defineColumn(column, table) {
149
149
  if (previousValue)
150
150
  column.validate = nestedValidate;
151
151
  else
152
- column.validate = value;
152
+ column.validate = invokeValidate;
153
153
 
154
154
  function nestedValidate() {
155
155
  try {
156
156
  previousValue.apply(null, arguments);
157
- value.apply(null, arguments);
157
+ invokeValidate.apply(null, arguments);
158
158
  }
159
159
  catch (e) {
160
160
  const error = new Error(e.message || e);
@@ -163,6 +163,10 @@ function defineColumn(column, table) {
163
163
  throw error;
164
164
  }
165
165
  }
166
+
167
+ function invokeValidate(inputValue, meta) {
168
+ value(inputValue, meta);
169
+ }
166
170
  return c;
167
171
  };
168
172
 
@@ -48,7 +48,7 @@ function newDecodeDbRow(table, dbRow, filteredAliases, shouldValidate, isInsert)
48
48
  value = purify(value);
49
49
  this._dbRow[key] = value;
50
50
  if (column.validate)
51
- column.validate(value, this._dbRow);
51
+ column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias, isInsert: false });
52
52
  updateField(this._context, table, column, this);
53
53
  let emit = this._emitColumnChanged[name];
54
54
  if (emit)
@@ -204,7 +204,7 @@ function newDecodeDbRow(table, dbRow, filteredAliases, shouldValidate, isInsert)
204
204
  if (row[key] !== undefined && !isInsert)
205
205
  row[key] = columns[i].decode(context, row[key]);
206
206
  if (shouldValidate && columns[i].validate)
207
- columns[i].validate(row[key], row, isInsert);
207
+ columns[i].validate(row[key], { table: table._dbName, column: columns[i]._dbName, property: columns[i].alias, isInsert });
208
208
  }
209
209
  let target = new Row(context, row);
210
210
  const p = new Proxy(target, {