Skip to content

merge dev to main (v1.12.2) #1239

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

Merged
merged 2 commits into from
Apr 11, 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.12.1",
"version": "1.12.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.12.1"
version = "1.12.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.12.1",
"version": "1.12.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.12.1",
"version": "1.12.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.12.1",
"version": "1.12.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.12.1",
"version": "1.12.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.12.1",
"version": "1.12.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.12.1",
"version": "1.12.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.12.1",
"version": "1.12.2",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export default function createRouter<Config extends BaseConfig>(
.input($Schema.PostInputSchema.aggregate)
.query(({ ctx, input }) => checkRead(db(ctx).post.aggregate(input as any))),

createMany: procedure
.input($Schema.PostInputSchema.createMany)
.mutation(async ({ ctx, input }) => checkMutate(db(ctx).post.createMany(input as any))),

create: procedure
.input($Schema.PostInputSchema.create)
.mutation(async ({ ctx, input }) => checkMutate(db(ctx).post.create(input as any))),
Expand Down Expand Up @@ -88,6 +92,29 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.GetPostAggregateType<T>, Error>,
) => UseTRPCInfiniteQueryResult<Prisma.GetPostAggregateType<T>, TRPCClientErrorLike<AppRouter>>;
};
createMany: {
useMutation: <T extends Prisma.PostCreateManyArgs>(
opts?: UseTRPCMutationOptions<
Prisma.PostCreateManyArgs,
TRPCClientErrorLike<AppRouter>,
Prisma.BatchPayload,
Context
>,
) => Omit<
UseTRPCMutationResult<
Prisma.BatchPayload,
TRPCClientErrorLike<AppRouter>,
Prisma.SelectSubset<T, Prisma.PostCreateManyArgs>,
Context
>,
'mutateAsync'
> & {
mutateAsync: <T extends Prisma.PostCreateManyArgs>(
variables: T,
opts?: UseTRPCMutationOptions<T, TRPCClientErrorLike<AppRouter>, Prisma.BatchPayload, Context>,
) => Promise<Prisma.BatchPayload>;
};
};
create: {
useMutation: <T extends Prisma.PostCreateArgs>(
opts?: UseTRPCMutationOptions<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export default function createRouter<Config extends BaseConfig>(
.input($Schema.UserInputSchema.aggregate)
.query(({ ctx, input }) => checkRead(db(ctx).user.aggregate(input as any))),

createMany: procedure
.input($Schema.UserInputSchema.createMany)
.mutation(async ({ ctx, input }) => checkMutate(db(ctx).user.createMany(input as any))),

create: procedure
.input($Schema.UserInputSchema.create)
.mutation(async ({ ctx, input }) => checkMutate(db(ctx).user.create(input as any))),
Expand Down Expand Up @@ -88,6 +92,29 @@ export interface ClientType<AppRouter extends AnyRouter, Context = AppRouter['_d
opts?: UseTRPCInfiniteQueryOptions<string, T, Prisma.GetUserAggregateType<T>, Error>,
) => UseTRPCInfiniteQueryResult<Prisma.GetUserAggregateType<T>, TRPCClientErrorLike<AppRouter>>;
};
createMany: {
useMutation: <T extends Prisma.UserCreateManyArgs>(
opts?: UseTRPCMutationOptions<
Prisma.UserCreateManyArgs,
TRPCClientErrorLike<AppRouter>,
Prisma.BatchPayload,
Context
>,
) => Omit<
UseTRPCMutationResult<
Prisma.BatchPayload,
TRPCClientErrorLike<AppRouter>,
Prisma.SelectSubset<T, Prisma.UserCreateManyArgs>,
Context
>,
'mutateAsync'
> & {
mutateAsync: <T extends Prisma.UserCreateManyArgs>(
variables: T,
opts?: UseTRPCMutationOptions<T, TRPCClientErrorLike<AppRouter>, Prisma.BatchPayload, Context>,
) => Promise<Prisma.BatchPayload>;
};
};
create: {
useMutation: <T extends Prisma.UserCreateArgs>(
opts?: UseTRPCMutationOptions<
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.12.1",
"version": "1.12.2",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
25 changes: 17 additions & 8 deletions packages/runtime/src/enhancements/policy/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,16 +690,25 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
const postWriteChecks: PostWriteCheckRecord[] = [];

// registers a post-update check task
const _registerPostUpdateCheck = async (model: string, uniqueFilter: any) => {
const _registerPostUpdateCheck = async (
model: string,
preUpdateLookupFilter: any,
postUpdateLookupFilter: any
) => {
// both "post-update" rules and Zod schemas require a post-update check
if (this.utils.hasAuthGuard(model, 'postUpdate') || this.utils.getZodSchema(model)) {
// select pre-update field values
let preValue: any;
const preValueSelect = this.utils.getPreValueSelect(model);
if (preValueSelect && Object.keys(preValueSelect).length > 0) {
preValue = await db[model].findFirst({ where: uniqueFilter, select: preValueSelect });
preValue = await db[model].findFirst({ where: preUpdateLookupFilter, select: preValueSelect });
}
postWriteChecks.push({ model, operation: 'postUpdate', uniqueFilter, preValue });
postWriteChecks.push({
model,
operation: 'postUpdate',
uniqueFilter: postUpdateLookupFilter,
preValue,
});
}
};

Expand Down Expand Up @@ -826,7 +835,7 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
await this.utils.checkPolicyForUnique(model, args, 'update', db, checkArgs);

// register post-update check
await _registerPostUpdateCheck(model, args);
await _registerPostUpdateCheck(model, args, args);
}
}
};
Expand Down Expand Up @@ -873,20 +882,20 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
await this.utils.checkPolicyForUnique(model, uniqueFilter, 'update', db, args);

// handles the case where id fields are updated
const ids = this.utils.clone(existing);
const postUpdateIds = this.utils.clone(existing);
for (const key of Object.keys(existing)) {
const updateValue = (args as any).data ? (args as any).data[key] : (args as any)[key];
if (
typeof updateValue === 'string' ||
typeof updateValue === 'number' ||
typeof updateValue === 'bigint'
) {
ids[key] = updateValue;
postUpdateIds[key] = updateValue;
}
}

// register post-update check
await _registerPostUpdateCheck(model, ids);
await _registerPostUpdateCheck(model, existing, postUpdateIds);
}
},

