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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"eslint": "^8.54.0",
"eslint-config-next": "^14.0.4",
"prisma": "^5.13.0",
"typescript": "^5.1.6"
"typescript": "^5.5.4"
},
"ct3aMetadata": {
"initVersion": "7.26.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ import styles from './index.module.css';
export default function Home() {
const hello = api.greet.hello.useQuery({ text: 'from tRPC' });
const posts = api.post.findMany.useQuery({ where: { published: true }, include: { author: true } });
const postsTransformed = api.post.findMany.useQuery({}, { select: (data) => data.map((p) => ({ title: p.name })) });
const postsTransformed = api.post.findMany.useQuery(
{},
{ select: (data) => data.map((p) => ({ id: p.id, title: p.name })) }
);

return (
<>
<main className={styles.main}>
{hello.data && <h1 className={styles.title}>{hello.data.greeting}</h1>}
{posts.data &&
posts.data.map((post) => (
<p>
{post.name} by {post.author.email}
</p>
))}
{postsTransformed.data && postsTransformed.data.map((post) => <p>{post.title}</p>)}
{posts.data?.map((post) => (
<p key={post.id}>
{post.name} by {post.author.email}
</p>
))}
{postsTransformed.data?.map((post) => (
<p key={post.id}>{post.title}</p>
))}
</main>
</>
);
Expand Down
14 changes: 11 additions & 3 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@
"types": "./edge.d.ts",
"default": "./edge.js"
},
"./enhancements": {
"types": "./enhancements/index.d.ts",
"default": "./enhancements/index.js"
"./enhancements/node": {
"types": "./enhancements/node/index.d.ts",
"default": "./enhancements/node/index.js"
},
"./enhancements/edge": {
"types": "./enhancements/edge/index.d.ts",
"default": "./enhancements/edge/index.js"
},
"./validation": {
"types": "./validation.d.ts",
"default": "./validation.js"
},
"./constraint-solver": {
"types": "./constraint-solver.d.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/res/enhance-edge.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { auth, enhance, type PrismaClient } from '.zenstack/enhance-edge';
10 changes: 10 additions & 0 deletions packages/runtime/res/enhance-edge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });

try {
exports.enhance = require('.zenstack/enhance-edge').enhance;
} catch {
exports.enhance = function () {
throw new Error('Generated "enhance" function not found. Please run `zenstack generate` first.');
};
}
1 change: 0 additions & 1 deletion packages/runtime/src/edge.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/runtime/src/edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './enhance-edge';
2 changes: 2 additions & 0 deletions packages/runtime/src/enhance-edge.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @ts-expect-error stub for re-exporting generated code
export { auth, enhance } from '.zenstack/enhance-edge';
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/default-auth.ts
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/delegate.ts
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/index.ts
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/logger.ts
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/omit.ts
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/password.ts
16 changes: 16 additions & 0 deletions packages/runtime/src/enhancements/edge/policy/check-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ModelMeta } from '..';
import type { DbClientContract } from '../../../types';
import { PermissionCheckArgs } from '../types';
import { PolicyUtil } from './policy-utils';

export async function checkPermission(
_model: string,
_args: PermissionCheckArgs,
_modelMeta: ModelMeta,
_policyUtils: PolicyUtil,
_prisma: DbClientContract,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_prismaModule: any
): Promise<boolean> {
throw new Error('`check()` API is not supported on edge runtime');
}
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/policy/handler.ts
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/policy/index.ts
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/promise.ts
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/proxy.ts
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/query-utils.ts
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/types.ts
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/utils.ts
1 change: 1 addition & 0 deletions packages/runtime/src/enhancements/edge/where-visitor.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,20 @@
import semver from 'semver';
import { PRISMA_MINIMUM_VERSION } from '../constants';
import { isDelegateModel, type ModelMeta } from '../cross';
import type { AuthUser } from '../types';
import { PRISMA_MINIMUM_VERSION } from '../../constants';
import { isDelegateModel, type ModelMeta } from '../../cross';
import type { EnhancementContext, EnhancementKind, EnhancementOptions, ZodSchemas } from '../../types';
import { withDefaultAuth } from './default-auth';
import { withDelegate } from './delegate';
import { Logger } from './logger';
import { withOmit } from './omit';
import { withPassword } from './password';
import { withPolicy } from './policy';
import type { ErrorTransformer } from './proxy';
import type { PolicyDef, ZodSchemas } from './types';

