-
Notifications
You must be signed in to change notification settings - Fork 171
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
Generate DBML from JS object #514
Comments
Looking a bit more at the API I found I could use the JSON parser to generate a const db: Database = (new Parser(undefined)).parse(JSON.stringify({...}), 'json') It seems the JSON parser takes a So I tried to export to JSON and then import: const dbmb = `...`
const db: Database = (new Parser(undefined)).parse(dbml, 'dbmlv2')
const json: string = ModelExporter.export(db, 'json', false)
const db2: Database = (new Parser(undefined)).parse(json, 'json')
const generated = ModelExporter.export(db2, 'dbml', false) But this fails when parsing the JSON, on the 4th line, with In the Edit: In investigated more and succedeed to make it work when the relation is not in the same schema as tables (maybe linked to looking for tables before adding them?).
It works because the relation is cross schema and then put inside the |
Hi @loicknuchel Can you help provide the JSON that failed initially (the one that give error |
I used the example from your documentation: https://dbml.dbdiagram.io/docs/#take-a-look-at-an-example-below test('https://github.com/holistics/dbml/issues/514', () => {
const content = `Table users {
id integer
username varchar
role varchar
created_at timestamp
}
Table posts {
id integer [primary key]
title varchar
body text [note: 'Content of the post']
user_id integer
created_at timestamp
}
Ref: posts.user_id > users.id // many-to-one`
const dbFromDbml = (new Parser(undefined)).parse(content, 'dbmlv2')
const json: string = ModelExporter.export(dbFromDbml, 'json', false)
console.log('json', json)
const dbFromJson = (new Parser(undefined)).parse(json, 'json')
console.log('json parsed!')
const generated = ModelExporter.export(dbFromJson, 'dbml', false)
console.log('dbml generated', generated)
}) It fails when trying to parse the
Here is the printed JSON: {
"schemas": [{
"name": "public",
"note": "Default Public Schema",
"tables": [{
"name": "users",
"alias": null,
"note": null,
"fields": [
{"name": "id", "type": {"schemaName": null, "type_name": "integer", "args": null}, "unique": false, "pk": false, "note": null},
{"name": "username", "type": {"schemaName": null, "type_name": "varchar", "args": null}, "unique": false, "pk": false, "note": null},
{"name": "role", "type": {"schemaName": null, "type_name": "varchar", "args": null}, "unique": false, "pk": false, "note": null},
{"name": "created_at", "type": {"schemaName": null, "type_name": "timestamp", "args": null}, "unique": false, "pk": false, "note": null}
],
"indexes": []
}, {
"name": "posts",
"alias": null,
"note": null,
"fields": [
{"name": "id", "type": {"schemaName": null, "type_name": "integer", "args": null}, "unique": false, "pk": true, "not_null": false, "note": null, "increment": false},
{"name": "title", "type": {"schemaName": null, "type_name": "varchar", "args": null}, "unique": false, "pk": false, "note": null},
{"name": "body", "type": {"schemaName": null, "type_name": "text", "args": null}, "unique": false, "pk": false, "not_null": false, "note": "Content of the post", "increment": false},
{"name": "user_id", "type": {"schemaName": null, "type_name": "integer", "args": null}, "unique": false, "pk": false, "note": null},
{"name": "created_at", "type": {"schemaName": null, "type_name": "timestamp", "args": null}, "unique": false, "pk": false, "note": null}
],
"indexes": []
}],
"enums": [],
"tableGroups": [],
"refs": [{
"name": null,
"endpoints": [
{"schemaName": null, "tableName": "posts", "fieldNames": ["user_id"], "relation": "*"},
{"schemaName": null, "tableName": "users", "fieldNames": ["id"], "relation": "1"}
]
}]
}]
} If the relation is not in the same schema as tables, it works. But I'm only using the JSON parser to build a |
Hi,
First, thank you for this project and the libraries <3
I have an object representing my db and I'm looking to generate DBML from it.
Looking at https://dbml.dbdiagram.io/js-module/#class-modelexporter I see I can generate DBML using:
My issue is, I don't know how to generate a
database
value withDatabase
type 😞In the example it's build using the parser but I don't have DBML as input.
I saw the
Database
class is exported here: dbml-core/src/model_structure/database.js, and I tried to import it using:But it gives me the error:
Cannot find module '@dbml/core/types/model_structure/database' from 'src/dbml.ts'
And when I try:
I got the error:
Cannot find module '@dbml/core/model_structure/database' or its corresponding type declarations.
Do you have any idea how I can configure TypeScript to match modules and type definitions or how I can create a Database class?
I think it would be nice to export the models in
model_structure
in theindex.js
, what do you think?Also, I don't know the difference between a Database and a NormalizedDatabase, is there any explanation somewhere?
Thanks!
The text was updated successfully, but these errors were encountered: