Skip to content

merge dev to main #567

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 5 commits into from
Jul 9, 2023
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-beta.6",
"version": "1.0.0-beta.7",
"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-beta.6",
"version": "1.0.0-beta.7",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
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.0.0-beta.6",
"version": "1.0.0-beta.7",
"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.0.0-beta.6",
"version": "1.0.0-beta.7",
"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.0.0-beta.6",
"version": "1.0.0-beta.7",
"description": "ZenStack plugin for generating tanstack-query hooks",
"main": "index.js",
"repository": {
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-beta.6",
"version": "1.0.0-beta.7",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
6 changes: 2 additions & 4 deletions 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-beta.6",
"version": "1.0.0-beta.7",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down Expand Up @@ -37,15 +37,13 @@
"zod": "3.21.1",
"zod-validation-error": "^0.2.1"
},
"peerDependencies": {
"@prisma/client": "^4.0.0"
},
"author": {
"name": "ZenStack Team"
},
"homepage": "https://zenstack.dev",
"license": "MIT",
"devDependencies": {
"@prisma/client": "^4.0.0",
"@types/bcryptjs": "^2.4.2",
"@types/jest": "^29.5.0",
"@types/lower-case-first": "^1.0.1",
Expand Down
7 changes: 7 additions & 0 deletions packages/runtime/src/enhancements/model-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ export function getDefaultModelMeta(): ModelMeta {
export function resolveField(modelMeta: ModelMeta, model: string, field: string) {
return modelMeta.fields[lowerCaseFirst(model)][field];
}

/**
* Gets all fields of a model.
*/
export function getFields(modelMeta: ModelMeta, model: string) {
return modelMeta.fields[lowerCaseFirst(model)];
}
40 changes: 37 additions & 3 deletions packages/runtime/src/enhancements/policy/policy-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
PrismaWriteActionType,
} from '../../types';
import { getVersion } from '../../version';
import { resolveField } from '../model-meta';
import { getFields, resolveField } from '../model-meta';
import { NestedWriteVisitor, type VisitorContext } from '../nested-write-vistor';
import type { ModelMeta, PolicyDef, PolicyFunc, ZodSchemas } from '../types';
import {
Expand Down Expand Up @@ -294,6 +294,37 @@ export class PolicyUtil {
return;
}

if (injectTarget._count !== undefined) {
// _count needs to respect read policies of related models
if (injectTarget._count === true) {
// include count for all relations, expand to all fields
// so that we can inject guard conditions for each of them
injectTarget._count = { select: {} };
const modelFields = getFields(this.modelMeta, model);
if (modelFields) {
for (const [k, v] of Object.entries(modelFields)) {
if (v.isDataModel && v.isArray) {
// create an entry for to-many relation
injectTarget._count.select[k] = {};
}
}
}
}

// inject conditions for each relation
for (const field of Object.keys(injectTarget._count.select)) {
if (typeof injectTarget._count.select[field] !== 'object') {
injectTarget._count.select[field] = {};
}
const fieldInfo = resolveField(this.modelMeta, model, field);
if (!fieldInfo) {
continue;
}
// inject into the "where" clause inside select
await this.injectAuthGuard(injectTarget._count.select[field], fieldInfo.type, 'read');
}
}

const idFields = this.getIdFields(model);
for (const field of getModelFields(injectTarget)) {
const fieldInfo = resolveField(this.modelMeta, model, field);
Expand Down Expand Up @@ -602,6 +633,9 @@ export class PolicyUtil {

// process relation updates: connect, connectOrCreate, and disconnect
const processRelationUpdate = async (model: string, args: any, context: VisitorContext) => {
// CHECK ME: equire the entity being connected readable?
// await this.checkPolicyForFilter(model, args, 'read', this.db);

if (context.field?.backLink) {
// fetch the backlink field of the model being connected
const backLinkField = resolveField(this.modelMeta, model, context.field.backLink);
Expand Down Expand Up @@ -720,9 +754,9 @@ export class PolicyUtil {
}
}

private getModelField(model: string, backlinkField: string) {
private getModelField(model: string, field: string) {
model = lowerCaseFirst(model);
return this.modelMeta.fields[model]?.[backlinkField];
return this.modelMeta.fields[model]?.[field];
}

private transaction(db: DbClientContract, action: (tx: Record<string, DbOperations>) => Promise<any>) {
Expand Down
5 changes: 1 addition & 4 deletions 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-beta.6",
"version": "1.0.0-beta.7",
"author": {
"name": "ZenStack Team"
},
Expand Down Expand Up @@ -78,9 +78,6 @@
"publish-dev": "pnpm publish --registry http://localhost:4873",
"postinstall": "node bin/post-install.js"
},
"peerDependencies": {
"prisma": "^4.0.0"
},
"dependencies": {
"@paralleldrive/cuid2": "^2.2.0",
"@prisma/generator-helper": "4.10.0",
Expand Down
19 changes: 16 additions & 3 deletions packages/schema/src/plugins/model-meta/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function generateModelMetadata(dataModels: DataModel[], writer: CodeBlockWriter)
isOptional: ${f.type.optional},
attributes: ${JSON.stringify(getFieldAttributes(f))},
backLink: ${backlink ? "'" + backlink.name + "'" : 'undefined'},
isRelationOwner: ${isRelationOwner(f)},
isRelationOwner: ${isRelationOwner(f, backlink)},
},`);
}
});
Expand Down Expand Up @@ -177,6 +177,19 @@ function getUniqueConstraints(model: DataModel) {
return constraints;
}

function isRelationOwner(field: DataModelField) {
return hasAttribute(field, '@relation');
function isRelationOwner(field: DataModelField, backLink: DataModelField | undefined) {
if (!isDataModel(field.type.reference?.ref)) {
return false;
}

if (hasAttribute(field, '@relation')) {
// this field has `@relation` attribute
return true;
} else if (!backLink || !hasAttribute(backLink, '@relation')) {
// if the opposite side field doesn't have `@relation` attribute either,
// it's an implicit many-to-many relation, both sides are owners
return true;
} else {
return false;
}
}
32 changes: 31 additions & 1 deletion packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
import fs from 'fs';
import { writeFile } from 'fs/promises';
import path from 'path';
import semver from 'semver';
import stripColor from 'strip-color';
import { name } from '.';
import { getStringLiteral } from '../../language-server/validator/utils';
Expand Down Expand Up @@ -236,14 +237,43 @@ export default class PrismaSchemaGenerator {
}
}

private getPrismaVersion() {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require('@prisma/client/package.json').version;
} catch {
return undefined;
}
}

private generateGenerator(prisma: PrismaModel, decl: GeneratorDecl) {
prisma.addGenerator(
const generator = prisma.addGenerator(
decl.name,
decl.fields.map((f) => {
const value = isArrayExpr(f.value) ? getLiteralArray(f.value) : getLiteral(f.value);
return { name: f.name, value };
})
);

// deal with configuring PrismaClient preview features
const provider = generator.fields.find((f) => f.name === 'provider');
if (provider?.value === 'prisma-client-js') {
const prismaVersion = this.getPrismaVersion();
if (prismaVersion && semver.lt(prismaVersion, '4.7.0')) {
// insert interactiveTransactions preview feature
let previewFeatures = generator.fields.find((f) => f.name === 'previewFeatures');
if (!previewFeatures) {
previewFeatures = { name: 'previewFeatures', value: [] };
generator.fields.push(previewFeatures);
}
if (!Array.isArray(previewFeatures.value)) {
throw new PluginError(name, 'option "previewFeatures" must be an array');
}
if (!previewFeatures.value.includes('interactiveTransactions')) {
previewFeatures.value.push('interactiveTransactions');
}
}
}
}

private generateModel(prisma: PrismaModel, decl: DataModel, config?: Record<string, string>) {
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.0.0-beta.6",
"version": "1.0.0-beta.7",
"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.0.0-beta.6",
"version": "1.0.0-beta.7",
"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.0.0-beta.6",
"version": "1.0.0-beta.7",
"description": "ZenStack Test Tools",
"main": "index.js",
"publishConfig": {
Expand Down
Loading