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

feat: make parameters of transactions configurable #988

Merged
merged 3 commits into from
Feb 10, 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.8.1",
"version": "1.8.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.8.1"
version = "1.8.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.8.1",
"version": "1.8.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.8.1",
"version": "1.8.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.8.1",
"version": "1.8.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.8.1",
"version": "1.8.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.8.1",
"version": "1.8.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.8.1",
"version": "1.8.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.8.1",
"version": "1.8.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.8.1",
"version": "1.8.2",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
24 changes: 19 additions & 5 deletions packages/runtime/src/enhancements/policy/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { formatObject, prismaClientValidationError } from '../utils';
import { Logger } from './logger';
import { PolicyUtil } from './policy-utils';
import { createDeferredPromise } from './promise';
import { WithPolicyOptions } from '.';

// a record for post-write policy check
type PostWriteCheckRecord = {
Expand All @@ -42,14 +43,17 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
private readonly utils: PolicyUtil;
private readonly model: string;

private readonly DEFAULT_TX_MAXWAIT = 100000;
private readonly DEFAULT_TX_TIMEOUT = 100000;

constructor(
private readonly prisma: DbClient,
private readonly policy: PolicyDef,
private readonly modelMeta: ModelMeta,
private readonly zodSchemas: ZodSchemas | undefined,
model: string,
private readonly user?: AuthUser,
private readonly logPrismaQuery?: boolean
private readonly user: AuthUser | undefined,
private readonly options: WithPolicyOptions | undefined
) {
this.logger = new Logger(prisma);
this.utils = new PolicyUtil(
Expand Down Expand Up @@ -1276,12 +1280,22 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
//#region Utils

private get shouldLogQuery() {
return !!this.logPrismaQuery && this.logger.enabled('info');
return !!this.options?.logPrismaQuery && this.logger.enabled('info');
}

private transaction(action: (tx: Record<string, DbOperations>) => Promise<any>) {
if (this.prisma['$transaction']) {
return this.prisma.$transaction((tx) => action(tx), { maxWait: 100000, timeout: 100000 });
const txOptions: any = { maxWait: this.DEFAULT_TX_MAXWAIT, timeout: this.DEFAULT_TX_TIMEOUT };
if (this.options?.transactionMaxWait !== undefined) {
txOptions.maxWait = this.options.transactionMaxWait;
}
if (this.options?.transactionTimeout !== undefined) {
txOptions.timeout = this.options.transactionTimeout;
}
if (this.options?.transactionIsolationLevel !== undefined) {
txOptions.isolationLevel = this.options.transactionIsolationLevel;
}
return this.prisma.$transaction((tx) => action(tx), txOptions);
} else {
// already in transaction, don't nest
return action(this.prisma);
Expand All @@ -1304,7 +1318,7 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
this.zodSchemas,
model,
this.user,
this.logPrismaQuery
this.options
);
}

Expand Down
27 changes: 26 additions & 1 deletion packages/runtime/src/enhancements/policy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ export type WithPolicyContext = {
user?: AuthUser;
};

/**
* Transaction isolation levels: https://www.prisma.io/docs/orm/prisma-client/queries/transactions#transaction-isolation-level
*/
export type TransactionIsolationLevel =
| 'ReadUncommitted'
| 'ReadCommitted'
| 'RepeatableRead'
| 'Snapshot'
| 'Serializable';

/**
* Options for @see withPolicy
*/
Expand Down Expand Up @@ -46,6 +56,21 @@ export interface WithPolicyOptions extends CommonEnhancementOptions {
* Hook for transforming errors before they are thrown to the caller.
*/
errorTransformer?: ErrorTransformer;

/**
* The `maxWait` option passed to `prisma.$transaction()` call for transactions initiated by ZenStack.
*/
transactionMaxWait?: number;

/**
* The `timeout` option passed to `prisma.$transaction()` call for transactions initiated by ZenStack.
*/
transactionTimeout?: number;

/**
* The `isolationLevel` option passed to `prisma.$transaction()` call for transactions initiated by ZenStack.
*/
transactionIsolationLevel?: TransactionIsolationLevel;
}

/**
Expand Down Expand Up @@ -115,7 +140,7 @@ export function withPolicy<DbClient extends object>(
_zodSchemas,
model,
context?.user,
options?.logPrismaQuery
options
),
'policy',
options?.errorTransformer
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.8.1",
"version": "1.8.2",
"author": {
"name": "ZenStack Team"
},
Expand Down
8 changes: 6 additions & 2 deletions packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,18 @@ export default class PrismaSchemaGenerator {
const generateClient = options.generateClient !== false;

if (generateClient) {
let generateCmd = `npx prisma generate --schema "${outFile}"`;
if (typeof options.generateArgs === 'string') {
generateCmd += ` ${options.generateArgs}`;
}
try {
// run 'prisma generate'
await execSync(`npx prisma generate --schema "${outFile}"`, 'ignore');
await execSync(generateCmd, 'ignore');
} catch {
await this.trackPrismaSchemaError(outFile);
try {
// run 'prisma generate' again with output to the console
await execSync(`npx prisma generate --schema "${outFile}"`);
await execSync(generateCmd);
} catch {
// noop
}
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.8.1",
"version": "1.8.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.8.1",
"version": "1.8.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.8.1",
"version": "1.8.2",
"description": "ZenStack Test Tools",
"main": "index.js",
"private": true,
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/tests/cli/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,18 @@ model Post {
expect(fs.existsSync('./node_modules/.zenstack/zod/index.js')).toBeFalsy();
expect(fs.existsSync('./node_modules/.zenstack/zod/index.ts')).toBeTruthy();
});

it('generate with prisma generateArgs', async () => {
fs.appendFileSync(
'schema.zmodel',
`
plugin prisma {
provider = '@core/prisma'
generateArgs = '--no-engine'
}
`
);
const program = createProgram();
await program.parseAsync(['generate', '--no-dependency-check', '--no-default-plugins'], { from: 'user' });
});
});
Loading