-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathsqlite.js
More file actions
44 lines (42 loc) · 1.52 KB
/
Copy pathsqlite.js
File metadata and controls
44 lines (42 loc) · 1.52 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
import { exportFieldComment, getInlineFK, parseDefault } from "./shared";
import { dbToTypes } from "../../data/datatypes";
export function toSqlite(diagram) {
return diagram.tables
.map((table) => {
const inlineFK = getInlineFK(table, diagram);
return `${
table.comment === "" ? "" : `/* ${table.comment} */\n`
}CREATE TABLE IF NOT EXISTS "${table.name}" (\n${table.fields
.map(
(field) =>
`${exportFieldComment(field.comment)}\t"${
field.name
}" ${field.type}${field.notNull ? " NOT NULL" : ""}${
field.unique && !field.primary ? " UNIQUE" : ""
}${field.default !== "" ? ` DEFAULT ${parseDefault(field, diagram.database)}` : ""}${
field.check === "" ||
!dbToTypes[diagram.database][field.type].hasCheck
? ""
: ` CHECK(${field.check})`
}`,
)
.join(",\n")}${
table.fields.filter((f) => f.primary).length > 0
? `,\n\tPRIMARY KEY(${table.fields
.filter((f) => f.primary)
.map((f) => `"${f.name}"`)
.join(", ")})${inlineFK !== "" ? ",\n" : ""}`
: ""
}${inlineFK}\n);\n${table.indices
.map(
(i) =>
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX IF NOT EXISTS "${
i.name
}"\nON "${table.name}" (${i.fields
.map((f) => `"${f}"`)
.join(", ")});`,
)
.join("\n")}`;
})
.join("\n");
}