keonik / prisma-erd-generator

Generate an ER Diagram based on your Prisma schema every time you run npx prisma generate
https://www.npmjs.com/package/prisma-erd-generator
MIT License
879 stars 48 forks source link

Option to ignore certain tables #82

Open jsbrain opened 2 years ago

jsbrain commented 2 years ago

How about a flag/regex to ignore certain tables/relations?

I got some "system" tables that I don't really want to end up in the ERD but they will obviously be introspected by prisma.

What do you think of that? Could be a good complement for https://github.com/keonik/prisma-erd-generator/issues/72.

Related: https://github.com/prisma/prisma/issues/807

keonik commented 2 years ago

Would it ignore tables and fields or just tables and relationships? I kind of like the @@ignore over the .prismaignore

jsbrain commented 2 years ago

I'm personally interested in ignoring tables and relationships so far, which helps to narrow down the ERD by not having to many nodes and edges. Ignoring fields particularly may be interesting for some users though but I would rate this as a secondary goal.

One could potentially salvage some of the @@ignore feature although if I understand it correctly it will only work if the schema has already be inferred and manually marked with the @@ignore so it would require additional steps instead of just having tables ignored by a flag.

So I was thinking about something like that:

generator prisma-erd-generator {
  ...
  ignore: "system_*, patter-2" // <- use regex or glob pattern here
}

model users {
  ...
  settings @relation("system_settings" ...) // <- won't show in ERD
  fields @relation("system_settings" ...) // <- won't show in ERD
}

model system_fields { // <- won't show in ERD
  ...
}

model system_settings { // <- won't show in ERD
  ...
}
jsbrain commented 2 years ago

My current "solution" is the following script I call from package.json. It first creates a "pure" prisma schema, removes/modifies the generators and then just calls prisma generate on the cleaned up schema.

// generate a "purified" ERD which only includes non-system tables.
const fs = require('fs')
const { spawnSync } = require('child_process')

const modelRegex = /model directus_.* {[^]*?\n}/gm
const propRegex = /^.*directus_.*[^{]$/gm
const knexRegex = /model knex_.* {[^]*?\n}/gm
const generatorRegex = /generator (client|erd) {[^]*?\n}/gm

function generatePureERD(inPath, outPath) {
  console.log('Generating pure ERD from', inPath)

  const contents = fs.readFileSync(inPath, 'utf8')
  // Replace regex from schema
  const pureSchema = contents
    .replace(modelRegex, '')
    .replace(propRegex, '')
    .replace(knexRegex, '')
    .replace(generatorRegex, '')

  const customGenerator = `generator erd {
    provider = "prisma-erd-generator"
    output = "./pure-ERD.svg"
  }
  `

  fs.writeFileSync(outPath, customGenerator + pureSchema)

  // Now generate the pure ERD
  spawnSync('prisma', ['format', `--schema=${outPath}`])
  spawnSync('prisma', ['generate', `--schema=${outPath}`])

  console.log('-> done')
}

const inPath = './prisma/schema.prisma'
// NOTE: The ERD will live relative to this file
const outPath = './docs/database/schema/pure-schema.prisma'

generatePureERD(inPath, outPath)

I just though this could be a nice core feature and probably be valuable for more users so I decided it's time to get some feedback on this and see if anyone is interested.

keonik commented 2 years ago

Personally I like the ignore param with regex patterns because it should stay separate from any future prisma updates.