Expand Down Expand Up @@ -978,7 +987,7 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
await this.utils.checkPolicyForUnique(model, uniqueFilter, 'update', db, args);

// register post-update check
await _registerPostUpdateCheck(model, uniqueFilter);
await _registerPostUpdateCheck(model, uniqueFilter, uniqueFilter);

// convert upsert to update
const convertedUpdate = {
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": "1.12.1",
"version": "1.12.2",
"author": {
"name": "ZenStack Team"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export class ExpressionWriter {
this.plainExprBuilder = new TypeScriptExpressionTransformer({
context: ExpressionContext.AccessPolicy,
isPostGuard: this.isPostGuard,
// in post-guard context, `this` references pre-update value
thisExprContext: this.isPostGuard ? 'context.preValue' : undefined,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Enum,
Expression,
Model,
isBinaryExpr,
isDataModel,
isDataModelField,
isEnum,
Expand All @@ -15,7 +14,6 @@ import {
isMemberAccessExpr,
isReferenceExpr,
isThisExpr,
isUnaryExpr,
} from '@zenstackhq/language/ast';
import {
FIELD_LEVEL_OVERRIDE_READ_GUARD_PREFIX,
Expand Down Expand Up @@ -281,30 +279,6 @@ export default class PolicyGenerator {
}
}

private visitPolicyExpression(expr: Expression, postUpdate: boolean): Expression | undefined {
if (isBinaryExpr(expr) && (expr.operator === '&&' || expr.operator === '||')) {
const left = this.visitPolicyExpression(expr.left, postUpdate);
const right = this.visitPolicyExpression(expr.right, postUpdate);
if (!left) return right;
if (!right) return left;
return { ...expr, left, right };
}

if (isUnaryExpr(expr) && expr.operator === '!') {
const operand = this.visitPolicyExpression(expr.operand, postUpdate);
if (!operand) return undefined;
return { ...expr, operand };
}

if (postUpdate && !this.hasFutureReference(expr)) {
return undefined;
} else if (!postUpdate && this.hasFutureReference(expr)) {
return undefined;
}

return expr;
}

private hasFutureReference(expr: Expression) {
for (const node of streamAst(expr)) {
if (isInvocationExpr(node) && node.function.ref?.name === 'future' && isFromStdlib(node.function.ref)) {
Expand Down Expand Up @@ -599,13 +573,19 @@ export default class PolicyGenerator {
// visit a reference or member access expression to build a
// selection path
const visit = (node: Expression): string[] | undefined => {
if (isThisExpr(node)) {
return [];
}

if (isReferenceExpr(node)) {
const target = resolved(node.target);
if (isDataModelField(target)) {
// a field selection, it's a terminal
return [target.name];
}
} else if (isMemberAccessExpr(node)) {
}

if (isMemberAccessExpr(node)) {
if (forAuthContext && isAuthInvocation(node.operand)) {
return [node.member.$refText];
}
Expand All @@ -621,6 +601,7 @@ export default class PolicyGenerator {
return [...inner, node.member.$refText];
}
}

return undefined;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ export class TypeScriptExpressionTransformer {
throw new TypeScriptExpressionTransformerError(`Unresolved MemberAccessExpr`);
}

if (isThisExpr(expr.operand)) {
return expr.member.ref.name;
} else if (isFutureExpr(expr.operand)) {
if (isFutureExpr(expr.operand)) {
if (this.options?.isPostGuard !== true) {
throw new TypeScriptExpressionTransformerError(`future() is only supported in postUpdate rules`);
}
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.12.1",
"version": "1.12.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.12.1",
"version": "1.12.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.12.1",
"version": "1.12.2",
"description": "ZenStack Test Tools",
"main": "index.js",
"private": true,
Expand Down
Loading