Skip to content
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

introduce ignoreViews parameter #214

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@
"contributions": [
"code"
]
},
{
"login": "hakandilek",
"name": "Hakan Dilek",
"avatar_url": "https://avatars.githubusercontent.com/u/1072473?v=4",
"profile": "http://www.dilek.me/",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ prisma/debug
coverage
!__test__/*.ts
__tests__/*.svg
__tests__/*.png
__tests__/*.png
.idea
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Prisma Entity Relationship Diagram Generator

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-15-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-16-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

Prisma generator to create an ER Diagram every time you generate your prisma client.
Expand Down Expand Up @@ -151,6 +151,17 @@ generator erd {
}
```

### Ignore views

If you enable this option, view entities will be hidden.
This is useful if you want to reduce the number of entities and focus on the tables without displaying all the views.

```prisma
generator erd {
provider = "prisma-erd-generator"
ignoreViews = 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 Expand Up @@ -222,6 +233,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://www.chintristan.io/"><img src="https://avatars.githubusercontent.com/u/23557893?v=4?s=100" width="100px;" alt="Tristan Chin"/><br /><sub><b>Tristan Chin</b></sub></a><br /><a href="https://github.com/keonik/prisma-erd-generator/commits?author=maxijonson" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="http://www.dilek.me/"><img src="https://avatars.githubusercontent.com/u/1072473?v=4?s=100" width="100px;" alt="Hakan Dilek"/><br /><sub><b>Hakan Dilek</b></sub></a><br /><a href="https://github.com/keonik/prisma-erd-generator/commits?author=hakandilek" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
41 changes: 41 additions & 0 deletions __tests__/ignoreViews.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as child_process from 'child_process';

test('ignore-views.prisma', async () => {
const fileName = 'ignoreViews.svg';
const folderName = '__tests__';
child_process.execSync(`rm -f ${folderName}/${fileName}`);
child_process.execSync(
`prisma generate --schema ./prisma/ignore-views.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`);
// include 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`);
// include enums
expect(svgAsString).toContain(`PENDING`);
expect(svgAsString).toContain(`CONFIRMED`);
expect(svgAsString).toContain(`CANCELLED`);
// exclude views
expect(svgAsString).not.toContain(`Attendance`);
expect(svgAsString).not.toContain(`email`);
expect(svgAsString).not.toContain(`eventDate`);
expect(svgAsString).not.toContain(`bookingStatus`);
});
44 changes: 44 additions & 0 deletions prisma/ignore-views.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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__/ignoreViews.svg"
theme = "forest"
previewFeatures = ["views"]
ignoreViews = 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
}

view Attendance {
id Int @unique
email String
eventDate DateTime
bookingStatus Status
}
26 changes: 25 additions & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,25 @@ export interface DMLModel {
} | null;
}

export interface DMLView {
name: string;
isEmbedded: boolean;
dbName: string | null;
fields: DMLField[];
idFields: any[];
uniqueFields: any[];
uniqueIndexes: any[];
isGenerated: boolean;
primaryKey: {
name: string | null;
fields: string[];
} | null;
}

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

Expand Down Expand Up @@ -55,7 +71,7 @@ export interface DMLField {
isRequired: boolean;
isUnique: boolean;
isUpdatedAt: boolean;
kind: 'scalar' | 'object' | 'enum';
kind: 'scalar' | 'object' | 'enum' | 'view';
type: string;
relationFromFields?: any[];
relationName?: string;
Expand All @@ -75,6 +91,7 @@ export interface DMLEnum {
export interface DML {
enums: DMLEnum[];
models: DMLModel[];
views: DMLView[];
types: DMLType[];
}

Expand Down Expand Up @@ -137,6 +154,7 @@ function renderDml(dml: DML, options?: DMLRendererOptions) {
const {
tableOnly = false,
ignoreEnums = false,
ignoreViews = false,
includeRelationFromFields = false,
} = options ?? {};

Expand Down Expand Up @@ -196,9 +214,13 @@ ${
for (const model of modellikes) {
for (const field of model.fields) {
const isEnum = field.kind === 'enum';
const isView = field.kind === 'view';
if (isEnum && (tableOnly || ignoreEnums)) {
continue;
}
if (isView && (tableOnly || ignoreViews)) {
continue;
}

const relationshipName = `${isEnum ? 'enum:' : ''}${field.name}`;
const thisSide = `"${model.dbName || model.name}"`;
Expand Down Expand Up @@ -373,6 +395,7 @@ export default async (options: GeneratorOptions) => {
);
const tableOnly = config.tableOnly === 'true';
const ignoreEnums = config.ignoreEnums === 'true';
const ignoreViews = config.ignoreViews === 'true';
const includeRelationFromFields =
config.includeRelationFromFields === 'true';
const disabled = Boolean(process.env.DISABLE_ERD);
Expand Down Expand Up @@ -428,6 +451,7 @@ export default async (options: GeneratorOptions) => {
const mermaid = renderDml(dml, {
tableOnly,
ignoreEnums,
ignoreViews,
includeRelationFromFields,
});
if (debug && mermaid) {
Expand Down