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): push base-level id assignment to base payload when creating a concrete entity #1521

Merged
merged 2 commits into from
Jun 19, 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
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',
},
});
});
});
Loading