Skip to content

Commit 4f56c15

Browse files
authored
fix(trpc): create/upsert routers shouldn't be generated for delegate models (#1566)
1 parent a4efa6e commit 4f56c15

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

packages/plugins/trpc/src/generator.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import {
22
CrudFailureReason,
3-
PluginError,
4-
RUNTIME_PACKAGE,
53
ensureEmptyDir,
4+
isDelegateModel,
65
parseOptionAsStrings,
6+
PluginError,
77
requireOption,
88
resolvePath,
9+
RUNTIME_PACKAGE,
910
saveProject,
1011
type PluginOptions,
1112
} from '@zenstackhq/sdk';
12-
import { Model } from '@zenstackhq/sdk/ast';
13+
import { DataModel, isDataModel, Model } from '@zenstackhq/sdk/ast';
1314
import { getPrismaClientImportSpec, supportCreateMany, type DMMF } from '@zenstackhq/sdk/prisma';
1415
import fs from 'fs';
1516
import { lowerCaseFirst } from 'lower-case-first';
@@ -287,10 +288,20 @@ function generateModelCreateRouter(
287288
};
288289
}
289290

291+
const dataModel = zmodel.declarations.find((d): d is DataModel => isDataModel(d) && d.name === model);
292+
if (!dataModel) {
293+
throw new Error(`Data model "${model}" not found`);
294+
}
295+
290296
createRouterFunc.setBodyText((funcWriter) => {
291297
funcWriter.write('return router(');
292298
funcWriter.block(() => {
293299
for (const [opType, opNameWithModel] of Object.entries(operations)) {
300+
if (isDelegateModel(dataModel) && (opType.startsWith('create') || opType.startsWith('upsert'))) {
301+
// delete models don't support create or upsert operations
302+
continue;
303+
}
304+
294305
const baseOpType = opType.replace('OrThrow', '');
295306
const inputType = getInputSchemaByOpName(baseOpType, model);
296307
const generateOpName = opType.replace(/One$/, '');

tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,20 @@ describe('Polymorphic Plugin Interaction Test', () => {
2222
extraDependencies: ['@tanstack/react-query'],
2323
});
2424
});
25+
26+
it('trpc', async () => {
27+
const schema = `
28+
${POLYMORPHIC_SCHEMA}
29+
30+
plugin trpc {
31+
provider = '@zenstackhq/trpc'
32+
output = '$projectRoot/routers'
33+
}
34+
`;
35+
36+
await loadSchema(schema, {
37+
compile: true,
38+
extraDependencies: ['@trpc/client', '@trpc/server'],
39+
});
40+
});
2541
});

0 commit comments

Comments
 (0)