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
10 changes: 9 additions & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ jobs:
strategy:
matrix:
node-version: [18.x]
prisma-version: [v4, v5]

steps:
- uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v3

- name: Set Prisma Version
if: ${{ matrix.prisma-version == 'v5' }}
shell: bash
run: |
bash ./script/test-prisma-v5.sh

- name: Install pnpm
uses: pnpm/action-setup@v2
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"devDependencies": {
"@changesets/cli": "^2.26.0",
"concurrently": "^7.4.0",
"replace-in-file": "^7.0.1",
"tsup": "^7.1.0"
}
}
5 changes: 2 additions & 3 deletions packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"author": "ZenStack Team",
"license": "MIT",
"dependencies": {
"@prisma/generator-helper": "4.10.0",
"@prisma/generator-helper": "^5.0.0",
"@zenstackhq/runtime": "workspace:*",
"@zenstackhq/sdk": "workspace:*",
"change-case": "^4.1.2",
Expand All @@ -36,10 +36,9 @@
"zod-validation-error": "^0.2.1"
},
"devDependencies": {
"@prisma/internals": "4.10.0",
"@readme/openapi-parser": "^2.4.0",
"@types/jest": "^29.5.0",
"@types/lower-case-first": "^1.0.1",
"@types/node": "^18.0.0",
"@types/pluralize": "^0.0.29",
"@types/tmp": "^0.2.3",
"@typescript-eslint/eslint-plugin": "^5.54.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/src/generator-base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DMMF } from '@prisma/generator-helper';
import type { DMMF } from '@prisma/generator-helper';
import { PluginError, PluginOptions, getDataModels, hasAttribute } from '@zenstackhq/sdk';
import { Model } from '@zenstackhq/sdk/ast';
import type { OpenAPIV3_1 as OAPI } from 'openapi-types';
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DMMF } from '@prisma/generator-helper';
import type { DMMF } from '@prisma/generator-helper';
import { PluginError, PluginOptions } from '@zenstackhq/sdk';
import { Model } from '@zenstackhq/sdk/ast';
import { RESTfulOpenAPIGenerator } from './rest-generator';
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/src/rest-generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Inspired by: https://github.com/omar-dulaimi/prisma-trpc-generator

