orange-orm 5.2.2 → 5.2.3-beta
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 +5 -4
- package/SKILL.md +4 -0
- package/dist/index.browser.mjs +10 -6
- package/dist/index.mjs +10 -6
- package/package.json +1 -1
- package/src/index.d.ts +12 -6
- package/src/map.d.ts +25 -18
- package/src/table/column.js +8 -4
- package/src/table/resultToRows/newDecodeDbRow.js +2 -2
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 an optional 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(
|
|
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(
|
|
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
|
package/dist/index.browser.mjs
CHANGED
|
@@ -6391,8 +6391,8 @@ function requireColumn () {
|
|
|
6391
6391
|
|
|
6392
6392
|
c.notNullExceptInsert = function() {
|
|
6393
6393
|
column._notNullExceptInsert = true;
|
|
6394
|
-
function validate(value,
|
|
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 =
|
|
6409
|
+
column.validate = invokeValidate;
|
|
6410
6410
|
|
|
6411
6411
|
function nestedValidate() {
|
|
6412
6412
|
try {
|
|
6413
6413
|
previousValue.apply(null, arguments);
|
|
6414
|
-
|
|
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,
|
|
8616
|
+
column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias });
|
|
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],
|
|
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,
|
|
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 =
|
|
6410
|
+
column.validate = invokeValidate;
|
|
6411
6411
|
|
|
6412
6412
|
function nestedValidate() {
|
|
6413
6413
|
try {
|
|
6414
6414
|
previousValue.apply(null, arguments);
|
|
6415
|
-
|
|
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,
|
|
8617
|
+
column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias });
|
|
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],
|
|
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
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,13 @@ 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
|
+
|
|
639
646
|
interface ColumnType<M> {
|
|
640
647
|
string(): StringColumnTypeDef<M & StringColumnSymbol>;
|
|
641
648
|
uuid(): UuidColumnTypeDef<M & UuidColumnSymbol>;
|
|
@@ -652,90 +659,90 @@ interface ColumnType<M> {
|
|
|
652
659
|
|
|
653
660
|
type UuidValidator<M> = M extends NotNull
|
|
654
661
|
? {
|
|
655
|
-
validate(validator: (value: string) => void): UuidColumnTypeDef<M>;
|
|
662
|
+
validate(validator: (value: string, meta?: ValidationMeta) => void): UuidColumnTypeDef<M>;
|
|
656
663
|
}
|
|
657
664
|
: {
|
|
658
665
|
validate(
|
|
659
|
-
validator: (value?: string | null) => void
|
|
666
|
+
validator: (value?: string | null, meta?: ValidationMeta) => void
|
|
660
667
|
): UuidColumnTypeDef<M>;
|
|
661
668
|
};
|
|
662
669
|
type StringValidator<M> = M extends NotNull
|
|
663
670
|
? {
|
|
664
|
-
validate(validator: (value: string) => void): StringColumnTypeDef<M>;
|
|
671
|
+
validate(validator: (value: string, meta?: ValidationMeta) => void): StringColumnTypeDef<M>;
|
|
665
672
|
}
|
|
666
673
|
: {
|
|
667
674
|
validate(
|
|
668
|
-
validator: (value?: string | null) => void
|
|
675
|
+
validator: (value?: string | null, meta?: ValidationMeta) => void
|
|
669
676
|
): StringColumnTypeDef<M>;
|
|
670
677
|
};
|
|
671
678
|
type NumericValidator<M> = M extends NotNull
|
|
672
679
|
? {
|
|
673
|
-
validate(validator: (value: number) => void): NumericColumnTypeDef<M>;
|
|
680
|
+
validate(validator: (value: number, meta?: ValidationMeta) => void): NumericColumnTypeDef<M>;
|
|
674
681
|
}
|
|
675
682
|
: {
|
|
676
683
|
validate(
|
|
677
|
-
validator: (value?: number | null) => void
|
|
684
|
+
validator: (value?: number | null, meta?: ValidationMeta) => void
|
|
678
685
|
): NumericColumnTypeDef<M>;
|
|
679
686
|
};
|
|
680
687
|
type BigIntValidator<M> = M extends NotNull
|
|
681
688
|
? {
|
|
682
|
-
validate(validator: (value: bigint) => void): BigIntColumnTypeDef<M>;
|
|
689
|
+
validate(validator: (value: bigint, meta?: ValidationMeta) => void): BigIntColumnTypeDef<M>;
|
|
683
690
|
}
|
|
684
691
|
: {
|
|
685
692
|
validate(
|
|
686
|
-
validator: (value?: bigint | null) => void
|
|
693
|
+
validator: (value?: bigint | null, meta?: ValidationMeta) => void
|
|
687
694
|
): BigIntColumnTypeDef<M>;
|
|
688
695
|
};
|
|
689
696
|
type BinaryValidator<M> = M extends NotNull
|
|
690
697
|
? {
|
|
691
|
-
validate(validator: (value: string) => void): BinaryColumnTypeDef<M>;
|
|
698
|
+
validate(validator: (value: string, meta?: ValidationMeta) => void): BinaryColumnTypeDef<M>;
|
|
692
699
|
}
|
|
693
700
|
: {
|
|
694
701
|
validate(
|
|
695
|
-
validator: (value?: string | null) => void
|
|
702
|
+
validator: (value?: string | null, meta?: ValidationMeta) => void
|
|
696
703
|
): BinaryColumnTypeDef<M>;
|
|
697
704
|
};
|
|
698
705
|
type BooleanValidator<M> = M extends NotNull
|
|
699
706
|
? {
|
|
700
|
-
validate(validator: (value: boolean) => void): BooleanColumnTypeDef<M>;
|
|
707
|
+
validate(validator: (value: boolean, meta?: ValidationMeta) => void): BooleanColumnTypeDef<M>;
|
|
701
708
|
}
|
|
702
709
|
: {
|
|
703
710
|
validate(
|
|
704
|
-
validator: (value?: boolean | null) => void
|
|
711
|
+
validator: (value?: boolean | null, meta?: ValidationMeta) => void
|
|
705
712
|
): BooleanColumnTypeDef<M>;
|
|
706
713
|
};
|
|
707
714
|
type JSONValidator<M> = M extends NotNull
|
|
708
715
|
? {
|
|
709
716
|
validate(
|
|
710
|
-
validator: (value: ToJsonType<M
|
|
717
|
+
validator: (value: ToJsonType<M>, meta?: ValidationMeta) => void
|
|
711
718
|
): JSONColumnTypeDef<M>;
|
|
712
719
|
}
|
|
713
720
|
: {
|
|
714
721
|
validate(
|
|
715
|
-
validator: (value?: ToJsonType<M> | null) => void
|
|
722
|
+
validator: (value?: ToJsonType<M> | null, meta?: ValidationMeta) => void
|
|
716
723
|
): JSONColumnTypeDef<M>;
|
|
717
724
|
};
|
|
718
725
|
type DateValidator<M> = M extends NotNull
|
|
719
726
|
? {
|
|
720
727
|
validate(
|
|
721
|
-
validator: (value: string | Date) => void
|
|
728
|
+
validator: (value: string | Date, meta?: ValidationMeta) => void
|
|
722
729
|
): DateColumnTypeDef<M>;
|
|
723
730
|
}
|
|
724
731
|
: {
|
|
725
732
|
validate(
|
|
726
|
-
validator: (value?: string | Date | null) => void
|
|
733
|
+
validator: (value?: string | Date | null, meta?: ValidationMeta) => void
|
|
727
734
|
): DateColumnTypeDef<M>;
|
|
728
735
|
};
|
|
729
736
|
|
|
730
737
|
type DateWithTimeZoneValidator<M> = M extends NotNull
|
|
731
738
|
? {
|
|
732
739
|
validate(
|
|
733
|
-
validator: (value: string | Date) => void
|
|
740
|
+
validator: (value: string | Date, meta?: ValidationMeta) => void
|
|
734
741
|
): DateWithTimeZoneColumnTypeDef<M>;
|
|
735
742
|
}
|
|
736
743
|
: {
|
|
737
744
|
validate(
|
|
738
|
-
validator: (value?: string | Date | null) => void
|
|
745
|
+
validator: (value?: string | Date | null, meta?: ValidationMeta) => void
|
|
739
746
|
): DateWithTimeZoneColumnTypeDef<M>;
|
|
740
747
|
};
|
|
741
748
|
|
package/src/table/column.js
CHANGED
|
@@ -134,8 +134,8 @@ function defineColumn(column, table) {
|
|
|
134
134
|
|
|
135
135
|
c.notNullExceptInsert = function() {
|
|
136
136
|
column._notNullExceptInsert = true;
|
|
137
|
-
function validate(value,
|
|
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 =
|
|
152
|
+
column.validate = invokeValidate;
|
|
153
153
|
|
|
154
154
|
function nestedValidate() {
|
|
155
155
|
try {
|
|
156
156
|
previousValue.apply(null, arguments);
|
|
157
|
-
|
|
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,
|
|
51
|
+
column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias });
|
|
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],
|
|
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, {
|