@cedarjs/cli 7.0.0-canary.3031 → 7.0.0-canary.3032

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.
@@ -250,6 +250,10 @@ const getIdName = async (model) => {
250
250
  if (!isServiceModel(schemaResult)) {
251
251
  return void 0;
252
252
  }
253
+ if (schemaResult.primaryKey?.fields.length) {
254
+ const { fields, name } = schemaResult.primaryKey;
255
+ return { fields, whereArg: name || fields.join("_") };
256
+ }
253
257
  return schemaResult.fields.find((field) => field.isId)?.name;
254
258
  };
255
259
  const files = async ({
@@ -262,6 +266,11 @@ const files = async ({
262
266
  const componentName = camelcase(pluralize(name));
263
267
  const model = name;
264
268
  const idName = await getIdName(model);
269
+ if (tests && rest.crud && idName !== void 0 && typeof idName !== "string") {
270
+ throw new Error(
271
+ `Cannot generate CRUD tests for "${model}": it has a compound primary key (@@id([${idName.fields.join(", ")}])), which isn't supported by the generated test file yet. Re-run with \`--tests=false\`.`
272
+ );
273
+ }
265
274
  const prismaImportSource = "src/lib/db";
266
275
  let modelRelations;
267
276
  if (relations) {
@@ -286,46 +295,46 @@ const files = async ({
286
295
  ...rest
287
296
  }
288
297
  });
289
- const testFile = await templateForFile({
290
- name,
291
- side: "api",
292
- sidePathSection: "services",
293
- generator: "service",
294
- outputPath: path.join(componentName, componentName + ".test.ts"),
295
- templatePath: "test.ts.template",
296
- templateVars: {
297
- relations: relations || [],
298
- create: await fieldsToInput(model),
299
- update: await fieldsToUpdate(model),
300
- types: await fieldTypes(model),
301
- prismaImport: (await parseSchema(model)).scalarFields.some(
302
- (field) => field.type === "Decimal"
303
- ),
304
- prismaModel: model,
305
- idName,
306
- prismaImportSource,
307
- ...rest
308
- }
309
- });
310
- const scenariosFile = await templateForFile({
311
- name,
312
- side: "api",
313
- sidePathSection: "services",
314
- generator: "service",
315
- outputPath: path.join(componentName, componentName + ".scenarios.ts"),
316
- templatePath: "scenarios.ts.template",
317
- templateVars: {
318
- scenario: await buildScenario(model),
319
- stringifiedScenario: await buildStringifiedScenario(model),
320
- prismaModel: model,
321
- idName,
322
- relations: modelRelations,
323
- prismaImportSource,
324
- ...rest
325
- }
326
- });
327
298
  const files2 = [serviceFile];
328
299
  if (tests) {
300
+ const testFile = await templateForFile({
301
+ name,
302
+ side: "api",
303
+ sidePathSection: "services",
304
+ generator: "service",
305
+ outputPath: path.join(componentName, componentName + ".test.ts"),
306
+ templatePath: "test.ts.template",
307
+ templateVars: {
308
+ relations: relations || [],
309
+ create: await fieldsToInput(model),
310
+ update: await fieldsToUpdate(model),
311
+ types: await fieldTypes(model),
312
+ prismaImport: (await parseSchema(model)).scalarFields.some(
313
+ (field) => field.type === "Decimal"
314
+ ),
315
+ prismaModel: model,
316
+ idName,
317
+ prismaImportSource,
318
+ ...rest
319
+ }
320
+ });
321
+ const scenariosFile = await templateForFile({
322
+ name,
323
+ side: "api",
324
+ sidePathSection: "services",
325
+ generator: "service",
326
+ outputPath: path.join(componentName, componentName + ".scenarios.ts"),
327
+ templatePath: "scenarios.ts.template",
328
+ templateVars: {
329
+ scenario: await buildScenario(model),
330
+ stringifiedScenario: await buildStringifiedScenario(model),
331
+ prismaModel: model,
332
+ idName,
333
+ relations: modelRelations,
334
+ prismaImportSource,
335
+ ...rest
336
+ }
337
+ });
329
338
  files2.push(testFile);
330
339
  files2.push(scenariosFile);
331
340
  }
@@ -1,14 +1,28 @@
1
1
  import type { QueryResolvers<% if (crud) { %>, MutationResolvers<% } %><% if (relations.length) { %>, ${singularPascalName}RelationResolvers<% } %> } from 'types/graphql'
2
2
 
3
3
  import { db } from 'src/lib/db'
4
-
4
+ <%
5
+ // A compound `@@id([...])` key comes through as `{ fields, whereArg }`
6
+ // instead of a single field name -- build the destructure/where-clause
7
+ // snippets for both shapes here so the rest of the template can stay
8
+ // shape-agnostic.
9
+ const idIsComposite = idName != null && typeof idName === 'object'
10
+ const idFields = idIsComposite ? idName.fields : [idName]
11
+ const idArgs = idFields.join(', ')
12
+ const idWhere = idIsComposite
13
+ ? `${idName.whereArg}: { ${idFields.join(', ')} }`
14
+ : idName
15
+ const idWhereFromRoot = idIsComposite
16
+ ? `${idName.whereArg}: { ${idFields.map((f) => `${f}: root?.${f}`).join(', ')} }`
17
+ : `${idName}: root?.${idName}`
18
+ %>
5
19
  export const ${pluralCamelName}: QueryResolvers['${pluralCamelName}'] = () => {
6
20
  return db.${singularCamelName}.findMany()
7
21
  }<% if (crud || relations.length) { %>
8
22
 
9
- export const ${singularCamelName}: QueryResolvers['${singularCamelName}'] = ({ ${idName} }) => {
23
+ export const ${singularCamelName}: QueryResolvers['${singularCamelName}'] = ({ ${idArgs} }) => {
10
24
  return db.${singularCamelName}.findUnique({
11
- where: { ${idName} },
25
+ where: { ${idWhere} },
12
26
  })
13
27
  }<% } %><% if (crud) { %>
14
28
 
@@ -18,21 +32,21 @@ export const create${singularPascalName}: MutationResolvers['create${singularPas
18
32
  })
19
33
  }
20
34
 
21
- export const update${singularPascalName}: MutationResolvers['update${singularPascalName}'] = ({ ${idName}, input }) => {
35
+ export const update${singularPascalName}: MutationResolvers['update${singularPascalName}'] = ({ ${idArgs}, input }) => {
22
36
  return db.${singularCamelName}.update({
23
37
  data: input,
24
- where: { ${idName} },
38
+ where: { ${idWhere} },
25
39
  })
26
40
  }
27
41
 
28
- export const delete${singularPascalName}: MutationResolvers['delete${singularPascalName}'] = ({ ${idName} }) => {
42
+ export const delete${singularPascalName}: MutationResolvers['delete${singularPascalName}'] = ({ ${idArgs} }) => {
29
43
  return db.${singularCamelName}.delete({
30
- where: { ${idName} },
44
+ where: { ${idWhere} },
31
45
  })
32
46
  }<% } %><% if (relations.length) { %>
33
47
 
34
48
  export const ${singularPascalName}: ${singularPascalName}RelationResolvers = {<% relations.forEach(relation => { %>
35
49
  ${relation}: (_obj, { root }) => {
36
- return db.${singularCamelName}.findUnique({ where: { ${idName}: root?.${idName} } }).${relation}()
50
+ return db.${singularCamelName}.findUnique({ where: { ${idWhereFromRoot} } }).${relation}()
37
51
  },<% }) %>
38
52
  }<% } %>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/cli",
3
- "version": "7.0.0-canary.3031",
3
+ "version": "7.0.0-canary.3032",
4
4
  "description": "The CedarJS Command Line",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,17 +36,17 @@
36
36
  "@babel/preset-typescript": "7.29.7",
37
37
  "@babel/traverse": "7.29.8",
38
38
  "@babel/types": "7.29.8",
39
- "@cedarjs/api-server": "7.0.0-canary.3031",
40
- "@cedarjs/babel-config": "7.0.0-canary.3031",
41
- "@cedarjs/cli-helpers": "7.0.0-canary.3031",
42
- "@cedarjs/internal": "7.0.0-canary.3031",
43
- "@cedarjs/prerender": "7.0.0-canary.3031",
44
- "@cedarjs/project-config": "7.0.0-canary.3031",
45
- "@cedarjs/structure": "7.0.0-canary.3031",
46
- "@cedarjs/telemetry": "7.0.0-canary.3031",
47
- "@cedarjs/utils": "7.0.0-canary.3031",
48
- "@cedarjs/vite": "7.0.0-canary.3031",
49
- "@cedarjs/web-server": "7.0.0-canary.3031",
39
+ "@cedarjs/api-server": "7.0.0-canary.3032",
40
+ "@cedarjs/babel-config": "7.0.0-canary.3032",
41
+ "@cedarjs/cli-helpers": "7.0.0-canary.3032",
42
+ "@cedarjs/internal": "7.0.0-canary.3032",
43
+ "@cedarjs/prerender": "7.0.0-canary.3032",
44
+ "@cedarjs/project-config": "7.0.0-canary.3032",
45
+ "@cedarjs/structure": "7.0.0-canary.3032",
46
+ "@cedarjs/telemetry": "7.0.0-canary.3032",
47
+ "@cedarjs/utils": "7.0.0-canary.3032",
48
+ "@cedarjs/vite": "7.0.0-canary.3032",
49
+ "@cedarjs/web-server": "7.0.0-canary.3032",
50
50
  "@listr2/prompt-adapter-enquirer": "4.3.0",
51
51
  "@opentelemetry/api": "1.9.1",
52
52
  "@opentelemetry/core": "1.30.1",
@@ -94,7 +94,7 @@
94
94
  "yargs": "17.7.3"
95
95
  },
96
96
  "devDependencies": {
97
- "@cedarjs/framework-tools": "7.0.0-canary.3031",
97
+ "@cedarjs/framework-tools": "7.0.0-canary.3032",
98
98
  "@prisma/dmmf": "7.8.0",
99
99
  "@types/archiver": "^7.0.0",
100
100
  "memfs": "4.68.1",