Skip to content

Commit

Permalink
feat: Add disable flag to prisma config options (#233)
Browse files Browse the repository at this point in the history
close issue #202 by adding an environment variable friendly approach to disabling the generator
  • Loading branch information
keonik authored Aug 11, 2023
1 parent 3973702 commit 7d3b9a5
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 22 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,23 @@ generator erd {

### Disabled

You won't always need to generate a new ERD. For instance, when you are building your docker containers you often run `prisma generate` and if this generator is included, odds are you aren't relying on an updated ERD inside your docker container. It also adds additional space to the container because of dependencies such as puppeteer. To disabled running this generator just add an environment variable to the environment running `prisma generate`.
You won't always need to generate a new ERD. For instance, when you are building your docker containers you often run `prisma generate` and if this generator is included, odds are you aren't relying on an updated ERD inside your docker container. It also adds additional space to the container because of dependencies such as puppeteer. There are two ways to disable this ERD generator.

1. Via environment variable

```bash
DISABLE_ERD=true
```

2. Via configuration

```prisma
generator erd {
provider = "prisma-erd-generator"
disabled = true
}
```

Another option used is to remove the generator lines from your schema before installing dependencies and running the `prisma generate` command. I have used `sed` to remove the lines the generator is located on in my `schema.prisma` file to do so. Here is an example of the ERD generator being removed on lines 5-9 in a dockerfile.

```dockerfile
Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions __tests__/disabled-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as child_process from 'child_process';
import fs from 'fs';

test('disabled-config.prisma', async () => {
const fileName = 'disabled-config.svg';
const folderName = '__tests__';
child_process.execSync(`rm -f ${folderName}/${fileName}`);
child_process.execSync(
`npx cross-env prisma generate --schema ./prisma/disabled-config.prisma`
);
const exists = fs.existsSync(`${folderName}/${fileName}`);
expect(exists).toBe(false);
});
13 changes: 13 additions & 0 deletions __tests__/disabled-env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as child_process from 'child_process';
import fs from 'fs';

test('disabled-env.prisma', async () => {
const fileName = 'disabled.svg';
const folderName = '__tests__';
child_process.execSync(`rm -f ${folderName}/${fileName}`);
child_process.execSync(
`npx cross-env DISABLE_ERD=true prisma generate --schema ./prisma/disabled-env.prisma`
);
const exists = fs.existsSync(`${folderName}/${fileName}`);
expect(exists).toBe(false);
});
20 changes: 0 additions & 20 deletions __tests__/disabled.ts

This file was deleted.

File renamed without changes.
29 changes: 29 additions & 0 deletions prisma/disabled-config.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

generator erd {
provider = "node ./dist/index.js"
output = "../__tests__/disabled-config.svg"
disabled = true
// debug = true
}

model Booking {
id Int @id @default(autoincrement())
inviteeEmail String
startDateUTC DateTime
cancelCode String
events Event[]
}

model Event {
id Int @id @default(autoincrement())
name String
startDate DateTime
bookings Booking[]
}
File renamed without changes.
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ generator erd {
theme = "default"
mmdcPath = "node_modules/.bin"
mermaidConfig = "example-mermaid-config.js"
disabled = false
}

model User {
Expand Down
3 changes: 2 additions & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ export default async (options: GeneratorOptions) => {
const ignoreEnums = config.ignoreEnums === 'true';
const includeRelationFromFields =
config.includeRelationFromFields === 'true';
const disabled = process.env.DISABLE_ERD === 'true';
const disabled =
process.env.DISABLE_ERD === 'true' || config.disabled === 'true';
const debug =
config.erdDebug === 'true' || Boolean(process.env.ERD_DEBUG);

Expand Down
1 change: 1 addition & 0 deletions src/types/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface PrismaERDConfig {
erdDebug?: string;
puppeteerConfig?: string;
mermaidConfig?: string;
disabled?: string;
}

0 comments on commit 7d3b9a5

Please sign in to comment.