Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "2.5.1",
"version": "2.6.0",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/jetbrains/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "dev.zenstack"
version = "2.5.1"
version = "2.6.0"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/jetbrains/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jetbrains",
"version": "2.5.1",
"version": "2.6.0",
"displayName": "ZenStack JetBrains IDE Plugin",
"description": "ZenStack JetBrains IDE plugin",
"homepage": "https://zenstack.dev",
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": "2.5.1",
"version": "2.6.0",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/misc/redwood/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/redwood",
"displayName": "ZenStack RedwoodJS Integration",
"version": "2.5.1",
"version": "2.6.0",
"description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/openapi",
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
"version": "2.5.1",
"version": "2.6.0",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/swr/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/swr",
"displayName": "ZenStack plugin for generating SWR hooks",
"version": "2.5.1",
"version": "2.6.0",
"description": "ZenStack plugin for generating SWR hooks",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/tanstack-query/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/tanstack-query",
"displayName": "ZenStack plugin for generating tanstack-query hooks",
"version": "2.5.1",
"version": "2.6.0",
"description": "ZenStack plugin for generating tanstack-query hooks",
"main": "index.js",
"exports": {
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": "2.5.1",
"version": "2.6.0",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
30 changes: 17 additions & 13 deletions packages/plugins/trpc/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ function getPrismaOperationTypes(model: string, operation: string) {

let argsType: string;
let resultType: string;
const argsOptional = ['findMany', 'findFirst', 'findFirstOrThrow', 'createMany', 'deleteMany', 'count'].includes(
operation
);
Comment on lines +51 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider Including 'aggregate' and 'groupBy' in Optional Arguments

The argsOptional array currently includes operations that can be invoked without arguments. However, both 'aggregate' and 'groupBy' operations can also be called without any parameters. Consider adding them to the list to ensure consistent handling of optional arguments.

Apply this diff to include 'aggregate' and 'groupBy':

 const argsOptional = ['findMany', 'findFirst', 'findFirstOrThrow', 'createMany', 'deleteMany', 'count'].includes(
     operation
 );
+// Adding 'aggregate' and 'groupBy' to the list of operations with optional arguments
+const argsOptional = [
+  'findMany',
+  'findFirst',
+  'findFirstOrThrow',
+  'createMany',
+  'deleteMany',
+  'count',
+  'aggregate',
+  'groupBy',
+].includes(operation);

Committable suggestion was skipped due to low confidence.


switch (operation) {
case 'findUnique':
Expand Down Expand Up @@ -178,7 +181,7 @@ function getPrismaOperationTypes(model: string, operation: string) {
throw new PluginError(name, `Unsupported operation: "${operation}"`);
}

return { genericBase, argsType, resultType };
return { genericBase, argsType, resultType, argsOptional };
}

/**
Expand All @@ -192,22 +195,23 @@ export function generateRouterTyping(
version: string
) {
const procType = getProcedureTypeByOpName(baseOpType);
const { genericBase, argsType, resultType } = getPrismaOperationTypes(modelName, opType);
const { genericBase, argsType, argsOptional, resultType } = getPrismaOperationTypes(modelName, opType);
const errorType = `TRPCClientErrorLike<AppRouter>`;
const inputOptional = argsOptional ? '?' : '';
Comment on lines +198 to +200
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure Optional Input Handling for Mutations

Currently, inputOptional is used to conditionally make the input parameter optional in query procedures. Consider applying the same logic to mutation procedures like 'createMany' and 'deleteMany', which can accept optional inputs, to maintain consistency.

Apply this diff to include optional input handling in mutations:

 if (procType === 'query') {
     // Existing code for queries
 } else if (procType === 'mutation') {
+    const inputOptional = argsOptional ? '?' : '';
     writer.writeLine(\`
         useMutation: <T extends \${genericBase}>(opts?: UseTRPCMutationOptions<
             \${genericBase},
             \${errorType},
             \${resultType},
             Context
         >,) =>
         Omit<UseTRPCMutationResult<\${resultType}, \${errorType}, \${argsType}, Context>, 'mutateAsync'> & {
             mutateAsync:
+                <T extends \${genericBase}>(variables\${inputOptional}: T, opts?: UseTRPCMutationOptions<T, \${errorType}, \${resultType}, Context>) => Promise<\${resultType}>
         };
     \`);
 }

Committable suggestion was skipped due to low confidence.


writer.block(() => {
if (procType === 'query') {
if (version === 'v10') {
writer.writeLine(`
useQuery: <T extends ${genericBase}, TData = ${resultType}>(
input: ${argsType},
input${inputOptional}: ${argsType},
opts?: UseTRPCQueryOptions<string, T, ${resultType}, TData, Error>
) => UseTRPCQueryResult<
TData,
${errorType}
>;
useInfiniteQuery: <T extends ${genericBase}>(
input: Omit<${argsType}, 'cursor'>,
input${inputOptional}: Omit<${argsType}, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, ${resultType}, Error>
) => UseTRPCInfiniteQueryResult<
${resultType},
Expand All @@ -217,26 +221,26 @@ export function generateRouterTyping(
} else {
writer.writeLine(`
useQuery: <T extends ${genericBase}, TData = ${resultType}>(
input: ${argsType},
input${inputOptional}: ${argsType},
opts?: UseTRPCQueryOptions<${resultType}, TData, Error>
) => UseTRPCQueryResult<
TData,
${errorType}
>;
useInfiniteQuery: <T extends ${genericBase}>(
input: Omit<${argsType}, 'cursor'>,
input${inputOptional}: Omit<${argsType}, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<T, ${resultType}, Error>
) => UseTRPCInfiniteQueryResult<
${resultType},
${errorType},
T
>;
useSuspenseQuery: <T extends ${genericBase}, TData = ${resultType}>(
input: ${argsType},
input${inputOptional}: ${argsType},
opts?: UseTRPCSuspenseQueryOptions<${resultType}, TData, Error>
) => UseTRPCSuspenseQueryResult<TData, ${errorType}>;
useSuspenseInfiniteQuery: <T extends ${genericBase}>(
input: Omit<${argsType}, 'cursor'>,
input${inputOptional}: Omit<${argsType}, 'cursor'>,
opts?: UseTRPCSuspenseInfiniteQueryOptions<T, ${resultType}, Error>
) => UseTRPCSuspenseInfiniteQueryResult<${resultType}, ${errorType}, T>;
`);
Expand Down Expand Up @@ -298,10 +302,10 @@ export const getInputSchemaByOpName = (opName: string, modelName: string) => {
inputType = `$Schema.${capModelName}InputSchema.findUnique`;
break;
case 'findFirst':
inputType = `$Schema.${capModelName}InputSchema.findFirst`;
inputType = `$Schema.${capModelName}InputSchema.findFirst.optional()`;
break;
case 'findMany':
inputType = `$Schema.${capModelName}InputSchema.findMany`;
inputType = `$Schema.${capModelName}InputSchema.findMany.optional()`;
break;
case 'findRaw':
inputType = `$Schema.${capModelName}InputSchema.findRawObject`;
Expand All @@ -310,7 +314,7 @@ export const getInputSchemaByOpName = (opName: string, modelName: string) => {
inputType = `$Schema.${capModelName}InputSchema.create`;
break;
case 'createMany':
inputType = `$Schema.${capModelName}InputSchema.createMany`;
inputType = `$Schema.${capModelName}InputSchema.createMany.optional()`;
break;
case 'deleteOne':
inputType = `$Schema.${capModelName}InputSchema.delete`;
Expand All @@ -319,7 +323,7 @@ export const getInputSchemaByOpName = (opName: string, modelName: string) => {
inputType = `$Schema.${capModelName}InputSchema.update`;
break;
case 'deleteMany':
inputType = `$Schema.${capModelName}InputSchema.deleteMany`;
inputType = `$Schema.${capModelName}InputSchema.deleteMany.optional()`;
break;
case 'updateMany':
inputType = `$Schema.${capModelName}InputSchema.updateMany`;
Expand All @@ -337,7 +341,7 @@ export const getInputSchemaByOpName = (opName: string, modelName: string) => {
inputType = `$Schema.${capModelName}InputSchema.groupBy`;
break;
case 'count':
inputType = `$Schema.${capModelName}InputSchema.count`;
inputType = `$Schema.${capModelName}InputSchema.count.optional()`;
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import styles from './index.module.css';

export default function Home() {
const hello = api.greet.hello.useQuery({ text: 'from tRPC' });
const posts = api.post.findMany.useQuery({ where: { published: true }, include: { author: true } });
const postsTransformed = api.post.findMany.useQuery(
const posts = api.post.post.findMany.useQuery({ where: { published: true }, include: { author: true } });
const postsTransformed = api.post.post.findMany.useQuery(
{},
{ select: (data) => data.map((p) => ({ id: p.id, title: p.name })) }
);
Expand All @@ -14,9 +14,7 @@ export default function Home() {
<main className={styles.main}>
{hello.data && <h1 className={styles.title}>{hello.data.greeting}</h1>}
{posts.data?.map((post) => (
<p key={post.id}>
{post.name} by {post.author.email}
</p>
<p key={post.id}>{post.name}</p>
))}
{postsTransformed.data?.map((post) => (
<p key={post.id}>{post.title}</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ export default function createRouter<Config extends BaseConfig>(router: RouterFa

aggregate: procedure.input($Schema.PostInputSchema.aggregate).query(({ ctx, input }) => checkRead(db(ctx).post.aggregate(input as any))),

createMany: procedure.input($Schema.PostInputSchema.createMany).mutation(async ({ ctx, input }) => checkMutate(db(ctx).post.createMany(input as any))),
createMany: procedure.input($Schema.PostInputSchema.createMany.optional()).mutation(async ({ ctx, input }) => checkMutate(db(ctx).post.createMany(input as any))),

create: procedure.input($Schema.PostInputSchema.create).mutation(async ({ ctx, input }) => checkMutate(db(ctx).post.create(input as any))),

deleteMany: procedure.input($Schema.PostInputSchema.deleteMany).mutation(async ({ ctx, input }) => checkMutate(db(ctx).post.deleteMany(input as any))),
deleteMany: procedure.input($Schema.PostInputSchema.deleteMany.optional()).mutation(async ({ ctx, input }) => checkMutate(db(ctx).post.deleteMany(input as any))),

delete: procedure.input($Schema.PostInputSchema.delete).mutation(async ({ ctx, input }) => checkMutate(db(ctx).post.delete(input as any))),

findFirst: procedure.input($Schema.PostInputSchema.findFirst).query(({ ctx, input }) => checkRead(db(ctx).post.findFirst(input as any))),
findFirst: procedure.input($Schema.PostInputSchema.findFirst.optional()).query(({ ctx, input }) => checkRead(db(ctx).post.findFirst(input as any))),

findFirstOrThrow: procedure.input($Schema.PostInputSchema.findFirst).query(({ ctx, input }) => checkRead(db(ctx).post.findFirstOrThrow(input as any))),
findFirstOrThrow: procedure.input($Schema.PostInputSchema.findFirst.optional()).query(({ ctx, input }) => checkRead(db(ctx).post.findFirstOrThrow(input as any))),

findMany: procedure.input($Schema.PostInputSchema.findMany).query(({ ctx, input }) => checkRead(db(ctx).post.findMany(input as any))),
findMany: procedure.input($Schema.PostInputSchema.findMany.optional()).query(({ ctx, input }) => checkRead(db(ctx).post.findMany(input as any))),

findUnique: procedure.input($Schema.PostInputSchema.findUnique).query(({ ctx, input }) => checkRead(db(ctx).post.findUnique(input as any))),

Expand All @@ -39,7 +39,7 @@ export default function createRouter<Config extends BaseConfig>(router: RouterFa

upsert: procedure.input($Schema.PostInputSchema.upsert).mutation(async ({ ctx, input }) => checkMutate(db(ctx).post.upsert(input as any))),

count: procedure.input($Schema.PostInputSchema.count).query(({ ctx, input }) => checkRead(db(ctx).post.count(input as any))),
count: procedure.input($Schema.PostInputSchema.count.optional()).query(({ ctx, input }) => checkRead(db(ctx).post.count(input as any))),

}
);
Expand Down Expand Up @@ -123,14 +123,14 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
findFirst: {

useQuery: <T extends Prisma.PostFindFirstArgs, TData = Prisma.PostGetPayload<T>>(
input: Prisma.SelectSubset<T, Prisma.PostFindFirstArgs>,
input?: Prisma.SelectSubset<T, Prisma.PostFindFirstArgs>,
opts?: UseTRPCQueryOptions<string, T, Prisma.PostGetPayload<T>, TData, Error>
) => UseTRPCQueryResult<
TData,
TRPCClientErrorLike<AppRouter>
>;
useInfiniteQuery: <T extends Prisma.PostFindFirstArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.PostFindFirstArgs>, 'cursor'>,
input?: Omit<Prisma.SelectSubset<T, Prisma.PostFindFirstArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.PostGetPayload<T>, Error>
) => UseTRPCInfiniteQueryResult<
Prisma.PostGetPayload<T>,
Expand All @@ -141,14 +141,14 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
findFirstOrThrow: {

useQuery: <T extends Prisma.PostFindFirstOrThrowArgs, TData = Prisma.PostGetPayload<T>>(
input: Prisma.SelectSubset<T, Prisma.PostFindFirstOrThrowArgs>,
input?: Prisma.SelectSubset<T, Prisma.PostFindFirstOrThrowArgs>,
opts?: UseTRPCQueryOptions<string, T, Prisma.PostGetPayload<T>, TData, Error>
) => UseTRPCQueryResult<
TData,
TRPCClientErrorLike<AppRouter>
>;
useInfiniteQuery: <T extends Prisma.PostFindFirstOrThrowArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.PostFindFirstOrThrowArgs>, 'cursor'>,
input?: Omit<Prisma.SelectSubset<T, Prisma.PostFindFirstOrThrowArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.PostGetPayload<T>, Error>
) => UseTRPCInfiniteQueryResult<
Prisma.PostGetPayload<T>,
Expand All @@ -159,14 +159,14 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
findMany: {

useQuery: <T extends Prisma.PostFindManyArgs, TData = Array<Prisma.PostGetPayload<T>>>(
input: Prisma.SelectSubset<T, Prisma.PostFindManyArgs>,
input?: Prisma.SelectSubset<T, Prisma.PostFindManyArgs>,
opts?: UseTRPCQueryOptions<string, T, Array<Prisma.PostGetPayload<T>>, TData, Error>
) => UseTRPCQueryResult<
TData,
TRPCClientErrorLike<AppRouter>
>;
useInfiniteQuery: <T extends Prisma.PostFindManyArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.PostFindManyArgs>, 'cursor'>,
input?: Omit<Prisma.SelectSubset<T, Prisma.PostFindManyArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Array<Prisma.PostGetPayload<T>>, Error>
) => UseTRPCInfiniteQueryResult<
Array<Prisma.PostGetPayload<T>>,
Expand Down Expand Up @@ -389,7 +389,7 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
? number
: Prisma.GetScalarType<T['select'], Prisma.PostCountAggregateOutputType>
: number>(
input: Prisma.Subset<T, Prisma.PostCountArgs>,
input?: Prisma.Subset<T, Prisma.PostCountArgs>,
opts?: UseTRPCQueryOptions<string, T, 'select' extends keyof T
? T['select'] extends true
? number
Expand All @@ -400,7 +400,7 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
TRPCClientErrorLike<AppRouter>
>;
useInfiniteQuery: <T extends Prisma.PostCountArgs>(
input: Omit<Prisma.Subset<T, Prisma.PostCountArgs>, 'cursor'>,
input?: Omit<Prisma.Subset<T, Prisma.PostCountArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, 'select' extends keyof T
? T['select'] extends true
? number
Expand Down
Loading
Loading