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

fix(delegate): null reference when reading an optional relation #1491

Merged
merged 1 commit into from
Jun 7, 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
8 changes: 8 additions & 0 deletions packages/runtime/src/enhancements/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,10 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
}

private assembleUp(model: string, entity: any) {
if (!entity) {
return entity;
}

const result: any = {};
const base = this.getBaseModel(model);

Expand Down Expand Up @@ -1148,6 +1152,10 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
}

private assembleDown(model: string, entity: any) {
if (!entity) {
return entity;
}

const result: any = {};
const modelInfo = getModelInfo(this.options.modelMeta, model, true);

Expand Down
68 changes: 68 additions & 0 deletions tests/regression/tests/issue-1483.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { loadSchema } from '@zenstackhq/testtools';
describe('issue 1483', () => {
it('regression', async () => {
const { enhance } = await loadSchema(
`
model User {
@@auth
id String @id
edits Edit[]
@@allow('all', true)
}

model Entity {

id String @id @default(cuid())
name String
edits Edit[]

type String
@@delegate(type)

@@allow('all', true)
}

model Person extends Entity {
}

model Edit {
id String @id @default(cuid())

authorId String?
author User? @relation(fields: [authorId], references: [id], onDelete: Cascade, onUpdate: NoAction)

entityId String
entity Entity @relation(fields: [entityId], references: [id], onDelete: Cascade, onUpdate: NoAction)

@@allow('all', true)
}
`
);

const db = enhance();
await db.edit.deleteMany({});
await db.person.deleteMany({});
await db.user.deleteMany({});

const person = await db.person.create({
data: {
name: 'test',
},
});

await db.edit.create({
data: {
entityId: person.id,
},
});

await expect(
db.edit.findMany({
include: {
author: true,
entity: true,
},
})
).resolves.toHaveLength(1);
});
});
Loading