Skip to content

Commit

Permalink
fix: incorrect relation owner analysis (zenstackhq#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Jul 31, 2023
2 parents ea7de9c + 86ecb4c commit c89012b
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "1.0.0-beta.12",
"version": "1.0.0-beta.13",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/language",
"version": "1.0.0-beta.12",
"version": "1.0.0-beta.13",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/openapi",
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
"version": "1.0.0-beta.12",
"version": "1.0.0-beta.13",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/swr/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/swr",
"displayName": "ZenStack plugin for generating SWR hooks",
"version": "1.0.0-beta.12",
"version": "1.0.0-beta.13",
"description": "ZenStack plugin for generating SWR hooks",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/tanstack-query/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/tanstack-query",
"displayName": "ZenStack plugin for generating tanstack-query hooks",
"version": "1.0.0-beta.12",
"version": "1.0.0-beta.13",
"description": "ZenStack plugin for generating tanstack-query hooks",
"main": "index.js",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/trpc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/trpc",
"displayName": "ZenStack plugin for tRPC",
"version": "1.0.0-beta.12",
"version": "1.0.0-beta.13",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/runtime",
"displayName": "ZenStack Runtime Library",
"version": "1.0.0-beta.12",
"version": "1.0.0-beta.13",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "A toolkit for building secure CRUD apps with Next.js + Typescript",
"version": "1.0.0-beta.12",
"version": "1.0.0-beta.13",
"author": {
"name": "ZenStack Team"
},
Expand Down
24 changes: 18 additions & 6 deletions packages/schema/src/plugins/model-meta/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { RuntimeAttribute } from '@zenstackhq/runtime';
import {
createProject,
emitProject,
getAttributeArg,
getAttributeArgs,
getDataModels,
getLiteral,
Expand Down Expand Up @@ -182,14 +183,25 @@ function isRelationOwner(field: DataModelField, backLink: DataModelField | undef
return false;
}

if (hasAttribute(field, '@relation')) {
// this field has `@relation` attribute
if (!backLink) {
// CHECKME: can this really happen?
return true;
} else if (!backLink || !hasAttribute(backLink, '@relation')) {
// if the opposite side field doesn't have `@relation` attribute either,
// it's an implicit many-to-many relation, both sides are owners
}

if (!hasAttribute(field, '@relation') && !hasAttribute(backLink, '@relation')) {
// if neither side has `@relation` attribute, it's an implicit many-to-many relation,
// both sides are owners
return true;
} else {
}

return holdsForeignKey(field);
}

function holdsForeignKey(field: DataModelField) {
const relation = field.attributes.find((attr) => attr.decl.ref?.name === '@relation');
if (!relation) {
return false;
}
const fields = getAttributeArg(relation, 'fields');
return !!fields;
}
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/sdk",
"version": "1.0.0-beta.12",
"version": "1.0.0-beta.13",
"description": "ZenStack plugin development SDK",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/server",
"version": "1.0.0-beta.12",
"version": "1.0.0-beta.13",
"displayName": "ZenStack Server-side Adapters",
"description": "ZenStack server-side adapters",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/testtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/testtools",
"version": "1.0.0-beta.12",
"version": "1.0.0-beta.13",
"description": "ZenStack Test Tools",
"main": "index.js",
"publishConfig": {
Expand Down
54 changes: 54 additions & 0 deletions tests/integration/tests/regression/issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,58 @@ describe('GitHub issues regression', () => {
},
});
});

it('issue 609', async () => {
const { withPolicy, prisma } = await loadSchema(
`
model User {
id String @id @default(cuid())
comments Comment[]
}
model Comment {
id String @id @default(cuid())
parentCommentId String?
replies Comment[] @relation("CommentToComment")
parent Comment? @relation("CommentToComment", fields: [parentCommentId], references: [id])
comment String
author User @relation(fields: [authorId], references: [id])
authorId String
@@allow('read,create', true)
@@allow('update,delete', auth() == author)
}
`
);

await prisma.user.create({
data: {
id: '1',
comments: {
create: {
id: '1',
comment: 'Comment 1',
},
},
},
});

await prisma.user.create({
data: {
id: '2',
},
});

// connecting a child comment from a different user to a parent comment should succeed
const db = withPolicy({ id: '2' });
await expect(
db.comment.create({
data: {
comment: 'Comment 2',
author: { connect: { id: '2' } },
parent: { connect: { id: '1' } },
},
})
).toResolveTruthy();
});
});

0 comments on commit c89012b

Please sign in to comment.