@casekit/orm2-config 0.0.0-20250331202540 → 0.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.
Files changed (73) hide show
  1. package/package.json +19 -19
  2. package/{build/index.d.ts → src/index.ts} +7 -1
  3. package/{build/normalize/defaultZodSchema.test.js → src/normalize/defaultZodSchema.test.ts} +13 -5
  4. package/{build/normalize/defaultZodSchema.js → src/normalize/defaultZodSchema.ts} +22 -30
  5. package/{build/normalize/getColumns.test.js → src/normalize/getColumns.test.ts} +17 -4
  6. package/{build/normalize/getColumns.js → src/normalize/getColumns.ts} +6 -1
  7. package/{build/normalize/normalizeConfig.test.js → src/normalize/normalizeConfig.test.ts} +70 -24
  8. package/{build/normalize/normalizeConfig.js → src/normalize/normalizeConfig.ts} +7 -2
  9. package/{build/normalize/normalizeField.test.js → src/normalize/normalizeField.test.ts} +12 -7
  10. package/src/normalize/normalizeField.ts +16 -0
  11. package/{build/normalize/normalizeForeignKeys.test.js → src/normalize/normalizeForeignKeys.test.ts} +26 -7
  12. package/src/normalize/normalizeForeignKeys.ts +89 -0
  13. package/{build/normalize/normalizeModel.test.js → src/normalize/normalizeModel.test.ts} +42 -18
  14. package/{build/normalize/normalizeModel.js → src/normalize/normalizeModel.ts} +8 -1
  15. package/{build/normalize/normalizePrimaryKey.test.js → src/normalize/normalizePrimaryKey.test.ts} +31 -7
  16. package/src/normalize/normalizePrimaryKey.ts +30 -0
  17. package/{build/normalize/normalizeRelations.test.js → src/normalize/normalizeRelations.test.ts} +38 -9
  18. package/{build/normalize/normalizeRelations.js → src/normalize/normalizeRelations.ts} +34 -12
  19. package/{build/normalize/normalizeUniqueConstraints.test.js → src/normalize/normalizeUniqueConstraints.test.ts} +46 -15
  20. package/src/normalize/normalizeUniqueConstraints.ts +58 -0
  21. package/src/normalize/populateField.test.ts +253 -0
  22. package/{build/normalize/populateField.js → src/normalize/populateField.ts} +9 -1
  23. package/{build/normalize/populateModels.test.js → src/normalize/populateModels.test.ts} +35 -14
  24. package/{build/normalize/populateModels.js → src/normalize/populateModels.ts} +11 -2
  25. package/{build/types/NormalizedConfig.d.ts → src/types/NormalizedConfig.ts} +8 -1
  26. package/{build/types/NormalizedFieldDefinition.d.ts → src/types/NormalizedFieldDefinition.ts} +2 -1
  27. package/{build/types/NormalizedModelDefinition.d.ts → src/types/NormalizedModelDefinition.ts} +1 -0
  28. package/{build/types/NormalizedRelationDefinition.d.ts → src/types/NormalizedRelationDefinition.ts} +7 -1
  29. package/{build/types/NormalizedUniqueConstraintDefinition.d.ts → src/types/NormalizedUniqueConstraintDefinition.ts} +1 -0
  30. package/{build/types/PopulatedFieldDefinition.d.ts → src/types/PopulatedFieldDefinition.ts} +1 -0
  31. package/{build/types/PopulatedModelDefinition.d.ts → src/types/PopulatedModelDefinition.ts} +2 -0
  32. package/src/util.ts +38 -0
  33. package/build/index.js +0 -4
  34. package/build/normalize/defaultZodSchema.d.ts +0 -8
  35. package/build/normalize/defaultZodSchema.test.d.ts +0 -1
  36. package/build/normalize/getColumns.d.ts +0 -2
  37. package/build/normalize/getColumns.test.d.ts +0 -1
  38. package/build/normalize/normalizeConfig.d.ts +0 -3
  39. package/build/normalize/normalizeConfig.test.d.ts +0 -1
  40. package/build/normalize/normalizeField.d.ts +0 -3
  41. package/build/normalize/normalizeField.js +0 -11
  42. package/build/normalize/normalizeField.test.d.ts +0 -1
  43. package/build/normalize/normalizeForeignKeys.d.ts +0 -5
  44. package/build/normalize/normalizeForeignKeys.js +0 -50
  45. package/build/normalize/normalizeForeignKeys.test.d.ts +0 -1
  46. package/build/normalize/normalizeModel.d.ts +0 -3
  47. package/build/normalize/normalizeModel.test.d.ts +0 -1
  48. package/build/normalize/normalizePrimaryKey.d.ts +0 -3
  49. package/build/normalize/normalizePrimaryKey.js +0 -18
  50. package/build/normalize/normalizePrimaryKey.test.d.ts +0 -1
  51. package/build/normalize/normalizeRelations.d.ts +0 -3
  52. package/build/normalize/normalizeRelations.test.d.ts +0 -1
  53. package/build/normalize/normalizeUniqueConstraints.d.ts +0 -5
  54. package/build/normalize/normalizeUniqueConstraints.js +0 -29
  55. package/build/normalize/normalizeUniqueConstraints.test.d.ts +0 -1
  56. package/build/normalize/populateField.d.ts +0 -3
  57. package/build/normalize/populateField.test.d.ts +0 -1
  58. package/build/normalize/populateField.test.js +0 -198
  59. package/build/normalize/populateModels.d.ts +0 -3
  60. package/build/normalize/populateModels.test.d.ts +0 -1
  61. package/build/types/NormalizedConfig.js +0 -1
  62. package/build/types/NormalizedFieldDefinition.js +0 -1
  63. package/build/types/NormalizedForeignKeyDefinition.js +0 -1
  64. package/build/types/NormalizedModelDefinition.js +0 -1
  65. package/build/types/NormalizedPrimaryKey.js +0 -1
  66. package/build/types/NormalizedRelationDefinition.js +0 -1
  67. package/build/types/NormalizedUniqueConstraintDefinition.js +0 -1
  68. package/build/types/PopulatedFieldDefinition.js +0 -1
  69. package/build/types/PopulatedModelDefinition.js +0 -1
  70. package/build/util.d.ts +0 -6
  71. package/build/util.js +0 -21
  72. /package/{build/types/NormalizedForeignKeyDefinition.d.ts → src/types/NormalizedForeignKeyDefinition.ts} +0 -0
  73. /package/{build/types/NormalizedPrimaryKey.d.ts → src/types/NormalizedPrimaryKey.ts} +0 -0
