Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 committed Aug 23, 2024
1 parent ad49153 commit dcfa0a8
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 61 deletions.
13 changes: 5 additions & 8 deletions packages/plugins/trpc/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ function createAppRouter(
}

appRouter.addStatements(`
export function db(ctx: any) {
if (!ctx.prisma) {
throw new Error('Missing "prisma" field in trpc context');
Expand All @@ -195,14 +194,14 @@ function createAppRouter(
parameters:
version === 'v10'
? [
{ name: 'createTRPCRouter', type: 'RouterFactory<Config>' },
{ name: 'router', type: 'RouterFactory<Config>' },
{ name: 'procedure', type: 'ProcBuilder<Config>' },
]
: [],
isExported: true,
})
.setBodyText((writer) => {
writer.write('return createTRPCRouter(');
writer.write(`return ${version === 'v10' ? 'router' : 'createTRPCRouter'}(`);
writer.block(() => {
for (const modelOperation of filteredModelOperations) {
const { model, ...operations } = modelOperation;
Expand All @@ -229,9 +228,7 @@ function createAppRouter(
});

if (version === 'v10') {
writer.writeLine(
`${lowerCaseFirst(model)}: create${model}Router(createTRPCRouter, procedure),`
);
writer.writeLine(`${lowerCaseFirst(model)}: create${model}Router(router, procedure),`);
} else {
writer.writeLine(`${lowerCaseFirst(model)}: create${model}Router(),`);
}
Expand Down Expand Up @@ -352,7 +349,7 @@ function generateModelCreateRouter(
? modelRouter.addFunction({
name: 'createRouter<Config extends BaseConfig>',
parameters: [
{ name: 'createTRPCRouter', type: 'RouterFactory<Config>' },
{ name: 'router', type: 'RouterFactory<Config>' },
{ name: 'procedure', type: 'ProcBuilder<Config>' },
],
isExported: true,
Expand Down Expand Up @@ -383,7 +380,7 @@ function generateModelCreateRouter(
}

createRouterFunc.setBodyText((funcWriter) => {
funcWriter.write('return createTRPCRouter(');
funcWriter.write(`return ${version === 'v10' ? 'router' : 'createTRPCRouter'}(`);
funcWriter.block(() => {
for (const [opType, opNameWithModel] of Object.entries(operations)) {
if (isDelegateModel(dataModel) && (opType.startsWith('create') || opType.startsWith('upsert'))) {
Expand Down
52 changes: 26 additions & 26 deletions packages/plugins/trpc/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,49 +197,49 @@ export function generateRouterTyping(

writer.block(() => {
if (procType === 'query') {
const queryOptions =
version === 'v10'
? `UseTRPCQueryOptions<string, T, ${resultType}, TData, Error>`
: `UseTRPCQueryOptions<T, ${resultType}, TData, Error>`;

const infiniteQueryOptions =
version === 'v10'
? `UseTRPCInfiniteQueryOptions<string, T, ${resultType}, Error>`
: `UseTRPCInfiniteQueryOptions<T, ${resultType}, Error>`;

const infiniteQueryResult =
version === 'v10'
? `UseTRPCInfiniteQueryResult<${resultType}, ${errorType}>`
: `UseTRPCInfiniteQueryResult<${resultType}, ${errorType}, T>`;

writer.writeLine(`
if (version === 'v10') {
writer.writeLine(`
useQuery: <T extends ${genericBase}, TData = ${resultType}>(
input: ${argsType},
opts?: ${queryOptions}
opts?: UseTRPCQueryOptions<string, T, ${resultType}, TData, Error>
) => UseTRPCQueryResult<
TData,
${errorType}
>;
useInfiniteQuery: <T extends ${genericBase}>(
input: Omit<${argsType}, 'cursor'>,
opts?: ${infiniteQueryOptions}
) => ${infiniteQueryResult};`);

if (version !== 'v10') {
// v11 uses tanstack-query v5 and supports suspense queries
opts?: UseTRPCInfiniteQueryOptions<string, T, ${resultType}, Error>
) => UseTRPCInfiniteQueryResult<
${resultType},
${errorType}
>;
`);
} else {
writer.writeLine(`
useSuspenseQuery: <T extends ${genericBase}, TData = ${resultType}>(
useQuery: <T extends ${genericBase}, TData = ${resultType}>(
input: ${argsType},
opts?: UseTRPCSuspenseQueryOptions<${resultType}, TData, Error>
) => UseTRPCSuspenseQueryResult<
opts?: UseTRPCQueryOptions<${resultType}, TData, Error>
) => UseTRPCQueryResult<
TData,
${errorType}
>;
useInfiniteQuery: <T extends ${genericBase}>(
input: Omit<${argsType}, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<T, ${resultType}, Error>
) => UseTRPCInfiniteQueryResult<
${resultType},
${errorType},
T
>;
useSuspenseQuery: <T extends ${genericBase}, TData = ${resultType}>(
input: ${argsType},
opts?: UseTRPCSuspenseQueryOptions<${resultType}, TData, Error>
) => UseTRPCSuspenseQueryResult<TData, ${errorType}>;
useSuspenseInfiniteQuery: <T extends ${genericBase}>(
input: Omit<${argsType}, 'cursor'>,
opts?: UseTRPCSuspenseInfiniteQueryOptions<T, ${resultType}, Error>
) => UseTRPCSuspenseInfiniteQueryResult<${resultType}, ${errorType}, T>;
`);
`);
}
} else if (procType === 'mutation') {
writer.writeLine(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type { UseTRPCMutationOptions, UseTRPCMutationResult, UseTRPCQueryOptions
import type { TRPCClientErrorLike } from '@trpc/client';
import type { AnyRouter } from '@trpc/server';

export default function createRouter<Config extends BaseConfig>(createTRPCRouter: RouterFactory<Config>, procedure: ProcBuilder<Config>) {
return createTRPCRouter({
export default function createRouter<Config extends BaseConfig>(router: RouterFactory<Config>, procedure: ProcBuilder<Config>) {
return router({

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

Expand Down Expand Up @@ -58,7 +58,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
useInfiniteQuery: <T extends Prisma.PostAggregateArgs>(
input: Omit<Prisma.Subset<T, Prisma.PostAggregateArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.GetPostAggregateType<T>, Error>
) => UseTRPCInfiniteQueryResult<Prisma.GetPostAggregateType<T>, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
Prisma.GetPostAggregateType<T>,
TRPCClientErrorLike<AppRouter>
>;

};
createMany: {

Expand Down Expand Up @@ -128,7 +132,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
useInfiniteQuery: <T extends Prisma.PostFindFirstArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.PostFindFirstArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.PostGetPayload<T>, Error>
) => UseTRPCInfiniteQueryResult<Prisma.PostGetPayload<T>, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
Prisma.PostGetPayload<T>,
TRPCClientErrorLike<AppRouter>
>;

};
findFirstOrThrow: {

Expand All @@ -142,7 +150,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
useInfiniteQuery: <T extends Prisma.PostFindFirstOrThrowArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.PostFindFirstOrThrowArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.PostGetPayload<T>, Error>
) => UseTRPCInfiniteQueryResult<Prisma.PostGetPayload<T>, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
Prisma.PostGetPayload<T>,
TRPCClientErrorLike<AppRouter>
>;

};
findMany: {

Expand All @@ -156,7 +168,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
useInfiniteQuery: <T extends Prisma.PostFindManyArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.PostFindManyArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Array<Prisma.PostGetPayload<T>>, Error>
) => UseTRPCInfiniteQueryResult<Array<Prisma.PostGetPayload<T>>, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
Array<Prisma.PostGetPayload<T>>,
TRPCClientErrorLike<AppRouter>
>;

};
findUnique: {

Expand All @@ -170,7 +186,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
useInfiniteQuery: <T extends Prisma.PostFindUniqueArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.PostFindUniqueArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.PostGetPayload<T>, Error>
) => UseTRPCInfiniteQueryResult<Prisma.PostGetPayload<T>, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
Prisma.PostGetPayload<T>,
TRPCClientErrorLike<AppRouter>
>;

};
findUniqueOrThrow: {

Expand All @@ -184,7 +204,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
useInfiniteQuery: <T extends Prisma.PostFindUniqueOrThrowArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.PostFindUniqueOrThrowArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.PostGetPayload<T>, Error>
) => UseTRPCInfiniteQueryResult<Prisma.PostGetPayload<T>, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
Prisma.PostGetPayload<T>,
TRPCClientErrorLike<AppRouter>
>;

};
groupBy: {

Expand Down Expand Up @@ -310,7 +334,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
>(
input: Omit<Prisma.SubsetIntersection<T, Prisma.PostGroupByArgs, OrderByArg> & InputErrors, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, {} extends InputErrors ? Prisma.GetPostGroupByPayload<T> : InputErrors, Error>
) => UseTRPCInfiniteQueryResult<{} extends InputErrors ? Prisma.GetPostGroupByPayload<T> : InputErrors, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
{} extends InputErrors ? Prisma.GetPostGroupByPayload<T> : InputErrors,
TRPCClientErrorLike<AppRouter>
>;

};
updateMany: {

Expand Down Expand Up @@ -378,10 +406,14 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
? number
: Prisma.GetScalarType<T['select'], Prisma.PostCountAggregateOutputType>
: number, Error>
) => UseTRPCInfiniteQueryResult<'select' extends keyof T
) => UseTRPCInfiniteQueryResult<
'select' extends keyof T
? T['select'] extends true
? number
: Prisma.GetScalarType<T['select'], Prisma.PostCountAggregateOutputType>
: number, TRPCClientErrorLike<AppRouter>>;
: number,
TRPCClientErrorLike<AppRouter>
>;

};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type { UseTRPCMutationOptions, UseTRPCMutationResult, UseTRPCQueryOptions
import type { TRPCClientErrorLike } from '@trpc/client';
import type { AnyRouter } from '@trpc/server';

export default function createRouter<Config extends BaseConfig>(createTRPCRouter: RouterFactory<Config>, procedure: ProcBuilder<Config>) {
return createTRPCRouter({
export default function createRouter<Config extends BaseConfig>(router: RouterFactory<Config>, procedure: ProcBuilder<Config>) {
return router({

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

Expand Down Expand Up @@ -58,7 +58,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
useInfiniteQuery: <T extends Prisma.UserAggregateArgs>(
input: Omit<Prisma.Subset<T, Prisma.UserAggregateArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.GetUserAggregateType<T>, Error>
) => UseTRPCInfiniteQueryResult<Prisma.GetUserAggregateType<T>, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
Prisma.GetUserAggregateType<T>,
TRPCClientErrorLike<AppRouter>
>;

};
createMany: {

Expand Down Expand Up @@ -128,7 +132,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
useInfiniteQuery: <T extends Prisma.UserFindFirstArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.UserFindFirstArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.UserGetPayload<T>, Error>
) => UseTRPCInfiniteQueryResult<Prisma.UserGetPayload<T>, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
Prisma.UserGetPayload<T>,
TRPCClientErrorLike<AppRouter>
>;

};
findFirstOrThrow: {

Expand All @@ -142,7 +150,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
useInfiniteQuery: <T extends Prisma.UserFindFirstOrThrowArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.UserFindFirstOrThrowArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.UserGetPayload<T>, Error>
) => UseTRPCInfiniteQueryResult<Prisma.UserGetPayload<T>, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
Prisma.UserGetPayload<T>,
TRPCClientErrorLike<AppRouter>
>;

};
findMany: {

Expand All @@ -156,7 +168,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
useInfiniteQuery: <T extends Prisma.UserFindManyArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.UserFindManyArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Array<Prisma.UserGetPayload<T>>, Error>
) => UseTRPCInfiniteQueryResult<Array<Prisma.UserGetPayload<T>>, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
Array<Prisma.UserGetPayload<T>>,
TRPCClientErrorLike<AppRouter>
>;

};
findUnique: {

Expand All @@ -170,7 +186,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
useInfiniteQuery: <T extends Prisma.UserFindUniqueArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.UserFindUniqueArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.UserGetPayload<T>, Error>
) => UseTRPCInfiniteQueryResult<Prisma.UserGetPayload<T>, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
Prisma.UserGetPayload<T>,
TRPCClientErrorLike<AppRouter>
>;

};
findUniqueOrThrow: {

Expand All @@ -184,7 +204,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
useInfiniteQuery: <T extends Prisma.UserFindUniqueOrThrowArgs>(
input: Omit<Prisma.SelectSubset<T, Prisma.UserFindUniqueOrThrowArgs>, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.UserGetPayload<T>, Error>
) => UseTRPCInfiniteQueryResult<Prisma.UserGetPayload<T>, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
Prisma.UserGetPayload<T>,
TRPCClientErrorLike<AppRouter>
>;

};
groupBy: {

Expand Down Expand Up @@ -310,7 +334,11 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
>(
input: Omit<Prisma.SubsetIntersection<T, Prisma.UserGroupByArgs, OrderByArg> & InputErrors, 'cursor'>,
opts?: UseTRPCInfiniteQueryOptions<string, T, {} extends InputErrors ? Prisma.GetUserGroupByPayload<T> : InputErrors, Error>
) => UseTRPCInfiniteQueryResult<{} extends InputErrors ? Prisma.GetUserGroupByPayload<T> : InputErrors, TRPCClientErrorLike<AppRouter>>;
) => UseTRPCInfiniteQueryResult<
{} extends InputErrors ? Prisma.GetUserGroupByPayload<T> : InputErrors,
TRPCClientErrorLike<AppRouter>
>;

};
updateMany: {

Expand Down Expand Up @@ -378,10 +406,14 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
? number
: Prisma.GetScalarType<T['select'], Prisma.UserCountAggregateOutputType>
: number, Error>
) => UseTRPCInfiniteQueryResult<'select' extends keyof T
) => UseTRPCInfiniteQueryResult<
'select' extends keyof T
? T['select'] extends true
? number
: Prisma.GetScalarType<T['select'], Prisma.UserCountAggregateOutputType>
: number, TRPCClientErrorLike<AppRouter>>;
: number,
TRPCClientErrorLike<AppRouter>
>;

};
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ export type ProcBuilder<Config extends BaseConfig> = ProcedureBuilder<
ProcedureParams<Config, any, any, any, UnsetMarker, UnsetMarker, any>
>;


export function db(ctx: any) {
if (!ctx.prisma) {
throw new Error('Missing "prisma" field in trpc context');
}
return ctx.prisma as PrismaClient;
}

export function createRouter<Config extends BaseConfig>(createTRPCRouter: RouterFactory<Config>, procedure: ProcBuilder<Config>) {
return createTRPCRouter({
user: createUserRouter(createTRPCRouter, procedure),
post: createPostRouter(createTRPCRouter, procedure),
export function createRouter<Config extends BaseConfig>(router: RouterFactory<Config>, procedure: ProcBuilder<Config>) {
return router({
user: createUserRouter(router, procedure),
post: createPostRouter(router, procedure),
}
);
}
Expand Down

0 comments on commit dcfa0a8

Please sign in to comment.