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

merge dev to main (v1.10.2) #1103

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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.10.1",
"version": "1.10.2",
"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 = "1.10.1"
version = "1.10.2"

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": "1.10.1",
"version": "1.10.2",
"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": "1.10.1",
"version": "1.10.2",
"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": "1.10.1",
"version": "1.10.2",
"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": "1.10.1",
"version": "1.10.2",
"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.10.1",
"version": "1.10.2",
"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.10.1",
"version": "1.10.2",
"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.10.1",
"version": "1.10.2",
"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.10.1",
"version": "1.10.2",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
32 changes: 22 additions & 10 deletions packages/runtime/src/cross/nested-write-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { FieldInfo, ModelMeta } from './model-meta';
import { resolveField } from './model-meta';
import { MaybePromise, PrismaWriteActionType, PrismaWriteActions } from './types';
import { enumerate, getModelFields } from './utils';
import { getModelFields } from './utils';

type NestingPathItem = { field?: FieldInfo; model: string; where: any; unique: boolean };

Expand Down Expand Up @@ -155,7 +155,7 @@ export class NestedWriteVisitor {
// visit payload
switch (action) {
case 'create':
for (const item of enumerate(data)) {
for (const item of this.enumerateReverse(data)) {
const newContext = pushNewContext(field, model, {});
let callbackResult: any;
if (this.callback.create) {
Expand Down Expand Up @@ -183,7 +183,7 @@ export class NestedWriteVisitor {
break;

case 'connectOrCreate':
for (const item of enumerate(data)) {
for (const item of this.enumerateReverse(data)) {
const newContext = pushNewContext(field, model, item.where);
let callbackResult: any;
if (this.callback.connectOrCreate) {
Expand All @@ -198,7 +198,7 @@ export class NestedWriteVisitor {

case 'connect':
if (this.callback.connect) {
for (const item of enumerate(data)) {
for (const item of this.enumerateReverse(data)) {
const newContext = pushNewContext(field, model, item, true);
await this.callback.connect(model, item, newContext);
}
Expand All @@ -210,7 +210,7 @@ export class NestedWriteVisitor {
// if relation is to-many, the payload is a unique filter object
// if relation is to-one, the payload can only be boolean `true`
if (this.callback.disconnect) {
for (const item of enumerate(data)) {
for (const item of this.enumerateReverse(data)) {
const newContext = pushNewContext(field, model, item, typeof item === 'object');
await this.callback.disconnect(model, item, newContext);
}
Expand All @@ -225,7 +225,7 @@ export class NestedWriteVisitor {
break;

case 'update':
for (const item of enumerate(data)) {
for (const item of this.enumerateReverse(data)) {
const newContext = pushNewContext(field, model, item.where);
let callbackResult: any;
if (this.callback.update) {
Expand All @@ -244,7 +244,7 @@ export class NestedWriteVisitor {
break;

case 'updateMany':
for (const item of enumerate(data)) {
for (const item of this.enumerateReverse(data)) {
const newContext = pushNewContext(field, model, item.where);
let callbackResult: any;
if (this.callback.updateMany) {
Expand All @@ -258,7 +258,7 @@ export class NestedWriteVisitor {
break;

case 'upsert': {
for (const item of enumerate(data)) {
for (const item of this.enumerateReverse(data)) {
const newContext = pushNewContext(field, model, item.where);
let callbackResult: any;
if (this.callback.upsert) {
Expand All @@ -278,7 +278,7 @@ export class NestedWriteVisitor {

case 'delete': {
if (this.callback.delete) {
for (const item of enumerate(data)) {
for (const item of this.enumerateReverse(data)) {
const newContext = pushNewContext(field, model, toplevel ? item.where : item);
await this.callback.delete(model, item, newContext);
}
Expand All @@ -288,7 +288,7 @@ export class NestedWriteVisitor {

case 'deleteMany':
if (this.callback.deleteMany) {
for (const item of enumerate(data)) {
for (const item of this.enumerateReverse(data)) {
const newContext = pushNewContext(field, model, toplevel ? item.where : item);
await this.callback.deleteMany(model, item, newContext);
}
Expand Down Expand Up @@ -336,4 +336,16 @@ export class NestedWriteVisitor {
}
}
}

// enumerate a (possible) array in reverse order, so that the enumeration
// callback can safely delete the current item
private *enumerateReverse(data: any) {
if (Array.isArray(data)) {
for (let i = data.length - 1; i >= 0; i--) {
yield data[i];
}
} else {
yield data;
}
}
}
53 changes: 35 additions & 18 deletions packages/runtime/src/enhancements/policy/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,29 +343,19 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
}
}

if (context.parent.connect) {
// if the payload parent already has a "connect" clause, merge it
if (Array.isArray(context.parent.connect)) {
context.parent.connect.push(args.where);
} else {
context.parent.connect = [context.parent.connect, args.where];
}
} else {
// otherwise, create a new "connect" clause
context.parent.connect = args.where;
}
this.mergeToParent(context.parent, 'connect', args.where);
// record the key of connected entities so we can avoid validating them later
connectedEntities.add(getEntityKey(model, existing));
} else {
// create case
pushIdFields(model, context);

// create a new "create" clause at the parent level
context.parent.create = args.create;
this.mergeToParent(context.parent, 'create', args.create);
}

// remove the connectOrCreate clause
delete context.parent['connectOrCreate'];
this.removeFromParent(context.parent, 'connectOrCreate', args);

// return false to prevent visiting the nested payload
return false;
Expand Down Expand Up @@ -895,7 +885,7 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
await _create(model, args, context);

// remove it from the update payload
delete context.parent.create;
this.removeFromParent(context.parent, 'create', args);

// don't visit payload
return false;
Expand Down Expand Up @@ -928,22 +918,23 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
await _registerPostUpdateCheck(model, uniqueFilter);

// convert upsert to update
context.parent.update = {
const convertedUpdate = {
where: args.where,
data: this.validateUpdateInputSchema(model, args.update),
};
delete context.parent.upsert;
this.mergeToParent(context.parent, 'update', convertedUpdate);
this.removeFromParent(context.parent, 'upsert', args);

// continue visiting the new payload
return context.parent.update;
return convertedUpdate;
} else {
// create case

// process the entire create subtree separately
await _create(model, args.create, context);

// remove it from the update payload
delete context.parent.upsert;
this.removeFromParent(context.parent, 'upsert', args);

// don't visit payload
return false;
Expand Down Expand Up @@ -1390,5 +1381,31 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
return requireField(this.modelMeta, fieldInfo.type, fieldInfo.backLink);
}

private mergeToParent(parent: any, key: string, value: any) {
if (parent[key]) {
if (Array.isArray(parent[key])) {
parent[key].push(value);
} else {
parent[key] = [parent[key], value];
}
} else {
parent[key] = value;
}
}

private removeFromParent(parent: any, key: string, data: any) {
if (parent[key] === data) {
delete parent[key];
} else if (Array.isArray(parent[key])) {
const idx = parent[key].indexOf(data);
if (idx >= 0) {
parent[key].splice(idx, 1);
if (parent[key].length === 0) {
delete parent[key];
}
}
}
}

//#endregion
}
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": "Build scalable web apps with minimum code by defining authorization and validation rules inside the data schema that closer to the database",
"version": "1.10.1",
"version": "1.10.2",
"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": "1.10.1",
"version": "1.10.2",
"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.10.1",
"version": "1.10.2",
"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.10.1",
"version": "1.10.2",
"description": "ZenStack Test Tools",
"main": "index.js",
"private": true,
Expand Down
25 changes: 14 additions & 11 deletions tests/integration/tests/regression/issue-1078.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { loadSchema } from '@zenstackhq/testtools';

describe('issue 1078', () => {
it('regression', async () => {
const { prisma, enhance } = await loadSchema(
const { enhance } = await loadSchema(
`
model Counter {
id String @id
Expand All @@ -12,21 +12,25 @@ describe('issue 1078', () => {

@@validate(value >= 0)
@@allow('all', true)
}
}
`
);

const db = enhance();

const counter = await db.counter.create({
data: { id: '1', name: 'It should create', value: 1 },
});
await expect(
db.counter.create({
data: { id: '1', name: 'It should create', value: 1 },
})
).toResolveTruthy();

//! This query fails validation
const updated = await db.counter.update({
where: { id: '1' },
data: { name: 'It should update' },
});
await expect(
db.counter.update({
where: { id: '1' },
data: { name: 'It should update' },
})
).toResolveTruthy();
});

it('read', async () => {
Expand All @@ -37,8 +41,7 @@ describe('issue 1078', () => {
title String @allow('read', true, true)
content String
}
`,
{ logPrismaQuery: true }
`
);

const db = enhance();
Expand Down
Loading
Loading