/**
* Kinds of enhancements to `PrismaClient`
*/
export type EnhancementKind = 'password' | 'omit' | 'policy' | 'validation' | 'delegate';
import type { PolicyDef } from './types';

/**
* All enhancement kinds
*/
const ALL_ENHANCEMENTS: EnhancementKind[] = ['password', 'omit', 'policy', 'validation', 'delegate'];

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

export type EnhancementOptions = {
/**
* The kinds of enhancements to apply. By default all enhancements are applied.
*/
kinds?: EnhancementKind[];

/**
* Whether to log Prisma query
*/
logPrismaQuery?: boolean;

/**
* 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;
};

/**
* Options for {@link createEnhancement}
*
Expand Down Expand Up @@ -91,13 +43,6 @@ export type InternalEnhancementOptions = EnhancementOptions & {
prismaModule: any;
};

/**
* Context for creating enhanced `PrismaClient`
*/
export type EnhancementContext<User extends AuthUser = AuthUser> = {
user?: User;
};

/**
* Gets a Prisma client enhanced with all enhancement behaviors, including access
* policy, field validation, field omission and password hashing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
enumerate,
getFields,
requireField,
} from '../cross';
import { DbClientContract } from '../types';
import { EnhancementContext, InternalEnhancementOptions } from './create-enhancement';
} from '../../cross';
import { DbClientContract, EnhancementContext } from '../../types';
import { InternalEnhancementOptions } from './create-enhancement';
import { DefaultPrismaProxyHandler, PrismaProxyActions, makeProxy } from './proxy';
import { isUnsafeMutate, prismaClientValidationError } from './utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import deepmerge, { type ArrayMergeOptions } from 'deepmerge';
import { isPlainObject } from 'is-plain-object';
import { lowerCaseFirst } from 'lower-case-first';
import { DELEGATE_AUX_RELATION_PREFIX } from '../constants';
import { DELEGATE_AUX_RELATION_PREFIX } from '../../constants';
import {
FieldInfo,
ModelInfo,
Expand All @@ -13,8 +13,8 @@ import {
getModelInfo,
isDelegateModel,
resolveField,
} from '../cross';
import type { CrudContract, DbClientContract } from '../types';
} from '../../cross';
import type { CrudContract, DbClientContract } from '../../types';
import type { InternalEnhancementOptions } from './create-enhancement';
import { Logger } from './logger';
import { DefaultPrismaProxyHandler, makeProxy } from './proxy';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from '../cross';
export * from '../../cross';
export * from './create-enhancement';
export * from './types';
export * from './utils';
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */

import { enumerate, getModelFields, resolveField } from '../cross';
import { DbClientContract } from '../types';
import { enumerate, getModelFields, resolveField } from '../../cross';
import { DbClientContract } from '../../types';
import { InternalEnhancementOptions } from './create-enhancement';
import { DefaultPrismaProxyHandler, makeProxy } from './proxy';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */

import { DEFAULT_PASSWORD_SALT_LENGTH } from '../constants';
import { NestedWriteVisitor, type PrismaWriteActionType } from '../cross';
import { DbClientContract } from '../types';
import { DEFAULT_PASSWORD_SALT_LENGTH } from '../../constants';
import { NestedWriteVisitor, type PrismaWriteActionType } from '../../cross';
import { DbClientContract } from '../../types';
import { InternalEnhancementOptions } from './create-enhancement';
import { DefaultPrismaProxyHandler, PrismaProxyActions, makeProxy } from './proxy';

Expand Down
Loading