@casekit/orm2-cli 0.0.0-20250331202540 → 1.0.0
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/build/cli.js +1 -2
- package/build/commands/db-drop/handler.js +0 -3
- package/build/commands/db-drop.test.js +1 -0
- package/build/commands/db-pull/handler.js +21 -8
- package/build/commands/db-pull/options.d.ts +5 -0
- package/build/commands/db-pull/options.js +5 -0
- package/build/commands/db-pull/util/relationNames.d.ts +3 -0
- package/build/commands/db-pull/util/relationNames.js +14 -0
- package/build/commands/db-pull/util/relationNames.test.js +61 -0
- package/build/commands/db-pull/util/renderDefault.d.ts +3 -0
- package/build/commands/db-pull/util/renderDefault.js +46 -0
- package/build/commands/db-pull/util/renderDefault.test.d.ts +1 -0
- package/build/commands/db-pull/util/renderDefault.test.js +67 -0
- package/build/commands/db-pull/util/renderFieldDefinition.d.ts +2 -0
- package/build/commands/db-pull/util/renderFieldDefinition.js +50 -0
- package/build/commands/db-pull/util/renderFieldDefinition.test.d.ts +1 -0
- package/build/commands/db-pull/util/renderFieldDefinition.test.js +182 -0
- package/build/commands/db-pull/util/renderModel.basic.test.d.ts +1 -0
- package/build/commands/db-pull/util/renderModel.basic.test.js +169 -0
- package/build/commands/db-pull/util/renderModel.constraints.test.d.ts +1 -0
- package/build/commands/db-pull/util/renderModel.constraints.test.js +677 -0
- package/build/commands/db-pull/util/renderModel.d.ts +2 -0
- package/build/commands/db-pull/util/renderModel.defaultValues.test.d.ts +1 -0
- package/build/commands/db-pull/util/renderModel.defaultValues.test.js +518 -0
- package/build/commands/db-pull/util/renderModel.js +88 -0
- package/build/commands/db-pull/util/renderModel.relations.test.d.ts +1 -0
- package/build/commands/db-pull/util/renderModel.relations.test.js +880 -0
- package/build/commands/db-pull/util/renderModel.types.test.d.ts +1 -0
- package/build/commands/db-pull/util/renderModel.types.test.js +703 -0
- package/build/commands/db-pull/util/renderRelations.d.ts +2 -0
- package/build/commands/db-pull/util/renderRelations.js +55 -0
- package/build/commands/db-pull/util/renderRelations.test.d.ts +1 -0
- package/build/commands/db-pull/util/renderRelations.test.js +165 -0
- package/build/commands/db-pull/util/renderType.d.ts +6 -0
- package/build/commands/db-pull/util/renderType.js +55 -0
- package/build/commands/db-pull/util/renderType.test.d.ts +1 -0
- package/build/commands/db-pull/util/renderType.test.js +46 -0
- package/build/commands/db-pull.d.ts +10 -0
- package/build/commands/db-pull.test.js +625 -0
- package/build/commands/db-push/handler.js +0 -3
- package/build/commands/init/handler.js +16 -3
- package/build/commands/init/util/generateConfigFile.js +2 -3
- package/build/commands/init/util/generateDbFile.js +13 -24
- package/build/commands/init/util/generateModelsFile.d.ts +1 -1
- package/build/commands/init/util/generateModelsFile.js +6 -2
- package/build/commands/init.test.js +19 -31
- package/build/types.d.ts +2 -2
- package/build/util/createOrOverwriteFile.d.ts +1 -1
- package/build/util/createOrOverwriteFile.js +1 -1
- package/build/util/loadConfig.js +5 -1
- package/package.json +26 -25
- package/build/commands/generate-model/handler.d.ts +0 -3
- package/build/commands/generate-model/handler.js +0 -10
- package/build/commands/generate-model/options.d.ts +0 -18
- package/build/commands/generate-model/options.js +0 -18
- package/build/commands/generate-model/util/generateModelFile.d.ts +0 -3
- package/build/commands/generate-model/util/generateModelFile.js +0 -59
- package/build/commands/generate-model/util/generateModelFile.test.js +0 -67
- package/build/commands/generate-model/util/regenerateModelsFile.d.ts +0 -2
- package/build/commands/generate-model/util/regenerateModelsFile.js +0 -50
- package/build/commands/generate-model.d.ts +0 -40
- package/build/commands/generate-model.js +0 -8
- package/build/commands/generate-model.test.js +0 -55
- /package/build/commands/{generate-model/util/generateModelFile.test.d.ts → db-pull/util/relationNames.test.d.ts} +0 -0
- /package/build/commands/{generate-model.test.d.ts → db-pull.test.d.ts} +0 -0
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { unindent } from "@casekit/unindent";
|
|
3
|
+
import { renderModel } from "./renderModel.js";
|
|
4
|
+
describe("renderModel - default values", () => {
|
|
5
|
+
it("handles numeric defaults", async () => {
|
|
6
|
+
const table = {
|
|
7
|
+
schema: "public",
|
|
8
|
+
name: "products",
|
|
9
|
+
columns: [
|
|
10
|
+
{
|
|
11
|
+
schema: "public",
|
|
12
|
+
table: "products",
|
|
13
|
+
column: "id",
|
|
14
|
+
ordinalPosition: 1,
|
|
15
|
+
type: "integer",
|
|
16
|
+
default: null,
|
|
17
|
+
nullable: false,
|
|
18
|
+
udtSchema: "pg_catalog",
|
|
19
|
+
udt: "int4",
|
|
20
|
+
elementType: null,
|
|
21
|
+
elementTypeSchema: null,
|
|
22
|
+
cardinality: 0,
|
|
23
|
+
size: null,
|
|
24
|
+
isSerial: false,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
schema: "public",
|
|
28
|
+
table: "products",
|
|
29
|
+
column: "price",
|
|
30
|
+
ordinalPosition: 2,
|
|
31
|
+
type: "numeric",
|
|
32
|
+
default: "0",
|
|
33
|
+
nullable: false,
|
|
34
|
+
udtSchema: "pg_catalog",
|
|
35
|
+
udt: "numeric",
|
|
36
|
+
elementType: null,
|
|
37
|
+
elementTypeSchema: null,
|
|
38
|
+
cardinality: 0,
|
|
39
|
+
size: null,
|
|
40
|
+
isSerial: false,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
schema: "public",
|
|
44
|
+
table: "products",
|
|
45
|
+
column: "discount",
|
|
46
|
+
ordinalPosition: 3,
|
|
47
|
+
type: "numeric",
|
|
48
|
+
default: "0.15",
|
|
49
|
+
nullable: false,
|
|
50
|
+
udtSchema: "pg_catalog",
|
|
51
|
+
udt: "numeric",
|
|
52
|
+
elementType: null,
|
|
53
|
+
elementTypeSchema: null,
|
|
54
|
+
cardinality: 0,
|
|
55
|
+
size: null,
|
|
56
|
+
isSerial: false,
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
foreignKeys: [],
|
|
60
|
+
primaryKey: null,
|
|
61
|
+
uniqueConstraints: [],
|
|
62
|
+
};
|
|
63
|
+
const result = await renderModel(table, []);
|
|
64
|
+
expect(result.trim()).toBe(unindent `
|
|
65
|
+
import { type ModelDefinition } from "@casekit/orm2";
|
|
66
|
+
|
|
67
|
+
export const products = {
|
|
68
|
+
fields: {
|
|
69
|
+
id: { type: "integer" },
|
|
70
|
+
price: { type: "numeric", default: 0 },
|
|
71
|
+
discount: { type: "numeric", default: 0.15 },
|
|
72
|
+
},
|
|
73
|
+
} as const satisfies ModelDefinition;
|
|
74
|
+
`);
|
|
75
|
+
});
|
|
76
|
+
it("handles boolean defaults", async () => {
|
|
77
|
+
const table = {
|
|
78
|
+
schema: "public",
|
|
79
|
+
name: "settings",
|
|
80
|
+
columns: [
|
|
81
|
+
{
|
|
82
|
+
schema: "public",
|
|
83
|
+
table: "settings",
|
|
84
|
+
column: "id",
|
|
85
|
+
ordinalPosition: 1,
|
|
86
|
+
type: "integer",
|
|
87
|
+
default: null,
|
|
88
|
+
nullable: false,
|
|
89
|
+
udtSchema: "pg_catalog",
|
|
90
|
+
udt: "int4",
|
|
91
|
+
elementType: null,
|
|
92
|
+
elementTypeSchema: null,
|
|
93
|
+
cardinality: 0,
|
|
94
|
+
size: null,
|
|
95
|
+
isSerial: false,
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
schema: "public",
|
|
99
|
+
table: "settings",
|
|
100
|
+
column: "is_active",
|
|
101
|
+
ordinalPosition: 2,
|
|
102
|
+
type: "boolean",
|
|
103
|
+
default: "true",
|
|
104
|
+
nullable: false,
|
|
105
|
+
udtSchema: "pg_catalog",
|
|
106
|
+
udt: "bool",
|
|
107
|
+
elementType: null,
|
|
108
|
+
elementTypeSchema: null,
|
|
109
|
+
cardinality: 0,
|
|
110
|
+
size: null,
|
|
111
|
+
isSerial: false,
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
schema: "public",
|
|
115
|
+
table: "settings",
|
|
116
|
+
column: "is_deleted",
|
|
117
|
+
ordinalPosition: 3,
|
|
118
|
+
type: "boolean",
|
|
119
|
+
default: "false",
|
|
120
|
+
nullable: false,
|
|
121
|
+
udtSchema: "pg_catalog",
|
|
122
|
+
udt: "bool",
|
|
123
|
+
elementType: null,
|
|
124
|
+
elementTypeSchema: null,
|
|
125
|
+
cardinality: 0,
|
|
126
|
+
size: null,
|
|
127
|
+
isSerial: false,
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
foreignKeys: [],
|
|
131
|
+
primaryKey: null,
|
|
132
|
+
uniqueConstraints: [],
|
|
133
|
+
};
|
|
134
|
+
const result = await renderModel(table, []);
|
|
135
|
+
expect(result.trim()).toBe(unindent `
|
|
136
|
+
import { type ModelDefinition } from "@casekit/orm2";
|
|
137
|
+
|
|
138
|
+
export const settings = {
|
|
139
|
+
fields: {
|
|
140
|
+
id: { type: "integer" },
|
|
141
|
+
isActive: { column: "is_active", type: "boolean", default: true },
|
|
142
|
+
isDeleted: { column: "is_deleted", type: "boolean", default: false },
|
|
143
|
+
},
|
|
144
|
+
} as const satisfies ModelDefinition;
|
|
145
|
+
`);
|
|
146
|
+
});
|
|
147
|
+
it("handles string literal defaults", async () => {
|
|
148
|
+
const table = {
|
|
149
|
+
schema: "public",
|
|
150
|
+
name: "users",
|
|
151
|
+
columns: [
|
|
152
|
+
{
|
|
153
|
+
schema: "public",
|
|
154
|
+
table: "users",
|
|
155
|
+
column: "id",
|
|
156
|
+
ordinalPosition: 1,
|
|
157
|
+
type: "integer",
|
|
158
|
+
default: null,
|
|
159
|
+
nullable: false,
|
|
160
|
+
udtSchema: "pg_catalog",
|
|
161
|
+
udt: "int4",
|
|
162
|
+
elementType: null,
|
|
163
|
+
elementTypeSchema: null,
|
|
164
|
+
cardinality: 0,
|
|
165
|
+
size: null,
|
|
166
|
+
isSerial: false,
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
schema: "public",
|
|
170
|
+
table: "users",
|
|
171
|
+
column: "status",
|
|
172
|
+
ordinalPosition: 2,
|
|
173
|
+
type: "text",
|
|
174
|
+
default: "'active'::text",
|
|
175
|
+
nullable: false,
|
|
176
|
+
udtSchema: "pg_catalog",
|
|
177
|
+
udt: "text",
|
|
178
|
+
elementType: null,
|
|
179
|
+
elementTypeSchema: null,
|
|
180
|
+
cardinality: 0,
|
|
181
|
+
size: null,
|
|
182
|
+
isSerial: false,
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
schema: "public",
|
|
186
|
+
table: "users",
|
|
187
|
+
column: "role",
|
|
188
|
+
ordinalPosition: 3,
|
|
189
|
+
type: "text",
|
|
190
|
+
default: "'user'::text",
|
|
191
|
+
nullable: false,
|
|
192
|
+
udtSchema: "pg_catalog",
|
|
193
|
+
udt: "text",
|
|
194
|
+
elementType: null,
|
|
195
|
+
elementTypeSchema: null,
|
|
196
|
+
cardinality: 0,
|
|
197
|
+
size: null,
|
|
198
|
+
isSerial: false,
|
|
199
|
+
},
|
|
200
|
+
],
|
|
201
|
+
foreignKeys: [],
|
|
202
|
+
primaryKey: null,
|
|
203
|
+
uniqueConstraints: [],
|
|
204
|
+
};
|
|
205
|
+
const result = await renderModel(table, []);
|
|
206
|
+
expect(result.trim()).toBe(unindent `
|
|
207
|
+
import { type ModelDefinition } from "@casekit/orm2";
|
|
208
|
+
|
|
209
|
+
export const users = {
|
|
210
|
+
fields: {
|
|
211
|
+
id: { type: "integer" },
|
|
212
|
+
status: { type: "text", default: "active" },
|
|
213
|
+
role: { type: "text", default: "user" },
|
|
214
|
+
},
|
|
215
|
+
} as const satisfies ModelDefinition;
|
|
216
|
+
`);
|
|
217
|
+
});
|
|
218
|
+
it("handles SQL function defaults", async () => {
|
|
219
|
+
const table = {
|
|
220
|
+
schema: "public",
|
|
221
|
+
name: "events",
|
|
222
|
+
columns: [
|
|
223
|
+
{
|
|
224
|
+
schema: "public",
|
|
225
|
+
table: "events",
|
|
226
|
+
column: "id",
|
|
227
|
+
ordinalPosition: 1,
|
|
228
|
+
type: "uuid",
|
|
229
|
+
default: "gen_random_uuid()",
|
|
230
|
+
nullable: false,
|
|
231
|
+
udtSchema: "pg_catalog",
|
|
232
|
+
udt: "uuid",
|
|
233
|
+
elementType: null,
|
|
234
|
+
elementTypeSchema: null,
|
|
235
|
+
cardinality: 0,
|
|
236
|
+
size: null,
|
|
237
|
+
isSerial: false,
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
schema: "public",
|
|
241
|
+
table: "events",
|
|
242
|
+
column: "created_at",
|
|
243
|
+
ordinalPosition: 2,
|
|
244
|
+
type: "timestamp",
|
|
245
|
+
default: "now()",
|
|
246
|
+
nullable: false,
|
|
247
|
+
udtSchema: "pg_catalog",
|
|
248
|
+
udt: "timestamp",
|
|
249
|
+
elementType: null,
|
|
250
|
+
elementTypeSchema: null,
|
|
251
|
+
cardinality: 0,
|
|
252
|
+
size: null,
|
|
253
|
+
isSerial: false,
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
schema: "public",
|
|
257
|
+
table: "events",
|
|
258
|
+
column: "event_date",
|
|
259
|
+
ordinalPosition: 3,
|
|
260
|
+
type: "date",
|
|
261
|
+
default: "CURRENT_DATE",
|
|
262
|
+
nullable: false,
|
|
263
|
+
udtSchema: "pg_catalog",
|
|
264
|
+
udt: "date",
|
|
265
|
+
elementType: null,
|
|
266
|
+
elementTypeSchema: null,
|
|
267
|
+
cardinality: 0,
|
|
268
|
+
size: null,
|
|
269
|
+
isSerial: false,
|
|
270
|
+
},
|
|
271
|
+
],
|
|
272
|
+
foreignKeys: [],
|
|
273
|
+
primaryKey: null,
|
|
274
|
+
uniqueConstraints: [],
|
|
275
|
+
};
|
|
276
|
+
const result = await renderModel(table, []);
|
|
277
|
+
expect(result.trim()).toBe(unindent `
|
|
278
|
+
import { type ModelDefinition, sql } from "@casekit/orm2";
|
|
279
|
+
|
|
280
|
+
export const events = {
|
|
281
|
+
fields: {
|
|
282
|
+
id: { type: "uuid", default: sql\`gen_random_uuid()\` },
|
|
283
|
+
createdAt: {
|
|
284
|
+
column: "created_at",
|
|
285
|
+
type: "timestamp",
|
|
286
|
+
default: sql\`now()\`,
|
|
287
|
+
},
|
|
288
|
+
eventDate: {
|
|
289
|
+
column: "event_date",
|
|
290
|
+
type: "date",
|
|
291
|
+
default: sql\`CURRENT_DATE\`,
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
} as const satisfies ModelDefinition;
|
|
295
|
+
`);
|
|
296
|
+
});
|
|
297
|
+
it("handles serial columns with nextval defaults", async () => {
|
|
298
|
+
const table = {
|
|
299
|
+
schema: "public",
|
|
300
|
+
name: "posts",
|
|
301
|
+
columns: [
|
|
302
|
+
{
|
|
303
|
+
schema: "public",
|
|
304
|
+
table: "posts",
|
|
305
|
+
column: "id",
|
|
306
|
+
ordinalPosition: 1,
|
|
307
|
+
type: "integer",
|
|
308
|
+
default: "nextval('posts_id_seq'::regclass)",
|
|
309
|
+
nullable: false,
|
|
310
|
+
udtSchema: "pg_catalog",
|
|
311
|
+
udt: "int4",
|
|
312
|
+
elementType: null,
|
|
313
|
+
elementTypeSchema: null,
|
|
314
|
+
cardinality: 0,
|
|
315
|
+
size: null,
|
|
316
|
+
isSerial: false,
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
schema: "public",
|
|
320
|
+
table: "posts",
|
|
321
|
+
column: "sequence_num",
|
|
322
|
+
ordinalPosition: 2,
|
|
323
|
+
type: "bigint",
|
|
324
|
+
default: "nextval('posts_sequence_num_seq'::regclass)",
|
|
325
|
+
nullable: false,
|
|
326
|
+
udtSchema: "pg_catalog",
|
|
327
|
+
udt: "int8",
|
|
328
|
+
elementType: null,
|
|
329
|
+
elementTypeSchema: null,
|
|
330
|
+
cardinality: 0,
|
|
331
|
+
size: null,
|
|
332
|
+
isSerial: false,
|
|
333
|
+
},
|
|
334
|
+
],
|
|
335
|
+
foreignKeys: [],
|
|
336
|
+
primaryKey: null,
|
|
337
|
+
uniqueConstraints: [],
|
|
338
|
+
};
|
|
339
|
+
const result = await renderModel(table, []);
|
|
340
|
+
expect(result.trim()).toBe(unindent `
|
|
341
|
+
import { type ModelDefinition, sql } from "@casekit/orm2";
|
|
342
|
+
|
|
343
|
+
export const posts = {
|
|
344
|
+
fields: {
|
|
345
|
+
id: {
|
|
346
|
+
type: "integer",
|
|
347
|
+
default: sql\`nextval('posts_id_seq'::regclass)\`,
|
|
348
|
+
},
|
|
349
|
+
sequenceNum: {
|
|
350
|
+
column: "sequence_num",
|
|
351
|
+
type: "bigint",
|
|
352
|
+
default: sql\`nextval('posts_sequence_num_seq'::regclass)\`,
|
|
353
|
+
},
|
|
354
|
+
},
|
|
355
|
+
} as const satisfies ModelDefinition;
|
|
356
|
+
`);
|
|
357
|
+
});
|
|
358
|
+
it("handles array and JSON defaults", async () => {
|
|
359
|
+
const table = {
|
|
360
|
+
schema: "public",
|
|
361
|
+
name: "configs",
|
|
362
|
+
columns: [
|
|
363
|
+
{
|
|
364
|
+
schema: "public",
|
|
365
|
+
table: "configs",
|
|
366
|
+
column: "id",
|
|
367
|
+
ordinalPosition: 1,
|
|
368
|
+
type: "integer",
|
|
369
|
+
default: null,
|
|
370
|
+
nullable: false,
|
|
371
|
+
udtSchema: "pg_catalog",
|
|
372
|
+
udt: "int4",
|
|
373
|
+
elementType: null,
|
|
374
|
+
elementTypeSchema: null,
|
|
375
|
+
cardinality: 0,
|
|
376
|
+
size: null,
|
|
377
|
+
isSerial: false,
|
|
378
|
+
},
|
|
379
|
+
{
|
|
380
|
+
schema: "public",
|
|
381
|
+
table: "configs",
|
|
382
|
+
column: "tags",
|
|
383
|
+
ordinalPosition: 2,
|
|
384
|
+
type: "ARRAY",
|
|
385
|
+
default: "'[]'::text[]",
|
|
386
|
+
nullable: false,
|
|
387
|
+
udtSchema: "pg_catalog",
|
|
388
|
+
udt: "_text",
|
|
389
|
+
elementType: "text",
|
|
390
|
+
elementTypeSchema: "pg_catalog",
|
|
391
|
+
cardinality: 1,
|
|
392
|
+
size: null,
|
|
393
|
+
isSerial: false,
|
|
394
|
+
},
|
|
395
|
+
{
|
|
396
|
+
schema: "public",
|
|
397
|
+
table: "configs",
|
|
398
|
+
column: "metadata",
|
|
399
|
+
ordinalPosition: 3,
|
|
400
|
+
type: "jsonb",
|
|
401
|
+
default: "'{}'::jsonb",
|
|
402
|
+
nullable: false,
|
|
403
|
+
udtSchema: "pg_catalog",
|
|
404
|
+
udt: "jsonb",
|
|
405
|
+
elementType: null,
|
|
406
|
+
elementTypeSchema: null,
|
|
407
|
+
cardinality: 0,
|
|
408
|
+
size: null,
|
|
409
|
+
isSerial: false,
|
|
410
|
+
},
|
|
411
|
+
{
|
|
412
|
+
schema: "public",
|
|
413
|
+
table: "configs",
|
|
414
|
+
column: "settings",
|
|
415
|
+
ordinalPosition: 4,
|
|
416
|
+
type: "json",
|
|
417
|
+
default: "'null'::json",
|
|
418
|
+
nullable: false,
|
|
419
|
+
udtSchema: "pg_catalog",
|
|
420
|
+
udt: "json",
|
|
421
|
+
elementType: null,
|
|
422
|
+
elementTypeSchema: null,
|
|
423
|
+
cardinality: 0,
|
|
424
|
+
size: null,
|
|
425
|
+
isSerial: false,
|
|
426
|
+
},
|
|
427
|
+
],
|
|
428
|
+
foreignKeys: [],
|
|
429
|
+
primaryKey: null,
|
|
430
|
+
uniqueConstraints: [],
|
|
431
|
+
};
|
|
432
|
+
const result = await renderModel(table, []);
|
|
433
|
+
expect(result.trim()).toBe(unindent `
|
|
434
|
+
import { type ModelDefinition, sql } from "@casekit/orm2";
|
|
435
|
+
|
|
436
|
+
export const configs = {
|
|
437
|
+
fields: {
|
|
438
|
+
id: { type: "integer" },
|
|
439
|
+
tags: { type: "text[]", default: sql\`'[]'::text[]\` },
|
|
440
|
+
metadata: { type: "jsonb", default: sql\`'{}'::jsonb\` },
|
|
441
|
+
settings: { type: "json", default: sql\`'null'::json\` },
|
|
442
|
+
},
|
|
443
|
+
} as const satisfies ModelDefinition;
|
|
444
|
+
`);
|
|
445
|
+
});
|
|
446
|
+
it("imports sql only when needed for defaults", async () => {
|
|
447
|
+
const table = {
|
|
448
|
+
schema: "public",
|
|
449
|
+
name: "simple",
|
|
450
|
+
columns: [
|
|
451
|
+
{
|
|
452
|
+
schema: "public",
|
|
453
|
+
table: "simple",
|
|
454
|
+
column: "id",
|
|
455
|
+
ordinalPosition: 1,
|
|
456
|
+
type: "integer",
|
|
457
|
+
default: "42",
|
|
458
|
+
nullable: false,
|
|
459
|
+
udtSchema: "pg_catalog",
|
|
460
|
+
udt: "int4",
|
|
461
|
+
elementType: null,
|
|
462
|
+
elementTypeSchema: null,
|
|
463
|
+
cardinality: 0,
|
|
464
|
+
size: null,
|
|
465
|
+
isSerial: false,
|
|
466
|
+
},
|
|
467
|
+
{
|
|
468
|
+
schema: "public",
|
|
469
|
+
table: "simple",
|
|
470
|
+
column: "active",
|
|
471
|
+
ordinalPosition: 2,
|
|
472
|
+
type: "boolean",
|
|
473
|
+
default: "true",
|
|
474
|
+
nullable: false,
|
|
475
|
+
udtSchema: "pg_catalog",
|
|
476
|
+
udt: "bool",
|
|
477
|
+
elementType: null,
|
|
478
|
+
elementTypeSchema: null,
|
|
479
|
+
cardinality: 0,
|
|
480
|
+
size: null,
|
|
481
|
+
isSerial: false,
|
|
482
|
+
},
|
|
483
|
+
{
|
|
484
|
+
schema: "public",
|
|
485
|
+
table: "simple",
|
|
486
|
+
column: "name",
|
|
487
|
+
ordinalPosition: 3,
|
|
488
|
+
type: "text",
|
|
489
|
+
default: null,
|
|
490
|
+
nullable: true,
|
|
491
|
+
udtSchema: "pg_catalog",
|
|
492
|
+
udt: "text",
|
|
493
|
+
elementType: null,
|
|
494
|
+
elementTypeSchema: null,
|
|
495
|
+
cardinality: 0,
|
|
496
|
+
size: null,
|
|
497
|
+
isSerial: false,
|
|
498
|
+
},
|
|
499
|
+
],
|
|
500
|
+
foreignKeys: [],
|
|
501
|
+
primaryKey: null,
|
|
502
|
+
uniqueConstraints: [],
|
|
503
|
+
};
|
|
504
|
+
const result = await renderModel(table, []);
|
|
505
|
+
// Note: no sql import since all defaults are simple values
|
|
506
|
+
expect(result.trim()).toBe(unindent `
|
|
507
|
+
import { type ModelDefinition } from "@casekit/orm2";
|
|
508
|
+
|
|
509
|
+
export const simple = {
|
|
510
|
+
fields: {
|
|
511
|
+
id: { type: "integer", default: 42 },
|
|
512
|
+
active: { type: "boolean", default: true },
|
|
513
|
+
name: { type: "text", nullable: true },
|
|
514
|
+
},
|
|
515
|
+
} as const satisfies ModelDefinition;
|
|
516
|
+
`);
|
|
517
|
+
});
|
|
518
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { camelCase } from "es-toolkit/string";
|
|
2
|
+
import { prettify } from "#util/prettify.js";
|
|
3
|
+
import { renderFieldDefinition } from "./renderFieldDefinition.js";
|
|
4
|
+
import { renderRelations } from "./renderRelations.js";
|
|
5
|
+
export const renderModel = async (table, allTables) => {
|
|
6
|
+
// Render fields
|
|
7
|
+
const fields = table.columns
|
|
8
|
+
.map((column) => {
|
|
9
|
+
const fieldName = camelCase(column.column);
|
|
10
|
+
const definition = renderFieldDefinition(column, table);
|
|
11
|
+
return ` ${fieldName}: ${definition}`;
|
|
12
|
+
})
|
|
13
|
+
.join(",\n");
|
|
14
|
+
// Render multi-column primary key
|
|
15
|
+
let primaryKeyDef = "";
|
|
16
|
+
if (table.primaryKey && table.primaryKey.columns.length > 1) {
|
|
17
|
+
const pkColumns = table.primaryKey.columns.map((col) => `"${camelCase(col)}"`);
|
|
18
|
+
primaryKeyDef = `\n primaryKey: [${pkColumns.join(", ")}],`;
|
|
19
|
+
}
|
|
20
|
+
// Render multi-column unique constraints
|
|
21
|
+
let uniqueConstraintsDef = "";
|
|
22
|
+
const multiColumnUniqueConstraints = table.uniqueConstraints.filter((uc) => {
|
|
23
|
+
// Parse columns from definition - this is a bit hacky but matches legacy
|
|
24
|
+
const match = /\(([^)]+)\)/.exec(uc.definition);
|
|
25
|
+
if (!match?.[1])
|
|
26
|
+
return false;
|
|
27
|
+
const columns = match[1].split(",").map((c) => c.trim());
|
|
28
|
+
return columns.length > 1;
|
|
29
|
+
});
|
|
30
|
+
if (multiColumnUniqueConstraints.length > 0) {
|
|
31
|
+
const constraints = multiColumnUniqueConstraints.map((uc) => {
|
|
32
|
+
const match = /\(([^)]+)\)/.exec(uc.definition);
|
|
33
|
+
const columns = match?.[1]
|
|
34
|
+
? match[1].split(",").map((c) => c.trim())
|
|
35
|
+
: [];
|
|
36
|
+
const fields = columns
|
|
37
|
+
.map((c) => `"${camelCase(c)}"`)
|
|
38
|
+
.join(", ");
|
|
39
|
+
const hasNullsNotDistinct = uc.definition.includes("NULLS NOT DISTINCT");
|
|
40
|
+
if (hasNullsNotDistinct) {
|
|
41
|
+
return `{ fields: [${fields}], nullsNotDistinct: true }`;
|
|
42
|
+
}
|
|
43
|
+
return `{ fields: [${fields}] }`;
|
|
44
|
+
});
|
|
45
|
+
uniqueConstraintsDef = `\n uniqueConstraints: [\n ${constraints.join(",\n ")}\n ],`;
|
|
46
|
+
}
|
|
47
|
+
// Render multi-column foreign keys
|
|
48
|
+
let foreignKeysDef = "";
|
|
49
|
+
const multiColumnForeignKeys = table.foreignKeys.filter((fk) => fk.columnsFrom.length > 1);
|
|
50
|
+
if (multiColumnForeignKeys.length > 0) {
|
|
51
|
+
const constraints = multiColumnForeignKeys.map((fk) => {
|
|
52
|
+
const fields = fk.columnsFrom
|
|
53
|
+
.map((f) => `"${camelCase(f)}"`)
|
|
54
|
+
.join(", ");
|
|
55
|
+
const toFields = fk.columnsTo
|
|
56
|
+
.map((f) => `"${camelCase(f)}"`)
|
|
57
|
+
.join(", ");
|
|
58
|
+
return `{\n fields: [${fields}],\n references: {\n model: "${camelCase(fk.tableTo)}",\n fields: [${toFields}]\n }\n }`;
|
|
59
|
+
});
|
|
60
|
+
foreignKeysDef = `\n foreignKeys: [\n ${constraints.join(",\n ")}\n ],`;
|
|
61
|
+
}
|
|
62
|
+
// Render relations
|
|
63
|
+
const allForeignKeys = allTables.flatMap((t) => t.foreignKeys);
|
|
64
|
+
const relations = renderRelations(table, allForeignKeys);
|
|
65
|
+
const relationsDef = relations
|
|
66
|
+
? `\n relations: {\n ${relations}\n },`
|
|
67
|
+
: "";
|
|
68
|
+
// Build the final model
|
|
69
|
+
const modelName = camelCase(table.name);
|
|
70
|
+
const tableDef = modelName !== table.name ? `\n table: "${table.name}",` : "";
|
|
71
|
+
const schemaDef = table.schema !== "public" ? `\n schema: "${table.schema}",` : "";
|
|
72
|
+
const hasAdditionalSections = primaryKeyDef || uniqueConstraintsDef || foreignKeysDef || relationsDef;
|
|
73
|
+
const codeWithoutImports = `
|
|
74
|
+
export const ${modelName} = {${schemaDef}${tableDef}
|
|
75
|
+
fields: {
|
|
76
|
+
${fields},
|
|
77
|
+
}${hasAdditionalSections ? "," : ""}${primaryKeyDef}${uniqueConstraintsDef}${foreignKeysDef}${relationsDef}
|
|
78
|
+
} as const satisfies ModelDefinition;
|
|
79
|
+
`;
|
|
80
|
+
const needsSql = codeWithoutImports.includes("sql`");
|
|
81
|
+
const imports = ["type ModelDefinition"];
|
|
82
|
+
if (needsSql) {
|
|
83
|
+
imports.push("sql");
|
|
84
|
+
}
|
|
85
|
+
const code = `import { ${imports.join(", ")} } from "@casekit/orm2";\n` +
|
|
86
|
+
codeWithoutImports;
|
|
87
|
+
return await prettify(process.cwd(), code);
|
|
88
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|