Skip to content

Commit e04cd46

Browse files
authored
merge dev to main (v1.1.1) (#779)
2 parents f2d6fee + e41fc74 commit e04cd46

File tree

6 files changed

+151
-11
lines changed

6 files changed

+151
-11
lines changed

packages/runtime/src/enhancements/nested-write-visitor.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,14 @@ export class NestedWriteVisitor {
323323
}
324324

325325
if (fieldInfo.isDataModel) {
326-
// recurse into nested payloads
327-
for (const [subAction, subData] of Object.entries<any>(payload[field])) {
328-
if (this.isPrismaWriteAction(subAction) && subData) {
329-
await this.doVisit(fieldInfo.type, subAction, subData, payload[field], fieldInfo, [
330-
...nestingPath,
331-
]);
326+
if (payload[field]) {
327+
// recurse into nested payloads
328+
for (const [subAction, subData] of Object.entries<any>(payload[field])) {
329+
if (this.isPrismaWriteAction(subAction) && subData) {
330+
await this.doVisit(fieldInfo.type, subAction, subData, payload[field], fieldInfo, [
331+
...nestingPath,
332+
]);
333+
}
332334
}
333335
}
334336
} else {

packages/runtime/src/enhancements/policy/policy-utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,10 @@ export class PolicyUtil {
466466
) {
467467
// multi-field unique constraint, flatten it
468468
delete args[field];
469-
for (const [f, v] of Object.entries(value)) {
470-
args[f] = v;
469+
if (value) {
470+
for (const [f, v] of Object.entries(value)) {
471+
args[f] = v;
472+
}
471473
}
472474
}
473475
}
@@ -515,7 +517,7 @@ export class PolicyUtil {
515517
throw this.unknownError(`missing backLink field ${currField.backLink} in ${currField.type}`);
516518
}
517519

518-
if (backLinkField.isArray) {
520+
if (backLinkField.isArray && !mutating) {
519521
// many-side of relationship, wrap with "some" query
520522
currQuery[currField.backLink] = { some: { ...visitWhere } };
521523
} else {

tests/integration/tests/cli/plugins.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('CLI Plugins Tests', () => {
7171
7272
'react',
7373
'swr',
74-
'@tanstack/react-query',
74+
'@tanstack/react-query@^4.0.0',
7575
'@trpc/server',
7676
'@prisma/client@^4.0.0',
7777
`${path.join(__dirname, '../../../../.build/zenstackhq-language-' + ver + '.tgz')}`,

tests/integration/tests/enhancements/with-policy/nested-to-many.test.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ describe('With Policy:nested to-many', () => {
284284
expect(r.m2).toEqual(expect.arrayContaining([expect.objectContaining({ id: '2', value: 3 })]));
285285
});
286286

287-
it('update with create', async () => {
287+
it('update with create from one to many', async () => {
288288
const { withPolicy } = await loadSchema(
289289
`
290290
model M1 {
@@ -341,6 +341,56 @@ describe('With Policy:nested to-many', () => {
341341
expect(r.m2).toHaveLength(3);
342342
});
343343

344+
it('update with create from many to one', async () => {
345+
const { withPolicy } = await loadSchema(
346+
`
347+
model M1 {
348+
id String @id @default(uuid())
349+
value Int
350+
m2 M2[]
351+
352+
@@allow('read', true)
353+
@@allow('create', value > 0)
354+
@@allow('update', value > 1)
355+
}
356+
357+
model M2 {
358+
id String @id @default(uuid())
359+
m1 M1? @relation(fields: [m1Id], references:[id])
360+
m1Id String?
361+
362+
@@allow('all', true)
363+
}
364+
`
365+
);
366+
367+
const db = withPolicy();
368+
369+
await db.m2.create({ data: { id: '1' } });
370+
371+
await expect(
372+
db.m2.update({
373+
where: { id: '1' },
374+
data: {
375+
m1: {
376+
create: { value: 0 },
377+
},
378+
},
379+
})
380+
).toBeRejectedByPolicy();
381+
382+
await expect(
383+
db.m2.update({
384+
where: { id: '1' },
385+
data: {
386+
m1: {
387+
create: { value: 1 },
388+
},
389+
},
390+
})
391+
).toResolveTruthy();
392+
});
393+
344394
it('update with delete', async () => {
345395
const { withPolicy, prisma } = await loadSchema(
346396
`
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('Regression: issue 764', () => {
4+
it('regression', async () => {
5+
const { prisma, enhance } = await loadSchema(
6+
`
7+
model User {
8+
id Int @id @default(autoincrement())
9+
name String
10+
11+
post Post? @relation(fields: [postId], references: [id])
12+
postId Int?
13+
14+
@@allow('all', true)
15+
}
16+
17+
model Post {
18+
id Int @id @default(autoincrement())
19+
title String
20+
User User[]
21+
22+
@@allow('all', true)
23+
}
24+
`
25+
);
26+
27+
const db = enhance();
28+
29+
const user = await prisma.user.create({
30+
data: { name: 'Me' },
31+
});
32+
33+
await db.user.update({
34+
where: { id: user.id },
35+
data: {
36+
post: {
37+
upsert: {
38+
create: {
39+
title: 'Hello World',
40+
},
41+
update: {
42+
title: 'Hello World',
43+
},
44+
},
45+
},
46+
},
47+
});
48+
});
49+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('Regression: issue 765', () => {
4+
it('regression', async () => {
5+
const { enhance } = await loadSchema(
6+
`
7+
model User {
8+
id Int @id @default(autoincrement())
9+
name String
10+
11+
post Post? @relation(fields: [postId], references: [id])
12+
postId Int?
13+
14+
@@allow('all', true)
15+
}
16+
17+
model Post {
18+
id Int @id @default(autoincrement())
19+
title String
20+
User User[]
21+
22+
@@allow('all', true)
23+
}
24+
`
25+
);
26+
27+
const db = enhance();
28+
const r = await db.user.create({
29+
data: {
30+
name: 'Me',
31+
post: undefined,
32+
},
33+
});
34+
expect(r.name).toBe('Me');
35+
expect(r.post).toBeUndefined();
36+
});
37+
});

0 commit comments

Comments
 (0)