metal-orm 1.0.66 → 1.0.68
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/dist/index.cjs +89 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +44 -9
- package/dist/index.d.ts +44 -9
- package/dist/index.js +89 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/core/ast/builders.ts +38 -38
- package/src/core/ast/expression-builders.ts +10 -6
- package/src/core/ast/expression-nodes.ts +5 -5
- package/src/core/ast/join.ts +16 -16
- package/src/core/ast/query.ts +219 -219
- package/src/core/dialect/abstract.ts +20 -20
- package/src/core/functions/array.ts +35 -35
- package/src/core/functions/json.ts +70 -70
- package/src/orm/entity-hydration.ts +72 -72
- package/src/orm/entity-materializer.ts +41 -41
- package/src/orm/entity-meta.ts +18 -18
- package/src/orm/entity-relation-cache.ts +39 -39
- package/src/orm/entity.ts +3 -3
- package/src/orm/orm-session.ts +139 -50
- package/src/orm/relations/belongs-to.ts +2 -2
- package/src/orm/relations/has-many.ts +24 -24
- package/src/orm/relations/has-one.ts +2 -2
- package/src/orm/relations/many-to-many.ts +29 -29
- package/src/orm/save-graph-types.ts +51 -50
- package/src/orm/save-graph.ts +48 -31
package/src/orm/save-graph.ts
CHANGED
|
@@ -28,13 +28,14 @@ import type { PrimaryKey } from './entity-context.js';
|
|
|
28
28
|
export interface SaveGraphOptions {
|
|
29
29
|
/** Remove existing collection members that are not present in the payload */
|
|
30
30
|
pruneMissing?: boolean;
|
|
31
|
-
/**
|
|
32
|
-
* Coerce JSON-friendly input values into DB-friendly primitives.
|
|
33
|
-
* Currently:
|
|
34
|
-
* - Date -> ISO string (for DATE/DATETIME/TIMESTAMP/TIMESTAMPTZ columns)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Coerce JSON-friendly input values into DB-friendly primitives.
|
|
33
|
+
* Currently:
|
|
34
|
+
* - `json`: Date -> ISO string (for DATE/DATETIME/TIMESTAMP/TIMESTAMPTZ columns)
|
|
35
|
+
* - `json-in`: string/number -> Date (for DATE/DATETIME/TIMESTAMP/TIMESTAMPTZ columns)
|
|
36
|
+
*/
|
|
37
|
+
coerce?: 'json' | 'json-in';
|
|
38
|
+
}
|
|
38
39
|
|
|
39
40
|
/** Represents an entity object with arbitrary properties. */
|
|
40
41
|
type AnyEntity = Record<string, unknown>;
|
|
@@ -51,33 +52,49 @@ type AnyEntity = Record<string, unknown>;
|
|
|
51
52
|
|
|
52
53
|
const toKey = (value: unknown): string => (value === null || value === undefined ? '' : String(value));
|
|
53
54
|
|
|
54
|
-
const coerceColumnValue = (
|
|
55
|
-
table: TableDef,
|
|
56
|
-
columnName: string,
|
|
57
|
-
value: unknown,
|
|
58
|
-
options: SaveGraphOptions
|
|
59
|
-
): unknown => {
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (!column) return value;
|
|
55
|
+
const coerceColumnValue = (
|
|
56
|
+
table: TableDef,
|
|
57
|
+
columnName: string,
|
|
58
|
+
value: unknown,
|
|
59
|
+
options: SaveGraphOptions
|
|
60
|
+
): unknown => {
|
|
61
|
+
if (value === null || value === undefined) return value;
|
|
62
|
+
|
|
63
|
+
const column = table.columns[columnName] as unknown as ColumnDef | undefined;
|
|
64
|
+
if (!column) return value;
|
|
65
65
|
|
|
66
66
|
const normalized = normalizeColumnType(column.type);
|
|
67
67
|
|
|
68
|
-
const isDateLikeColumn =
|
|
69
|
-
normalized === 'date' ||
|
|
70
|
-
normalized === 'datetime' ||
|
|
71
|
-
normalized === 'timestamp' ||
|
|
72
|
-
normalized === 'timestamptz';
|
|
73
|
-
|
|
74
|
-
if (isDateLikeColumn
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
68
|
+
const isDateLikeColumn =
|
|
69
|
+
normalized === 'date' ||
|
|
70
|
+
normalized === 'datetime' ||
|
|
71
|
+
normalized === 'timestamp' ||
|
|
72
|
+
normalized === 'timestamptz';
|
|
73
|
+
|
|
74
|
+
if (!isDateLikeColumn) return value;
|
|
75
|
+
|
|
76
|
+
if (options.coerce === 'json') {
|
|
77
|
+
if (value instanceof Date) {
|
|
78
|
+
return value.toISOString();
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (options.coerce === 'json-in') {
|
|
84
|
+
if (value instanceof Date) return value;
|
|
85
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
86
|
+
const date = new Date(value);
|
|
87
|
+
if (Number.isNaN(date.getTime())) {
|
|
88
|
+
throw new Error(`Invalid date value for column "${columnName}"`);
|
|
89
|
+
}
|
|
90
|
+
return date;
|
|
91
|
+
}
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Future coercions can be added here based on `normalized`.
|
|
96
|
+
return value;
|
|
97
|
+
};
|
|
81
98
|
|
|
82
99
|
const pickColumns = (table: TableDef, payload: AnyEntity, options: SaveGraphOptions): Record<string, unknown> => {
|
|
83
100
|
const columns: Record<string, unknown> = {};
|