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): avoid merging into object of Decimal, Date, etc. #1489

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"decimal.js": "^10.4.2",
"deepcopy": "^2.1.0",
"deepmerge": "^4.3.1",
"is-plain-object": "^5.0.0",
"logic-solver": "^2.0.1",
"lower-case-first": "^2.0.2",
"pluralize": "^8.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime/src/enhancements/delegate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import deepcopy from 'deepcopy';
import deepmerge, { type ArrayMergeOptions } from 'deepmerge';
import { isPlainObject } from 'is-plain-object';
import { lowerCaseFirst } from 'lower-case-first';
import { DELEGATE_AUX_RELATION_PREFIX } from '../constants';
import {
Expand Down Expand Up @@ -1094,6 +1095,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {

const result = deepmerge(upMerged, downMerged, {
arrayMerge: combineMerge,
isMergeableObject: isPlainObject, // avoid messing with Decimal, Date, etc.
});
return result;
}
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions tests/regression/tests/issue-1487.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { createPostgresDb, dropPostgresDb, loadSchema } from '@zenstackhq/testtools';
import Decimal from 'decimal.js';

describe('issue 1487', () => {
it('regression2', async () => {
const dbUrl = await createPostgresDb('issue-1487');
let prisma: any;

try {
const r = await loadSchema(
`
model LineItem {
id Int @id @default(autoincrement())
price Decimal
createdAt DateTime @default(now())
orderId Int
order Order @relation(fields: [orderId], references: [id])
}
model Order extends BaseType {
total Decimal
createdAt DateTime @default(now())
lineItems LineItem[]
}
model BaseType {
id Int @id @default(autoincrement())
entityType String
@@delegate(entityType)
}
`,
{
provider: 'postgresql',
dbUrl,
enhancements: ['omit', 'delegate'],
}
);

prisma = r.prisma;
const db = r.enhance();

const create = await db.Order.create({
data: {
total: new Decimal(100_100.99),
lineItems: { create: [{ price: 90_000.66 }, { price: 20_100.33 }] },
},
});

const order = await db.Order.findFirst({ where: { id: create.id }, include: { lineItems: true } });
expect(Decimal.isDecimal(order.total)).toBe(true);
expect(order.createdAt instanceof Date).toBe(true);
expect(order.total.toString()).toEqual('100100.99');
order.lineItems.forEach((item: any) => {
expect(Decimal.isDecimal(item.price)).toBe(true);
expect(item.price.toString()).not.toEqual('[object Object]');
});

const lineItems = await db.LineItem.findMany();
lineItems.forEach((item: any) => {
expect(item.createdAt instanceof Date).toBe(true);
expect(Decimal.isDecimal(item.price)).toBe(true);
expect(item.price.toString()).not.toEqual('[object Object]');
});
} finally {
if (prisma) {
await prisma.$disconnect();
}
await dropPostgresDb('issue-1487');
}
});
});
Loading