Skip to content

Commit

Permalink
merge dev to main (v2.3.1) (#1593)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason MacDonald <[email protected]>
Co-authored-by: Jason MacDonald <[email protected]>
  • Loading branch information
3 people authored Jul 15, 2024
1 parent e40779e commit 5034fdc
Show file tree
Hide file tree
Showing 26 changed files with 439 additions and 112 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.3.0",
"version": "2.3.1",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
1 change: 1 addition & 0 deletions packages/ide/jetbrains/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/ide/jetbrains/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## [Unreleased]
### Added
- New `check()` policy rule function.

### Fixed
- Fixed the issue with formatting schemas containing `Unsupported` type.

## 2.2.0
### Added
- Support comparing fields from different models in mutation policy rules ("create", "update", and "delete).

## 2.1.0
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.3.0"
version = "2.3.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.3.0",
"version": "2.3.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.3.0",
"version": "2.3.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.3.0",
"version": "2.3.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.3.0",
"version": "2.3.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.3.0",
"version": "2.3.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.3.0",
"version": "2.3.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.3.0",
"version": "2.3.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.3.0",
"version": "2.3.1",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
105 changes: 54 additions & 51 deletions packages/runtime/src/enhancements/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
FieldInfo,
ModelInfo,
NestedWriteVisitor,
clone,
enumerate,
getIdFields,
getModelInfo,
isDelegateModel,
resolveField,
} from '../cross';
import { clone } from '../cross';
import type { CrudContract, DbClientContract } from '../types';
import type { InternalEnhancementOptions } from './create-enhancement';
import { Logger } from './logger';
Expand Down Expand Up @@ -79,7 +79,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {

if (args.orderBy) {
// `orderBy` may contain fields from base types
args.orderBy = this.buildWhereHierarchy(this.model, args.orderBy);
this.injectWhereHierarchy(this.model, args.orderBy);
}

if (this.options.logPrismaQuery) {
Expand All @@ -95,7 +95,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
}

private injectWhereHierarchy(model: string, where: any) {
if (!where || typeof where !== 'object') {
if (!where || !isPlainObject(where)) {
return;
}

Expand All @@ -108,44 +108,9 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {

const fieldInfo = resolveField(this.options.modelMeta, model, field);
if (!fieldInfo?.inheritedFrom) {
return;
}

let base = this.getBaseModel(model);
let target = where;

while (base) {
const baseRelationName = this.makeAuxRelationName(base);

// prepare base layer where
let thisLayer: any;
if (target[baseRelationName]) {
thisLayer = target[baseRelationName];
} else {
thisLayer = target[baseRelationName] = {};
}

if (base.name === fieldInfo.inheritedFrom) {
thisLayer[field] = value;
delete where[field];
break;
} else {
target = thisLayer;
base = this.getBaseModel(base.name);
if (fieldInfo?.isDataModel) {
this.injectWhereHierarchy(fieldInfo.type, value);
}
}
});
}

private buildWhereHierarchy(model: string, where: any) {
if (!where) {
return undefined;
}

where = clone(where);
Object.entries(where).forEach(([field, value]) => {
const fieldInfo = resolveField(this.options.modelMeta, model, field);
if (!fieldInfo?.inheritedFrom) {
return;
}

Expand All @@ -164,6 +129,9 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
}

if (base.name === fieldInfo.inheritedFrom) {
if (fieldInfo.isDataModel) {
this.injectWhereHierarchy(base.name, value);
}
thisLayer[field] = value;
delete where[field];
break;
Expand All @@ -173,8 +141,6 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
}
}
});

return where;
}

private injectSelectIncludeHierarchy(model: string, args: any) {
Expand All @@ -189,7 +155,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
if (fieldInfo && value !== undefined) {
if (value?.orderBy) {
// `orderBy` may contain fields from base types
value.orderBy = this.buildWhereHierarchy(fieldInfo.type, value.orderBy);
this.injectWhereHierarchy(fieldInfo.type, value.orderBy);
}

if (this.injectBaseFieldSelect(model, field, value, args, kind)) {
Expand Down Expand Up @@ -401,9 +367,46 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
return this.doCreate(tx, this.model, { data: item });
})
);
return { count: r.length };
});
}

