orange-orm 5.2.3-beta → 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,7 +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 an optional metadata object with <code>table</code>, <code>column</code>, <code>property</code>, and <code>isInsert</code>.</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>
2062
2062
 
2063
2063
  <sub>📄 map.ts</sub>
2064
2064
  ```ts
@@ -2076,9 +2076,9 @@ let petSchema = {
2076
2076
  }
2077
2077
  };
2078
2078
 
2079
- function validateName(value?: string, meta?: { table?: string; column?: string; property?: string; isInsert?: boolean }) {
2079
+ function validateName(value?: string, meta: { table: string; column: string; property: string; isInsert: boolean }) {
2080
2080
  if (value && value.length > 10)
2081
- throw new Error(`Length cannot exceed 10 characters in ${meta?.table}.${meta?.column}`);
2081
+ throw new Error(`Length cannot exceed 10 characters in ${meta.table}.${meta.column}`);
2082
2082
  }
2083
2083
 
2084
2084
  const map = orange.map(x => ({
@@ -2113,7 +2113,7 @@ let petSchema = {
2113
2113
 
2114
2114
  function validateName(value, meta) {
2115
2115
  if (value && value.length > 10)
2116
- throw new Error(`Length cannot exceed 10 characters in ${meta?.table}.${meta?.column}`);
2116
+ throw new Error(`Length cannot exceed 10 characters in ${meta.table}.${meta.column}`);
2117
2117
  }
2118
2118
 
2119
2119
  const map = orange.map(x => ({
@@ -6392,7 +6392,7 @@ function requireColumn () {
6392
6392
  c.notNullExceptInsert = function() {
6393
6393
  column._notNullExceptInsert = true;
6394
6394
  function validate(value, meta) {
6395
- if (meta?.isInsert)
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`);
@@ -8613,7 +8613,7 @@ function requireNewDecodeDbRow () {
8613
8613
  value = purify(value);
8614
8614
  this._dbRow[key] = value;
8615
8615
  if (column.validate)
8616
- column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias });
8616
+ column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias, isInsert: false });
8617
8617
  updateField(this._context, table, column, this);
8618
8618
  let emit = this._emitColumnChanged[name];
8619
8619
  if (emit)
