Skip to content

Commit

Permalink
Merge pull request #86 from prisma-idb/83-explicit-many-to-many
Browse files Browse the repository at this point in the history
Explicit many to many
  • Loading branch information
WhyAsh5114 authored Dec 11, 2024
2 parents ef5e7b8 + d0927fa commit f3f7aad
Show file tree
Hide file tree
Showing 13 changed files with 1,422 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function applyClausesAndReturnRecords(writer: CodeBlockWriter, model: Model) {
writer
.writeLine(`const data = (await tx.objectStore("${model.name}").get(keyPath))!;`)
.write(`const recordsWithRelations = this._applySelectClause`)
.write(`(await this._applyRelations([data], tx, query), query.select)[0];`)
.write(`(await this._applyRelations<object>([data], tx, query), query.select)[0];`)
.writeLine(`this._preprocessListFields([recordsWithRelations]);`);

writer.writeLine(`return recordsWithRelations as Prisma.Result<Prisma.${model.name}Delegate, Q, "create">;`);
Expand Down Expand Up @@ -182,7 +182,7 @@ function addOneToManyRelation(
const nestedDirectLine = otherField.relationFromFields!.map((field, idx) => `${field}: keyPath[${idx}]`).join(", ");
const connectQuery = getCreateQuery(nestedConnectLine);

writer.writeLine(`if (query.data.${field.name}?.create)`).block(() => {
writer.writeLine(`if (query.data?.${field.name}?.create)`).block(() => {
if (fkFields.length === 1) {
writer.writeLine(`for (const elem of IDBUtils.convertToArray(query.data.${field.name}.create))`).block(() => {
writer.writeLine(connectQuery);
Expand All @@ -203,7 +203,7 @@ function addOneToManyRelation(
}
});

writer.writeLine(`if (query.data.${field.name}?.connect)`).block(() => {
writer.writeLine(`if (query.data?.${field.name}?.connect)`).block(() => {
writer
.writeLine(`await Promise.all(`)
.indent(() => {
Expand All @@ -218,10 +218,10 @@ function addOneToManyRelation(
})
.writeLine(");");
});
writer.writeLine(`if (query.data.${field.name}?.connectOrCreate)`).block(() => {
writer.writeLine(`if (query.data?.${field.name}?.connectOrCreate)`).block(() => {
writer.writeLine(`throw new Error('connectOrCreate not yet implemented')`);
});
writer.writeLine(`if (query.data.${field.name}?.createMany)`).block(() => {
writer.writeLine(`if (query.data?.${field.name}?.createMany)`).block(() => {
writer
.write(`await this.client.${toCamelCase(field.type)}.createMany(`)
.block(() => {
Expand All @@ -242,7 +242,7 @@ function handleForeignKeyValidation(writer: CodeBlockWriter, field: Field, fkFie
const otherModelPkFields = JSON.parse(otherModelPk.keyPath) as string[];

writer
.writeLine(`else if (query.data.${fkField.name} !== undefined && query.data.${fkField.name} !== null)`)
.writeLine(`else if (query.data?.${fkField.name} !== undefined && query.data.${fkField.name} !== null)`)
.block(() => {
writer
.writeLine(`await this.client.${toCamelCase(field.type)}.findUniqueOrThrow(`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function addFillDefaultsFunction(modelClass: ClassDeclaration, model: Mod
],
returnType: `Promise<D>`,
statements: (writer) => {
writer.writeLine("if (data === undefined) data = {} as D;");
writer.writeLine("if (data === undefined) data = {} as NonNullable<D>;");
model.fields
.filter(({ kind }) => kind !== "object")
.filter(({ hasDefaultValue, isRequired }) => hasDefaultValue || !isRequired)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function addGetNeededStoresForCreate(modelClass: ClassDeclaration, model:
function processRelationsInData(writer: CodeBlockWriter, model: Model) {
const relationFields = model.fields.filter(({ kind }) => kind === "object");
relationFields.forEach((field) => {
writer.writeLine(`if (data.${field.name})`).block(() => {
writer.writeLine(`if (data?.${field.name})`).block(() => {
writer.writeLine(`neededStores.add('${field.type}')`);
writer.writeLine(`if (data.${field.name}.create)`).block(() => {
writer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export function addRemoveNestedCreateDataMethod(modelClass: ClassDeclaration, mo
function addRelationProcessing(writer: CodeBlockWriter, model: Model) {
const relationFields = model.fields.filter(({ kind }) => kind === "object");
relationFields.forEach((field) => {
writer.writeLine(`delete recordWithoutNestedCreate.${field.name};`);
writer.writeLine(`delete recordWithoutNestedCreate?.${field.name};`);
});
}
2 changes: 1 addition & 1 deletion packages/usage/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default defineConfig({
],

webServer: {
command: "npx prisma db push --force-reset && npx prisma generate && npm run build && npm run preview",
command: "npx prisma db push --force-reset && npm run build && npm run preview",
port: 4173,
reuseExistingServer: false,
},
Expand Down
8 changes: 8 additions & 0 deletions packages/usage/src/prisma/prisma-idb/idb-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ export interface PrismaIDBSchema extends DBSchema {
key: [id: Prisma.User["id"]];
value: Prisma.User;
};
Group: {
key: [id: Prisma.Group["id"]];
value: Prisma.Group;
};
UserGroup: {
key: [groupId: Prisma.UserGroup["groupId"], userId: Prisma.UserGroup["userId"]];
value: Prisma.UserGroup;
};
Profile: {
key: [id: Prisma.Profile["id"]];
value: Prisma.Profile;
Expand Down
Loading

0 comments on commit f3f7aad

Please sign in to comment.