Skip to content
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.0.0-alpha.30",
"version": "1.0.0-alpha.31",
"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-alpha.30",
"version": "1.0.0-alpha.31",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/next",
"version": "1.0.0-alpha.30",
"version": "1.0.0-alpha.31",
"displayName": "ZenStack Next.js integration",
"description": "ZenStack Next.js integration",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/react",
"displayName": "ZenStack plugin and runtime for ReactJS",
"version": "1.0.0-alpha.30",
"version": "1.0.0-alpha.31",
"description": "ZenStack plugin and runtime for ReactJS",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/react/src/react-hooks-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function wrapReadbackErrorCheck(code: string) {
return `try {
${code}
} catch (err: any) {
if (err.prisma && err.code === 'P2004') {
if (err.info?.prisma && err.info?.code === 'P2004') {
// unable to readback data
return undefined;
} else {
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-alpha.30",
"version": "1.0.0-alpha.31",
"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-alpha.30",
"version": "1.0.0-alpha.31",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
47 changes: 45 additions & 2 deletions packages/runtime/src/enhancements/policy/policy-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ export class PolicyUtil {
*/
async readWithCheck(model: string, args: any): Promise<unknown[]> {
args = this.clone(args);

if (args.where) {
// query args will be used with findMany, so we need to
// translate unique constraint filters into a flat filter
// e.g.: { a_b: { a: '1', b: '1' } } => { a: '1', b: '1' }
await this.flattenGeneratedUniqueField(model, args.where);
}

await this.injectAuthGuard(args, model, 'read');

// recursively inject read guard conditions into the query args
Expand All @@ -143,6 +151,28 @@ export class PolicyUtil {
return result;
}

// flatten unique constraint filters
async flattenGeneratedUniqueField(model: string, args: any) {
// e.g.: { a_b: { a: '1', b: '1' } } => { a: '1', b: '1' }
const uniqueConstraints = this.modelMeta.uniqueConstraints?.[camelCase(model)];
let flattened = false;
if (uniqueConstraints) {
for (const [field, value] of Object.entries<any>(args)) {
if (uniqueConstraints[field] && typeof value === 'object') {
for (const [f, v] of Object.entries(value)) {
args[f] = v;
}
delete args[field];
flattened = true;
}
}
}

if (flattened) {
this.logger.info(`Filter flattened: ${JSON.stringify(args)}`);
}
}

private async injectNestedReadConditions(model: string, args: any) {
const injectTarget = args.select ?? args.include;
if (!injectTarget) {
Expand Down Expand Up @@ -376,6 +406,12 @@ export class PolicyUtil {
// fetch preValue selection (analyzed from the post-update rules)
const preValueSelect = await this.getPreValueSelect(model);
const filter = await buildReversedQuery(context);

// query args will be used with findMany, so we need to
// translate unique constraint filters into a flat filter
// e.g.: { a_b: { a: '1', b: '1' } } => { a: '1', b: '1' }
await this.flattenGeneratedUniqueField(model, filter);

const idField = this.getIdField(model);
const query = { where: filter, select: { ...preValueSelect, [idField.name]: true } };
this.logger.info(`fetching pre-update entities for ${model}: ${format(query)})}`);
Expand Down Expand Up @@ -543,11 +579,18 @@ export class PolicyUtil {
) {
this.logger.info(`Checking policy for ${model}#${JSON.stringify(filter)} for ${operation}`);

const count = (await db[model].count({ where: filter })) as number;
const queryFilter = deepcopy(filter);

// query args will be used with findMany, so we need to
// translate unique constraint filters into a flat filter
// e.g.: { a_b: { a: '1', b: '1' } } => { a: '1', b: '1' }
await this.flattenGeneratedUniqueField(model, queryFilter);

const count = (await db[model].count({ where: queryFilter })) as number;
const guard = await this.getAuthGuard(model, operation);

// build a query condition with policy injected
const guardedQuery = { where: this.and(filter, guard) };
const guardedQuery = { where: this.and(queryFilter, guard) };

const schema = (operation === 'create' || operation === 'update') && (await this.getModelSchema(model));

Expand Down
11 changes: 10 additions & 1 deletion packages/runtime/src/enhancements/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { z } from 'zod';
import { FieldInfo, PolicyOperationKind, QueryContext } from '../types';