// filter out undefined value (due to skipping duplicates)
return { count: r.filter((item) => !!item).length };
override createManyAndReturn(args: { data: any; select?: any; skipDuplicates?: boolean }): Promise<unknown[]> {
if (!args) {
throw prismaClientValidationError(this.prisma, this.options.prismaModule, 'query argument is required');
}
if (!args.data) {
throw prismaClientValidationError(
this.prisma,
this.options.prismaModule,
'data field is required in query argument'
);
}

if (!this.involvesDelegateModel(this.model)) {
return super.createManyAndReturn(args);
}

if (this.isDelegateOrDescendantOfDelegate(this.model) && args.skipDuplicates) {
throw prismaClientValidationError(
this.prisma,
this.options.prismaModule,
'`createManyAndReturn` with `skipDuplicates` set to true is not supported for delegated models'
);
}

// `createManyAndReturn` doesn't support nested create, which is needed for creating entities
// inheriting a delegate base, so we need to convert it to a regular `create` here.
// Note that the main difference is `create` doesn't support `skipDuplicates` as
// `createManyAndReturn` does.

return this.queryUtils.transaction(this.prisma, async (tx) => {
const r = await Promise.all(
enumerate(args.data).map(async (item) => {
return this.doCreate(tx, this.model, { data: item, select: args.select });
})
);
return r;
});
}

Expand Down Expand Up @@ -921,15 +924,15 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
args = clone(args);

if (args.cursor) {
args.cursor = this.buildWhereHierarchy(this.model, args.cursor);
this.injectWhereHierarchy(this.model, args.cursor);
}

if (args.orderBy) {
args.orderBy = this.buildWhereHierarchy(this.model, args.orderBy);
this.injectWhereHierarchy(this.model, args.orderBy);
}

if (args.where) {
args.where = this.buildWhereHierarchy(this.model, args.where);
this.injectWhereHierarchy(this.model, args.where);
}

if (this.options.logPrismaQuery) {
Expand All @@ -949,11 +952,11 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
args = clone(args);

if (args?.cursor) {
args.cursor = this.buildWhereHierarchy(this.model, args.cursor);
this.injectWhereHierarchy(this.model, args.cursor);
}

if (args?.where) {
args.where = this.buildWhereHierarchy(this.model, args.where);
this.injectWhereHierarchy(this.model, args.where);
}

if (this.options.logPrismaQuery) {
Expand Down Expand Up @@ -989,7 +992,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
args = clone(args);

if (args.where) {
args.where = this.buildWhereHierarchy(this.model, args.where);
this.injectWhereHierarchy(this.model, args.where);
}

if (this.options.logPrismaQuery) {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/enhancements/policy/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
});
}

createManyAndReturn(args: { select: any; include: any; data: any; skipDuplicates?: boolean }) {
createManyAndReturn(args: { data: any; select?: any; skipDuplicates?: boolean }) {
if (!args) {
throw prismaClientValidationError(this.prisma, this.prismaModule, 'query argument is required');
}
Expand Down
8 changes: 7 additions & 1 deletion packages/runtime/src/enhancements/policy/policy-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,13 @@ export class PolicyUtil extends QueryUtils {
}
} else {
// hoist non-nullable to-one filter to the parent level
hoisted = this.getAuthGuard(db, fieldInfo.type, 'read');
let injected = this.safeClone(injectTarget[field]);
if (typeof injected !== 'object') {
injected = {};
}
this.injectAuthGuardAsWhere(db, injected, fieldInfo.type, 'read');
hoisted = injected.where;

// recurse
const subHoisted = this.injectNestedReadConditions(db, fieldInfo.type, injectTarget[field]);
if (subHoisted.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/src/enhancements/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface PrismaProxyHandler {

createMany(args: { data: any; skipDuplicates?: boolean }): Promise<BatchResult>;

createManyAndReturn(args: { data: any; select: any; include: any; skipDuplicates?: boolean }): Promise<unknown[]>;
createManyAndReturn(args: { data: any; select?: any; skipDuplicates?: boolean }): Promise<unknown[]>;

update(args: any): Promise<unknown>;

Expand Down Expand Up @@ -124,7 +124,7 @@ export class DefaultPrismaProxyHandler implements PrismaProxyHandler {
return this.deferred<{ count: number }>('createMany', args, false);
}

createManyAndReturn(args: { data: any; select: any; include: any; skipDuplicates?: boolean }) {
createManyAndReturn(args: { data: any; select?: any; skipDuplicates?: boolean }) {
return this.deferred<unknown[]>('createManyAndReturn', args);
}

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": "Build scalable web apps with minimum code by defining authorization and validation rules inside the data schema that closer to the database",
"version": "2.3.0",
"version": "2.3.1",
"author": {
"name": "ZenStack Team"
},
Expand Down
Loading

0 comments on commit 5034fdc

Please sign in to comment.