Skip to content
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

merge from dev #1250

Merged
merged 2 commits into from
Apr 13, 2024
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
10 changes: 9 additions & 1 deletion packages/plugins/openapi/src/rpc-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,15 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
type: 'object',
required: ['data'],
properties: {
data: this.ref(`${modelName}CreateManyInput`),
data: this.oneOf(
this.ref(`${modelName}CreateManyInput`),
this.array(this.ref(`${modelName}CreateManyInput`))
),
skipDuplicates: {
type: 'boolean',
description:
'Do not insert records with unique fields or ID fields that already exist.',
},
meta: this.ref('_Meta'),
},
},
Expand Down
30 changes: 27 additions & 3 deletions packages/plugins/openapi/tests/baseline/rpc-3.0.0.baseline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3275,7 +3275,15 @@ components:
- data
properties:
data:
$ref: '#/components/schemas/UserCreateManyInput'
oneOf:
- $ref: '#/components/schemas/UserCreateManyInput'
- type: array
items:
$ref: '#/components/schemas/UserCreateManyInput'
skipDuplicates:
type: boolean
description: Do not insert records with unique fields or ID fields that already
exist.
meta:
$ref: '#/components/schemas/_Meta'
UserFindUniqueArgs:
Expand Down Expand Up @@ -3455,7 +3463,15 @@ components:
- data
properties:
data:
$ref: '#/components/schemas/ProfileCreateManyInput'
oneOf:
- $ref: '#/components/schemas/ProfileCreateManyInput'
- type: array
items:
$ref: '#/components/schemas/ProfileCreateManyInput'
skipDuplicates:
type: boolean
description: Do not insert records with unique fields or ID fields that already
exist.
meta:
$ref: '#/components/schemas/_Meta'
ProfileFindUniqueArgs:
Expand Down Expand Up @@ -3635,7 +3651,15 @@ components:
- data
properties:
data:
$ref: '#/components/schemas/Post_ItemCreateManyInput'
oneOf:
- $ref: '#/components/schemas/Post_ItemCreateManyInput'
- type: array
items:
$ref: '#/components/schemas/Post_ItemCreateManyInput'
skipDuplicates:
type: boolean
description: Do not insert records with unique fields or ID fields that already
exist.
meta:
$ref: '#/components/schemas/_Meta'
Post_ItemFindUniqueArgs:
Expand Down
30 changes: 27 additions & 3 deletions packages/plugins/openapi/tests/baseline/rpc-3.1.0.baseline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3339,7 +3339,15 @@ components:
- data
properties:
data:
$ref: '#/components/schemas/UserCreateManyInput'
oneOf:
- $ref: '#/components/schemas/UserCreateManyInput'
- type: array
items:
$ref: '#/components/schemas/UserCreateManyInput'
skipDuplicates:
type: boolean
description: Do not insert records with unique fields or ID fields that already
exist.
meta:
$ref: '#/components/schemas/_Meta'
UserFindUniqueArgs:
Expand Down Expand Up @@ -3519,7 +3527,15 @@ components:
- data
properties:
data:
$ref: '#/components/schemas/ProfileCreateManyInput'
oneOf:
- $ref: '#/components/schemas/ProfileCreateManyInput'
- type: array
items:
$ref: '#/components/schemas/ProfileCreateManyInput'
skipDuplicates:
type: boolean
description: Do not insert records with unique fields or ID fields that already
exist.
meta:
$ref: '#/components/schemas/_Meta'
ProfileFindUniqueArgs:
Expand Down Expand Up @@ -3699,7 +3715,15 @@ components:
- data
properties:
data:
$ref: '#/components/schemas/Post_ItemCreateManyInput'
oneOf:
- $ref: '#/components/schemas/Post_ItemCreateManyInput'
- type: array
items:
$ref: '#/components/schemas/Post_ItemCreateManyInput'
skipDuplicates:
type: boolean
description: Do not insert records with unique fields or ID fields that already
exist.
meta:
$ref: '#/components/schemas/_Meta'
Post_ItemFindUniqueArgs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1922,7 +1922,15 @@ components:
- data
properties:
data:
$ref: '#/components/schemas/FooCreateManyInput'
oneOf:
- $ref: '#/components/schemas/FooCreateManyInput'
- type: array
items:
$ref: '#/components/schemas/FooCreateManyInput'
skipDuplicates:
type: boolean
description: Do not insert records with unique fields or ID fields that already
exist.
meta:
$ref: '#/components/schemas/_Meta'
FooFindUniqueArgs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,15 @@ components:
- data
properties:
data:
$ref: '#/components/schemas/FooCreateManyInput'
oneOf:
- $ref: '#/components/schemas/FooCreateManyInput'
- type: array
items:
$ref: '#/components/schemas/FooCreateManyInput'
skipDuplicates:
type: boolean
description: Do not insert records with unique fields or ID fields that already
exist.
meta:
$ref: '#/components/schemas/_Meta'
FooFindUniqueArgs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ 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))),

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))),
Expand Down Expand Up @@ -62,20 +60,6 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
TRPCClientErrorLike<AppRouter>
>;

};
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: {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export default function createRouter<Config extends BaseConfig>(router: RouterFa

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

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

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

deleteMany: procedure.input($Schema.UserInputSchema.deleteMany).mutation(async ({ ctx, input }) => checkMutate(db(ctx).user.deleteMany(input as any))),
Expand Down Expand Up @@ -62,20 +60,6 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
TRPCClientErrorLike<AppRouter>
>;

};
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: {

Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/plugins/zod/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
imports.push(
`import { ${modelName}CreateManyInputObjectSchema } from '../objects/${modelName}CreateManyInput.schema'`
);
codeBody += `createMany: z.object({ data: z.union([${modelName}CreateManyInputObjectSchema, z.array(${modelName}CreateManyInputObjectSchema)]) }),`;
codeBody += `createMany: z.object({ data: z.union([${modelName}CreateManyInputObjectSchema, z.array(${modelName}CreateManyInputObjectSchema)]), skipDuplicates: z.boolean().optional() }),`;
operations.push(['createMany', origModelName]);
}

Expand Down
88 changes: 88 additions & 0 deletions tests/integration/tests/regression/issue-1241.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { loadSchema } from '@zenstackhq/testtools';
import { randomBytes } from 'crypto';

describe('issue 1241', () => {
it('regression', async () => {
const { enhance, prisma } = await loadSchema(
`
model User {
id String @id @default(uuid())
todos Todo[]

@@auth
@@allow('all', true)
}

model Todo {
id String @id @default(uuid())

user_id String
user User @relation(fields: [user_id], references: [id])

images File[] @relation("todo_images")
documents File[] @relation("todo_documents")

@@allow('all', true)
}

model File {
id String @id @default(uuid())
s3_key String @unique
label String

todo_image_id String?
todo_image Todo? @relation("todo_images", fields: [todo_image_id], references: [id])

todo_document_id String?
todo_document Todo? @relation("todo_documents", fields: [todo_document_id], references: [id])

@@allow('all', true)
}
`,
{ logPrismaQuery: true }
ymc9 marked this conversation as resolved.
Show resolved Hide resolved
);

const user = await prisma.user.create({
data: {},
});
await prisma.todo.create({
data: {
user_id: user.id,

images: {
create: new Array(3).fill(null).map((_, i) => ({
s3_key: randomBytes(8).toString('hex'),
label: `img-label-${i + 1}`,
})),
},

documents: {
create: new Array(3).fill(null).map((_, i) => ({
s3_key: randomBytes(8).toString('hex'),
label: `doc-label-${i + 1}`,
})),
},
},
});

const db = enhance();

const todo = await db.todo.findFirst({ where: {}, include: { documents: true } });
await expect(
db.todo.update({
where: { id: todo.id },
data: {
documents: {
update: todo.documents.map((doc: any) => {
return {
where: { s3_key: doc.s3_key },
data: { label: 'updated' },
};
}),
},
},
include: { documents: true },
})
).toResolveTruthy();
});
});
Loading