Skip to content

fix: trpc mutation route should return undefined when result is not readable #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "zenstack-monorepo",
"version": "1.0.0-alpha.49",
"version": "1.0.0-alpha.52",
"description": "",
"scripts": {
"build": "pnpm -r build",
"test": "pnpm -r run test --silent",
"lint": "pnpm -r lint",
"publish-all": "pnpm --filter \"./packages/**\" -r publish",
"publish-dev": "pnpm --filter \"./packages/**\" -r publish --tag dev",
"publish-canary": "pnpm --filter \"./packages/**\" -r publish --tag canary"
"publish-all": "pnpm --filter \"./packages/**\" -r publish --access public",
"publish-dev": "pnpm --filter \"./packages/**\" -r publish --access public --tag dev",
"publish-canary": "pnpm --filter \"./packages/**\" -r publish --access public --tag canary"
},
"keywords": [],
"author": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/language",
"version": "1.0.0-alpha.49",
"version": "1.0.0-alpha.52",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/next",
"version": "1.0.0-alpha.49",
"version": "1.0.0-alpha.52",
"displayName": "ZenStack Next.js integration",
"description": "ZenStack Next.js integration",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/react",
"displayName": "ZenStack plugin and runtime for ReactJS",
"version": "1.0.0-alpha.49",
"version": "1.0.0-alpha.52",
"description": "ZenStack plugin and runtime for ReactJS",
"main": "index.js",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/react/src/react-hooks-generator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DMMF } from '@prisma/generator-helper';
import { PluginError, PluginOptions } from '@zenstackhq/sdk';
import { CrudFailureReason, PluginError, PluginOptions } from '@zenstackhq/sdk';
import { DataModel, isDataModel, Model } from '@zenstackhq/sdk/ast';
import { camelCase, paramCase } from 'change-case';
import * as path from 'path';
Expand Down Expand Up @@ -35,7 +35,7 @@ function wrapReadbackErrorCheck(code: string) {
return `try {
${code}
} catch (err: any) {
if (err.info?.prisma && err.info?.code === 'P2004' && err.info?.reason === 'RESULT_NOT_READABLE') {
if (err.info?.prisma && err.info?.code === 'P2004' && err.info?.reason === '${CrudFailureReason.RESULT_NOT_READABLE}') {
// unable to readback data
return undefined;
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/trpc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/trpc",
"displayName": "ZenStack plugin for tRPC",
"version": "1.0.0-alpha.49",
"version": "1.0.0-alpha.52",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
28 changes: 24 additions & 4 deletions packages/plugins/trpc/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DMMF } from '@prisma/generator-helper';
import { CrudFailureReason } from '@zenstackhq/sdk';
import { CodeBlockWriter, SourceFile } from 'ts-morph';
import { uncapitalizeFirstLetter } from './utils/uncapitalizeFirstLetter';

Expand All @@ -24,11 +25,30 @@ export function generateProcedure(
baseOpType: string
) {
const procType = getProcedureTypeByOpName(baseOpType);
writer.write(`
${opType}: procedure.input(${typeName}).${procType}(({ctx, input}) => db(ctx).${uncapitalizeFirstLetter(
modelName
)}.${opType.replace('One', '')}(input)),
const prismaMethod = opType.replace('One', '');

if (procType === 'query') {
writer.write(`
${opType}: procedure.input(${typeName}).query(({ctx, input}) => db(ctx).${uncapitalizeFirstLetter(
modelName
)}.${prismaMethod}(input)),
`);
} else if (procType === 'mutation') {
writer.write(`
${opType}: procedure.input(${typeName}).mutation(async ({ctx, input}) => {
try {
return await db(ctx).${uncapitalizeFirstLetter(modelName)}.${prismaMethod}(input);
} catch (err: any) {
if (err.code === 'P2004' && err.meta?.reason === '${CrudFailureReason.RESULT_NOT_READABLE}') {
// unable to readback data
return undefined;
} else {
throw err;
}
}
}),
`);
}
}

export function generateRouterSchemaImports(sourceFile: SourceFile, name: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/runtime",
"displayName": "ZenStack Runtime Library",
"version": "1.0.0-alpha.49",
"version": "1.0.0-alpha.52",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/src/enhancements/policy/policy-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { PrismaClientKnownRequestError, PrismaClientUnknownRequestError } from '@prisma/client/runtime';
import { AUXILIARY_FIELDS, TRANSACTION_FIELD_NAME } from '@zenstackhq/sdk';
import { AUXILIARY_FIELDS, CrudFailureReason, TRANSACTION_FIELD_NAME } from '@zenstackhq/sdk';
import { camelCase } from 'change-case';
import cuid from 'cuid';
import deepcopy from 'deepcopy';
Expand Down Expand Up @@ -647,7 +647,7 @@ export class PolicyUtil {
deniedByPolicy(model: string, operation: PolicyOperationKind, extra?: string) {
return new PrismaClientKnownRequestError(
`denied by policy: ${model} entities failed '${operation}' check${extra ? ', ' + extra : ''}`,
{ clientVersion: getVersion(), code: 'P2004', meta: { reason: 'RESULT_NOT_READABLE' } }
{ clientVersion: getVersion(), code: 'P2004', meta: { reason: CrudFailureReason.RESULT_NOT_READABLE } }
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "A toolkit for building secure CRUD apps with Next.js + Typescript",
"version": "1.0.0-alpha.49",
"version": "1.0.0-alpha.52",
"author": {
"name": "ZenStack Team"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/sdk",
"version": "1.0.0-alpha.49",
"version": "1.0.0-alpha.52",
"description": "ZenStack plugin development SDK",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ export const GUARD_FIELD_NAME = 'zenstack_guard';
* All Auxiliary fields.
*/
export const AUXILIARY_FIELDS = [TRANSACTION_FIELD_NAME, GUARD_FIELD_NAME];

/**
* Reasons for a CRUD operation to fail.
*/
export enum CrudFailureReason {
/**
* CRUD suceeded but the result was not readable.
*/
RESULT_NOT_READABLE = 'RESULT_NOT_READABLE',
}
2 changes: 1 addition & 1 deletion packages/testtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/testtools",
"version": "1.0.0-alpha.49",
"version": "1.0.0-alpha.52",
"description": "ZenStack Test Tools",
"main": "index.js",
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test-run/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.