/**
* Metadata for a model-level unique constraint
* e.g.: @@unique([a, b])
*/
export type UniqueConstraint = { name: string; fields: string[] };

/**
* ZModel data model metadata
*/
export type ModelMeta = { fields: Record<string, Record<string, FieldInfo>> };
export type ModelMeta = {
fields: Record<string, Record<string, FieldInfo>>;
uniqueConstraints: Record<string, Record<string, UniqueConstraint>>;
};

/**
* Function for getting policy guard with a given context
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-alpha.30",
"version": "1.0.0-alpha.31",
"author": {
"name": "ZenStack Team"
},
Expand Down
48 changes: 46 additions & 2 deletions packages/schema/src/plugins/model-meta/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { DataModel, DataModelField, Model, isDataModel, isLiteralExpr } from '@zenstackhq/language/ast';
import {
ArrayExpr,
DataModel,
DataModelField,
isDataModel,
isLiteralExpr,
Model,
ReferenceExpr,
} from '@zenstackhq/language/ast';
import { RuntimeAttribute } from '@zenstackhq/runtime';
import { PluginOptions, getLiteral, resolved } from '@zenstackhq/sdk';
import { getAttributeArgs, getLiteral, PluginOptions, resolved } from '@zenstackhq/sdk';
import { camelCase } from 'change-case';
import path from 'path';
import { CodeBlockWriter, Project, VariableDeclarationKind } from 'ts-morph';
Expand Down Expand Up @@ -66,6 +74,23 @@ function generateModelMetadata(dataModels: DataModel[], writer: CodeBlockWriter)
}
});
writer.write(',');

writer.write('uniqueConstraints:');
writer.block(() => {
for (const model of dataModels) {
writer.write(`${camelCase(model.name)}:`);
writer.block(() => {
for (const constraint of getUniqueConstraints(model)) {
writer.write(`${constraint.name}: {
name: "${constraint.name}",
fields: ${JSON.stringify(constraint.fields)}
},`);
}
});
writer.write(',');
}
});
writer.write(',');
});
}

Expand Down Expand Up @@ -119,3 +144,22 @@ function getFieldAttributes(field: DataModelField): RuntimeAttribute[] {
function isIdField(field: DataModelField) {
return field.attributes.some((attr) => attr.decl.ref?.name === '@id');
}

function getUniqueConstraints(model: DataModel) {
const constraints: Array<{ name: string; fields: string[] }> = [];
for (const attr of model.attributes.filter((attr) => attr.decl.ref?.name === '@@unique')) {
const argsMap = getAttributeArgs(attr);
if (argsMap.fields) {
const fieldNames = (argsMap.fields as ArrayExpr).items.map(
(item) => resolved((item as ReferenceExpr).target).name
);
let constraintName = argsMap.name && getLiteral<string>(argsMap.name);
if (!constraintName) {
// default constraint name is fields concatenated with underscores
constraintName = fieldNames.join('_');
}
constraints.push({ name: constraintName, fields: fieldNames });
}
}
return constraints;
}
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-alpha.30",
"version": "1.0.0-alpha.31",
"description": "ZenStack plugin development SDK",
"main": "index.js",
"scripts": {
Expand Down
21 changes: 20 additions & 1 deletion packages/sdk/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { AstNode, Expression, isArrayExpr, isLiteralExpr, Reference } from '@zenstackhq/language/ast';
import {
AstNode,
DataModelAttribute,
DataModelFieldAttribute,
Expression,
isArrayExpr,
isLiteralExpr,
Reference,
} from '@zenstackhq/language/ast';

export function resolved<T extends AstNode>(ref: Reference<T>): T {
if (!ref.ref) {
Expand Down Expand Up @@ -36,3 +44,14 @@ export default function indentString(string: string, count = 4): string {
const indent = ' ';
return string.replace(/^(?!\s*$)/gm, indent.repeat(count));
}

export function getAttributeArgs(attr: DataModelAttribute | DataModelFieldAttribute): Record<string, Expression> {
const result: Record<string, Expression> = {};
for (const arg of attr.args) {
if (!arg.$resolvedParam) {
continue;
}
result[arg.$resolvedParam.name] = arg.value;
}
return result;
}
4 changes: 2 additions & 2 deletions tests/integration/test-run/package-lock.json

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

Loading