Skip to content

Commit

Permalink
merge dev to main (v1.1.1) (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Oct 23, 2023
2 parents f2d6fee + e41fc74 commit e04cd46
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 11 deletions.
14 changes: 8 additions & 6 deletions packages/runtime/src/enhancements/nested-write-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,14 @@ export class NestedWriteVisitor {
}

if (fieldInfo.isDataModel) {
// recurse into nested payloads
for (const [subAction, subData] of Object.entries<any>(payload[field])) {
if (this.isPrismaWriteAction(subAction) && subData) {
await this.doVisit(fieldInfo.type, subAction, subData, payload[field], fieldInfo, [
...nestingPath,
]);
if (payload[field]) {
// recurse into nested payloads
for (const [subAction, subData] of Object.entries<any>(payload[field])) {
if (this.isPrismaWriteAction(subAction) && subData) {
await this.doVisit(fieldInfo.type, subAction, subData, payload[field], fieldInfo, [
...nestingPath,
]);
}
}
}
} else {
Expand Down
8 changes: 5 additions & 3 deletions packages/runtime/src/enhancements/policy/policy-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,10 @@ export class PolicyUtil {
) {
// multi-field unique constraint, flatten it
delete args[field];
for (const [f, v] of Object.entries(value)) {
args[f] = v;
if (value) {
for (const [f, v] of Object.entries(value)) {
args[f] = v;
}
}
}
}
Expand Down Expand Up @@ -515,7 +517,7 @@ export class PolicyUtil {
throw this.unknownError(`missing backLink field ${currField.backLink} in ${currField.type}`);
}

if (backLinkField.isArray) {
if (backLinkField.isArray && !mutating) {
// many-side of relationship, wrap with "some" query
currQuery[currField.backLink] = { some: { ...visitWhere } };
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/tests/cli/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('CLI Plugins Tests', () => {
'[email protected]',
'react',
'swr',
'@tanstack/react-query',
'@tanstack/react-query@^4.0.0',
'@trpc/server',
'@prisma/client@^4.0.0',
`${path.join(__dirname, '../../../../.build/zenstackhq-language-' + ver + '.tgz')}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe('With Policy:nested to-many', () => {
expect(r.m2).toEqual(expect.arrayContaining([expect.objectContaining({ id: '2', value: 3 })]));
});

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

it('update with create from many to one', async () => {
const { withPolicy } = await loadSchema(
`
model M1 {
id String @id @default(uuid())
value Int
m2 M2[]
@@allow('read', true)
@@allow('create', value > 0)
@@allow('update', value > 1)
}
model M2 {
id String @id @default(uuid())
m1 M1? @relation(fields: [m1Id], references:[id])
m1Id String?
@@allow('all', true)
}
`
);

const db = withPolicy();

await db.m2.create({ data: { id: '1' } });

await expect(
db.m2.update({
where: { id: '1' },
data: {
m1: {
create: { value: 0 },
},
},
})
).toBeRejectedByPolicy();

await expect(
db.m2.update({
where: { id: '1' },
data: {
m1: {
create: { value: 1 },
},
},
})
).toResolveTruthy();
});

it('update with delete', async () => {
const { withPolicy, prisma } = await loadSchema(
`
Expand Down
49 changes: 49 additions & 0 deletions tests/integration/tests/regression/issue-764.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('Regression: issue 764', () => {
it('regression', async () => {
const { prisma, enhance } = await loadSchema(
`
model User {
id Int @id @default(autoincrement())
name String
post Post? @relation(fields: [postId], references: [id])
postId Int?
@@allow('all', true)
}
model Post {
id Int @id @default(autoincrement())
title String
User User[]
@@allow('all', true)
}
`
);

const db = enhance();

const user = await prisma.user.create({
data: { name: 'Me' },
});

await db.user.update({
where: { id: user.id },
data: {
post: {
upsert: {
create: {
title: 'Hello World',
},
update: {
title: 'Hello World',
},
},
},
},
});
});
});
37 changes: 37 additions & 0 deletions tests/integration/tests/regression/issue-765.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('Regression: issue 765', () => {
it('regression', async () => {
const { enhance } = await loadSchema(
`
model User {
id Int @id @default(autoincrement())
name String
post Post? @relation(fields: [postId], references: [id])
postId Int?
@@allow('all', true)
}
model Post {
id Int @id @default(autoincrement())
title String
User User[]
@@allow('all', true)
}
`
);

const db = enhance();
const r = await db.user.create({
data: {
name: 'Me',
post: undefined,
},
});
expect(r.name).toBe('Me');
expect(r.post).toBeUndefined();
});
});

0 comments on commit e04cd46

Please sign in to comment.