Skip to content

Commit

Permalink
feat: Support table-only mode (#90)
Browse files Browse the repository at this point in the history
* feat: Support table-only mode

* remove fields from models in table only mode

* Add @ripry as a contributor

Co-authored-by: John Fay <[email protected]>
  • Loading branch information
ripry and keonik authored May 9, 2022
1 parent 19c40b2 commit c349c97
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 20 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@
"contributions": [
"code"
]
},
{
"login": "ripry",
"name": "rikuyam",
"avatar_url": "https://avatars.githubusercontent.com/u/59201481?v=4",
"profile": "https://github.com/ripry",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="postgresql://johndoe:[email protected]:6543/mydb?schema=public"
ERD_DEBUG=true
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->

[![All Contributors](https://img.shields.io/badge/all_contributors-8-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-9-orange.svg?style=flat-square)](#contributors-)

<!-- ALL-CONTRIBUTORS-BADGE:END -->

Expand Down Expand Up @@ -111,6 +111,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
</tr>
<tr>
<td align="center"><a href="https://lukevers.com/"><img src="https://avatars.githubusercontent.com/u/1558388?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Luke Evers</b></sub></a><br /><a href="https://github.com/keonik/prisma-erd-generator/commits?author=lukevers" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/ripry"><img src="https://avatars.githubusercontent.com/u/59201481?v=4?s=100" width="100px;" alt=""/><br /><sub><b>rikuyam</b></sub></a><br /><a href="https://github.com/keonik/prisma-erd-generator/commits?author=ripry" title="Code">💻</a></td>
</tr>
</table>

Expand Down
1 change: 1 addition & 0 deletions __tests__/TableOnly.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions __tests__/TableOnly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as child_process from 'child_process';

test('table-only.prisma', async () => {
const fileName = 'TableOnly.svg';
const folderName = '__tests__';
child_process.execSync(`rm -f ${folderName}/${fileName}`);
child_process.execSync(
`npx prisma generate --schema ./prisma/table-only.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`);
// exclude enums
expect(svgAsString).not.toContain(`PENDING`);
expect(svgAsString).not.toContain(`CONFIRMED`);
expect(svgAsString).not.toContain(`CANCELLED`);
// exclude table columns
expect(svgAsString).not.toContain(`name`);
expect(svgAsString).not.toContain(`startDate`);
expect(svgAsString).not.toContain(`status`);
expect(svgAsString).not.toContain(`inviteeEmail`);
expect(svgAsString).not.toContain(`startDateUTC`);
expect(svgAsString).not.toContain(`cancelCode`);
});
36 changes: 36 additions & 0 deletions prisma/table-only.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__/TableOnly.svg"
theme = "forest"
tableOnly = 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
}
61 changes: 42 additions & 19 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface DMLModel {
isGenerated: boolean;
}

export interface DMLRendererOptions {
tableOnly?: boolean;
}

// Copy paste of the DMLModel
// TODO Adapt to real type of composite types - and then make them concat-enable anyway
export interface DMLType {
Expand Down Expand Up @@ -133,15 +137,19 @@ export async function parseDatamodel(
return getDataModelFieldWithoutParsing(parsed);
}

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

const diagram = 'erDiagram';

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

const enums = dml.enums
.map(
(model: DMLEnum) => `
const enums = tableOnly
? ''
: dml.enums
.map(
(model: DMLEnum) => `
${model.dbName || model.name} {
${model.values
.map(
Expand All @@ -153,26 +161,36 @@ function renderDml(dml: DML) {
.join('\n')}
}
`
)
.join('\n\n');
)
.join('\n\n');

const classes = modellikes
.map(
(model) =>
` ${model.dbName || model.name} {
${model.fields
.filter(
(field) =>
field.kind !== 'object' &&
!model.fields.find(
({ relationFromFields }) =>
relationFromFields &&
relationFromFields.includes(field.name)
${
tableOnly
? ''
: model.fields
.filter(
(field) =>
field.kind !== 'object' &&
!model.fields.find(
({ relationFromFields }) =>
relationFromFields &&
relationFromFields.includes(field.name)
)
)
// the replace is a hack to make MongoDB style ID columns like _id valid for Mermaid
.map(
(field) =>
` ${field.type.trimStart()} ${field.name.replace(
/^_/,
'z_'
)}`
)
)
// the replace is a hack to make MongoDB style ID columns like _id valid for Mermaid
.map((field) => ` ${field.type} ${field.name.replace(/^_/, 'z_')}`)
.join('\n')}
.join('\n')
}
}
`
)
Expand All @@ -181,6 +199,10 @@ function renderDml(dml: DML) {
let relationships = '';
for (const model of modellikes) {
for (const field of model.fields) {
if (tableOnly && field.kind === 'enum') {
continue;
}

const relationshipName = `${field.kind === 'enum' ? 'enum:' : ''}${
field.name
}`;
Expand Down Expand Up @@ -306,6 +328,7 @@ export default async (options: GeneratorOptions) => {
const output = options.generator.output?.value || './prisma/ERD.svg';
const config = options.generator.config;
const theme = config.theme || 'forest';
const tableOnly = config.tableOnly === 'true';
const disabled = Boolean(process.env.DISABLE_ERD);
const debug = Boolean(process.env.ERD_DEBUG);

Expand Down Expand Up @@ -341,7 +364,7 @@ export default async (options: GeneratorOptions) => {
}
if (debug && dml.models) console.log('mapped models: ', dml.models);

const mermaid = renderDml(dml);
const mermaid = renderDml(dml, { tableOnly });
if (debug && mermaid) console.log('mermaid string: ', mermaid);

if (!mermaid)
Expand Down

0 comments on commit c349c97

Please sign in to comment.