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

How to put interfaces into each own files? #135

Open
mbektimirov opened this issue Nov 28, 2023 · 1 comment
Open

How to put interfaces into each own files? #135

mbektimirov opened this issue Nov 28, 2023 · 1 comment

Comments

@mbektimirov
Copy link

Is it possible to have exported interfaces separated by files with the rule "one table - one .ts file"?

@rmp135
Copy link
Owner

rmp135 commented Mar 16, 2024

By default, no.

However, by careful manipulation of the object definition you can extract each table and save them as files manually. I can imagine this won't work with enums as it requires linkage between definitions which will now be broken.

This effectively goes through each table of each schema and creates a database that contains only that table. Then it builds a TypeScript file with that database. It uses the 2.0 syntax.

const asObject = await Client
  .fromConfig(config)
  .fetchDatabase(db)
  .toObject()

const tablesAsDatabases = asObject.schemas.flatMap(schema => {
  return schema.tables.map(table => {
    return {
      ...schema,
      tables: [table]
    }
  })
})
.map(schema => {
  return {
    schemas: [schema]
  }
})

for (const database of tablesAsDatabases) {
  const ts = await Client.fromObject(database, config).toTypescript()
  await fs.writeFile(`./${database.schemas[0].tables[0].name}.ts`, ts)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants