Skip to content

Commit

Permalink
fix(delegate): push base-level id assignment to base payload when cre…
Browse files Browse the repository at this point in the history
…ating a concrete entity (#1521)
  • Loading branch information
ymc9 authored Jun 19, 2024
1 parent 908048b commit a14bf29
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/runtime/src/enhancements/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
}

// ensure the full nested "create" structure is created for base types
private ensureBaseCreateHierarchy(model: string, result: any) {
let curr = result;
private ensureBaseCreateHierarchy(model: string, args: any) {
let curr = args;
let base = this.getBaseModel(model);
let sub = this.getModelInfo(model);

Expand All @@ -478,6 +478,16 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
curr[baseRelationName].create[base.discriminator] = sub.name;
}
}

// Look for base id field assignments in the current level, and push
// them down to the base level
for (const idField of getIdFields(this.options.modelMeta, base.name)) {
if (curr[idField.name] !== undefined) {
curr[baseRelationName].create[idField.name] = curr[idField.name];
delete curr[idField.name];
}
}

curr = curr[baseRelationName].create;
sub = base;
base = this.getBaseModel(base.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ describe('Polymorphism Test', () => {
).resolves.toMatchObject({ count: 2 });
});

it('create concrete with explicit id', async () => {
const { enhance } = await loadSchema(schema, { enhancements: ['delegate'] });
const db = enhance();

await expect(
db.ratedVideo.create({ data: { id: 1, duration: 100, url: 'xyz', rating: 5 } })
).resolves.toMatchObject({
id: 1,
duration: 100,
url: 'xyz',
rating: 5,
assetType: 'Video',
videoType: 'RatedVideo',
});
});

it('read with concrete', async () => {
const { db, user, video } = await setup();

Expand Down
31 changes: 31 additions & 0 deletions tests/regression/tests/issue-1518.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { loadSchema } from '@zenstackhq/testtools';
describe('issue 1518', () => {
it('regression', async () => {
const { enhance } = await loadSchema(
`
model Activity {
id String @id @default(uuid())
title String
type String
@@delegate(type)
@@allow('all', true)
}
model TaskActivity extends Activity {
description String
@@map("task_activity")
@@allow('all', true)
}
`
);

const db = enhance();
await db.taskActivity.create({
data: {
id: '00000000-0000-0000-0000-111111111111',
title: 'Test Activity',
description: 'Description of task',
},
});
});
});

0 comments on commit a14bf29

Please sign in to comment.