Skip to content

Commit

Permalink
feat: Add option to hide enums (#201)
Browse files Browse the repository at this point in the history
* feat: Add option to hide enums

* refactor: Change option name to ignoreEnums

* docs: Add description of ignoreEnums

* refactor: Lint by prettier
  • Loading branch information
ripry committed May 3, 2023
1 parent e6a3838 commit 8b3bf99
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 10 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ generator erd {
}
```

### Ignore enums

If you enable this option, enum entities will be hidden.
This is useful if you want to reduce the number of entities and focus on the tables and their columns and relationships.

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

### Include relation from field

By default this module skips relation fields in the result diagram. For example fields `userId` and `productId` will not be generated from this prisma schema.
Expand Down
36 changes: 36 additions & 0 deletions __tests__/ignoreEnums.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as child_process from 'child_process';

test('ignore-enums.prisma', async () => {
const fileName = 'ignoreEnums.svg';
const folderName = '__tests__';
child_process.execSync(`rm -f ${folderName}/${fileName}`);
child_process.execSync(
`prisma generate --schema ./prisma/ignore-enums.prisma`
);
const listFile = child_process.execSync(`ls -la ${folderName}/${fileName}`);
// did it generate a file
expect(listFile.toString()).toContain(fileName);

const svgAsString = child_process
.execSync(`cat ${folderName}/${fileName}`)
.toString();

// did it generate a file without enum
expect(svgAsString).toContain(`<svg`);
// include tables
expect(svgAsString).toContain(`Booking`);
expect(svgAsString).toContain(`Event`);
// incluse table columns
expect(svgAsString).toContain(`name`);
expect(svgAsString).toContain(`startDate`);
expect(svgAsString).toContain(`status`);
expect(svgAsString).toContain(`inviteeEmail`);
expect(svgAsString).toContain(`startDateUTC`);
expect(svgAsString).toContain(`cancelCode`);
// include enum names
expect(svgAsString).toContain(`Status`);
// exclude enums
expect(svgAsString).not.toContain(`PENDING`);
expect(svgAsString).not.toContain(`CONFIRMED`);
expect(svgAsString).not.toContain(`CANCELLED`);
});
36 changes: 36 additions & 0 deletions prisma/ignore-enums.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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__/ignoreEnums.svg"
theme = "forest"
ignoreEnums = 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[]
status Status @default(PENDING)
}

enum Status {
PENDING
CANCELLED
CONFIRMED
}
27 changes: 17 additions & 10 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface DMLModel {

export interface DMLRendererOptions {
tableOnly?: boolean;
ignoreEnums?: boolean;
includeRelationFromFields?: boolean;
}

Expand Down Expand Up @@ -133,19 +134,23 @@ export async function parseDatamodel(
}

function renderDml(dml: DML, options?: DMLRendererOptions) {
const { tableOnly = false, includeRelationFromFields = false } =
options ?? {};
const {
tableOnly = false,
ignoreEnums = false,
includeRelationFromFields = false,
} = options ?? {};

const diagram = 'erDiagram';

// Combine Models and Types as they are pretty similar
const modellikes = dml.models.concat(dml.types);

const enums = tableOnly
? ''
: dml.enums
.map(
(model: DMLEnum) => `
const enums =
tableOnly || ignoreEnums
? ''
: dml.enums
.map(
(model: DMLEnum) => `
${model.dbName || model.name} {
${model.values
.map(
Expand All @@ -157,8 +162,8 @@ function renderDml(dml: DML, options?: DMLRendererOptions) {
.join('\n')}
}
`
)
.join('\n\n');
)
.join('\n\n');

const classes = modellikes
.map(
Expand Down Expand Up @@ -192,7 +197,7 @@ ${
for (const model of modellikes) {
for (const field of model.fields) {
const isEnum = field.kind === 'enum';
if (tableOnly && isEnum) {
if (isEnum && (tableOnly || ignoreEnums)) {
continue;
}

Expand Down Expand Up @@ -364,6 +369,7 @@ export default async (options: GeneratorOptions) => {
path.join(config.mmdcPath || 'node_modules/.bin', 'mmdc')
);
const tableOnly = config.tableOnly === 'true';
const ignoreEnums = config.ignoreEnums === 'true';
const includeRelationFromFields =
config.includeRelationFromFields === 'true';
const disabled = Boolean(process.env.DISABLE_ERD);
Expand Down Expand Up @@ -418,6 +424,7 @@ export default async (options: GeneratorOptions) => {

const mermaid = renderDml(dml, {
tableOnly,
ignoreEnums,
includeRelationFromFields,
});
if (debug && mermaid) {
Expand Down

0 comments on commit 8b3bf99

Please sign in to comment.