Skip to content

Commit

Permalink
fix: unintended changes to "backlink" of abstract model's metadata (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Sep 7, 2024
1 parent dc72b64 commit 9e93985
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 16 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": "2.5.0",
"version": "2.5.1",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/jetbrains/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "dev.zenstack"
version = "2.5.0"
version = "2.5.1"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/jetbrains/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jetbrains",
"version": "2.5.0",
"version": "2.5.1",
"displayName": "ZenStack JetBrains IDE Plugin",
"description": "ZenStack JetBrains IDE plugin",
"homepage": "https://zenstack.dev",
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": "2.5.0",
"version": "2.5.1",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/misc/redwood/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/redwood",
"displayName": "ZenStack RedwoodJS Integration",
"version": "2.5.0",
"version": "2.5.1",
"description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.",
"repository": {
"type": "git",
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": "2.5.0",
"version": "2.5.1",
"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": "2.5.0",
"version": "2.5.1",
"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": "2.5.0",
"version": "2.5.1",
"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": "2.5.0",
"version": "2.5.1",
"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": "2.5.0",
"version": "2.5.1",
"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": "FullStack enhancement for Prisma ORM: seamless integration from database to UI",
"version": "2.5.0",
"version": "2.5.1",
"author": {
"name": "ZenStack Team"
},
Expand Down
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": "2.5.0",
"version": "2.5.1",
"description": "ZenStack plugin development SDK",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 10 additions & 2 deletions packages/sdk/src/model-meta-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
getRelationField,
hasAttribute,
isAuthInvocation,
isDelegateModel,
isEnumFieldReference,
isForeignKeyField,
isIdField,
Expand Down Expand Up @@ -298,8 +299,15 @@ function getBackLink(field: DataModelField) {

const relName = getRelationName(field);

// in case of polymorphism, the source model is the base delegate model
const sourceModel = field.$inheritedFrom ?? (field.$container as DataModel);
let sourceModel: DataModel;
if (field.$inheritedFrom && isDelegateModel(field.$inheritedFrom)) {
// field is inherited from a delegate model, use it as the source
sourceModel = field.$inheritedFrom;
} else {
// otherwise use the field's container model as the source
sourceModel = field.$container as DataModel;
}

const targetModel = field.type.reference.ref as DataModel;

for (const otherField of targetModel.fields) {
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": "2.5.0",
"version": "2.5.1",
"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": "2.5.0",
"version": "2.5.1",
"description": "ZenStack Test Tools",
"main": "index.js",
"private": true,
Expand Down
91 changes: 91 additions & 0 deletions tests/integration/tests/enhancements/with-policy/abstract.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('Abstract models', () => {
it('connect test1', async () => {
const { enhance } = await loadSchema(
`
model User {
id Int @id @default(autoincrement())
profile Profile? @relation(fields: [profileId], references: [id])
profileId Int? @unique
@@allow('create,read', true)
@@allow('update', auth().id == 1)
}
abstract model BaseProfile {
id Int @id @default(autoincrement())
user User?
@@allow('all', true)
}
model Profile extends BaseProfile {
name String
}
`
);

const db = enhance({ id: 2 });
const user = await db.user.create({ data: { id: 1 } });
const profile = await db.profile.create({ data: { id: 1, name: 'John' } });
await expect(
db.profile.update({ where: { id: 1 }, data: { user: { connect: { id: user.id } } } })
).toBeRejectedByPolicy();
await expect(
db.user.update({ where: { id: 1 }, data: { profile: { connect: { id: profile.id } } } })
).toBeRejectedByPolicy();

const db1 = enhance({ id: 1 });
await expect(
db1.profile.update({ where: { id: 1 }, data: { user: { connect: { id: user.id } } } })
).toResolveTruthy();
await expect(
db1.user.update({ where: { id: 1 }, data: { profile: { connect: { id: profile.id } } } })
).toResolveTruthy();
});

it('connect test2', async () => {
const { enhance } = await loadSchema(
`
model User {
id Int @id @default(autoincrement())
profile Profile?
@@allow('all', true)
}
abstract model BaseProfile {
id Int @id @default(autoincrement())
user User? @relation(fields: [userId], references: [id])
userId Int? @unique
@@allow('create,read', true)
@@allow('update', auth().id == 1)
}
model Profile extends BaseProfile {
name String
}
`
);

const db = enhance({ id: 2 });
const user = await db.user.create({ data: { id: 1 } });
const profile = await db.profile.create({ data: { id: 1, name: 'John' } });
await expect(
db.profile.update({ where: { id: 1 }, data: { user: { connect: { id: user.id } } } })
).toBeRejectedByPolicy();
await expect(
db.user.update({ where: { id: 1 }, data: { profile: { connect: { id: profile.id } } } })
).toBeRejectedByPolicy();

const db1 = enhance({ id: 1 });
await expect(
db1.profile.update({ where: { id: 1 }, data: { user: { connect: { id: user.id } } } })
).toResolveTruthy();
await expect(
db1.user.update({ where: { id: 1 }, data: { profile: { connect: { id: profile.id } } } })
).toResolveTruthy();
});
});

0 comments on commit 9e93985

Please sign in to comment.