Skip to content

fix: add "interactiveTransactions" preview features for lower version of Prisma #569

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 2 commits into from
Jul 9, 2023
Merged
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
32 changes: 31 additions & 1 deletion packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
import fs from 'fs';
import { writeFile } from 'fs/promises';
import path from 'path';
import semver from 'semver';
import stripColor from 'strip-color';
import { name } from '.';
import { getStringLiteral } from '../../language-server/validator/utils';
Expand Down Expand Up @@ -236,14 +237,43 @@ export default class PrismaSchemaGenerator {
}
}

private getPrismaVersion() {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require('@prisma/client/package.json').version;
} catch {
return undefined;
}
}

private generateGenerator(prisma: PrismaModel, decl: GeneratorDecl) {
prisma.addGenerator(
const generator = prisma.addGenerator(
decl.name,
decl.fields.map((f) => {
const value = isArrayExpr(f.value) ? getLiteralArray(f.value) : getLiteral(f.value);
return { name: f.name, value };
})
);

// deal with configuring PrismaClient preview features
const provider = generator.fields.find((f) => f.name === 'provider');
if (provider?.value === 'prisma-client-js') {
const prismaVersion = this.getPrismaVersion();
if (prismaVersion && semver.lt(prismaVersion, '4.7.0')) {
// insert interactiveTransactions preview feature
let previewFeatures = generator.fields.find((f) => f.name === 'previewFeatures');
if (!previewFeatures) {
previewFeatures = { name: 'previewFeatures', value: [] };
generator.fields.push(previewFeatures);
}
if (!Array.isArray(previewFeatures.value)) {
throw new PluginError(name, 'option "previewFeatures" must be an array');
}
if (!previewFeatures.value.includes('interactiveTransactions')) {
previewFeatures.value.push('interactiveTransactions');
}
}
}
}

private generateModel(prisma: PrismaModel, decl: DataModel, config?: Record<string, string>) {
Expand Down