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
4 changes: 2 additions & 2 deletions packages/plugins/swr/tests/test-model-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const modelMeta: ModelMeta = {
name: 'posts',
},
},
uniqueConstraints: {},
uniqueConstraints: { id: { name: 'id', fields: ['id'] } },
},
post: {
name: 'post',
Expand All @@ -48,7 +48,7 @@ export const modelMeta: ModelMeta = {
owner: { ...fieldDefaults, type: 'User', name: 'owner', isDataModel: true, isRelationOwner: true },
ownerId: { ...fieldDefaults, type: 'User', name: 'owner', isForeignKey: true },
},
uniqueConstraints: {},
uniqueConstraints: { id: { name: 'id', fields: ['id'] } },
},
},
deleteCascade: {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/tanstack-query/tests/test-model-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const modelMeta: ModelMeta = {
name: 'posts',
},
},
uniqueConstraints: {},
uniqueConstraints: { id: { name: 'id', fields: ['id'] } },
},
post: {
name: 'post',
Expand All @@ -48,7 +48,7 @@ export const modelMeta: ModelMeta = {
owner: { ...fieldDefaults, type: 'User', name: 'owner', isDataModel: true, isRelationOwner: true },
ownerId: { ...fieldDefaults, type: 'User', name: 'owner', isForeignKey: true },
},
uniqueConstraints: {},
uniqueConstraints: { id: { name: 'id', fields: ['id'] } },
},
},
deleteCascade: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
>;

};
createMany: {
useMutation: <T extends Prisma.PostCreateManyArgs>(
opts?: UseTRPCMutationOptions<
Prisma.PostCreateManyArgs,
TRPCClientErrorLike<AppRouter>,
Prisma.BatchPayload,
Context
>,
) => Omit<
UseTRPCMutationResult<
Prisma.BatchPayload,
TRPCClientErrorLike<AppRouter>,
Prisma.SelectSubset<T, Prisma.PostCreateManyArgs>,
Context
>,
'mutateAsync'
> & {
mutateAsync: <T extends Prisma.PostCreateManyArgs>(
variables: T,
opts?: UseTRPCMutationOptions<T, TRPCClientErrorLike<AppRouter>, Prisma.BatchPayload, Context>,
) => Promise<Prisma.BatchPayload>;
};
};
create: {

useMutation: <T extends Prisma.PostCreateArgs>(opts?: UseTRPCMutationOptions<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
>;

};
createMany: {
useMutation: <T extends Prisma.UserCreateManyArgs>(
opts?: UseTRPCMutationOptions<
Prisma.UserCreateManyArgs,
TRPCClientErrorLike<AppRouter>,
Prisma.BatchPayload,
Context
>,
) => Omit<
UseTRPCMutationResult<
Prisma.BatchPayload,
TRPCClientErrorLike<AppRouter>,
Prisma.SelectSubset<T, Prisma.UserCreateManyArgs>,
Context
>,
'mutateAsync'
> & {
mutateAsync: <T extends Prisma.UserCreateManyArgs>(
variables: T,
opts?: UseTRPCMutationOptions<T, TRPCClientErrorLike<AppRouter>, Prisma.BatchPayload, Context>,
) => Promise<Prisma.BatchPayload>;
};
};
create: {

useMutation: <T extends Prisma.UserCreateArgs>(opts?: UseTRPCMutationOptions<
Expand Down
20 changes: 9 additions & 11 deletions packages/runtime/src/cross/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { lowerCaseFirst } from 'lower-case-first';
import { ModelInfo, ModelMeta } from '.';
import { requireField, type ModelInfo, type ModelMeta } from '.';

/**
* Gets field names in a data model entity, filtering out internal fields.
Expand Down Expand Up @@ -47,19 +47,17 @@ export function zip<T1, T2>(x: Enumerable<T1>, y: Enumerable<T2>): Array<[T1, T2
}

export function getIdFields(modelMeta: ModelMeta, model: string, throwIfNotFound = false) {
let fields = modelMeta.models[lowerCaseFirst(model)]?.fields;
if (!fields) {
const uniqueConstraints = modelMeta.models[lowerCaseFirst(model)]?.uniqueConstraints ?? {};

const entries = Object.values(uniqueConstraints);
if (entries.length === 0) {
if (throwIfNotFound) {
throw new Error(`Unable to load fields for ${model}`);
} else {
fields = {};
throw new Error(`Model ${model} does not have any id field`);
}
return [];
}
const result = Object.values(fields).filter((f) => f.isId);
if (result.length === 0 && throwIfNotFound) {
throw new Error(`model ${model} does not have an id field`);
}
return result;

return entries[0].fields.map((f) => requireField(modelMeta, model, f));
}

export function getModelInfo<Throw extends boolean = false>(
Expand Down
Loading