Skip to content

Commit

Permalink
fix(sdk): exclude prisma module from barrel file to avoid eager loadi…
Browse files Browse the repository at this point in the history
…ng of prisma packages (#1255)
  • Loading branch information
ymc9 authored Apr 14, 2024
1 parent b702a10 commit c37bf92
Show file tree
Hide file tree
Showing 30 changed files with 54 additions and 89 deletions.
10 changes: 2 additions & 8 deletions packages/plugins/openapi/src/generator-base.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import {
PluginError,
getDataModels,
hasAttribute,
type DMMF,
type PluginOptions,
type PluginResult,
} from '@zenstackhq/sdk';
import { PluginError, getDataModels, hasAttribute, type PluginOptions, type PluginResult } from '@zenstackhq/sdk';
import { Model } from '@zenstackhq/sdk/ast';
import type { DMMF } from '@zenstackhq/sdk/prisma';
import type { OpenAPIV3_1 as OAPI } from 'openapi-types';
import semver from 'semver';
import { fromZodError } from 'zod-validation-error';
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
Expand Up @@ -9,9 +9,9 @@ import {
isRelationshipField,
requireOption,
resolvePath,
type DMMF,
} from '@zenstackhq/sdk';
import { DataModel, DataModelField, DataModelFieldType, Enum, isDataModel, isEnum } from '@zenstackhq/sdk/ast';
import type { DMMF } from '@zenstackhq/sdk/prisma';
import fs from 'fs';
import { lowerCaseFirst } from 'lower-case-first';
import type { OpenAPIV3_1 as OAPI } from 'openapi-types';
Expand Down
3 changes: 2 additions & 1 deletion packages/plugins/openapi/src/rpc-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 { analyzePolicies, PluginError, requireOption, resolvePath, type DMMF } from '@zenstackhq/sdk';
import { analyzePolicies, PluginError, requireOption, resolvePath } from '@zenstackhq/sdk';
import { DataModel, isDataModel } from '@zenstackhq/sdk/ast';
import {
addMissingInputObjectTypesForAggregate,
Expand All @@ -10,6 +10,7 @@ import {
AggregateOperationSupport,
resolveAggregateOperationSupport,
} from '@zenstackhq/sdk/dmmf-helpers';
import type { DMMF } from '@zenstackhq/sdk/prisma';
import * as fs from 'fs';
import { lowerCaseFirst } from 'lower-case-first';
import type { OpenAPIV3_1 as OAPI } from 'openapi-types';
Expand Down
12 changes: 2 additions & 10 deletions packages/plugins/swr/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import {
ensureEmptyDir,
generateModelMeta,
getDataModels,
getPrismaClientImportSpec,
getPrismaVersion,
requireOption,
resolvePath,
saveProject,
type DMMF,
} from '@zenstackhq/sdk';
import { DataModel, Model } from '@zenstackhq/sdk/ast';
import { getPrismaClientImportSpec, type DMMF } from '@zenstackhq/sdk/prisma';
import { paramCase } from 'change-case';
import path from 'path';
import semver from 'semver';
import type { OptionalKind, ParameterDeclarationStructure, Project, SourceFile } from 'ts-morph';
import { upperCaseFirst } from 'upper-case-first';
import { name } from '.';
Expand Down Expand Up @@ -74,7 +71,6 @@ function generateModelHooks(
]);

const modelNameCap = upperCaseFirst(model.name);
const prismaVersion = getPrismaVersion();

const mutationFuncs: string[] = [];

Expand Down Expand Up @@ -168,11 +164,7 @@ function generateModelHooks(

// groupBy
if (mapping.groupBy) {
let useName = modelNameCap;
if (prismaVersion && semver.gte(prismaVersion, '5.0.0')) {
// prisma 4 and 5 different typing for "groupBy" and we have to deal with it separately
useName = model.name;
}
const useName = model.name;
const typeParameters = [
`T extends Prisma.${useName}GroupByArgs`,
`HasSelectOrTake extends Prisma.Or<Prisma.Extends<'skip', Prisma.Keys<T>>, Prisma.Extends<'take', Prisma.Keys<T>>>`,
Expand Down
12 changes: 2 additions & 10 deletions packages/plugins/tanstack-query/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ import {
ensureEmptyDir,
generateModelMeta,
getDataModels,
getPrismaClientImportSpec,
getPrismaVersion,
requireOption,
resolvePath,
saveProject,
type DMMF,
} from '@zenstackhq/sdk';
import { DataModel, Model } from '@zenstackhq/sdk/ast';
import { getPrismaClientImportSpec, type DMMF } from '@zenstackhq/sdk/prisma';
import { paramCase } from 'change-case';
import { lowerCaseFirst } from 'lower-case-first';
import path from 'path';
import semver from 'semver';
import { Project, SourceFile, VariableDeclarationKind } from 'ts-morph';
import { match } from 'ts-pattern';
import { upperCaseFirst } from 'upper-case-first';
Expand Down Expand Up @@ -274,7 +271,6 @@ function generateModelHooks(
options: PluginOptions
) {
const modelNameCap = upperCaseFirst(model.name);
const prismaVersion = getPrismaVersion();
const fileName = paramCase(model.name);
const sf = project.createSourceFile(path.join(outDir, `${fileName}.ts`), undefined, { overwrite: true });

Expand Down Expand Up @@ -401,11 +397,7 @@ function generateModelHooks(

// groupBy
if (mapping.groupBy) {
let useName = modelNameCap;
// prisma 4 and 5 different typing for "groupBy" and we have to deal with it separately
if (prismaVersion && semver.gte(prismaVersion, '5.0.0')) {
useName = model.name;
}
const useName = model.name;

const returnType = `{} extends InputErrors ?
Array<PickEnumerable<Prisma.${modelNameCap}GroupByOutputType, TArgs['by']> &
Expand Down
3 changes: 1 addition & 2 deletions packages/plugins/trpc/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import {
PluginError,
RUNTIME_PACKAGE,
ensureEmptyDir,
getPrismaClientImportSpec,
parseOptionAsStrings,
requireOption,
resolvePath,
saveProject,
type DMMF,
type PluginOptions,
} from '@zenstackhq/sdk';
import { Model } from '@zenstackhq/sdk/ast';
import { getPrismaClientImportSpec, type DMMF } from '@zenstackhq/sdk/prisma';
import fs from 'fs';
import { lowerCaseFirst } from 'lower-case-first';
import path from 'path';
Expand Down
3 changes: 2 additions & 1 deletion packages/plugins/trpc/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PluginError, getPrismaClientImportSpec, type DMMF, type PluginOptions } from '@zenstackhq/sdk';
import { PluginError, type PluginOptions } from '@zenstackhq/sdk';
import { getPrismaClientImportSpec, type DMMF } from '@zenstackhq/sdk/prisma';
import { lowerCaseFirst } from 'lower-case-first';
import { CodeBlockWriter, SourceFile } from 'ts-morph';
import { upperCaseFirst } from 'upper-case-first';
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
Expand Up @@ -11,12 +11,12 @@ import {
PluginError,
resolvePath,
saveProject,
type DMMF,
type OptionValue,
type PluginDeclaredOptions,
type PluginFunction,
type PluginResult,
} from '@zenstackhq/sdk';
import { type DMMF } from '@zenstackhq/sdk/prisma';
import colors from 'colors';
import ora from 'ora';
import path from 'path';
Expand Down
4 changes: 1 addition & 3 deletions packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import {
getAttribute,
getAttributeArg,
getAuthModel,
getDMMF,
getDataModels,
getPrismaClientImportSpec,
isDelegateModel,
type DMMF,
type PluginOptions,
} from '@zenstackhq/sdk';
import {
Expand All @@ -20,6 +17,7 @@ import {
isReferenceExpr,
type Model,
} from '@zenstackhq/sdk/ast';
import { getDMMF, getPrismaClientImportSpec, type DMMF } from '@zenstackhq/sdk/prisma';
import fs from 'fs';
import path from 'path';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
getDataModels,
getIdFields,
getLiteral,
getPrismaClientImportSpec,
hasAttribute,
hasValidationAttributes,
isAuthInvocation,
Expand All @@ -47,6 +46,7 @@ import {
isFutureExpr,
resolved,
} from '@zenstackhq/sdk';
import { getPrismaClientImportSpec } from '@zenstackhq/sdk/prisma';
import { streamAllContents, streamAst, streamContents } from 'langium';
import { lowerCaseFirst } from 'lower-case-first';
import path from 'path';
Expand Down
3 changes: 2 additions & 1 deletion packages/schema/src/plugins/prisma/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PluginError, PluginFunction, getDMMF, getLiteral, resolvePath } from '@zenstackhq/sdk';
import { PluginError, PluginFunction, getLiteral, resolvePath } from '@zenstackhq/sdk';
import { GeneratorDecl, isGeneratorDecl } from '@zenstackhq/sdk/ast';
import { getDMMF } from '@zenstackhq/sdk/prisma';
import fs from 'fs';
import path from 'path';
import stripColor from 'strip-color';
Expand Down
16 changes: 1 addition & 15 deletions packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
NumberLiteral,
StringLiteral,
} from '@zenstackhq/language/ast';
import { getPrismaVersion } from '@zenstackhq/sdk/prisma';
import { match, P } from 'ts-pattern';
import { getIdFields } from '../../utils/ast-utils';

Expand All @@ -37,7 +38,6 @@ import {
getAttributeArg,
getAttributeArgLiteral,
getLiteral,
getPrismaVersion,
isDelegateModel,
isIdField,
PluginError,
Expand Down Expand Up @@ -224,20 +224,6 @@ export class PrismaSchemaGenerator {
throw new PluginError(name, 'option "previewFeatures" must be an array');
}

if (semver.lt(prismaVersion, '5.0.0')) {
// extendedWhereUnique feature is opt-in pre V5
if (!previewFeatures.includes('extendedWhereUnique')) {
previewFeatures.push('extendedWhereUnique');
}
}

if (semver.lt(prismaVersion, '5.0.0')) {
// fieldReference feature is opt-in pre V5
if (!previewFeatures.includes('fieldReference')) {
previewFeatures.push('fieldReference');
}
}

if (previewFeatures.length > 0) {
const curr = generator.fields.find((f) => f.name === 'previewFeatures');
if (!curr) {
Expand Down
3 changes: 1 addition & 2 deletions packages/schema/src/plugins/zod/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import {
PluginOptions,
ensureEmptyDir,
getDataModels,
getPrismaClientImportSpec,
hasAttribute,
isEnumFieldReference,
isForeignKeyField,
isFromStdlib,
parseOptionAsStrings,
resolvePath,
type DMMF,
} from '@zenstackhq/sdk';
import { DataModel, EnumField, Model, isDataModel, isEnum } from '@zenstackhq/sdk/ast';
import { addMissingInputObjectTypes, resolveAggregateOperationSupport } from '@zenstackhq/sdk/dmmf-helpers';
import { getPrismaClientImportSpec, type DMMF } from '@zenstackhq/sdk/prisma';
import { streamAllContents } from 'langium';
import path from 'path';
import type { SourceFile } from 'ts-morph';
Expand Down
21 changes: 3 additions & 18 deletions packages/schema/src/plugins/zod/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import {
getPrismaClientImportSpec,
getPrismaVersion,
type PluginOptions,
type DMMF as PrismaDMMF,
} from '@zenstackhq/sdk';
import { indentString, type PluginOptions } from '@zenstackhq/sdk';
import { checkModelHasModelRelation, findModelByName, isAggregateInputType } from '@zenstackhq/sdk/dmmf-helpers';
import { indentString } from '@zenstackhq/sdk/utils';
import { getPrismaClientImportSpec, type DMMF as PrismaDMMF } from '@zenstackhq/sdk/prisma';
import path from 'path';
import * as semver from 'semver';
import type { Project, SourceFile } from 'ts-morph';
import { upperCaseFirst } from 'upper-case-first';
import { AggregateOperationSupport, TransformerParams } from './types';
Expand Down Expand Up @@ -568,10 +562,6 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;

const aggregateOperations = [];

// DMMF messed up the model name casing used in the aggregate operations,
// AND the casing behavior varies from version to version -_-||
const prismaVersion = getPrismaVersion();

if (this.aggregateOperationSupport[modelName]?.count) {
imports.push(
`import { ${modelName}CountAggregateInputObjectSchema } from '../objects/${modelName}CountAggregateInput.schema'`
Expand Down Expand Up @@ -629,12 +619,7 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
', '
)} }),`;

// prisma 4 and 5 different typing for "groupBy" and we have to deal with it separately
if (prismaVersion && semver.gte(prismaVersion, '5.0.0')) {
operations.push(['groupBy', origModelName]);
} else {
operations.push(['groupBy', modelName]);
}
operations.push(['groupBy', origModelName]);
}

// count
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/plugins/zod/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DMMF as PrismaDMMF } from '@zenstackhq/sdk';
import type { DMMF as PrismaDMMF } from '@zenstackhq/sdk/prisma';
import { Project } from 'ts-morph';

export type TransformerParams = {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createId } from '@paralleldrive/cuid2';
import { getPrismaVersion } from '@zenstackhq/sdk';
import { getPrismaVersion } from '@zenstackhq/sdk/prisma';
import exitHook from 'async-exit-hook';
import { CommanderError } from 'commander';
import { init, Mixpanel } from 'mixpanel';
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/tests/generator/prisma-builder.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDMMF } from '@zenstackhq/sdk';
import { getDMMF } from '@zenstackhq/sdk/prisma';
import {
AttributeArg,
AttributeArgValue,
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/tests/generator/prisma-generator.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="@types/jest" />

import { getDMMF } from '@zenstackhq/sdk';
import { getDMMF } from '@zenstackhq/sdk/prisma';
import fs from 'fs';
import path from 'path';
import tmp from 'tmp';
Expand Down
18 changes: 18 additions & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,23 @@
},
"devDependencies": {
"@types/semver": "^7.3.13"
},
"exports": {
".": {
"types": "./index.d.ts",
"default": "./index.js"
},
"./ast": {
"types": "./ast.d.ts",
"default": "./ast.js"
},
"./prisma": {
"types": "./prisma.d.ts",
"default": "./prisma.js"
},
"./dmmf-helpers": {
"types": "./dmmf-helpers/index.d.ts",
"default": "./dmmf-helpers/index.js"
}
}
}
2 changes: 1 addition & 1 deletion packages/sdk/src/dmmf-helpers/aggregate-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { upperCaseFirst } from 'upper-case-first';
import type { DMMF } from '../types';
import type { DMMF } from '../prisma';
import { AggregateOperationSupport } from './types';

const isAggregateOutputType = (name: string) => /(?:Count|Avg|Sum|Min|Max)AggregateOutputType$/.test(name);
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/dmmf-helpers/include-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DMMF } from '../types';
import type { DMMF } from '../prisma';
import { checkIsModelRelationField, checkModelHasManyModelRelation, checkModelHasModelRelation } from './model-helpers';

export function addMissingInputObjectTypesForInclude(inputObjectTypes: DMMF.InputType[], models: DMMF.Model[]) {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/dmmf-helpers/missing-types-helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DMMF } from '../types';
import type { DMMF } from '../prisma';
import { addMissingInputObjectTypesForAggregate } from './aggregate-helpers';
import { addMissingInputObjectTypesForInclude } from './include-helpers';
import { addMissingInputObjectTypesForModelArgs } from './modelArgs-helpers';
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/dmmf-helpers/model-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DMMF } from '../types';
import type { DMMF } from '../prisma';

export function checkModelHasModelRelation(model: DMMF.Model) {
const { fields: modelFields } = model;
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/dmmf-helpers/modelArgs-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DMMF } from '../types';
import type { DMMF } from '../prisma';
import { checkModelHasModelRelation } from './model-helpers';

export function addMissingInputObjectTypesForModelArgs(inputObjectTypes: DMMF.InputType[], models: DMMF.Model[]) {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/dmmf-helpers/select-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DMMF } from '../types';
import type { DMMF } from '../prisma';
import { checkIsModelRelationField, checkModelHasManyModelRelation } from './model-helpers';

export function addMissingInputObjectTypesForSelect(
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/dmmf-helpers/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DMMF } from '../types';
import type { DMMF } from '../prisma';

export type TransformerParams = {
enumTypes?: DMMF.SchemaEnum[];
Expand Down
Loading

0 comments on commit c37bf92

Please sign in to comment.