package/dist/index.mjs CHANGED
@@ -6393,7 +6393,7 @@ function requireColumn () {
6393
6393
  c.notNullExceptInsert = function() {
6394
6394
  column._notNullExceptInsert = true;
6395
6395
  function validate(value, meta) {
6396
- if (meta?.isInsert)
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`);
@@ -8614,7 +8614,7 @@ function requireNewDecodeDbRow () {
8614
8614
  value = purify(value);
8615
8615
  this._dbRow[key] = value;
8616
8616
  if (column.validate)
8617
- column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias });
8617
+ column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias, isInsert: false });
8618
8618
  updateField(this._context, table, column, this);
8619
8619
  let emit = this._emitColumnChanged[name];
8620
8620
  if (emit)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orange-orm",
3
- "version": "5.2.3-beta",
3
+ "version": "5.2.3-beta2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/index.d.ts CHANGED
@@ -11,10 +11,10 @@ declare function r(config: r.Config): unknown;
11
11
 
12
12
  declare namespace r {
13
13
  export interface ValidationMeta {
14
- table?: string;
15
- column?: string;
16
- property?: string;
17
- isInsert?: boolean;
14
+ table: string;
15
+ column: string;
16
+ property: string;
17
+ isInsert: boolean;
18
18
  }
19
19
 
20
20
  function table(name: string): Table;
@@ -100,7 +100,7 @@ declare namespace r {
100
100
  date(): DateColumnDef;
101
101
  }
102
102
  export interface DateColumnDef {
103
- validate(validator: (value?: Date | string, meta?: ValidationMeta) => void): DateColumnDef;
103
+ validate(validator: (value?: Date | string, meta: ValidationMeta) => void): DateColumnDef;
104
104
  notNull(): DateColumnNotNullDef;
105
105
  JSONSchema(schema: object, options?: Options): DateColumnDef;
106
106
  serializable(value: boolean): DateColumnDef;
@@ -110,7 +110,7 @@ declare namespace r {
110
110
  }
111
111
 
112
112
  export interface DateColumnNotNullDef {
113
- validate(validator: (value: Date | string, meta?: ValidationMeta) => void): DateColumnNotNullDef;
113
+ validate(validator: (value: Date | string, meta: ValidationMeta) => void): DateColumnNotNullDef;
114
114
  JSONSchema(schema: object, options?: Options): DateColumnNotNullDef;
115
115
  serializable(value: boolean): DateColumnNotNullDef;
116
116
  as(dbName: string): DateColumnNotNullDef;
@@ -119,7 +119,7 @@ declare namespace r {
119
119
  }
120
120
 
121
121
  export interface BinaryColumnDef {
122
- validate(validator: (value?: Buffer | string, meta?: ValidationMeta) => void): BinaryColumnDef;
122
+ validate(validator: (value?: Buffer | string, meta: ValidationMeta) => void): BinaryColumnDef;
123
123
  notNull(): BinaryColumnNotNullDef;
124
124
  JSONSchema(schema: object, options?: Options): BinaryColumnDef;
125
125
  serializable(value: boolean): BinaryColumnDef;
@@ -129,7 +129,7 @@ declare namespace r {
129
129
  }
130
130
 
131
131
  export interface BinaryColumnNotNullDef {
132
- validate(validator: (value: Buffer | string, meta?: ValidationMeta) => void): BinaryColumnNotNullDef;
132
+ validate(validator: (value: Buffer | string, meta: ValidationMeta) => void): BinaryColumnNotNullDef;
133
133
  JSONSchema(schema: object, options?: Options): BinaryColumnNotNullDef;
134
134
  serializable(value: boolean): BinaryColumnNotNullDef;
135
135
  as(dbName: string): BinaryColumnNotNullDef;
@@ -138,7 +138,7 @@ declare namespace r {
138
138
  }
139
139
 
140
140
  export interface ColumnOf<T> {
141
- validate(validator: (value?: T, meta?: ValidationMeta) => void): ColumnOf<T>;
141
+ validate(validator: (value?: T, meta: ValidationMeta) => void): ColumnOf<T>;
142
142
  notNull(): ColumnNotNullOf<T>;
143
143
  JSONSchema(schema: object, options?: Options): ColumnOf<T>;
144
144
  serializable(value: boolean): ColumnOf<T>;
@@ -148,7 +148,7 @@ declare namespace r {
148
148
  }
149
149
 
150
150
  export interface ColumnNotNullOf<T> {
151
- validate(validator: (value: T, meta?: ValidationMeta) => void): ColumnNotNullOf<T>;
151
+ validate(validator: (value: T, meta: ValidationMeta) => void): ColumnNotNullOf<T>;
152
152
  notNull(): ColumnNotNullOf<T>;
153
153
  JSONSchema(schema: object, options?: Options): ColumnNotNullOf<T>;
154
154
  serializable(value: boolean): ColumnNotNullOf<T>;
package/src/map.d.ts CHANGED
@@ -637,12 +637,17 @@ type JsonOf<T> = {
637
637
  };
638
638
 
639
639
  interface ValidationMeta {
640
- table?: string;
641
- column?: string;
642
- property?: string;
643
- isInsert?: boolean;
640
+ table: string;
641
+ column: string;
642
+ property: string;
643
+ isInsert: boolean;
644
644
  }
645
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
+
646
651
  interface ColumnType<M> {
647
652
  string(): StringColumnTypeDef<M & StringColumnSymbol>;
648
653
  uuid(): UuidColumnTypeDef<M & UuidColumnSymbol>;
@@ -657,94 +662,15 @@ interface ColumnType<M> {
657
662
  jsonOf<T>(helper: T): JSONColumnTypeDef<M & JSONColumnSymbol & JsonOf<T>>;
658
663
  }
659
664
 
660
- type UuidValidator<M> = M extends NotNull
661
- ? {
662
- validate(validator: (value: string, meta?: ValidationMeta) => void): UuidColumnTypeDef<M>;
663
- }
664
- : {
665
- validate(
666
- validator: (value?: string | null, meta?: ValidationMeta) => void
667
- ): UuidColumnTypeDef<M>;
668
- };
669
- type StringValidator<M> = M extends NotNull
670
- ? {
671
- validate(validator: (value: string, meta?: ValidationMeta) => void): StringColumnTypeDef<M>;
672
- }
673
- : {
674
- validate(
675
- validator: (value?: string | null, meta?: ValidationMeta) => void
676
- ): StringColumnTypeDef<M>;
677
- };
678
- type NumericValidator<M> = M extends NotNull
679
- ? {
680
- validate(validator: (value: number, meta?: ValidationMeta) => void): NumericColumnTypeDef<M>;
681
- }
682
- : {
683
- validate(
684
- validator: (value?: number | null, meta?: ValidationMeta) => void
685
- ): NumericColumnTypeDef<M>;
686
- };
687
- type BigIntValidator<M> = M extends NotNull
688
- ? {
689
- validate(validator: (value: bigint, meta?: ValidationMeta) => void): BigIntColumnTypeDef<M>;
690
- }
691
- : {
692
- validate(
693
- validator: (value?: bigint | null, meta?: ValidationMeta) => void
694
- ): BigIntColumnTypeDef<M>;
695
- };
696
- type BinaryValidator<M> = M extends NotNull
697
- ? {
698
- validate(validator: (value: string, meta?: ValidationMeta) => void): BinaryColumnTypeDef<M>;
699
- }
700
- : {
701
- validate(
702
- validator: (value?: string | null, meta?: ValidationMeta) => void
703
- ): BinaryColumnTypeDef<M>;
704
- };
705
- type BooleanValidator<M> = M extends NotNull
706
- ? {
707
- validate(validator: (value: boolean, meta?: ValidationMeta) => void): BooleanColumnTypeDef<M>;
708
- }
709
- : {
710
- validate(
711
- validator: (value?: boolean | null, meta?: ValidationMeta) => void
712
- ): BooleanColumnTypeDef<M>;
713
- };
714
- type JSONValidator<M> = M extends NotNull
715
- ? {
716
- validate(
717
- validator: (value: ToJsonType<M>, meta?: ValidationMeta) => void
718
- ): JSONColumnTypeDef<M>;
719
- }
720
- : {
721
- validate(
722
- validator: (value?: ToJsonType<M> | null, meta?: ValidationMeta) => void
723
- ): JSONColumnTypeDef<M>;
724
- };
725
- type DateValidator<M> = M extends NotNull
726
- ? {
727
- validate(
728
- validator: (value: string | Date, meta?: ValidationMeta) => void
729
- ): DateColumnTypeDef<M>;
730
- }
731
- : {
732
- validate(
733
- validator: (value?: string | Date | null, meta?: ValidationMeta) => void
734
- ): DateColumnTypeDef<M>;
735
- };
736
-
737
- type DateWithTimeZoneValidator<M> = M extends NotNull
738
- ? {
739
- validate(
740
- validator: (value: string | Date, meta?: ValidationMeta) => void
741
- ): DateWithTimeZoneColumnTypeDef<M>;
742
- }
743
- : {
744
- validate(
745
- validator: (value?: string | Date | null, meta?: ValidationMeta) => void
746
- ): DateWithTimeZoneColumnTypeDef<M>;
747
- };
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>>;
748
674
 
749
675
  type StringColumnTypeDef<M> = StringValidator<M> & {
750
676
  enum<const V extends readonly string[]>(values: V): StringColumnTypeDef<M & EnumOf<V[number]>> & EnumOf<V[number]>;
@@ -863,7 +789,7 @@ type DateColumnTypeDef<M> = DateValidator<M> & {
863
789
  } & ColumnTypeOf<DateColumnType<M>> &
864
790
  M;
865
791
 
866
- type DateWithTimeZoneColumnTypeDef<M> = DateValidator<M> & {
792
+ type DateWithTimeZoneColumnTypeDef<M> = DateWithTimeZoneValidator<M> & {
867
793
  enum<const V extends Record<string, string | Date>>(values: V): DateWithTimeZoneColumnTypeDef<M & EnumOf<V[keyof V]>> & EnumOf<V[keyof V]>;
868
794
  enum<TEnum>(values: Record<string, TEnum>): DateWithTimeZoneColumnTypeDef<M & EnumOf<TEnum>> & EnumOf<TEnum>;
869
795
  enum<E extends string | Date>(values: readonly E[]): DateWithTimeZoneColumnTypeDef<M & EnumOf<E>> & EnumOf<E>;
@@ -135,7 +135,7 @@ function defineColumn(column, table) {
135
135
  c.notNullExceptInsert = function() {
136
136
  column._notNullExceptInsert = true;
137
137
  function validate(value, meta) {
138
- if (meta?.isInsert)
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`);
@@ -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, { table: table._dbName, column: column._dbName, property: column.alias });
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)