forked from findyourmagic/dber
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport-sql.js
More file actions
58 lines (54 loc) · 2 KB
/
export-sql.js
File metadata and controls
58 lines (54 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { ModelExporter, Parser } from '@dbml/core';
/**
* It takes a table dictionary and a link dictionary and returns a SQL string
* @param tableDict - a dictionary of tables, where the key is the table ID and the value is the table
* object
* @param linkDict - a dictionary of links, where the key is the link id and the value is the link
* object
* @param [databaseType=postgres] - The type of database you want to export to.
* @returns SQL string.
*/
const exportSQL = (tableDict, linkDict, databaseType = 'postgres') => {
const combined = {
name: 'public',
note: '',
tables: Object.values(tableDict).map(table => {
return {
name: table.name,
note: table.note,
fields: table.fields.map(field => {
return {
...field,
type: {
// To lower case because of typing 'BIGINT' with upper case and increment get wrong pg sql type when export
type_name: field.type.toLowerCase(),
args: null,
},
};
}),
};
}),
enums: [],
tableGroups: [],
refs: Object.values(linkDict).map(ref => {
return {
...ref,
endpoints: ref.endpoints.map(endpoint => {
return {
...endpoint,
tableName: tableDict[endpoint.id].name,
fieldNames: [
tableDict[endpoint.id].fields.find(
field => field.id == endpoint.fieldId
).name,
],
};
}),
};
}),
};
const database = Parser.parse(combined, 'json');
const sql = ModelExporter.export(database, databaseType, false);
return sql;
};
export default exportSQL;