Skip to content

Commit a3926c2

Browse files
authored
fix: trpc mutation route should return undefined when result is not readable (#227)
1 parent cabe9dc commit a3926c2

File tree

14 files changed

+52
-22
lines changed

14 files changed

+52
-22
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "zenstack-monorepo",
3-
"version": "1.0.0-alpha.49",
3+
"version": "1.0.0-alpha.52",
44
"description": "",
55
"scripts": {
66
"build": "pnpm -r build",
77
"test": "pnpm -r run test --silent",
88
"lint": "pnpm -r lint",
9-
"publish-all": "pnpm --filter \"./packages/**\" -r publish",
10-
"publish-dev": "pnpm --filter \"./packages/**\" -r publish --tag dev",
11-
"publish-canary": "pnpm --filter \"./packages/**\" -r publish --tag canary"
9+
"publish-all": "pnpm --filter \"./packages/**\" -r publish --access public",
10+
"publish-dev": "pnpm --filter \"./packages/**\" -r publish --access public --tag dev",
11+
"publish-canary": "pnpm --filter \"./packages/**\" -r publish --access public --tag canary"
1212
},
1313
"keywords": [],
1414
"author": "",

packages/language/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/language",
3-
"version": "1.0.0-alpha.49",
3+
"version": "1.0.0-alpha.52",
44
"displayName": "ZenStack modeling language compiler",
55
"description": "ZenStack modeling language compiler",
66
"homepage": "https://zenstack.dev",

packages/next/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/next",
3-
"version": "1.0.0-alpha.49",
3+
"version": "1.0.0-alpha.52",
44
"displayName": "ZenStack Next.js integration",
55
"description": "ZenStack Next.js integration",
66
"homepage": "https://zenstack.dev",

packages/plugins/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/react",
33
"displayName": "ZenStack plugin and runtime for ReactJS",
4-
"version": "1.0.0-alpha.49",
4+
"version": "1.0.0-alpha.52",
55
"description": "ZenStack plugin and runtime for ReactJS",
66
"main": "index.js",
77
"repository": {

packages/plugins/react/src/react-hooks-generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DMMF } from '@prisma/generator-helper';
2-
import { PluginError, PluginOptions } from '@zenstackhq/sdk';
2+
import { CrudFailureReason, PluginError, PluginOptions } from '@zenstackhq/sdk';
33
import { DataModel, isDataModel, Model } from '@zenstackhq/sdk/ast';
44
import { camelCase, paramCase } from 'change-case';
55
import * as path from 'path';
@@ -35,7 +35,7 @@ function wrapReadbackErrorCheck(code: string) {
3535
return `try {
3636
${code}
3737
} catch (err: any) {
38-
if (err.info?.prisma && err.info?.code === 'P2004' && err.info?.reason === 'RESULT_NOT_READABLE') {
38+
if (err.info?.prisma && err.info?.code === 'P2004' && err.info?.reason === '${CrudFailureReason.RESULT_NOT_READABLE}') {
3939
// unable to readback data
4040
return undefined;
4141
} else {

packages/plugins/trpc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/trpc",
33
"displayName": "ZenStack plugin for tRPC",
4-
"version": "1.0.0-alpha.49",
4+
"version": "1.0.0-alpha.52",
55
"description": "ZenStack plugin for tRPC",
66
"main": "index.js",
77
"repository": {

packages/plugins/trpc/src/helpers.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DMMF } from '@prisma/generator-helper';
2+
import { CrudFailureReason } from '@zenstackhq/sdk';
23
import { CodeBlockWriter, SourceFile } from 'ts-morph';
34
import { uncapitalizeFirstLetter } from './utils/uncapitalizeFirstLetter';
45

@@ -24,11 +25,30 @@ export function generateProcedure(
2425
baseOpType: string
2526
) {
2627
const procType = getProcedureTypeByOpName(baseOpType);
27-
writer.write(`
28-
${opType}: procedure.input(${typeName}).${procType}(({ctx, input}) => db(ctx).${uncapitalizeFirstLetter(
29-
modelName
30-
)}.${opType.replace('One', '')}(input)),
28+
const prismaMethod = opType.replace('One', '');
29+
30+
if (procType === 'query') {
31+
writer.write(`
32+
${opType}: procedure.input(${typeName}).query(({ctx, input}) => db(ctx).${uncapitalizeFirstLetter(
33+
modelName
34+
)}.${prismaMethod}(input)),
3135
`);
36+
} else if (procType === 'mutation') {
37+
writer.write(`
38+
${opType}: procedure.input(${typeName}).mutation(async ({ctx, input}) => {
39+
try {
40+
return await db(ctx).${uncapitalizeFirstLetter(modelName)}.${prismaMethod}(input);
41+
} catch (err: any) {
42+
if (err.code === 'P2004' && err.meta?.reason === '${CrudFailureReason.RESULT_NOT_READABLE}') {
43+
// unable to readback data
44+
return undefined;
45+
} else {
46+
throw err;
47+
}
48+
}
49+
}),
50+
`);
51+
}
3252
}
3353

3454
export function generateRouterSchemaImports(sourceFile: SourceFile, name: string) {

packages/runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/runtime",
33
"displayName": "ZenStack Runtime Library",
4-
"version": "1.0.0-alpha.49",
4+
"version": "1.0.0-alpha.52",
55
"description": "Runtime of ZenStack for both client-side and server-side environments.",
66
"repository": {
77
"type": "git",

packages/runtime/src/enhancements/policy/policy-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22

33
import { PrismaClientKnownRequestError, PrismaClientUnknownRequestError } from '@prisma/client/runtime';
4-
import { AUXILIARY_FIELDS, TRANSACTION_FIELD_NAME } from '@zenstackhq/sdk';
4+
import { AUXILIARY_FIELDS, CrudFailureReason, TRANSACTION_FIELD_NAME } from '@zenstackhq/sdk';
55
import { camelCase } from 'change-case';
66
import cuid from 'cuid';
77
import deepcopy from 'deepcopy';
@@ -647,7 +647,7 @@ export class PolicyUtil {
647647
deniedByPolicy(model: string, operation: PolicyOperationKind, extra?: string) {
648648
return new PrismaClientKnownRequestError(
649649
`denied by policy: ${model} entities failed '${operation}' check${extra ? ', ' + extra : ''}`,
650-
{ clientVersion: getVersion(), code: 'P2004', meta: { reason: 'RESULT_NOT_READABLE' } }
650+
{ clientVersion: getVersion(), code: 'P2004', meta: { reason: CrudFailureReason.RESULT_NOT_READABLE } }
651651
);
652652
}
653653

packages/schema/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"publisher": "zenstack",
44
"displayName": "ZenStack Language Tools",
55
"description": "A toolkit for building secure CRUD apps with Next.js + Typescript",
6-
"version": "1.0.0-alpha.49",
6+
"version": "1.0.0-alpha.52",
77
"author": {
88
"name": "ZenStack Team"
99
},

0 commit comments

Comments
 (0)