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

Simpler nested creates #85

Merged
merged 1 commit into from
Dec 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function addOneToManyRelation(
model: Model,
) {
const getCreateQuery = (extraDataFields: string) =>
`await this.client.${toCamelCase(field.type)}.create({ data: { ...elem, ${extraDataFields} } }, tx);`;
`await this.client.${toCamelCase(field.type)}.create({ data: { ...elem, ${extraDataFields} } as Prisma.Args<Prisma.${field.type}Delegate, "create">['data'] }, tx);`;

const modelPk = getUniqueIdentifiers(model)[0];
const modelPkFields = JSON.parse(modelPk.keyPath) as string[];
Expand All @@ -180,9 +180,7 @@ function addOneToManyRelation(
nestedConnectLine += ` }`;

const nestedDirectLine = otherField.relationFromFields!.map((field, idx) => `${field}: keyPath[${idx}]`).join(", ");

const connectQuery = getCreateQuery(nestedConnectLine);
const directQuery = getCreateQuery(nestedDirectLine);

writer.writeLine(`if (query.data.${field.name}?.create)`).block(() => {
if (fkFields.length === 1) {
Expand All @@ -194,24 +192,13 @@ function addOneToManyRelation(
This is due to Prisma's create query's constraint of using either
{ connect: { pk: value } } OR { fk: value } for all the fields
*/
const otherFkField =
fkFields.find(({ relationName, isRequired }) => relationName !== field.relationName && isRequired) ??
fkFields.find(({ relationName }) => relationName !== field.relationName)!;
writer
.writeLine(`const createData = Array.isArray(query.data.${field.name}.create)`)
.writeLine(`? query.data.${field.name}.create`)
.writeLine(`: [query.data.${field.name}.create]`)
.writeLine(`for (const elem of createData)`)
.block(() => {
writer
.writeLine(`if ("${otherFkField.name}" in elem && !("${otherFkField.relationFromFields?.at(0)}" in elem))`)
.block(() => {
writer.writeLine(connectQuery);
})
.writeLine(`else if (elem.${otherFkField.relationFromFields?.at(0)} !== undefined)`)
.block(() => {
writer.writeLine(directQuery);
});
writer.writeLine(connectQuery);
});
}
});
Expand Down
126 changes: 72 additions & 54 deletions packages/usage/src/prisma/prisma-idb/prisma-idb-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,15 @@ class UserIDBClass extends BaseIDBModelClass {
}
if (query.data.posts?.create) {
for (const elem of IDBUtils.convertToArray(query.data.posts.create)) {
await this.client.post.create({ data: { ...elem, author: { connect: { id: keyPath[0] } } } }, tx);
await this.client.post.create(
{
data: { ...elem, author: { connect: { id: keyPath[0] } } } as Prisma.Args<
Prisma.PostDelegate,
"create"
>["data"],
},
tx,
);
}
}
if (query.data.posts?.connect) {
Expand Down Expand Up @@ -912,11 +920,15 @@ class UserIDBClass extends BaseIDBModelClass {
? query.data.comments.create
: [query.data.comments.create];
for (const elem of createData) {
if ("post" in elem && !("postId" in elem)) {
await this.client.comment.create({ data: { ...elem, user: { connect: { id: keyPath[0] } } } }, tx);
} else if (elem.postId !== undefined) {
await this.client.comment.create({ data: { ...elem, userId: keyPath[0] } }, tx);
}
await this.client.comment.create(
{
data: { ...elem, user: { connect: { id: keyPath[0] } } } as Prisma.Args<
Prisma.CommentDelegate,
"create"
>["data"],
},
tx,
);
}
}
if (query.data.comments?.connect) {
Expand All @@ -942,7 +954,15 @@ class UserIDBClass extends BaseIDBModelClass {
}
if (query.data.Mother?.create) {
for (const elem of IDBUtils.convertToArray(query.data.Mother.create)) {
await this.client.mother.create({ data: { ...elem, user: { connect: { id: keyPath[0] } } } }, tx);
await this.client.mother.create(
{
data: { ...elem, user: { connect: { id: keyPath[0] } } } as Prisma.Args<
Prisma.MotherDelegate,
"create"
>["data"],
},
tx,
);
}
}
if (query.data.Mother?.connect) {
Expand Down Expand Up @@ -971,11 +991,15 @@ class UserIDBClass extends BaseIDBModelClass {
? query.data.Father.create
: [query.data.Father.create];
for (const elem of createData) {
if ("wife" in elem && !("motherFirstName" in elem)) {
await this.client.father.create({ data: { ...elem, user: { connect: { id: keyPath[0] } } } }, tx);
} else if (elem.motherFirstName !== undefined) {
await this.client.father.create({ data: { ...elem, userId: keyPath[0] } }, tx);
}
await this.client.father.create(
{
data: { ...elem, user: { connect: { id: keyPath[0] } } } as Prisma.Args<
Prisma.FatherDelegate,
"create"
>["data"],
},
tx,
);
}
}
if (query.data.Father?.connect) {
Expand All @@ -1002,11 +1026,15 @@ class UserIDBClass extends BaseIDBModelClass {
if (query.data.Child?.create) {
const createData = Array.isArray(query.data.Child.create) ? query.data.Child.create : [query.data.Child.create];
for (const elem of createData) {
if ("father" in elem && !("fatherFirstName" in elem)) {
await this.client.child.create({ data: { ...elem, user: { connect: { id: keyPath[0] } } } }, tx);
} else if (elem.fatherFirstName !== undefined) {
await this.client.child.create({ data: { ...elem, userId: keyPath[0] } }, tx);
}
await this.client.child.create(
{
data: { ...elem, user: { connect: { id: keyPath[0] } } } as Prisma.Args<
Prisma.ChildDelegate,
"create"
>["data"],
},
tx,
);
}
}
if (query.data.Child?.connect) {
Expand Down Expand Up @@ -2161,11 +2189,15 @@ class PostIDBClass extends BaseIDBModelClass {
? query.data.comments.create
: [query.data.comments.create];
for (const elem of createData) {
if ("user" in elem && !("userId" in elem)) {
await this.client.comment.create({ data: { ...elem, post: { connect: { id: keyPath[0] } } } }, tx);
} else if (elem.userId !== undefined) {
await this.client.comment.create({ data: { ...elem, postId: keyPath[0] } }, tx);
}
await this.client.comment.create(
{
data: { ...elem, post: { connect: { id: keyPath[0] } } } as Prisma.Args<
Prisma.CommentDelegate,
"create"
>["data"],
},
tx,
);
}
}
if (query.data.comments?.connect) {
Expand Down Expand Up @@ -4053,22 +4085,15 @@ class FatherIDBClass extends BaseIDBModelClass {
? query.data.children.create
: [query.data.children.create];
for (const elem of createData) {
if ("mother" in elem && !("motherFirstName" in elem)) {
await this.client.child.create(
{
data: {
...elem,
father: { connect: { firstName_lastName: { firstName: keyPath[0], lastName: keyPath[1] } } },
},
},
tx,
);
} else if (elem.motherFirstName !== undefined) {
await this.client.child.create(
{ data: { ...elem, fatherFirstName: keyPath[0], fatherLastName: keyPath[1] } },
tx,
);
}
await this.client.child.create(
{
data: {
...elem,
father: { connect: { firstName_lastName: { firstName: keyPath[0], lastName: keyPath[1] } } },
} as Prisma.Args<Prisma.ChildDelegate, "create">["data"],
},
tx,
);
}
}
if (query.data.children?.connect) {
Expand Down Expand Up @@ -4832,22 +4857,15 @@ class MotherIDBClass extends BaseIDBModelClass {
? query.data.children.create
: [query.data.children.create];
for (const elem of createData) {
if ("father" in elem && !("fatherFirstName" in elem)) {
await this.client.child.create(
{
data: {
...elem,
mother: { connect: { firstName_lastName: { firstName: keyPath[0], lastName: keyPath[1] } } },
},
},
tx,
);
} else if (elem.fatherFirstName !== undefined) {
await this.client.child.create(
{ data: { ...elem, motherFirstName: keyPath[0], motherLastName: keyPath[1] } },
tx,
);
}
await this.client.child.create(
{
data: {
...elem,
mother: { connect: { firstName_lastName: { firstName: keyPath[0], lastName: keyPath[1] } } },
} as Prisma.Args<Prisma.ChildDelegate, "create">["data"],
},
tx,
);
}
}
if (query.data.children?.connect) {
Expand Down
Loading