Skip to content

Commit

Permalink
fix(polymorphism): wrong query result when selecting "_count"
Browse files Browse the repository at this point in the history
Fixes #1123
  • Loading branch information
ymc9 committed Mar 11, 2024
1 parent c0a9a02 commit 8be5b35
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 20 deletions.
56 changes: 36 additions & 20 deletions packages/runtime/src/enhancements/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1106,23 +1106,31 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {

const modelInfo = getModelInfo(this.options.modelMeta, model, true);

for (const field of Object.values(modelInfo.fields)) {
for (const [key, value] of Object.entries(entity)) {
if (key.startsWith(DELEGATE_AUX_RELATION_PREFIX)) {
continue;
}

const field = modelInfo.fields[key];
if (!field) {
// not a field, could be `_count`, `_sum`, etc.
result[key] = value;
continue;
}

if (field.inheritedFrom) {
// already merged from base
continue;
}

if (field.name in entity) {
const fieldValue = entity[field.name];
if (field.isDataModel) {
if (Array.isArray(fieldValue)) {
result[field.name] = fieldValue.map((item) => this.assembleUp(field.type, item));
} else {
result[field.name] = this.assembleUp(field.type, fieldValue);
}
if (field.isDataModel) {
if (Array.isArray(value)) {
result[field.name] = value.map((item) => this.assembleUp(field.type, item));
} else {
result[field.name] = fieldValue;
result[field.name] = this.assembleUp(field.type, value);
}
} else {
result[field.name] = value;
}
}

Expand All @@ -1147,18 +1155,26 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
}
}

for (const field of Object.values(modelInfo.fields)) {
if (field.name in entity) {
const fieldValue = entity[field.name];
if (field.isDataModel) {
if (Array.isArray(fieldValue)) {
result[field.name] = fieldValue.map((item) => this.assembleDown(field.type, item));
} else {
result[field.name] = this.assembleDown(field.type, fieldValue);
}
for (const [key, value] of Object.entries(entity)) {
if (key.startsWith(DELEGATE_AUX_RELATION_PREFIX)) {
continue;
}

const field = modelInfo.fields[key];
if (!field) {
// not a field, could be `_count`, `_sum`, etc.
result[key] = value;
continue;
}

if (field.isDataModel) {
if (Array.isArray(value)) {
result[field.name] = value.map((item) => this.assembleDown(field.type, item));
} else {
result[field.name] = fieldValue;
result[field.name] = this.assembleDown(field.type, value);
}
} else {
result[field.name] = value;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('Regression for issue 1123', () => {
it('regression', async () => {
const { enhance } = await loadSchema(
`
model Content {
id String @id @default(cuid())
published Boolean @default(false)
contentType String
likes Like[]
@@delegate(contentType)
@@allow('all', true)
}
model Post extends Content {
title String
}
model Image extends Content {
url String
}
model Like {
id String @id @default(cuid())
content Content @relation(fields: [contentId], references: [id])
contentId String
@@allow('all', true)
}
`
);

const db = enhance();
await db.post.create({
data: {
title: 'a post',
likes: { create: {} },
},
});

await expect(db.content.findFirst({ include: { _count: { select: { likes: true } } } })).resolves.toMatchObject(
{
_count: { likes: 1 },
}
);
});
});

0 comments on commit 8be5b35

Please sign in to comment.