Skip to content

Commit

Permalink
fix(trpc): create/upsert routers shouldn't be generated for delegate …
Browse files Browse the repository at this point in the history
…models
  • Loading branch information
ymc9 committed Jul 9, 2024
1 parent a4efa6e commit 8c6a517
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/plugins/trpc/src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {
CrudFailureReason,
PluginError,
RUNTIME_PACKAGE,
ensureEmptyDir,
isDelegateModel,
parseOptionAsStrings,
PluginError,
requireOption,
resolvePath,
RUNTIME_PACKAGE,
saveProject,
type PluginOptions,
} from '@zenstackhq/sdk';
import { Model } from '@zenstackhq/sdk/ast';
import { DataModel, isDataModel, Model } from '@zenstackhq/sdk/ast';
import { getPrismaClientImportSpec, supportCreateMany, type DMMF } from '@zenstackhq/sdk/prisma';
import fs from 'fs';
import { lowerCaseFirst } from 'lower-case-first';
Expand Down Expand Up @@ -287,10 +288,20 @@ function generateModelCreateRouter(
};
}

const dataModel = zmodel.declarations.find((d): d is DataModel => isDataModel(d) && d.name === model);
if (!dataModel) {
throw new Error(`Data model "${model}" not found`);
}

createRouterFunc.setBodyText((funcWriter) => {
funcWriter.write('return router(');
funcWriter.block(() => {
for (const [opType, opNameWithModel] of Object.entries(operations)) {
if (isDelegateModel(dataModel) && (opType.startsWith('create') || opType.startsWith('upsert'))) {
// delete models don't support create or upsert operations
continue;
}

const baseOpType = opType.replace('OrThrow', '');
const inputType = getInputSchemaByOpName(baseOpType, model);
const generateOpName = opType.replace(/One$/, '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,20 @@ describe('Polymorphic Plugin Interaction Test', () => {
extraDependencies: ['@tanstack/react-query'],
});
});

it('trpc', async () => {
const schema = `
${POLYMORPHIC_SCHEMA}
plugin trpc {
provider = '@zenstackhq/trpc'
output = '$projectRoot/routers'
}
`;

await loadSchema(schema, {
compile: true,
extraDependencies: ['@trpc/client', '@trpc/server'],
});
});
});

0 comments on commit 8c6a517

Please sign in to comment.