Skip to content
This repository has been archived by the owner on Oct 9, 2022. It is now read-only.

Commit

Permalink
feat: update to prisma v3 (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyhou authored Nov 5, 2021
1 parent 601ffe8 commit 5e53b19
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 261 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# prisma-schema-transformer

> **EXPERIMENTAL**
> **EXPERIMENTAL FOR PRISMA V3**
This project utilizes the [getDMMF](https://github.com/prisma/prisma/blob/023249752380976d797518e1350199895246d099/src/packages/sdk/src/engineCommands.ts#L45) method from `@prisma/sdk` to perform some post-processing work on generated Prisma schema, including the following.

Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@
"lint": "xo --fix"
},
"dependencies": {
"prisma": "^2.20.0",
"@prisma/generator-helper": "^2.20.1",
"@prisma/sdk": "^2.20.1",
"@prisma/engine-core": "^3.2.1",
"@prisma/generator-helper": "^3.2.1",
"@prisma/sdk": "^3.2.1",
"arg": "^4.1.3",
"camelcase": "^6.0.0",
"dotenv": "^8.2.0",
"immer": "^9.0.6",
"pluralize": "^8.0.0"
"pluralize": "^8.0.0",
"prisma": "^3.2.1"
},
"devDependencies": {
"@ava/typescript": "^1.1.1",
Expand Down
29 changes: 3 additions & 26 deletions src/deserializer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ConnectorType, DataSource, DMMF, EnvValue, GeneratorConfig} from '@prisma/generator-helper/dist';
import {printGeneratorConfig} from '@prisma/engine-core';

export interface Field {
kind: DMMF.FieldKind;
Expand Down Expand Up @@ -125,7 +126,7 @@ function handleFields(fields: Field[]) {
}

function handleIdFields(idFields: string[]) {
return idFields.length > 0 ? `@@id([${idFields.join(', ')}])` : '';
return idFields && idFields.length > 0 ? `@@id([${idFields.join(', ')}])` : '';
}

function handleUniqueFieds(uniqueFields: string[][]) {
Expand All @@ -146,18 +147,6 @@ function handleProvider(provider: ConnectorType | string) {
return `provider = "${provider}"`;
}

function handleOutput(path: string | null) {
return path ? `output = "${path}"` : '';
}

function handleBinaryTargets(binaryTargets?: string[]) {
return binaryTargets?.length ? `binaryTargets = ${JSON.stringify(binaryTargets)}` : '';
}

function handlePreviewFeatures(previewFeatures: GeneratorConfig['previewFeatures']) {
return previewFeatures.length ? `previewFeatures = ${JSON.stringify(previewFeatures)}` : '';
}

function deserializeModel(model: Model) {
const {name, uniqueFields, dbName, idFields} = model;
const fields = model.fields as unknown as Field[];
Expand All @@ -182,18 +171,6 @@ datasource ${name} {
}`;
}

function deserializeGenerator(generator: GeneratorConfig) {
const {binaryTargets, name, output, provider, previewFeatures} = generator;

return `
generator ${name} {
${handleProvider(provider.value)}
${handleOutput(output?.value || null)}
${handleBinaryTargets(binaryTargets)}
${handlePreviewFeatures(previewFeatures)}
}`;
}

function deserializeEnum({name, values, dbName}: DMMF.DatamodelEnum) {
const outputValues = values.map(({ name, dbName }) => {
let result = name
Expand All @@ -220,7 +197,7 @@ export async function datasourcesDeserializer(datasources: DataSource[]) {
}

export async function generatorsDeserializer(generators: GeneratorConfig[]) {
return generators.map(generator => deserializeGenerator(generator)).join('\n');
return generators.map(generator => printGeneratorConfig(generator)).join('\n');
}

export async function dmmfEnumsDeserializer(enums: DMMF.DatamodelEnum[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function transformModel(model: Model) {
});

const fixIdFieldsName = produce(fixUniqueName, draftModel => {
if (idFields.length > 0) {
if (idFields && idFields.length > 0) {
draftModel.idFields = idFields.map(eachIdField => camelcase(eachIdField));
}
});
Expand Down
7 changes: 2 additions & 5 deletions test/snapshots/deserializer.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ Generated by [AVA](https://avajs.dev).
}␊
generator client {␊
provider = "prisma-client-js"␊
output = "node_modules/@prisma/client"␊
binaryTargets = ["native"]␊
previewFeatures = ["orderByRelation","selectRelationCount"]␊
provider = "prisma-client-js"␊
binaryTargets = ["native"]␊
}␊
Expand Down
Binary file modified test/snapshots/deserializer.ts.snap
Binary file not shown.
Loading

0 comments on commit 5e53b19

Please sign in to comment.