Skip to content

fix: avoid importing prisma-related code into language server #1441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2024
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
4 changes: 2 additions & 2 deletions 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 { 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,
Expand All @@ -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';
Expand Down
3 changes: 1 addition & 2 deletions packages/plugins/swr/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 1 addition & 2 deletions packages/plugins/tanstack-query/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
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 @@ -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';
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 @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions packages/schema/src/plugins/zod/transformer.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
13 changes: 13 additions & 0 deletions packages/sdk/src/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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';
12 changes: 0 additions & 12 deletions packages/sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -556,13 +554,3 @@ export function getDataSourceProvider(model: Model) {
}
return getLiteral<string>(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'));
}