import { DMMF } from '@prisma/generator-helper';
import type { DMMF } from '@prisma/generator-helper';
import {
AUXILIARY_FIELDS,
analyzePolicies,
Expand Down
7 changes: 6 additions & 1 deletion packages/plugins/openapi/src/rpc-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,12 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
const fields = input.fields.filter((f) => !AUXILIARY_FIELDS.includes(f.name));
for (const field of fields) {
const options = field.inputTypes
.filter((f) => f.type !== 'Null')
.filter(
(f) =>
f.type !== 'Null' &&
// fieldRefTypes refer to other fields in the model and don't need to be generated as part of schema
f.location !== 'fieldRefTypes'
)
.map((f) => {
return this.wrapArray(this.prismaTypeToOpenAPIType(f.type), f.isList);
});
Expand Down
12 changes: 12 additions & 0 deletions packages/plugins/prisma-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/// Types copied over from Prisma's generated code to avoid being broken due to Prisma upgrades

export type Enumerable<T> = T | Array<T>;

type _TupleToUnion<T> = T extends (infer E)[] ? E : never;

export type TupleToUnion<K extends readonly any[]> = _TupleToUnion<K>;

export type MaybeTupleToUnion<T> = T extends any[] ? TupleToUnion<T> : T;

export type PickEnumerable<T, K extends Enumerable<keyof T> | keyof T> = Pick<T, MaybeTupleToUnion<K>>;
5 changes: 2 additions & 3 deletions packages/plugins/swr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"author": "ZenStack Team",
"license": "MIT",
"dependencies": {
"@prisma/generator-helper": "4.10.0",
"@prisma/generator-helper": "^5.0.0",
"@zenstackhq/runtime": "workspace:*",
"@zenstackhq/sdk": "workspace:*",
"change-case": "^4.1.2",
Expand All @@ -37,10 +37,9 @@
"devDependencies": {
"@tanstack/react-query": "^4.28.0",
"@types/jest": "^29.5.0",
"@types/lower-case-first": "^1.0.1",
"@types/node": "^18.0.0",
"@types/react": "18.2.0",
"@types/tmp": "^0.2.3",
"@types/upper-case-first": "^1.1.2",
"@zenstackhq/testtools": "workspace:*",
"copyfiles": "^2.4.1",
"jest": "^29.5.0",
Expand Down
8 changes: 4 additions & 4 deletions packages/plugins/swr/src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DMMF } from '@prisma/generator-helper';
import type { DMMF } from '@prisma/generator-helper';
import {
PluginOptions,
createProject,
Expand Down Expand Up @@ -59,7 +59,7 @@ function generateModelHooks(project: Project, outDir: string, model: DataModel,
});
sf.addStatements([
`import { useContext } from 'react';`,
`import { RequestHandlerContext, type RequestOptions } from '@zenstackhq/swr/runtime';`,
`import { RequestHandlerContext, type RequestOptions, type PickEnumerable } from '@zenstackhq/swr/runtime';`,
`import * as request from '@zenstackhq/swr/runtime';`,
]);

Expand Down Expand Up @@ -193,7 +193,7 @@ function generateModelHooks(project: Project, outDir: string, model: DataModel,
`HasSelectOrTake extends Prisma.Or<Prisma.Extends<'skip', Prisma.Keys<T>>, Prisma.Extends<'take', Prisma.Keys<T>>>`,
`OrderByArg extends Prisma.True extends HasSelectOrTake ? { orderBy: Prisma.${model.name}GroupByArgs['orderBy'] }: { orderBy?: Prisma.${model.name}GroupByArgs['orderBy'] },`,
`OrderFields extends Prisma.ExcludeUnderscoreKeys<Prisma.Keys<Prisma.MaybeTupleToUnion<T['orderBy']>>>`,
`ByFields extends Prisma.TupleToUnion<T['by']>`,
`ByFields extends Prisma.MaybeTupleToUnion<T['by']>`,
`ByValid extends Prisma.Has<ByFields, OrderFields>`,
`HavingFields extends Prisma.GetHavingFields<T['having']>`,
`HavingValid extends Prisma.Has<ByFields, HavingFields>`,
Expand Down Expand Up @@ -243,7 +243,7 @@ function generateModelHooks(project: Project, outDir: string, model: DataModel,
];
const inputType = `Prisma.SubsetIntersection<T, Prisma.${model.name}GroupByArgs, OrderByArg> & InputErrors`;
const returnType = `{} extends InputErrors ?
Array<Prisma.PickArray<Prisma.${model.name}GroupByOutputType, T['by']> &
Array<PickEnumerable<Prisma.${model.name}GroupByOutputType, T['by']> &
{
[P in ((keyof T) & (keyof Prisma.${model.name}GroupByOutputType))]: P extends '_count'
? T[P] extends boolean
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/swr/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { deserialize, serialize } from '@zenstackhq/runtime/browser';
import { createContext } from 'react';
import type { MutatorCallback, MutatorOptions, SWRResponse } from 'swr';
import useSWR, { useSWRConfig } from 'swr';
export * from './prisma-types';

/**
* Function signature for `fetch`.
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/swr/src/runtime/prisma-types.ts
5 changes: 2 additions & 3 deletions packages/plugins/tanstack-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"author": "ZenStack Team",
"license": "MIT",
"dependencies": {
"@prisma/generator-helper": "4.10.0",
"@prisma/generator-helper": "^5.0.0",
"@zenstackhq/runtime": "workspace:*",
"@zenstackhq/sdk": "workspace:*",
"change-case": "^4.1.2",
Expand All @@ -56,10 +56,9 @@
"@tanstack/react-query": "4.29.7",
"@tanstack/svelte-query": "4.29.7",
"@types/jest": "^29.5.0",
"@types/lower-case-first": "^1.0.1",
"@types/node": "^18.0.0",
"@types/react": "18.2.0",
"@types/tmp": "^0.2.3",
"@types/upper-case-first": "^1.1.2",
"@zenstackhq/testtools": "workspace:*",
"copyfiles": "^2.4.1",
"jest": "^29.5.0",
Expand Down
7 changes: 4 additions & 3 deletions packages/plugins/tanstack-query/src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DMMF } from '@prisma/generator-helper';
import type { DMMF } from '@prisma/generator-helper';
import {
PluginError,
PluginOptions,
Expand Down Expand Up @@ -300,7 +300,7 @@ function generateModelHooks(
`HasSelectOrTake extends Prisma.Or<Prisma.Extends<'skip', Prisma.Keys<T>>, Prisma.Extends<'take', Prisma.Keys<T>>>`,
`OrderByArg extends Prisma.True extends HasSelectOrTake ? { orderBy: Prisma.${model.name}GroupByArgs['orderBy'] }: { orderBy?: Prisma.${model.name}GroupByArgs['orderBy'] },`,
`OrderFields extends Prisma.ExcludeUnderscoreKeys<Prisma.Keys<Prisma.MaybeTupleToUnion<T['orderBy']>>>`,
`ByFields extends Prisma.TupleToUnion<T['by']>`,
`ByFields extends Prisma.MaybeTupleToUnion<T['by']>`,
`ByValid extends Prisma.Has<ByFields, OrderFields>`,
`HavingFields extends Prisma.GetHavingFields<T['having']>`,
`HavingValid extends Prisma.Has<ByFields, HavingFields>`,
Expand Down Expand Up @@ -350,7 +350,7 @@ function generateModelHooks(
];

const returnType = `{} extends InputErrors ?
Array<Prisma.PickArray<Prisma.${model.name}GroupByOutputType, T['by']> &
Array<PickEnumerable<Prisma.${model.name}GroupByOutputType, T['by']> &
{
[P in ((keyof T) & (keyof Prisma.${model.name}GroupByOutputType))]: P extends '_count'
? T[P] extends boolean
Expand Down Expand Up @@ -406,6 +406,7 @@ function makeGetContext(target: TargetFramework) {
function makeBaseImports(target: TargetFramework) {
const shared = [
`import { query, postMutation, putMutation, deleteMutation } from '@zenstackhq/tanstack-query/runtime/${target}';`,
`import type { PickEnumerable } from '@zenstackhq/tanstack-query/runtime';`,
];
switch (target) {
case 'react':
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/tanstack-query/src/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './prisma-types';
2 changes: 1 addition & 1 deletion packages/plugins/tanstack-query/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: ['src/runtime/react.ts', 'src/runtime/svelte.ts'],
entry: ['src/runtime/index.ts', 'src/runtime/react.ts', 'src/runtime/svelte.ts'],
outDir: 'dist/runtime',
splitting: false,
sourcemap: true,
Expand Down
5 changes: 2 additions & 3 deletions packages/plugins/trpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"author": "ZenStack Team",
"license": "MIT",
"dependencies": {
"@prisma/generator-helper": "4.10.0",
"@prisma/internals": "4.10.0",
"@prisma/generator-helper": "^5.0.0",
"@zenstackhq/sdk": "workspace:*",
"change-case": "^4.1.2",
"lower-case-first": "^2.0.2",
Expand All @@ -40,7 +39,7 @@
"@trpc/react-query": "^10.32.0",
"@trpc/server": "^10.32.0",
"@types/jest": "^29.5.0",
"@types/lower-case-first": "^1.0.1",
"@types/node": "^18.0.0",
"@types/prettier": "^2.7.2",
"@zenstackhq/testtools": "workspace:*",
"copyfiles": "^2.4.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/trpc/src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DMMF } from '@prisma/generator-helper';
import type { DMMF } from '@prisma/generator-helper';
import {
CrudFailureReason,
PluginError,
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/trpc/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DMMF } from '@prisma/generator-helper';
import type { DMMF } from '@prisma/generator-helper';
import { PluginError, getPrismaClientImportSpec } from '@zenstackhq/sdk';
import { Model } from '@zenstackhq/sdk/ast';
import { lowerCaseFirst } from 'lower-case-first';
Expand Down Expand Up @@ -123,7 +123,7 @@ function getPrismaOperationTypes(model: string, operation: string) {
? { orderBy: Prisma.${capModel}GroupByArgs['orderBy'] }
: { orderBy?: Prisma.${capModel}GroupByArgs['orderBy'] },
OrderFields extends Prisma.ExcludeUnderscoreKeys<Prisma.Keys<Prisma.MaybeTupleToUnion<T['orderBy']>>>,
ByFields extends Prisma.TupleToUnion<T['by']>,
ByFields extends Prisma.MaybeTupleToUnion<T['by']>,
ByValid extends Prisma.Has<ByFields, OrderFields>,
HavingFields extends Prisma.GetHavingFields<T['having']>,
HavingValid extends Prisma.Has<ByFields, HavingFields>,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/trpc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DMMF } from '@prisma/generator-helper';
import type { DMMF } from '@prisma/generator-helper';
import { PluginOptions } from '@zenstackhq/sdk';
import { Model } from '@zenstackhq/sdk/ast';
import { generate } from './generator';
Expand Down
4 changes: 1 addition & 3 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@
"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",
"@types/node": "^14.18.29",
"@types/node": "^18.0.0",
"@types/pluralize": "^0.0.29",
"copyfiles": "^2.4.1",
"rimraf": "^3.0.2",
Expand Down
29 changes: 25 additions & 4 deletions packages/runtime/src/enhancements/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,37 @@ let _PrismaClientUnknownRequestError: new (...args: unknown[]) => Error;
function loadPrismaModule(prisma: any) {
// https://github.com/prisma/prisma/discussions/17832
if (prisma._engineConfig?.datamodelPath) {
// try engine path first
const loadPath = path.dirname(prisma._engineConfig.datamodelPath);
try {
const _prisma = require(loadPath).Prisma;
if (typeof _prisma !== 'undefined') return _prisma;
return require('@prisma/client/runtime');
if (typeof _prisma !== 'undefined') {
return _prisma;
}
} catch {
return require('@prisma/client/runtime');
// noop
}
} else {
}

try {
// Prisma v4
return require('@prisma/client/runtime');
} catch {
try {
// Prisma v5
return require('@prisma/client');
} catch (err) {
if (process.env.ZENSTACK_TEST === '1') {
// running in test, try cwd
try {
return require(path.join(process.cwd(), 'node_modules/@prisma/client/runtime'));
} catch {
return require(path.join(process.cwd(), 'node_modules/@prisma/client'));
}
} else {
throw err;
}
}
}
}

Expand Down
8 changes: 2 additions & 6 deletions packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@
},
"dependencies": {
"@paralleldrive/cuid2": "^2.2.0",
"@prisma/generator-helper": "4.10.0",
"@prisma/internals": "4.10.0",
"@prisma/generator-helper": "^5.0.0",
"@zenstackhq/language": "workspace:*",
"@zenstackhq/sdk": "workspace:*",
"async-exit-hook": "^2.0.1",
Expand Down Expand Up @@ -114,13 +113,11 @@
"devDependencies": {
"@types/async-exit-hook": "^2.0.0",
"@types/jest": "^29.5.0",
"@types/lower-case-first": "^1.0.1",
"@types/node": "^14.18.32",
"@types/node": "^18.0.0",
"@types/pluralize": "^0.0.29",
"@types/semver": "^7.3.13",
"@types/strip-color": "^0.1.0",
"@types/tmp": "^0.2.3",
"@types/upper-case-first": "^1.1.2",
"@types/uuid": "^8.3.4",
"@types/vscode": "^1.56.0",
"@typescript-eslint/eslint-plugin": "^5.42.0",
Expand All @@ -134,7 +131,6 @@
"eslint": "^8.27.0",
"eslint-plugin-jest": "^27.1.7",
"jest": "^29.5.0",
"prisma": "^4.0.0",
"renamer": "^4.0.0",
"rimraf": "^3.0.2",
"tmp": "^0.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/cli/plugin-runner.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-var-requires */
import type { DMMF } from '@prisma/generator-helper';
import { getDMMF } from '@prisma/internals';
import { isPlugin, Plugin } from '@zenstackhq/language/ast';
import {
getDataModels,
getDMMF,
getLiteral,
getLiteralArray,
hasValidationAttributes,
Expand Down
Loading