Skip to content

Commit ea7de9c

Browse files
authored
merge dev to main (#605)
2 parents 2fa2f81 + 43b175d commit ea7de9c

File tree

40 files changed

+309
-191
lines changed

40 files changed

+309
-191
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zenstack-monorepo",
3-
"version": "1.0.0-beta.11",
3+
"version": "1.0.0-beta.12",
44
"description": "",
55
"scripts": {
66
"build": "pnpm -r build",

packages/language/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/language",
3-
"version": "1.0.0-beta.11",
3+
"version": "1.0.0-beta.12",
44
"displayName": "ZenStack modeling language compiler",
55
"description": "ZenStack modeling language compiler",
66
"homepage": "https://zenstack.dev",

packages/plugins/openapi/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/openapi",
33
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
4-
"version": "1.0.0-beta.11",
4+
"version": "1.0.0-beta.12",
55
"description": "ZenStack plugin and runtime supporting OpenAPI",
66
"main": "index.js",
77
"repository": {

packages/plugins/swr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/swr",
33
"displayName": "ZenStack plugin for generating SWR hooks",
4-
"version": "1.0.0-beta.11",
4+
"version": "1.0.0-beta.12",
55
"description": "ZenStack plugin for generating SWR hooks",
66
"main": "index.js",
77
"repository": {

packages/plugins/tanstack-query/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/tanstack-query",
33
"displayName": "ZenStack plugin for generating tanstack-query hooks",
4-
"version": "1.0.0-beta.11",
4+
"version": "1.0.0-beta.12",
55
"description": "ZenStack plugin for generating tanstack-query hooks",
66
"main": "index.js",
77
"exports": {

packages/plugins/trpc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/trpc",
33
"displayName": "ZenStack plugin for tRPC",
4-
"version": "1.0.0-beta.11",
4+
"version": "1.0.0-beta.12",
55
"description": "ZenStack plugin for tRPC",
66
"main": "index.js",
77
"repository": {

packages/runtime/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/runtime",
33
"displayName": "ZenStack Runtime Library",
4-
"version": "1.0.0-beta.11",
4+
"version": "1.0.0-beta.12",
55
"description": "Runtime of ZenStack for both client-side and server-side environments.",
66
"repository": {
77
"type": "git",
@@ -55,6 +55,7 @@
5555
"deepcopy": "^2.1.0",
5656
"lower-case-first": "^2.0.2",
5757
"pluralize": "^8.0.0",
58+
"semver": "^7.3.8",
5859
"superjson": "^1.11.0",
5960
"tslib": "^2.4.1",
6061
"upper-case-first": "^2.0.2",
@@ -71,6 +72,7 @@
7172
"@types/jest": "^29.5.0",
7273
"@types/node": "^18.0.0",
7374
"@types/pluralize": "^0.0.29",
75+
"@types/semver": "^7.3.13",
7476
"copyfiles": "^2.4.1",
7577
"rimraf": "^3.0.2",
7678
"typescript": "^4.9.3"

packages/runtime/src/constants.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,13 @@ export enum CrudFailureReason {
3939
export enum PrismaErrorCode {
4040
CONSTRAINED_FAILED = 'P2004',
4141
}
42+
43+
/**
44+
* Field name for storing in-transaction flag
45+
*/
46+
export const PRISIMA_TX_FLAG = '$__zenstack_tx';
47+
48+
/**
49+
* Field name for getting current enhancer
50+
*/
51+
export const PRISMA_PROXY_ENHANCER = '$__zenstack_enhancer';
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { getDefaultModelMeta } from './model-meta';
2+
import { withOmit, WithOmitOptions } from './omit';
3+
import { withPassword, WithPasswordOptions } from './password';
4+
import { withPolicy, WithPolicyContext, WithPolicyOptions } from './policy';
5+
6+
/**
7+
* Options @see enhance
8+
*/
9+
export type EnhancementOptions = WithPolicyOptions & WithPasswordOptions & WithOmitOptions;
10+
11+
let hasPassword: boolean | undefined = undefined;
12+
let hasOmit: boolean | undefined = undefined;
13+
14+
/**
15+
* Gets a Prisma client enhanced with all essential behaviors, including access
16+
* policy, field validation, field omission and password hashing.
17+
*
18+
* It's a shortcut for calling withOmit(withPassword(withPolicy(prisma, options))).
19+
*
20+
* @param prisma The Prisma client to enhance.
21+
* @param context The context to for evaluating access policies.
22+
* @param options Options.
23+
*/
24+
export function enhance<DbClient extends object>(
25+
prisma: DbClient,
26+
context?: WithPolicyContext,
27+
options?: EnhancementOptions
28+
) {
29+
let result = prisma;
30+
31+
if (hasPassword === undefined || hasOmit === undefined) {
32+
const modelMeta = options?.modelMeta ?? getDefaultModelMeta();
33+
const allFields = Object.values(modelMeta.fields).flatMap((modelInfo) => Object.values(modelInfo));
34+
hasPassword = allFields.some((field) => field.attributes?.some((attr) => attr.name === '@password'));
35+
hasOmit = allFields.some((field) => field.attributes?.some((attr) => attr.name === '@omit'));
36+
}
37+
38+
if (hasPassword) {
39+
// @password proxy
40+
result = withPassword(result, options);
41+
}
42+
43+
if (hasOmit) {
44+
// @omit proxy
45+
result = withOmit(result, options);
46+
}
47+
48+
// policy proxy
49+
result = withPolicy(result, context, options);
50+
51+
return result;
52+
}

packages/runtime/src/enhancements/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './omit';
44
export * from './password';
55
export * from './policy';
66
export * from './preset';
7+
export * from './enhance';
78
export * from './types';
89
export * from './utils';
910
export * from './where-visitor';

0 commit comments

Comments
 (0)