@casekit/orm2-config 1.0.0 → 1.0.1
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/{src/index.ts → build/index.d.ts} +1 -7
- package/build/index.js +4 -0
- package/build/normalize/defaultZodSchema.d.ts +8 -0
- package/{src/normalize/defaultZodSchema.ts → build/normalize/defaultZodSchema.js} +28 -20
- package/build/normalize/defaultZodSchema.test.d.ts +1 -0
- package/{src/normalize/defaultZodSchema.test.ts → build/normalize/defaultZodSchema.test.js} +5 -13
- package/build/normalize/getColumns.d.ts +2 -0
- package/{src/normalize/getColumns.ts → build/normalize/getColumns.js} +1 -6
- package/build/normalize/getColumns.test.d.ts +1 -0
- package/{src/normalize/getColumns.test.ts → build/normalize/getColumns.test.js} +4 -17
- package/build/normalize/normalizeConfig.d.ts +3 -0
- package/{src/normalize/normalizeConfig.ts → build/normalize/normalizeConfig.js} +1 -6
- package/build/normalize/normalizeConfig.test.d.ts +1 -0
- package/{src/normalize/normalizeConfig.test.ts → build/normalize/normalizeConfig.test.js} +22 -68
- package/build/normalize/normalizeField.d.ts +3 -0
- package/build/normalize/normalizeField.js +11 -0
- package/build/normalize/normalizeField.test.d.ts +1 -0
- package/{src/normalize/normalizeField.test.ts → build/normalize/normalizeField.test.js} +3 -8
- package/build/normalize/normalizeForeignKeys.d.ts +5 -0
- package/build/normalize/normalizeForeignKeys.js +50 -0
- package/build/normalize/normalizeForeignKeys.test.d.ts +1 -0
- package/{src/normalize/normalizeForeignKeys.test.ts → build/normalize/normalizeForeignKeys.test.js} +7 -26
- package/build/normalize/normalizeModel.d.ts +3 -0
- package/{src/normalize/normalizeModel.ts → build/normalize/normalizeModel.js} +1 -8
- package/build/normalize/normalizeModel.test.d.ts +1 -0
- package/{src/normalize/normalizeModel.test.ts → build/normalize/normalizeModel.test.js} +14 -38
- package/build/normalize/normalizePrimaryKey.d.ts +3 -0
- package/build/normalize/normalizePrimaryKey.js +18 -0
- package/build/normalize/normalizePrimaryKey.test.d.ts +1 -0
- package/{src/normalize/normalizePrimaryKey.test.ts → build/normalize/normalizePrimaryKey.test.js} +7 -31
- package/build/normalize/normalizeRelations.d.ts +3 -0
- package/{src/normalize/normalizeRelations.ts → build/normalize/normalizeRelations.js} +12 -34
- package/build/normalize/normalizeRelations.test.d.ts +1 -0
- package/{src/normalize/normalizeRelations.test.ts → build/normalize/normalizeRelations.test.js} +9 -38
- package/build/normalize/normalizeUniqueConstraints.d.ts +5 -0
- package/build/normalize/normalizeUniqueConstraints.js +29 -0
- package/build/normalize/normalizeUniqueConstraints.test.d.ts +1 -0
- package/{src/normalize/normalizeUniqueConstraints.test.ts → build/normalize/normalizeUniqueConstraints.test.js} +15 -46
- package/build/normalize/populateField.d.ts +3 -0
- package/{src/normalize/populateField.ts → build/normalize/populateField.js} +1 -9
- package/build/normalize/populateField.test.d.ts +1 -0
- package/build/normalize/populateField.test.js +198 -0
- package/build/normalize/populateModels.d.ts +3 -0
- package/{src/normalize/populateModels.ts → build/normalize/populateModels.js} +2 -11
- package/build/normalize/populateModels.test.d.ts +1 -0
- package/{src/normalize/populateModels.test.ts → build/normalize/populateModels.test.js} +11 -32
- package/{src/types/NormalizedConfig.ts → build/types/NormalizedConfig.d.ts} +1 -8
- package/build/types/NormalizedConfig.js +1 -0
- package/{src/types/NormalizedFieldDefinition.ts → build/types/NormalizedFieldDefinition.d.ts} +0 -1
- package/build/types/NormalizedFieldDefinition.js +1 -0
- package/build/types/NormalizedForeignKeyDefinition.js +1 -0
- package/{src/types/NormalizedModelDefinition.ts → build/types/NormalizedModelDefinition.d.ts} +0 -1
- package/build/types/NormalizedModelDefinition.js +1 -0
- package/build/types/NormalizedPrimaryKey.js +1 -0
- package/{src/types/NormalizedRelationDefinition.ts → build/types/NormalizedRelationDefinition.d.ts} +1 -7
- package/build/types/NormalizedRelationDefinition.js +1 -0
- package/{src/types/NormalizedUniqueConstraintDefinition.ts → build/types/NormalizedUniqueConstraintDefinition.d.ts} +0 -1
- package/build/types/NormalizedUniqueConstraintDefinition.js +1 -0
- package/{src/types/PopulatedFieldDefinition.ts → build/types/PopulatedFieldDefinition.d.ts} +0 -1
- package/build/types/PopulatedFieldDefinition.js +1 -0
- package/{src/types/PopulatedModelDefinition.ts → build/types/PopulatedModelDefinition.d.ts} +0 -2
- package/build/types/PopulatedModelDefinition.js +1 -0
- package/build/util.d.ts +6 -0
- package/build/util.js +21 -0
- package/package.json +10 -10
- package/src/normalize/normalizeField.ts +0 -16
- package/src/normalize/normalizeForeignKeys.ts +0 -89
- package/src/normalize/normalizePrimaryKey.ts +0 -30
- package/src/normalize/normalizeUniqueConstraints.ts +0 -58
- package/src/normalize/populateField.test.ts +0 -253
- package/src/util.ts +0 -38
- /package/{src/types/NormalizedForeignKeyDefinition.ts → build/types/NormalizedForeignKeyDefinition.d.ts} +0 -0
- /package/{src/types/NormalizedPrimaryKey.ts → build/types/NormalizedPrimaryKey.d.ts} +0 -0
package/{src/types/NormalizedModelDefinition.ts → build/types/NormalizedModelDefinition.d.ts}
RENAMED
|
@@ -3,7 +3,6 @@ import { NormalizedForeignKeyDefinition } from "./NormalizedForeignKeyDefinition
|
|
|
3
3
|
import { NormalizedPrimaryKey } from "./NormalizedPrimaryKey.js";
|
|
4
4
|
import { NormalizedRelationDefinition } from "./NormalizedRelationDefinition.js";
|
|
5
5
|
import { NormalizedUniqueConstraintDefinition } from "./NormalizedUniqueConstraintDefinition.js";
|
|
6
|
-
|
|
7
6
|
export interface NormalizedModelDefinition {
|
|
8
7
|
name: string;
|
|
9
8
|
schema: string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/{src/types/NormalizedRelationDefinition.ts → build/types/NormalizedRelationDefinition.d.ts}
RENAMED
|
@@ -12,7 +12,6 @@ export interface NormalizedOneToManyRelationDefinition {
|
|
|
12
12
|
columns: string[];
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
|
|
16
15
|
export interface NormalizedManyToManyRelationDefinition {
|
|
17
16
|
name: string;
|
|
18
17
|
type: "N:N";
|
|
@@ -25,7 +24,6 @@ export interface NormalizedManyToManyRelationDefinition {
|
|
|
25
24
|
toRelation: string;
|
|
26
25
|
};
|
|
27
26
|
}
|
|
28
|
-
|
|
29
27
|
export interface NormalizedManyToOneRelationDefinition {
|
|
30
28
|
name: string;
|
|
31
29
|
type: "N:1";
|
|
@@ -41,8 +39,4 @@ export interface NormalizedManyToOneRelationDefinition {
|
|
|
41
39
|
columns: string[];
|
|
42
40
|
};
|
|
43
41
|
}
|
|
44
|
-
|
|
45
|
-
export type NormalizedRelationDefinition =
|
|
46
|
-
| NormalizedOneToManyRelationDefinition
|
|
47
|
-
| NormalizedManyToManyRelationDefinition
|
|
48
|
-
| NormalizedManyToOneRelationDefinition;
|
|
42
|
+
export type NormalizedRelationDefinition = NormalizedOneToManyRelationDefinition | NormalizedManyToManyRelationDefinition | NormalizedManyToOneRelationDefinition;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { ModelDefinition } from "@casekit/orm2-schema";
|
|
2
|
-
|
|
3
2
|
import { PopulatedFieldDefinition } from "./PopulatedFieldDefinition.js";
|
|
4
|
-
|
|
5
3
|
export type PopulatedModelDefinition = Required<ModelDefinition> & {
|
|
6
4
|
name: string;
|
|
7
5
|
fields: Record<string, PopulatedFieldDefinition>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/build/util.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { NormalizedFieldDefinition } from "./types/NormalizedFieldDefinition.js";
|
|
2
|
+
import { NormalizedModelDefinition } from "./types/NormalizedModelDefinition.js";
|
|
3
|
+
import { NormalizedRelationDefinition } from "./types/NormalizedRelationDefinition.js";
|
|
4
|
+
export declare const getModel: (models: Record<string, NormalizedModelDefinition>, name: string) => NormalizedModelDefinition;
|
|
5
|
+
export declare const getRelation: (model: NormalizedModelDefinition, name: string) => NormalizedRelationDefinition;
|
|
6
|
+
export declare const getField: (model: NormalizedModelDefinition, name: string) => NormalizedFieldDefinition;
|
package/build/util.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const getModel = (models, name) => {
|
|
2
|
+
const model = models[name];
|
|
3
|
+
if (!model) {
|
|
4
|
+
throw new Error(`Model "${name}" not found in config`);
|
|
5
|
+
}
|
|
6
|
+
return model;
|
|
7
|
+
};
|
|
8
|
+
export const getRelation = (model, name) => {
|
|
9
|
+
const relation = model.relations[name];
|
|
10
|
+
if (!relation) {
|
|
11
|
+
throw new Error(`Relation "${name}" not found in model "${model.name}"`);
|
|
12
|
+
}
|
|
13
|
+
return relation;
|
|
14
|
+
};
|
|
15
|
+
export const getField = (model, name) => {
|
|
16
|
+
const field = model.fields[name];
|
|
17
|
+
if (!field) {
|
|
18
|
+
throw new Error(`Field "${name}" not found in model "${model.name}"`);
|
|
19
|
+
}
|
|
20
|
+
return field;
|
|
21
|
+
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@casekit/orm2-config",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.1",
|
|
5
5
|
"author": "",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"es-toolkit": "^1.39.3",
|
|
8
|
-
"@casekit/
|
|
9
|
-
"@casekit/
|
|
10
|
-
"@casekit/
|
|
8
|
+
"@casekit/orm2-schema": "1.0.1",
|
|
9
|
+
"@casekit/sql": "1.0.1",
|
|
10
|
+
"@casekit/toolbox": "1.0.1"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"@trivago/prettier-plugin-sort-imports": "^5.2.2",
|
|
@@ -21,18 +21,18 @@
|
|
|
21
21
|
"typescript-eslint": "^8.34.1",
|
|
22
22
|
"vite-tsconfig-paths": "^5.1.4",
|
|
23
23
|
"vitest": "^3.2.4",
|
|
24
|
-
"@casekit/
|
|
25
|
-
"@casekit/
|
|
26
|
-
"@casekit/
|
|
24
|
+
"@casekit/tsconfig": "1.0.1",
|
|
25
|
+
"@casekit/orm2-schema": "1.0.1",
|
|
26
|
+
"@casekit/prettier-config": "1.0.1"
|
|
27
27
|
},
|
|
28
28
|
"exports": {
|
|
29
|
-
".": "./
|
|
29
|
+
".": "./build/index.js"
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
|
32
|
-
"/
|
|
32
|
+
"/build"
|
|
33
33
|
],
|
|
34
34
|
"imports": {
|
|
35
|
-
"#*": "./
|
|
35
|
+
"#*": "./build/*"
|
|
36
36
|
},
|
|
37
37
|
"keywords": [],
|
|
38
38
|
"license": "ISC",
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { NormalizedFieldDefinition } from "#types/NormalizedFieldDefinition.js";
|
|
2
|
-
import { PopulatedFieldDefinition } from "#types/PopulatedFieldDefinition.js";
|
|
3
|
-
|
|
4
|
-
export const normalizeField = (
|
|
5
|
-
field: PopulatedFieldDefinition,
|
|
6
|
-
): NormalizedFieldDefinition => {
|
|
7
|
-
return {
|
|
8
|
-
name: field.name,
|
|
9
|
-
column: field.column,
|
|
10
|
-
type: field.type,
|
|
11
|
-
zodSchema: field.zodSchema,
|
|
12
|
-
nullable: field.nullable,
|
|
13
|
-
default: field.default,
|
|
14
|
-
provided: field.provided,
|
|
15
|
-
};
|
|
16
|
-
};
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { isEqual } from "es-toolkit";
|
|
2
|
-
|
|
3
|
-
import { FieldDefinition, ForeignKeyDefinition } from "@casekit/orm2-schema";
|
|
4
|
-
import { MarkNonNullable } from "@casekit/toolbox";
|
|
5
|
-
|
|
6
|
-
import { NormalizedForeignKeyDefinition } from "#types/NormalizedForeignKeyDefinition.js";
|
|
7
|
-
import { PopulatedFieldDefinition } from "#types/PopulatedFieldDefinition.js";
|
|
8
|
-
import { PopulatedModelDefinition } from "#types/PopulatedModelDefinition.js";
|
|
9
|
-
import { getColumns } from "./getColumns.js";
|
|
10
|
-
|
|
11
|
-
export const normalizeForeignKeys = (
|
|
12
|
-
models: Record<string, PopulatedModelDefinition>,
|
|
13
|
-
model: PopulatedModelDefinition,
|
|
14
|
-
): NormalizedForeignKeyDefinition[] => {
|
|
15
|
-
const columnLevelForeignKeys = Object.values(model.fields)
|
|
16
|
-
.filter(hasReference)
|
|
17
|
-
.map(referenceToForeignKey)
|
|
18
|
-
.map((fk) => normalizeForeignKey(models, model, fk));
|
|
19
|
-
|
|
20
|
-
const modelLevelForeignKeys = model.foreignKeys.map((fk) =>
|
|
21
|
-
normalizeForeignKey(models, model, fk),
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
for (const fk of columnLevelForeignKeys) {
|
|
25
|
-
if (
|
|
26
|
-
modelLevelForeignKeys.some((other) =>
|
|
27
|
-
isEqual(fk.columns, other.columns),
|
|
28
|
-
)
|
|
29
|
-
) {
|
|
30
|
-
throw new Error(
|
|
31
|
-
`Duplicate foreign key defined in model "${model.name}"`,
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return [...columnLevelForeignKeys, ...modelLevelForeignKeys];
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export const normalizeForeignKey = (
|
|
40
|
-
models: Record<string, PopulatedModelDefinition>,
|
|
41
|
-
model: PopulatedModelDefinition,
|
|
42
|
-
fk: ForeignKeyDefinition,
|
|
43
|
-
): NormalizedForeignKeyDefinition => {
|
|
44
|
-
const referencedModel = models[fk.references.model];
|
|
45
|
-
if (!referencedModel) {
|
|
46
|
-
throw new Error(
|
|
47
|
-
`Referenced model "${fk.references.model}" not found in models`,
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const columns = getColumns(model, fk.fields);
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
name: fk.name ?? [model.table, ...columns, "fkey"].join("_"),
|
|
55
|
-
fields: fk.fields,
|
|
56
|
-
columns: columns,
|
|
57
|
-
references: {
|
|
58
|
-
model: fk.references.model,
|
|
59
|
-
fields: fk.references.fields,
|
|
60
|
-
schema: referencedModel.schema,
|
|
61
|
-
table: referencedModel.table,
|
|
62
|
-
columns: getColumns(referencedModel, fk.references.fields),
|
|
63
|
-
},
|
|
64
|
-
onUpdate: fk.onUpdate ?? null,
|
|
65
|
-
onDelete: fk.onDelete ?? null,
|
|
66
|
-
};
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const referenceToForeignKey = (
|
|
70
|
-
field: MarkNonNullable<PopulatedFieldDefinition, "references">,
|
|
71
|
-
): ForeignKeyDefinition => {
|
|
72
|
-
return {
|
|
73
|
-
fields: [field.name],
|
|
74
|
-
references: {
|
|
75
|
-
model: field.references.model,
|
|
76
|
-
fields: [field.references.field],
|
|
77
|
-
},
|
|
78
|
-
onUpdate: field.references.onUpdate,
|
|
79
|
-
onDelete: field.references.onDelete,
|
|
80
|
-
};
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const hasReference = (
|
|
84
|
-
field: FieldDefinition,
|
|
85
|
-
): field is PopulatedFieldDefinition & {
|
|
86
|
-
references: MarkNonNullable<PopulatedFieldDefinition, "references">;
|
|
87
|
-
} => {
|
|
88
|
-
return !!field.references;
|
|
89
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { NormalizedPrimaryKey } from "#types/NormalizedPrimaryKey.js";
|
|
2
|
-
import { PopulatedModelDefinition } from "#types/PopulatedModelDefinition.js";
|
|
3
|
-
|
|
4
|
-
export const normalizePrimaryKey = (
|
|
5
|
-
model: PopulatedModelDefinition,
|
|
6
|
-
): NormalizedPrimaryKey[] => {
|
|
7
|
-
const fieldLevelPrimaryKey = Object.entries(model.fields)
|
|
8
|
-
.filter(([, field]) => field.primaryKey)
|
|
9
|
-
.map(([name]) => name);
|
|
10
|
-
|
|
11
|
-
if (model.primaryKey && fieldLevelPrimaryKey.length > 0) {
|
|
12
|
-
throw new Error(
|
|
13
|
-
`Model "${model.name}" has primary key fields defined at both the model and field levels.`,
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const fields = model.primaryKey ?? fieldLevelPrimaryKey;
|
|
18
|
-
|
|
19
|
-
return fields.map((name) => {
|
|
20
|
-
if (!model.fields[name]) {
|
|
21
|
-
throw new Error(
|
|
22
|
-
`Primary key field "${name}" does not exist in model "${model.name}".`,
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
return {
|
|
26
|
-
field: name,
|
|
27
|
-
column: model.fields[name].column,
|
|
28
|
-
};
|
|
29
|
-
});
|
|
30
|
-
};
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { UniqueConstraintDefinition } from "@casekit/orm2-schema";
|
|
2
|
-
|
|
3
|
-
import { NormalizedUniqueConstraintDefinition } from "#types/NormalizedUniqueConstraintDefinition.js";
|
|
4
|
-
import { PopulatedModelDefinition } from "#types/PopulatedModelDefinition.js";
|
|
5
|
-
import { getColumns } from "./getColumns.js";
|
|
6
|
-
|
|
7
|
-
export const normalizeUniqueConstraint = (
|
|
8
|
-
model: PopulatedModelDefinition,
|
|
9
|
-
constraint: UniqueConstraintDefinition,
|
|
10
|
-
): NormalizedUniqueConstraintDefinition => {
|
|
11
|
-
const columns = getColumns(model, constraint.fields);
|
|
12
|
-
return {
|
|
13
|
-
name: constraint.name ?? [model.table, ...columns, "ukey"].join("_"),
|
|
14
|
-
fields: constraint.fields,
|
|
15
|
-
columns,
|
|
16
|
-
where: constraint.where ?? null,
|
|
17
|
-
nullsNotDistinct: constraint.nullsNotDistinct ?? false,
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const normalizeUniqueConstraints = (
|
|
22
|
-
model: PopulatedModelDefinition,
|
|
23
|
-
): NormalizedUniqueConstraintDefinition[] => {
|
|
24
|
-
const columnLevelUniqueConstraints = Object.values(model.fields)
|
|
25
|
-
.filter((field) => field.unique)
|
|
26
|
-
.map((field) =>
|
|
27
|
-
normalizeUniqueConstraint(
|
|
28
|
-
model,
|
|
29
|
-
typeof field.unique === "boolean"
|
|
30
|
-
? {
|
|
31
|
-
fields: [field.name],
|
|
32
|
-
where: null,
|
|
33
|
-
nullsNotDistinct: false,
|
|
34
|
-
}
|
|
35
|
-
: { fields: [field.name], ...field.unique },
|
|
36
|
-
),
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
const modelLevelUniqueConstraints = model.uniqueConstraints.map(
|
|
40
|
-
(constraint) => normalizeUniqueConstraint(model, constraint),
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
for (const constraint of columnLevelUniqueConstraints) {
|
|
44
|
-
if (
|
|
45
|
-
modelLevelUniqueConstraints.some((other) =>
|
|
46
|
-
constraint.fields.every((field) =>
|
|
47
|
-
other.fields.includes(field),
|
|
48
|
-
),
|
|
49
|
-
)
|
|
50
|
-
) {
|
|
51
|
-
throw new Error(
|
|
52
|
-
`Duplicate unique constraint defined in model "${model.name}"`,
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return [...columnLevelUniqueConstraints, ...modelLevelUniqueConstraints];
|
|
58
|
-
};
|
|
@@ -1,253 +0,0 @@
|
|
|
1
|
-
import { snakeCase } from "es-toolkit";
|
|
2
|
-
import { describe, expect, test } from "vitest";
|
|
3
|
-
import { ZodType, z } from "zod";
|
|
4
|
-
|
|
5
|
-
import { populateField } from "./populateField.js";
|
|
6
|
-
|
|
7
|
-
describe("populateField", () => {
|
|
8
|
-
test("populates minimal field definition with defaults", () => {
|
|
9
|
-
const result = populateField(
|
|
10
|
-
{
|
|
11
|
-
models: {
|
|
12
|
-
user: {
|
|
13
|
-
fields: {
|
|
14
|
-
name: { type: "text" },
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
{ type: "text" },
|
|
20
|
-
"name",
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
expect(result).toEqual({
|
|
24
|
-
name: "name",
|
|
25
|
-
column: "name",
|
|
26
|
-
type: "text",
|
|
27
|
-
zodSchema: expect.any(ZodType),
|
|
28
|
-
default: null,
|
|
29
|
-
references: null,
|
|
30
|
-
nullable: false,
|
|
31
|
-
unique: false,
|
|
32
|
-
primaryKey: false,
|
|
33
|
-
provided: false,
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test("applies naming function to column name when provided", () => {
|
|
38
|
-
const result = populateField(
|
|
39
|
-
{
|
|
40
|
-
naming: { column: snakeCase },
|
|
41
|
-
models: {
|
|
42
|
-
user: {
|
|
43
|
-
fields: {
|
|
44
|
-
fullName: { type: "text" },
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
},
|
|
49
|
-
{ type: "text" },
|
|
50
|
-
"fullName",
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
expect(result.column).toBe("full_name");
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test("uses explicit column name over naming function", () => {
|
|
57
|
-
const result = populateField(
|
|
58
|
-
{
|
|
59
|
-
naming: { column: snakeCase },
|
|
60
|
-
models: {
|
|
61
|
-
user: {
|
|
62
|
-
fields: {
|
|
63
|
-
fullName: { type: "text", column: "explicit_name" },
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
{ type: "text", column: "explicit_name" },
|
|
69
|
-
"fullName",
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
expect(result.column).toBe("explicit_name");
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test("preserves custom zodSchema", () => {
|
|
76
|
-
const customSchema = z.email();
|
|
77
|
-
const result = populateField(
|
|
78
|
-
{
|
|
79
|
-
models: {
|
|
80
|
-
user: {
|
|
81
|
-
fields: {
|
|
82
|
-
email: { type: "text", zodSchema: customSchema },
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
{ type: "text", zodSchema: customSchema },
|
|
88
|
-
"email",
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
expect(result.zodSchema).toEqual(customSchema);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
test("preserves all provided values", () => {
|
|
95
|
-
const result = populateField(
|
|
96
|
-
{
|
|
97
|
-
models: {
|
|
98
|
-
user: {
|
|
99
|
-
fields: {
|
|
100
|
-
id: {
|
|
101
|
-
type: "integer",
|
|
102
|
-
column: "custom_id",
|
|
103
|
-
zodSchema: z.number().min(1),
|
|
104
|
-
default: 42,
|
|
105
|
-
references: { model: "other", field: "id" },
|
|
106
|
-
nullable: true,
|
|
107
|
-
unique: true,
|
|
108
|
-
primaryKey: true,
|
|
109
|
-
provided: true,
|
|
110
|
-
},
|
|
111
|
-
},
|
|
112
|
-
},
|
|
113
|
-
other: {
|
|
114
|
-
fields: {
|
|
115
|
-
id: { type: "serial", primaryKey: true },
|
|
116
|
-
},
|
|
117
|
-
},
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
{
|
|
121
|
-
type: "integer",
|
|
122
|
-
column: "custom_id",
|
|
123
|
-
zodSchema: z.number().min(1),
|
|
124
|
-
default: 42,
|
|
125
|
-
references: { model: "other", field: "id" },
|
|
126
|
-
nullable: true,
|
|
127
|
-
unique: true,
|
|
128
|
-
primaryKey: true,
|
|
129
|
-
provided: true,
|
|
130
|
-
},
|
|
131
|
-
"id",
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
expect(result).toEqual({
|
|
135
|
-
name: "id",
|
|
136
|
-
type: "integer",
|
|
137
|
-
column: "custom_id",
|
|
138
|
-
zodSchema: expect.any(ZodType),
|
|
139
|
-
default: 42,
|
|
140
|
-
references: { model: "other", field: "id" },
|
|
141
|
-
nullable: true,
|
|
142
|
-
unique: true,
|
|
143
|
-
primaryKey: true,
|
|
144
|
-
provided: true,
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
test("handles array types", () => {
|
|
149
|
-
const result = populateField(
|
|
150
|
-
{
|
|
151
|
-
models: {
|
|
152
|
-
post: {
|
|
153
|
-
fields: {
|
|
154
|
-
tags: { type: "text[]" },
|
|
155
|
-
},
|
|
156
|
-
},
|
|
157
|
-
},
|
|
158
|
-
},
|
|
159
|
-
{ type: "text[]" },
|
|
160
|
-
"tags",
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
expect(result).toEqual({
|
|
164
|
-
name: "tags",
|
|
165
|
-
column: "tags",
|
|
166
|
-
type: "text[]",
|
|
167
|
-
zodSchema: expect.any(ZodType),
|
|
168
|
-
default: null,
|
|
169
|
-
references: null,
|
|
170
|
-
nullable: false,
|
|
171
|
-
unique: false,
|
|
172
|
-
primaryKey: false,
|
|
173
|
-
provided: false,
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
test("handles nullable fields", () => {
|
|
178
|
-
const result = populateField(
|
|
179
|
-
{
|
|
180
|
-
models: {
|
|
181
|
-
post: {
|
|
182
|
-
fields: {
|
|
183
|
-
description: { type: "text", nullable: true },
|
|
184
|
-
},
|
|
185
|
-
},
|
|
186
|
-
},
|
|
187
|
-
},
|
|
188
|
-
{ type: "text", nullable: true },
|
|
189
|
-
"description",
|
|
190
|
-
);
|
|
191
|
-
|
|
192
|
-
expect(result.nullable).toBe(true);
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
test("handles fields with defaults", () => {
|
|
196
|
-
const result = populateField(
|
|
197
|
-
{
|
|
198
|
-
models: {
|
|
199
|
-
user: {
|
|
200
|
-
fields: {
|
|
201
|
-
active: { type: "boolean", default: false },
|
|
202
|
-
},
|
|
203
|
-
},
|
|
204
|
-
},
|
|
205
|
-
},
|
|
206
|
-
{ type: "boolean", default: false },
|
|
207
|
-
"active",
|
|
208
|
-
);
|
|
209
|
-
|
|
210
|
-
expect(result.default).toBe(false);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
test("handles fields with foreign keys", () => {
|
|
214
|
-
const result = populateField(
|
|
215
|
-
{
|
|
216
|
-
models: {
|
|
217
|
-
user: {
|
|
218
|
-
fields: {
|
|
219
|
-
id: { type: "serial", primaryKey: true },
|
|
220
|
-
},
|
|
221
|
-
},
|
|
222
|
-
post: {
|
|
223
|
-
fields: {
|
|
224
|
-
userId: {
|
|
225
|
-
type: "integer",
|
|
226
|
-
references: {
|
|
227
|
-
model: "user",
|
|
228
|
-
field: "id",
|
|
229
|
-
onDelete: "CASCADE",
|
|
230
|
-
},
|
|
231
|
-
},
|
|
232
|
-
},
|
|
233
|
-
},
|
|
234
|
-
},
|
|
235
|
-
},
|
|
236
|
-
{
|
|
237
|
-
type: "integer",
|
|
238
|
-
references: {
|
|
239
|
-
model: "user",
|
|
240
|
-
field: "id",
|
|
241
|
-
onDelete: "CASCADE",
|
|
242
|
-
},
|
|
243
|
-
},
|
|
244
|
-
"userId",
|
|
245
|
-
);
|
|
246
|
-
|
|
247
|
-
expect(result.references).toEqual({
|
|
248
|
-
model: "user",
|
|
249
|
-
field: "id",
|
|
250
|
-
onDelete: "CASCADE",
|
|
251
|
-
});
|
|
252
|
-
});
|
|
253
|
-
});
|
package/src/util.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { NormalizedFieldDefinition } from "./types/NormalizedFieldDefinition.js";
|
|
2
|
-
import { NormalizedModelDefinition } from "./types/NormalizedModelDefinition.js";
|
|
3
|
-
import { NormalizedRelationDefinition } from "./types/NormalizedRelationDefinition.js";
|
|
4
|
-
|
|
5
|
-
export const getModel = (
|
|
6
|
-
models: Record<string, NormalizedModelDefinition>,
|
|
7
|
-
name: string,
|
|
8
|
-
): NormalizedModelDefinition => {
|
|
9
|
-
const model = models[name];
|
|
10
|
-
if (!model) {
|
|
11
|
-
throw new Error(`Model "${name}" not found in config`);
|
|
12
|
-
}
|
|
13
|
-
return model;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export const getRelation = (
|
|
17
|
-
model: NormalizedModelDefinition,
|
|
18
|
-
name: string,
|
|
19
|
-
): NormalizedRelationDefinition => {
|
|
20
|
-
const relation = model.relations[name];
|
|
21
|
-
if (!relation) {
|
|
22
|
-
throw new Error(
|
|
23
|
-
`Relation "${name}" not found in model "${model.name}"`,
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
return relation;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export const getField = (
|
|
30
|
-
model: NormalizedModelDefinition,
|
|
31
|
-
name: string,
|
|
32
|
-
): NormalizedFieldDefinition => {
|
|
33
|
-
const field = model.fields[name];
|
|
34
|
-
if (!field) {
|
|
35
|
-
throw new Error(`Field "${name}" not found in model "${model.name}"`);
|
|
36
|
-
}
|
|
37
|
-
return field;
|
|
38
|
-
};
|
|
File without changes
|
|
File without changes
|