@@ -1,7 +1,9 @@
1
1
  import { snakeCase } from "es-toolkit";
2
2
  import { describe, expect, test } from "vitest";
3
- import { ZodSchema } from "zod";
3
+ import { ZodType } from "zod";
4
+
4
5
  import { populateModels } from "./populateModels.js";
6
+
5
7
  describe("populateModels", () => {
6
8
  test("populates basic models with defaults", () => {
7
9
  const result = populateModels({
@@ -14,6 +16,7 @@ describe("populateModels", () => {
14
16
  },
15
17
  },
16
18
  });
19
+
17
20
  expect(result).toEqual({
18
21
  user: {
19
22
  name: "user",
@@ -28,7 +31,7 @@ describe("populateModels", () => {
28
31
  name: "id",
29
32
  column: "id",
30
33
  type: "serial",
31
- zodSchema: expect.any(ZodSchema),
34
+ zodSchema: expect.any(ZodType),
32
35
  default: null,
33
36
  references: null,
34
37
  nullable: false,
@@ -40,7 +43,7 @@ describe("populateModels", () => {
40
43
  name: "name",
41
44
  column: "name",
42
45
  type: "text",
43
- zodSchema: expect.any(ZodSchema),
46
+ zodSchema: expect.any(ZodType),
44
47
  default: null,
45
48
  references: null,
46
49
  nullable: false,
@@ -52,6 +55,7 @@ describe("populateModels", () => {
52
55
  },
53
56
  });
54
57
  });
58
+
55
59
  test("uses config schema when model schema not provided", () => {
56
60
  const result = populateModels({
57
61
  schema: "custom",
@@ -63,8 +67,10 @@ describe("populateModels", () => {
63
67
  },
64
68
  },
65
69
  });
66
- expect(result["user"].schema).toBe("custom");
70
+
71
+ expect(result["user"]!.schema).toBe("custom");
67
72
  });
73
+
68
74
  test("model schema overrides config schema", () => {
69
75
  const result = populateModels({
70
76
  schema: "config_schema",
@@ -77,8 +83,10 @@ describe("populateModels", () => {
77
83
  },
78
84
  },
79
85
  });
80
- expect(result["user"].schema).toBe("model_schema");
86
+
87
+ expect(result["user"]!.schema).toBe("model_schema");
81
88
  });
89
+
82
90
  test("applies table naming function when provided", () => {
83
91
  const result = populateModels({
84
92
  naming: { table: snakeCase },
@@ -90,8 +98,10 @@ describe("populateModels", () => {
90
98
  },
91
99
  },
92
100
  });
93
- expect(result["userProfile"].table).toBe("user_profile");
101
+
102
+ expect(result["userProfile"]!.table).toBe("user_profile");
94
103
  });
104
+
95
105
  test("explicit table name overrides naming function", () => {
96
106
  const result = populateModels({
97
107
  naming: { table: snakeCase },
@@ -104,8 +114,10 @@ describe("populateModels", () => {
104
114
  },
105
115
  },
106
116
  });
107
- expect(result["userProfile"].table).toBe("custom_table");
117
+
118
+ expect(result["userProfile"]!.table).toBe("custom_table");
108
119
  });
120
+
109
121
  test("uses explicit primary key when provided", () => {
110
122
  const result = populateModels({
111
123
  models: {
@@ -119,8 +131,10 @@ describe("populateModels", () => {
119
131
  },
120
132
  },
121
133
  });
122
- expect(result["post"].primaryKey).toEqual(["authorId", "slug"]);
134
+
135
+ expect(result["post"]!.primaryKey).toEqual(["authorId", "slug"]);
123
136
  });
137
+
124
138
  test("handles unique constraints", () => {
125
139
  const result = populateModels({
126
140
  models: {
@@ -138,13 +152,15 @@ describe("populateModels", () => {
138
152
  },
139
153
  },
140
154
  });
141
- expect(result["user"].uniqueConstraints).toEqual([
155
+
156
+ expect(result["user"]!.uniqueConstraints).toEqual([
142
157
  {
143
158
  name: "unique_email",
144
159
  fields: ["email"],
145
160
  },
146
161
  ]);
147
162
  });
163
+
148
164
  test("handles foreign keys", () => {
149
165
  const result = populateModels({
150
166
  models: {
@@ -169,7 +185,8 @@ describe("populateModels", () => {
169
185
  },
170
186
  },
171
187
  });
172
- expect(result["post"].foreignKeys).toEqual([
188
+
189
+ expect(result["post"]!.foreignKeys).toEqual([
173
190
  {
174
191
  fields: ["authorId"],
175
192
  references: {
@@ -179,6 +196,7 @@ describe("populateModels", () => {
179
196
  },
180
197
  ]);
181
198
  });
199
+
182
200
  test("handles relations", () => {
183
201
  const result = populateModels({
184
202
  models: {
@@ -203,7 +221,8 @@ describe("populateModels", () => {
203
221
  },
204
222
  },
205
223
  });
206
- expect(result["user"].relations).toEqual({
224
+
225
+ expect(result["user"]!.relations).toEqual({
207
226
  posts: {
208
227
  type: "1:N",
209
228
  model: "post",
@@ -212,6 +231,7 @@ describe("populateModels", () => {
212
231
  },
213
232
  });
214
233
  });
234
+
215
235
  test("handles multiple models", () => {
216
236
  const result = populateModels({
217
237
  models: {
@@ -232,9 +252,10 @@ describe("populateModels", () => {
232
252
  },
233
253
  },
234
254
  });
255
+
235
256
  expect(Object.keys(result)).toEqual(["user", "post", "comment"]);
236
- expect(result["user"].name).toBe("user");
237
- expect(result["post"].name).toBe("post");
238
- expect(result["comment"].name).toBe("comment");
257
+ expect(result["user"]!.name).toBe("user");
258
+ expect(result["post"]!.name).toBe("post");
259
+ expect(result["comment"]!.name).toBe("comment");
239
260
  });
240
261
  });
@@ -1,6 +1,13 @@
1
1
  import { mapValues } from "es-toolkit";
2
+
3
+ import { Config } from "@casekit/orm2-schema";
4
+
5
+ import { PopulatedModelDefinition } from "#types/PopulatedModelDefinition.js";
2
6
  import { populateField } from "./populateField.js";
3
- export const populateModels = (config) => {
7
+
8
+ export const populateModels = (
9
+ config: Config,
10
+ ): Record<string, PopulatedModelDefinition> => {
4
11
  return mapValues(config.models, (model, name) => {
5
12
  return {
6
13
  name,
@@ -10,7 +17,9 @@ export const populateModels = (config) => {
10
17
  uniqueConstraints: model.uniqueConstraints ?? [],
11
18
  foreignKeys: model.foreignKeys ?? [],
12
19
  relations: model.relations ?? {},
13
- fields: mapValues(model.fields, (field, name) => populateField(config, field, name)),
20
+ fields: mapValues(model.fields, (field, name) =>
21
+ populateField(config, field, name),
22
+ ),
14
23
  };
15
24
  });
16
25
  };
@@ -1,12 +1,19 @@
1
1
  import pg from "pg";
2
+
2
3
  import { Logger, OperatorDefinitions } from "@casekit/orm2-schema";
4
+
3
5
  import { NormalizedModelDefinition } from "./NormalizedModelDefinition.js";
6
+
4
7
  export interface NormalizedConfig {
5
8
  readonly schema: string;
6
9
  readonly models: Record<string, NormalizedModelDefinition>;
7
10
  readonly operators: OperatorDefinitions;
8
11
  readonly extensions: readonly string[];
9
- readonly connection: pg.ConnectionConfig | pg.PoolConfig | pg.PoolOptions | null;
12
+ readonly connection:
13
+ | pg.ConnectionConfig
14
+ | pg.PoolConfig
15
+ | pg.PoolOptions
16
+ | null;
10
17
  readonly pool: boolean;
11
18
  readonly logger: Logger;
12
19
  readonly naming: {
@@ -1,9 +1,10 @@
1
1
  import { z } from "zod";
2
+
2
3
  export interface NormalizedFieldDefinition {
3
4
  name: string;
4
5
  column: string;
5
6
  type: string;
6
- zodSchema: z.ZodType<unknown>;
7
+ zodSchema: z.ZodType;
7
8
  nullable: boolean;
8
9
  default: unknown;
9
10
  provided?: boolean;
@@ -3,6 +3,7 @@ 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
+
6
7
  export interface NormalizedModelDefinition {
7
8
  name: string;
8
9
  schema: string;
@@ -12,6 +12,7 @@ export interface NormalizedOneToManyRelationDefinition {
12
12
  columns: string[];
13
13
  };
14
14
  }
15
+
15
16
  export interface NormalizedManyToManyRelationDefinition {
16
17
  name: string;
17
18
  type: "N:N";
@@ -24,6 +25,7 @@ export interface NormalizedManyToManyRelationDefinition {
24
25
  toRelation: string;
25
26
  };
26
27
  }
28
+
27
29
  export interface NormalizedManyToOneRelationDefinition {
28
30
  name: string;
29
31
  type: "N:1";
@@ -39,4 +41,8 @@ export interface NormalizedManyToOneRelationDefinition {
39
41
  columns: string[];
40
42
  };
41
43
  }
42
- export type NormalizedRelationDefinition = NormalizedOneToManyRelationDefinition | NormalizedManyToManyRelationDefinition | NormalizedManyToOneRelationDefinition;
44
+
45
+ export type NormalizedRelationDefinition =
46
+ | NormalizedOneToManyRelationDefinition
47
+ | NormalizedManyToManyRelationDefinition
48
+ | NormalizedManyToOneRelationDefinition;
@@ -1,4 +1,5 @@
1
1
  import { SQLStatement } from "@casekit/sql";
2
+
2
3
  export interface NormalizedUniqueConstraintDefinition {
3
4
  name: string;
4
5
  fields: string[];
@@ -1,4 +1,5 @@
1
1
  import { FieldDefinition } from "@casekit/orm2-schema";
2
+
2
3
  export type PopulatedFieldDefinition = Required<FieldDefinition> & {
3
4
  name: string;
4
5
  };
@@ -1,5 +1,7 @@
1
1
  import { ModelDefinition } from "@casekit/orm2-schema";
2
+
2
3
  import { PopulatedFieldDefinition } from "./PopulatedFieldDefinition.js";
4
+
3
5
  export type PopulatedModelDefinition = Required<ModelDefinition> & {
4
6
  name: string;
5
7
  fields: Record<string, PopulatedFieldDefinition>;
package/src/util.ts ADDED
@@ -0,0 +1,38 @@
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
+ };
package/build/index.js DELETED
@@ -1,4 +0,0 @@
1
- export { normalizeConfig } from "./normalize/normalizeConfig.js";
2
- export { normalizeModel } from "./normalize/normalizeModel.js";
3
- export { normalizeUniqueConstraint } from "./normalize/normalizeUniqueConstraints.js";
4
- export { getField, getModel, getRelation } from "./util.js";
@@ -1,8 +0,0 @@
1
- import { z } from "zod";
2
- /**
3
- * WARNING!!! The schemas in this file must be kept in sync
4
- * with DefaultFieldType in packages/orm-schema/src/helper/DefaultFieldType.ts.
5
- * If you make a change here, make sure to update the
6
- * corresponding type.
7
- */
8
- export declare const defaultZodSchema: (type: string) => z.ZodSchema<unknown>;
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- import { PopulatedModelDefinition } from "#types/PopulatedModelDefinition.js";
2
- export declare const getColumns: (model: PopulatedModelDefinition, fields: string[]) => string[];
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import { Config } from "@casekit/orm2-schema";
2
- import { NormalizedConfig } from "#types/NormalizedConfig.js";
3
- export declare const normalizeConfig: (config: Config) => NormalizedConfig;
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import { NormalizedFieldDefinition } from "#types/NormalizedFieldDefinition.js";
2
- import { PopulatedFieldDefinition } from "#types/PopulatedFieldDefinition.js";
3
- export declare const normalizeField: (field: PopulatedFieldDefinition) => NormalizedFieldDefinition;
@@ -1,11 +0,0 @@
1
- export const normalizeField = (field) => {
2
- return {
3
- name: field.name,
4
- column: field.column,
5
- type: field.type,
6
- zodSchema: field.zodSchema,
7
- nullable: field.nullable,
8
- default: field.default,
9
- provided: field.provided,
10
- };
11
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,5 +0,0 @@
1
- import { ForeignKeyDefinition } from "@casekit/orm2-schema";
2
- import { NormalizedForeignKeyDefinition } from "#types/NormalizedForeignKeyDefinition.js";
3
- import { PopulatedModelDefinition } from "#types/PopulatedModelDefinition.js";
4
- export declare const normalizeForeignKeys: (models: Record<string, PopulatedModelDefinition>, model: PopulatedModelDefinition) => NormalizedForeignKeyDefinition[];
5
- export declare const normalizeForeignKey: (models: Record<string, PopulatedModelDefinition>, model: PopulatedModelDefinition, fk: ForeignKeyDefinition) => NormalizedForeignKeyDefinition;
@@ -1,50 +0,0 @@
1
- import { isEqual } from "es-toolkit";
2
- import { getColumns } from "./getColumns.js";
3
- export const normalizeForeignKeys = (models, model) => {
4
- const columnLevelForeignKeys = Object.values(model.fields)
5
- .filter(hasReference)
6
- .map(referenceToForeignKey)
7
- .map((fk) => normalizeForeignKey(models, model, fk));
8
- const modelLevelForeignKeys = model.foreignKeys.map((fk) => normalizeForeignKey(models, model, fk));
9
- for (const fk of columnLevelForeignKeys) {
10
- if (modelLevelForeignKeys.some((other) => isEqual(fk.columns, other.columns))) {
11
- throw new Error(`Duplicate foreign key defined in model "${model.name}"`);
12
- }
13
- }
14
- return [...columnLevelForeignKeys, ...modelLevelForeignKeys];
15
- };
16
- export const normalizeForeignKey = (models, model, fk) => {
17
- const referencedModel = models[fk.references.model];
18
- if (!referencedModel) {
19
- throw new Error(`Referenced model "${fk.references.model}" not found in models`);
20
- }
21
- const columns = getColumns(model, fk.fields);
22
- return {
23
- name: fk.name ?? [model.table, ...columns, "fkey"].join("_"),
24
- fields: fk.fields,
25
- columns: columns,
26
- references: {
27
- model: fk.references.model,
28
- fields: fk.references.fields,
29
- schema: referencedModel.schema,
30
- table: referencedModel.table,
31
- columns: getColumns(referencedModel, fk.references.fields),
32
- },
33
- onUpdate: fk.onUpdate ?? null,
34
- onDelete: fk.onDelete ?? null,
35
- };
36
- };
37
- const referenceToForeignKey = (field) => {
38
- return {
39
- fields: [field.name],
40
- references: {
41
- model: field.references.model,
42
- fields: [field.references.field],
43
- },
44
- onUpdate: field.references.onUpdate,
45
- onDelete: field.references.onDelete,
46
- };
47
- };
48
- const hasReference = (field) => {
49
- return !!field.references;
50
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import { NormalizedModelDefinition } from "#types/NormalizedModelDefinition.js";
2
- import { PopulatedModelDefinition } from "#types/PopulatedModelDefinition.js";
3
- export declare const normalizeModel: (models: Record<string, PopulatedModelDefinition>, model: PopulatedModelDefinition) => NormalizedModelDefinition;
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import { NormalizedPrimaryKey } from "#types/NormalizedPrimaryKey.js";
2
- import { PopulatedModelDefinition } from "#types/PopulatedModelDefinition.js";
3
- export declare const normalizePrimaryKey: (model: PopulatedModelDefinition) => NormalizedPrimaryKey[];
@@ -1,18 +0,0 @@
1
- export const normalizePrimaryKey = (model) => {
2
- const fieldLevelPrimaryKey = Object.entries(model.fields)
3
- .filter(([, field]) => field.primaryKey)
4
- .map(([name]) => name);
5
- if (model.primaryKey && fieldLevelPrimaryKey.length > 0) {
6
- throw new Error(`Model "${model.name}" has primary key fields defined at both the model and field levels.`);
7
- }
8
- const fields = model.primaryKey ?? fieldLevelPrimaryKey;
9
- return fields.map((name) => {
10
- if (!model.fields[name]) {
11
- throw new Error(`Primary key field "${name}" does not exist in model "${model.name}".`);
12
- }
13
- return {
14
- field: name,
15
- column: model.fields[name].column,
16
- };
17
- });
18
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import { NormalizedRelationDefinition } from "#types/NormalizedRelationDefinition.js";
2
- import { PopulatedModelDefinition } from "#types/PopulatedModelDefinition.js";
3
- export declare const normalizeRelations: (models: Record<string, PopulatedModelDefinition>, model: PopulatedModelDefinition) => Record<string, NormalizedRelationDefinition>;
@@ -1 +0,0 @@
1
- export {};
@@ -1,5 +0,0 @@
1
- import { UniqueConstraintDefinition } from "@casekit/orm2-schema";
2
- import { NormalizedUniqueConstraintDefinition } from "#types/NormalizedUniqueConstraintDefinition.js";
3
- import { PopulatedModelDefinition } from "#types/PopulatedModelDefinition.js";
4
- export declare const normalizeUniqueConstraint: (model: PopulatedModelDefinition, constraint: UniqueConstraintDefinition) => NormalizedUniqueConstraintDefinition;
5
- export declare const normalizeUniqueConstraints: (model: PopulatedModelDefinition) => NormalizedUniqueConstraintDefinition[];
@@ -1,29 +0,0 @@
1
- import { getColumns } from "./getColumns.js";
2
- export const normalizeUniqueConstraint = (model, constraint) => {
3
- const columns = getColumns(model, constraint.fields);
4
- return {
5
- name: constraint.name ?? [model.table, ...columns, "ukey"].join("_"),
6
- fields: constraint.fields,
7
- columns,
8
- where: constraint.where ?? null,
9
- nullsNotDistinct: constraint.nullsNotDistinct ?? false,
10
- };
11
- };
12
- export const normalizeUniqueConstraints = (model) => {
13
- const columnLevelUniqueConstraints = Object.values(model.fields)
14
- .filter((field) => field.unique)
15
- .map((field) => normalizeUniqueConstraint(model, typeof field.unique === "boolean"
16
- ? {
17
- fields: [field.name],
18
- where: null,
19
- nullsNotDistinct: false,
20
- }
21
- : { fields: [field.name], ...field.unique }));
22
- const modelLevelUniqueConstraints = model.uniqueConstraints.map((constraint) => normalizeUniqueConstraint(model, constraint));
23
- for (const constraint of columnLevelUniqueConstraints) {
24
- if (modelLevelUniqueConstraints.some((other) => constraint.fields.every((field) => other.fields.includes(field)))) {
25
- throw new Error(`Duplicate unique constraint defined in model "${model.name}"`);
26
- }
27
- }
28
- return [...columnLevelUniqueConstraints, ...modelLevelUniqueConstraints];
29
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import { Config, FieldDefinition } from "@casekit/orm2-schema";
2
- import { PopulatedFieldDefinition } from "#types/PopulatedFieldDefinition.js";
3
- export declare const populateField: (config: Config, definition: FieldDefinition, name: string) => PopulatedFieldDefinition;
@@ -1 +0,0 @@
1
- export {};