From a6382747643b0bd7c28f4f3889f9594fa60cc39f Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Sun, 12 May 2024 18:06:27 +0800 Subject: [PATCH] fix: avoid importing prisma-related code into language server --- packages/plugins/openapi/src/rpc-generator.ts | 4 ++-- packages/plugins/swr/src/generator.ts | 3 +-- packages/plugins/tanstack-query/src/generator.ts | 3 +-- packages/plugins/trpc/src/generator.ts | 3 +-- packages/schema/src/plugins/zod/generator.ts | 3 +-- packages/schema/src/plugins/zod/transformer.ts | 4 ++-- packages/sdk/src/prisma.ts | 13 +++++++++++++ packages/sdk/src/utils.ts | 12 ------------ 8 files changed, 21 insertions(+), 24 deletions(-) diff --git a/packages/plugins/openapi/src/rpc-generator.ts b/packages/plugins/openapi/src/rpc-generator.ts index 9e20df4ad..7339ef788 100644 --- a/packages/plugins/openapi/src/rpc-generator.ts +++ b/packages/plugins/openapi/src/rpc-generator.ts @@ -1,6 +1,6 @@ // Inspired by: https://github.com/omar-dulaimi/prisma-trpc-generator -import { PluginError, analyzePolicies, requireOption, resolvePath, supportCreateMany } from '@zenstackhq/sdk'; +import { PluginError, analyzePolicies, requireOption, resolvePath } from '@zenstackhq/sdk'; import { DataModel, isDataModel } from '@zenstackhq/sdk/ast'; import { AggregateOperationSupport, @@ -10,7 +10,7 @@ import { addMissingInputObjectTypesForSelect, resolveAggregateOperationSupport, } from '@zenstackhq/sdk/dmmf-helpers'; -import { type DMMF } from '@zenstackhq/sdk/prisma'; +import { supportCreateMany, 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'; diff --git a/packages/plugins/swr/src/generator.ts b/packages/plugins/swr/src/generator.ts index a785ed8f4..65c80fc8c 100644 --- a/packages/plugins/swr/src/generator.ts +++ b/packages/plugins/swr/src/generator.ts @@ -8,10 +8,9 @@ import { requireOption, resolvePath, saveProject, - supportCreateMany, } from '@zenstackhq/sdk'; import { DataModel, DataModelFieldType, Model, isEnum } from '@zenstackhq/sdk/ast'; -import { getPrismaClientImportSpec, type DMMF } from '@zenstackhq/sdk/prisma'; +import { getPrismaClientImportSpec, supportCreateMany, type DMMF } from '@zenstackhq/sdk/prisma'; import { paramCase } from 'change-case'; import path from 'path'; import type { OptionalKind, ParameterDeclarationStructure, Project, SourceFile } from 'ts-morph'; diff --git a/packages/plugins/tanstack-query/src/generator.ts b/packages/plugins/tanstack-query/src/generator.ts index db7b30e83..6a4ba53d9 100644 --- a/packages/plugins/tanstack-query/src/generator.ts +++ b/packages/plugins/tanstack-query/src/generator.ts @@ -9,10 +9,9 @@ import { requireOption, resolvePath, saveProject, - supportCreateMany, } from '@zenstackhq/sdk'; import { DataModel, DataModelFieldType, Model, isEnum } from '@zenstackhq/sdk/ast'; -import { getPrismaClientImportSpec, type DMMF } from '@zenstackhq/sdk/prisma'; +import { getPrismaClientImportSpec, supportCreateMany, type DMMF } from '@zenstackhq/sdk/prisma'; import { paramCase } from 'change-case'; import { lowerCaseFirst } from 'lower-case-first'; import path from 'path'; diff --git a/packages/plugins/trpc/src/generator.ts b/packages/plugins/trpc/src/generator.ts index 01b1ca2f7..0180cc649 100644 --- a/packages/plugins/trpc/src/generator.ts +++ b/packages/plugins/trpc/src/generator.ts @@ -7,11 +7,10 @@ import { requireOption, resolvePath, saveProject, - supportCreateMany, type PluginOptions, } from '@zenstackhq/sdk'; import { Model } from '@zenstackhq/sdk/ast'; -import { getPrismaClientImportSpec, type DMMF } from '@zenstackhq/sdk/prisma'; +import { getPrismaClientImportSpec, supportCreateMany, type DMMF } from '@zenstackhq/sdk/prisma'; import fs from 'fs'; import { lowerCaseFirst } from 'lower-case-first'; import path from 'path'; diff --git a/packages/schema/src/plugins/zod/generator.ts b/packages/schema/src/plugins/zod/generator.ts index 2054acac5..91f152af8 100644 --- a/packages/schema/src/plugins/zod/generator.ts +++ b/packages/schema/src/plugins/zod/generator.ts @@ -10,11 +10,10 @@ import { isFromStdlib, parseOptionAsStrings, resolvePath, - supportCreateMany, } 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 { getPrismaClientImportSpec, supportCreateMany, type DMMF } from '@zenstackhq/sdk/prisma'; import { streamAllContents } from 'langium'; import path from 'path'; import type { SourceFile } from 'ts-morph'; diff --git a/packages/schema/src/plugins/zod/transformer.ts b/packages/schema/src/plugins/zod/transformer.ts index 677fa6284..09804f42b 100644 --- a/packages/schema/src/plugins/zod/transformer.ts +++ b/packages/schema/src/plugins/zod/transformer.ts @@ -1,8 +1,8 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ -import { indentString, supportCreateMany, type PluginOptions } from '@zenstackhq/sdk'; +import { indentString, type PluginOptions } from '@zenstackhq/sdk'; import type { Model } from '@zenstackhq/sdk/ast'; import { checkModelHasModelRelation, findModelByName, isAggregateInputType } from '@zenstackhq/sdk/dmmf-helpers'; -import { type DMMF as PrismaDMMF } from '@zenstackhq/sdk/prisma'; +import { supportCreateMany, type DMMF as PrismaDMMF } from '@zenstackhq/sdk/prisma'; import path from 'path'; import type { Project, SourceFile } from 'ts-morph'; import { upperCaseFirst } from 'upper-case-first'; diff --git a/packages/sdk/src/prisma.ts b/packages/sdk/src/prisma.ts index b45dd7cfb..1edab3c3d 100644 --- a/packages/sdk/src/prisma.ts +++ b/packages/sdk/src/prisma.ts @@ -4,8 +4,11 @@ import type { DMMF } from '@prisma/generator-helper'; import { getDMMF as _getDMMF, type GetDMMFOptions } from '@prisma/internals'; import { DEFAULT_RUNTIME_LOAD_PATH } from '@zenstackhq/runtime'; import path from 'path'; +import semver from 'semver'; +import { Model } from './ast'; import { RUNTIME_PACKAGE } from './constants'; import type { PluginOptions } from './types'; +import { getDataSourceProvider } from './utils'; /** * Given an import context directory and plugin options, compute the import spec for the Prisma Client. @@ -75,4 +78,14 @@ export function getPrismaVersion(): string | undefined { } } +/** + * Returns if the given model supports `createMany` operation. + */ +export function supportCreateMany(model: Model) { + // `createMany` is supported for sqlite since Prisma 5.12.0 + const prismaVersion = getPrismaVersion(); + const dsProvider = getDataSourceProvider(model); + return dsProvider !== 'sqlite' || (prismaVersion && semver.gte(prismaVersion, '5.12.0')); +} + export type { DMMF } from '@prisma/generator-helper'; diff --git a/packages/sdk/src/utils.ts b/packages/sdk/src/utils.ts index 8c3a00087..df83186b5 100644 --- a/packages/sdk/src/utils.ts +++ b/packages/sdk/src/utils.ts @@ -33,9 +33,7 @@ import { } from '@zenstackhq/language/ast'; import fs from 'node:fs'; import path from 'path'; -import semver from 'semver'; import { ExpressionContext, STD_LIB_MODULE_NAME } from './constants'; -import { getPrismaVersion } from './prisma'; import { PluginError, type PluginDeclaredOptions, type PluginOptions } from './types'; /** @@ -556,13 +554,3 @@ export function getDataSourceProvider(model: Model) { } return getLiteral(provider.value); } - -/** - * Returns if the given model supports `createMany` operation. - */ -export function supportCreateMany(model: Model) { - // `createMany` is supported for sqlite since Prisma 5.12.0 - const prismaVersion = getPrismaVersion(); - const dsProvider = getDataSourceProvider(model); - return dsProvider !== 'sqlite' || (prismaVersion && semver.gte(prismaVersion, '5.12.0')); -}