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

Option to ignore certain tables #82

Open
jsbrain opened this issue Mar 17, 2022 · 4 comments
Open

Option to ignore certain tables #82

jsbrain opened this issue Mar 17, 2022 · 4 comments
Labels
discussion enhancement New feature or request

Comments

@jsbrain
Copy link
Contributor

jsbrain commented Mar 17, 2022

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 #72.

Related: prisma/prisma#807

@keonik
Copy link
Owner

keonik commented Mar 20, 2022

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

@jsbrain
Copy link
Contributor Author

jsbrain commented Mar 20, 2022

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
Copy link
Contributor Author

jsbrain commented Mar 20, 2022

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.

@jsbrain jsbrain changed the title Ignore filter Option to ignore certain tables Mar 20, 2022
@keonik
Copy link
Owner

keonik commented Mar 21, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants