@absolutejs/dbml 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +45 -7
- package/changelog.json +44 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +1267 -121
- package/dist/drizzle/index.js +59 -8
- package/dist/drizzle/relations.d.ts +1 -0
- package/dist/index.js +66 -9
- package/dist/model.d.ts +10 -0
- package/dist/mysql/index.d.ts +20 -0
- package/dist/mysql/index.js +452 -0
- package/dist/postgres/details.d.ts +40 -0
- package/dist/postgres/index.d.ts +5 -0
- package/dist/postgres/index.js +367 -26
- package/dist/postgres/queries.d.ts +13 -4
- package/dist/postgres/rows.d.ts +66 -0
- package/dist/sqlite/index.d.ts +14 -0
- package/dist/sqlite/index.js +420 -0
- package/dist/sqlite/text.d.ts +22 -0
- package/package.json +16 -3
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/model.ts
|
|
3
|
+
var emptyModel = (project) => ({
|
|
4
|
+
deps: [],
|
|
5
|
+
enums: [],
|
|
6
|
+
notes: [],
|
|
7
|
+
...project ? { project } : {},
|
|
8
|
+
records: [],
|
|
9
|
+
refs: [],
|
|
10
|
+
tableGroups: [],
|
|
11
|
+
tables: []
|
|
12
|
+
});
|
|
13
|
+
var unqualified = (place, schema) => {
|
|
14
|
+
if (place.schema !== schema)
|
|
15
|
+
return place;
|
|
16
|
+
const { schema: dropped, ...rest } = place;
|
|
17
|
+
return rest;
|
|
18
|
+
};
|
|
19
|
+
var withDefaultSchema = (model, schema) => {
|
|
20
|
+
const result = {
|
|
21
|
+
...model,
|
|
22
|
+
deps: model.deps.map((dep) => ({
|
|
23
|
+
...dep,
|
|
24
|
+
downstream: unqualified(dep.downstream, schema),
|
|
25
|
+
upstream: unqualified(dep.upstream, schema)
|
|
26
|
+
})),
|
|
27
|
+
enums: model.enums.map((entry) => unqualified(entry, schema)),
|
|
28
|
+
records: model.records.map((records) => unqualified(records, schema)),
|
|
29
|
+
refs: model.refs.map((ref) => ({
|
|
30
|
+
...ref,
|
|
31
|
+
from: unqualified(ref.from, schema),
|
|
32
|
+
to: unqualified(ref.to, schema)
|
|
33
|
+
})),
|
|
34
|
+
tableGroups: model.tableGroups.map((group) => ({
|
|
35
|
+
...group,
|
|
36
|
+
tables: group.tables.map((entry) => unqualified(entry, schema))
|
|
37
|
+
})),
|
|
38
|
+
tables: model.tables.map((table) => ({
|
|
39
|
+
...unqualified(table, schema),
|
|
40
|
+
columns: table.columns.map((column) => {
|
|
41
|
+
if (column.typeSchema !== schema)
|
|
42
|
+
return column;
|
|
43
|
+
const { typeSchema: dropped, ...rest } = column;
|
|
44
|
+
return rest;
|
|
45
|
+
})
|
|
46
|
+
}))
|
|
47
|
+
};
|
|
48
|
+
return result;
|
|
49
|
+
};
|
|
50
|
+
var groupBySchema = (tables, defaultSchema = "public") => {
|
|
51
|
+
const byName = new Map;
|
|
52
|
+
tables.forEach((table) => {
|
|
53
|
+
const name = table.schema ?? defaultSchema;
|
|
54
|
+
byName.set(name, [
|
|
55
|
+
...byName.get(name) ?? [],
|
|
56
|
+
{
|
|
57
|
+
...table.schema ? { schema: table.schema } : {},
|
|
58
|
+
table: table.name
|
|
59
|
+
}
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
return [...byName].map(([name, members]) => {
|
|
63
|
+
const group = { name, tables: members };
|
|
64
|
+
return group;
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
var sortedColumns = (index) => index.parts.map((part) => part.kind === "column" ? part.column : "").sort().join("\x00");
|
|
68
|
+
var coversAll = (index) => index.pk === true || index.unique === true && index.note?.includes("Where ") !== true;
|
|
69
|
+
var isUniqueSet = (table, columns) => {
|
|
70
|
+
const [only] = columns;
|
|
71
|
+
const single = columns.length === 1 ? table.columns.find((column) => column.name === only) : undefined;
|
|
72
|
+
if (single?.pk === true || single?.unique === true)
|
|
73
|
+
return true;
|
|
74
|
+
const wanted = [...columns].sort().join("\x00");
|
|
75
|
+
return table.indexes.some((index) => coversAll(index) && index.parts.every((part) => part.kind === "column") && sortedColumns(index) === wanted);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// src/mysql/index.ts
|
|
79
|
+
var lowered = (row) => Object.fromEntries(Object.entries(row).map(([key, value]) => [key.toLowerCase(), value]));
|
|
80
|
+
var text = (value) => {
|
|
81
|
+
if (value === null || value === undefined)
|
|
82
|
+
return;
|
|
83
|
+
if (value instanceof Uint8Array)
|
|
84
|
+
return new TextDecoder().decode(value);
|
|
85
|
+
return typeof value === "string" ? value : `${value}`;
|
|
86
|
+
};
|
|
87
|
+
var integer = (value) => Number.parseInt(text(value) ?? "0", 10);
|
|
88
|
+
var VIEW_COLOR = "#64748b";
|
|
89
|
+
var ACTIONS = {
|
|
90
|
+
CASCADE: "cascade",
|
|
91
|
+
"NO ACTION": undefined,
|
|
92
|
+
RESTRICT: undefined,
|
|
93
|
+
"SET DEFAULT": "set default",
|
|
94
|
+
"SET NULL": "set null"
|
|
95
|
+
};
|
|
96
|
+
var NUMBER = /^-?\d+(\.\d+)?$/u;
|
|
97
|
+
var actionOf = (rule) => ACTIONS[rule ?? ""];
|
|
98
|
+
var defaultOf = (raw, extra, mariadb) => {
|
|
99
|
+
if (raw === undefined)
|
|
100
|
+
return;
|
|
101
|
+
if (/^NULL$/iu.test(raw))
|
|
102
|
+
return mariadb || extra === "" ? { kind: "null" } : undefined;
|
|
103
|
+
if (/DEFAULT_GENERATED/iu.test(extra) || /^(CURRENT_TIMESTAMP|now\(\))/iu.test(raw))
|
|
104
|
+
return {
|
|
105
|
+
kind: "expression",
|
|
106
|
+
value: raw.replace(/^\((.*)\)$/su, "$1")
|
|
107
|
+
};
|
|
108
|
+
const unquoted = mariadb && /^'.*'$/su.test(raw) ? raw.slice(1, -1).replaceAll("''", "'") : raw;
|
|
109
|
+
if (NUMBER.test(unquoted) && Number.isSafeInteger(Math.trunc(Number.parseFloat(unquoted))))
|
|
110
|
+
return {
|
|
111
|
+
kind: "number",
|
|
112
|
+
value: Number.parseFloat(unquoted)
|
|
113
|
+
};
|
|
114
|
+
if (mariadb && !/^'.*'$/su.test(raw))
|
|
115
|
+
return { kind: "expression", value: raw };
|
|
116
|
+
return { kind: "string", value: unquoted };
|
|
117
|
+
};
|
|
118
|
+
var ENUMISH = /^(enum|set)\((.*)\)$/isu;
|
|
119
|
+
var valuesOf = (list) => [...list.matchAll(/'((?:[^']|'')*)'/gu)].map((match) => (match[1] ?? "").replaceAll("''", "'"));
|
|
120
|
+
var columnOf = (row, table, context) => {
|
|
121
|
+
const type = text(row.column_type) ?? "";
|
|
122
|
+
const extra = text(row.extra) ?? "";
|
|
123
|
+
const enumish = ENUMISH.exec(type);
|
|
124
|
+
const name = text(row.column_name) ?? "";
|
|
125
|
+
const generated = /\b(VIRTUAL|STORED|PERSISTENT) GENERATED\b/iu.exec(extra)?.[1]?.toLowerCase();
|
|
126
|
+
const fallback = generated ? undefined : defaultOf(text(row.column_default), extra, context.mariadb);
|
|
127
|
+
const metadata = {
|
|
128
|
+
...text(row.collation_name) && text(row.collation_name) !== text(row.table_collation) ? { collation: text(row.collation_name) ?? "" } : {},
|
|
129
|
+
...text(row.srs_id) ? { srid: integer(row.srs_id) } : {}
|
|
130
|
+
};
|
|
131
|
+
const notes = [
|
|
132
|
+
text(row.column_comment) || undefined,
|
|
133
|
+
generated ? `Generated always as (${text(row.generation_expression) ?? ""}), ${generated}` : undefined,
|
|
134
|
+
/on update/iu.test(extra) ? `Set to ${/on update (.*)$/iu.exec(extra)?.[1] ?? "the current time"} on every update` : undefined,
|
|
135
|
+
/INVISIBLE/iu.test(extra) ? "Invisible: SELECT * leaves it out" : undefined,
|
|
136
|
+
enumish ? `Stored as ${type}` : undefined,
|
|
137
|
+
enumish?.[1]?.toLowerCase() === "set" ? "A set: any number of these values at once" : undefined
|
|
138
|
+
].filter((line) => line !== undefined);
|
|
139
|
+
const enumType = enumish ? {
|
|
140
|
+
name: `${table.name}_${name}`,
|
|
141
|
+
schema: table.schema,
|
|
142
|
+
values: valuesOf(enumish[2] ?? "").map((value) => ({
|
|
143
|
+
name: value
|
|
144
|
+
}))
|
|
145
|
+
} : undefined;
|
|
146
|
+
const column = {
|
|
147
|
+
name,
|
|
148
|
+
type: enumType ? enumType.name : type,
|
|
149
|
+
...enumType ? { typeSchema: table.schema } : {},
|
|
150
|
+
.../auto_increment/iu.test(extra) ? { increment: true } : {},
|
|
151
|
+
...text(row.is_nullable) === "NO" ? { notNull: true } : {},
|
|
152
|
+
...fallback ? { default: fallback } : {},
|
|
153
|
+
...Object.keys(metadata).length > 0 ? { metadata } : {},
|
|
154
|
+
...notes.length > 0 ? { note: notes.join(`
|
|
155
|
+
`) } : {}
|
|
156
|
+
};
|
|
157
|
+
return { column, enumType };
|
|
158
|
+
};
|
|
159
|
+
var keyOf = (schema, table) => `${text(schema)}.${text(table)}`;
|
|
160
|
+
var group = (rows, keyFn) => {
|
|
161
|
+
const groups = new Map;
|
|
162
|
+
rows.forEach((row) => groups.set(keyFn(row), [...groups.get(keyFn(row)) ?? [], row]));
|
|
163
|
+
return groups;
|
|
164
|
+
};
|
|
165
|
+
var indexOf = (rows) => {
|
|
166
|
+
const [first] = rows;
|
|
167
|
+
const name = text(first?.index_name) ?? "";
|
|
168
|
+
const sorted = [...rows].sort((left, right) => integer(left.seq_in_index) - integer(right.seq_in_index));
|
|
169
|
+
const prefixes = sorted.filter((row) => text(row.sub_part)).map((row) => `${text(row.column_name)}(${text(row.sub_part)})`);
|
|
170
|
+
const method = text(first?.index_type)?.toLowerCase();
|
|
171
|
+
const notes = [
|
|
172
|
+
text(first?.index_comment) || undefined,
|
|
173
|
+
prefixes.length > 0 ? `Prefix: ${prefixes.join(", ")}` : undefined,
|
|
174
|
+
text(first?.is_visible) === "NO" || text(first?.ignored) === "YES" ? "Invisible: the optimizer ignores it" : undefined
|
|
175
|
+
].filter((line) => line !== undefined);
|
|
176
|
+
const index = {
|
|
177
|
+
parts: sorted.map((row) => {
|
|
178
|
+
const desc = text(row.collation) === "D" ? { desc: true } : {};
|
|
179
|
+
const column = text(row.column_name);
|
|
180
|
+
const part = column ? { column, kind: "column", ...desc } : {
|
|
181
|
+
expression: (text(row.expression) ?? "").replace(/^\((.*)\)$/su, "$1"),
|
|
182
|
+
kind: "expression",
|
|
183
|
+
...desc
|
|
184
|
+
};
|
|
185
|
+
return part;
|
|
186
|
+
}),
|
|
187
|
+
...name === "PRIMARY" ? { pk: true } : { name },
|
|
188
|
+
...integer(first?.non_unique) === 0 && name !== "PRIMARY" ? { unique: true } : {},
|
|
189
|
+
...method && method !== "btree" ? { type: method } : {},
|
|
190
|
+
...notes.length > 0 ? { note: notes.join(`
|
|
191
|
+
`) } : {}
|
|
192
|
+
};
|
|
193
|
+
return index;
|
|
194
|
+
};
|
|
195
|
+
var placeIndex = (table, index) => {
|
|
196
|
+
const [part] = index.parts;
|
|
197
|
+
const plain = index.parts.length === 1 && part?.kind === "column" && index.note === undefined;
|
|
198
|
+
const column = plain && part?.kind === "column" ? table.columns.find((entry) => entry.name === part.column) : undefined;
|
|
199
|
+
if (column && index.pk) {
|
|
200
|
+
column.pk = true;
|
|
201
|
+
delete column.notNull;
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (column && index.unique && index.name === column.name) {
|
|
205
|
+
column.unique = true;
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
table.indexes.push(index);
|
|
209
|
+
};
|
|
210
|
+
var tableMetadata = (row, context, grants) => {
|
|
211
|
+
const metadata = {
|
|
212
|
+
...text(row.engine) ? { engine: text(row.engine) ?? "" } : {},
|
|
213
|
+
...text(row.table_collation) ? { collation: text(row.table_collation) ?? "" } : {},
|
|
214
|
+
...text(row.row_format) ? { row_format: text(row.row_format) ?? "" } : {},
|
|
215
|
+
...text(row.create_options) ? { options: text(row.create_options) ?? "" } : {},
|
|
216
|
+
...grants ? { grants } : {},
|
|
217
|
+
...context.statistics && text(row.table_rows) ? { rows: integer(row.table_rows) } : {},
|
|
218
|
+
...context.statistics && text(row.data_length) ? {
|
|
219
|
+
size_bytes: integer(row.data_length) + integer(row.index_length)
|
|
220
|
+
} : {},
|
|
221
|
+
...context.statistics && text(row.auto_increment) ? { next_id: integer(row.auto_increment) } : {}
|
|
222
|
+
};
|
|
223
|
+
return metadata;
|
|
224
|
+
};
|
|
225
|
+
var VIEWS = [
|
|
226
|
+
"tables",
|
|
227
|
+
"columns",
|
|
228
|
+
"statistics",
|
|
229
|
+
"table_constraints",
|
|
230
|
+
"key_column_usage",
|
|
231
|
+
"referential_constraints",
|
|
232
|
+
"check_constraints",
|
|
233
|
+
"views",
|
|
234
|
+
"triggers",
|
|
235
|
+
"routines",
|
|
236
|
+
"parameters",
|
|
237
|
+
"partitions",
|
|
238
|
+
"events",
|
|
239
|
+
"table_privileges",
|
|
240
|
+
"view_table_usage"
|
|
241
|
+
];
|
|
242
|
+
var SCHEMA_COLUMN = {
|
|
243
|
+
check_constraints: "constraint_schema",
|
|
244
|
+
events: "event_schema",
|
|
245
|
+
parameters: "specific_schema",
|
|
246
|
+
referential_constraints: "constraint_schema",
|
|
247
|
+
routines: "routine_schema",
|
|
248
|
+
table_constraints: "constraint_schema",
|
|
249
|
+
table_privileges: "table_schema",
|
|
250
|
+
triggers: "trigger_schema",
|
|
251
|
+
view_table_usage: "view_schema"
|
|
252
|
+
};
|
|
253
|
+
var readCatalog = async (query, schemas) => {
|
|
254
|
+
const marks = schemas.map(() => "?").join(", ");
|
|
255
|
+
const entries = await Promise.all(VIEWS.map(async (view) => {
|
|
256
|
+
const column = SCHEMA_COLUMN[view] ?? "table_schema";
|
|
257
|
+
const rows = await query(`select * from information_schema.${view} where ${column} in (${marks})`, schemas).catch(() => []);
|
|
258
|
+
const entry = [view, rows.map(lowered)];
|
|
259
|
+
return entry;
|
|
260
|
+
}));
|
|
261
|
+
const catalog = Object.fromEntries(entries);
|
|
262
|
+
return catalog;
|
|
263
|
+
};
|
|
264
|
+
var triggerLine = (row) => `Trigger ${text(row.trigger_name)} ${text(row.action_timing)} ${text(row.event_manipulation)}: ${(text(row.action_statement) ?? "").replace(/\s+/gu, " ")}`;
|
|
265
|
+
var partitionLines = (rows) => {
|
|
266
|
+
const named = rows.filter((row) => text(row.partition_name));
|
|
267
|
+
const [first] = named;
|
|
268
|
+
if (!first)
|
|
269
|
+
return [];
|
|
270
|
+
return [
|
|
271
|
+
`Partitioned by ${text(first.partition_method)} (${text(first.partition_expression)})`,
|
|
272
|
+
...named.map((row) => `Partition ${text(row.partition_name)}${text(row.partition_description) ? `: ${text(row.partition_description)}` : ""}`)
|
|
273
|
+
];
|
|
274
|
+
};
|
|
275
|
+
var routineNote = (catalog) => {
|
|
276
|
+
const routines = catalog.routines ?? [];
|
|
277
|
+
if (routines.length === 0)
|
|
278
|
+
return [];
|
|
279
|
+
const parameters = group(catalog.parameters ?? [], (row) => keyOf(row.specific_schema, row.specific_name));
|
|
280
|
+
const lines = routines.map((row) => {
|
|
281
|
+
const args = (parameters.get(keyOf(row.routine_schema, row.specific_name)) ?? []).filter((parameter) => text(parameter.parameter_name)).sort((left, right) => integer(left.ordinal_position) - integer(right.ordinal_position)).map((parameter) => `${text(parameter.parameter_mode) ?? ""} ${text(parameter.parameter_name)} ${text(parameter.dtd_identifier)}`.trim()).join(", ");
|
|
282
|
+
const kind = text(row.routine_type)?.toLowerCase() ?? "function";
|
|
283
|
+
const result = kind === "function" ? ` \u2192 ${text(row.dtd_identifier)}` : "";
|
|
284
|
+
return `${text(row.routine_name)}(${args})${result}, ${kind}`;
|
|
285
|
+
});
|
|
286
|
+
const note = {
|
|
287
|
+
content: `Functions and procedures
|
|
288
|
+
${lines.join(`
|
|
289
|
+
`)}`,
|
|
290
|
+
name: "functions"
|
|
291
|
+
};
|
|
292
|
+
return [note];
|
|
293
|
+
};
|
|
294
|
+
var eventNote = (catalog) => {
|
|
295
|
+
const events = catalog.events ?? [];
|
|
296
|
+
if (events.length === 0)
|
|
297
|
+
return [];
|
|
298
|
+
const note = {
|
|
299
|
+
content: `Scheduled events
|
|
300
|
+
${events.map((row) => `${text(row.event_name)}: ${text(row.interval_value) ? `every ${text(row.interval_value)} ${text(row.interval_field)}` : `at ${text(row.execute_at)}`}, ${text(row.status)?.toLowerCase()}`).join(`
|
|
301
|
+
`)}`,
|
|
302
|
+
name: "events"
|
|
303
|
+
};
|
|
304
|
+
return [note];
|
|
305
|
+
};
|
|
306
|
+
var grantsOf = (catalog) => group(catalog.table_privileges ?? [], (row) => keyOf(row.table_schema, row.table_name));
|
|
307
|
+
var checksOf = (catalog) => {
|
|
308
|
+
const tableOfCheck = new Map((catalog.table_constraints ?? []).filter((row) => text(row.constraint_type) === "CHECK").map((row) => [
|
|
309
|
+
keyOf(row.constraint_schema, row.constraint_name),
|
|
310
|
+
keyOf(row.table_schema, row.table_name)
|
|
311
|
+
]));
|
|
312
|
+
return group(catalog.check_constraints ?? [], (row) => text(row.table_name) ? keyOf(row.constraint_schema, row.table_name) : tableOfCheck.get(keyOf(row.constraint_schema, row.constraint_name)) ?? "");
|
|
313
|
+
};
|
|
314
|
+
var isOwnCheck = (row) => !/^`?\w+`? IS NOT NULL$/iu.test(text(row.check_clause) ?? "") || text(row.level) !== "Column";
|
|
315
|
+
var foreignKeys = (catalog, tables) => {
|
|
316
|
+
const rules = new Map((catalog.referential_constraints ?? []).map((row) => [
|
|
317
|
+
keyOf(row.constraint_schema, row.constraint_name),
|
|
318
|
+
row
|
|
319
|
+
]));
|
|
320
|
+
const usage = group((catalog.key_column_usage ?? []).filter((row) => text(row.referenced_table_name)), (row) => `${keyOf(row.constraint_schema, row.table_name)}.${text(row.constraint_name)}`);
|
|
321
|
+
return [...usage.values()].flatMap((rows) => {
|
|
322
|
+
const sorted = [...rows].sort((left, right) => integer(left.ordinal_position) - integer(right.ordinal_position));
|
|
323
|
+
const [first] = sorted;
|
|
324
|
+
if (!first)
|
|
325
|
+
return [];
|
|
326
|
+
const table = tables.get(keyOf(first.table_schema, first.table_name));
|
|
327
|
+
const rule = rules.get(keyOf(first.constraint_schema, first.constraint_name));
|
|
328
|
+
const columns = sorted.map((row) => text(row.column_name) ?? "");
|
|
329
|
+
const onDelete = actionOf(text(rule?.delete_rule));
|
|
330
|
+
const onUpdate = actionOf(text(rule?.update_rule));
|
|
331
|
+
const name = text(first.constraint_name) ?? "";
|
|
332
|
+
const ref = {
|
|
333
|
+
from: {
|
|
334
|
+
columns,
|
|
335
|
+
schema: text(first.table_schema) ?? "",
|
|
336
|
+
table: text(first.table_name) ?? ""
|
|
337
|
+
},
|
|
338
|
+
.../_ibfk_\d+$/u.test(name) ? {} : { name },
|
|
339
|
+
...onDelete ? { onDelete } : {},
|
|
340
|
+
...onUpdate ? { onUpdate } : {},
|
|
341
|
+
relation: table && isUniqueSet(table, columns) ? "-" : ">",
|
|
342
|
+
to: {
|
|
343
|
+
columns: sorted.map((row) => text(row.referenced_column_name) ?? ""),
|
|
344
|
+
schema: text(first.referenced_table_schema) ?? "",
|
|
345
|
+
table: text(first.referenced_table_name) ?? ""
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
return [ref];
|
|
349
|
+
});
|
|
350
|
+
};
|
|
351
|
+
var viewDeps = (catalog, views, tables) => {
|
|
352
|
+
const usage = catalog.view_table_usage ?? [];
|
|
353
|
+
if (usage.length > 0)
|
|
354
|
+
return usage.map((row) => ({
|
|
355
|
+
downstream: {
|
|
356
|
+
schema: text(row.view_schema) ?? "",
|
|
357
|
+
table: text(row.view_name) ?? ""
|
|
358
|
+
},
|
|
359
|
+
upstream: {
|
|
360
|
+
schema: text(row.table_schema) ?? "",
|
|
361
|
+
table: text(row.table_name) ?? ""
|
|
362
|
+
}
|
|
363
|
+
}));
|
|
364
|
+
const definitions = new Map((catalog.views ?? []).map((row) => [
|
|
365
|
+
keyOf(row.table_schema, row.table_name),
|
|
366
|
+
text(row.view_definition) ?? ""
|
|
367
|
+
]));
|
|
368
|
+
return views.flatMap((view) => {
|
|
369
|
+
const sql = definitions.get(keyOf(view.schema, view.name)) ?? "";
|
|
370
|
+
return tables.filter((table) => sql.includes(`\`${table.name}\``)).map((table) => ({
|
|
371
|
+
downstream: { schema: view.schema ?? "", table: view.name },
|
|
372
|
+
upstream: { schema: table.schema ?? "", table: table.name }
|
|
373
|
+
}));
|
|
374
|
+
});
|
|
375
|
+
};
|
|
376
|
+
var fromMysql = async (query, options = {}) => {
|
|
377
|
+
const [version] = await query("select version() as version", []);
|
|
378
|
+
const mariadb = /mariadb/iu.test(text(version?.version) ?? "");
|
|
379
|
+
const schemas = options.schemas ?? [
|
|
380
|
+
text((await query("select database() as name", []))[0]?.name) ?? ""
|
|
381
|
+
];
|
|
382
|
+
const catalog = await readCatalog(query, schemas);
|
|
383
|
+
const context = {
|
|
384
|
+
mariadb,
|
|
385
|
+
statistics: options.statistics === true
|
|
386
|
+
};
|
|
387
|
+
const model = emptyModel({
|
|
388
|
+
databaseType: mariadb ? "MariaDB" : "MySQL",
|
|
389
|
+
...options.project
|
|
390
|
+
});
|
|
391
|
+
const columns = group(catalog.columns ?? [], (row) => keyOf(row.table_schema, row.table_name));
|
|
392
|
+
const indexes = group(catalog.statistics ?? [], (row) => `${keyOf(row.table_schema, row.table_name)}.${text(row.index_name)}`);
|
|
393
|
+
const checks = checksOf(catalog);
|
|
394
|
+
const grants = grantsOf(catalog);
|
|
395
|
+
const triggers = group(catalog.triggers ?? [], (row) => keyOf(row.event_object_schema, row.event_object_table));
|
|
396
|
+
const partitions = group(catalog.partitions ?? [], (row) => keyOf(row.table_schema, row.table_name));
|
|
397
|
+
const views = new Map((catalog.views ?? []).map((row) => [
|
|
398
|
+
keyOf(row.table_schema, row.table_name),
|
|
399
|
+
row
|
|
400
|
+
]));
|
|
401
|
+
const tables = new Map;
|
|
402
|
+
const enums = [];
|
|
403
|
+
(catalog.tables ?? []).forEach((row) => {
|
|
404
|
+
const schema = text(row.table_schema) ?? "";
|
|
405
|
+
const name = text(row.table_name) ?? "";
|
|
406
|
+
const key = keyOf(schema, name);
|
|
407
|
+
const view = views.get(key);
|
|
408
|
+
const built = (columns.get(key) ?? []).sort((left, right) => integer(left.ordinal_position) - integer(right.ordinal_position)).map((column) => columnOf({ ...column, table_collation: row.table_collation }, { name, schema }, context));
|
|
409
|
+
enums.push(...built.flatMap((entry) => entry.enumType ? [entry.enumType] : []));
|
|
410
|
+
const grantLine = (grants.get(key) ?? []).map((grant) => `${text(grant.privilege_type)} to ${text(grant.grantee)}`).join(", ");
|
|
411
|
+
const metadata = view ? {
|
|
412
|
+
...text(view.check_option) && text(view.check_option) !== "NONE" ? { check_option: text(view.check_option) ?? "" } : {},
|
|
413
|
+
...text(view.security_type) ? { security: text(view.security_type) ?? "" } : {}
|
|
414
|
+
} : tableMetadata(row, context, grantLine || undefined);
|
|
415
|
+
const notes = [
|
|
416
|
+
text(row.table_comment) && text(row.table_comment) !== "VIEW" ? text(row.table_comment) : undefined,
|
|
417
|
+
view ? `View:
|
|
418
|
+
${text(view.view_definition) ?? ""}` : undefined,
|
|
419
|
+
...partitionLines(partitions.get(key) ?? []),
|
|
420
|
+
...(triggers.get(key) ?? []).map(triggerLine)
|
|
421
|
+
].filter((line) => line !== undefined && line !== "");
|
|
422
|
+
const table = {
|
|
423
|
+
checks: (checks.get(key) ?? []).filter(isOwnCheck).map((check) => ({
|
|
424
|
+
expression: (text(check.check_clause) ?? "").replace(/^\((.*)\)$/su, "$1"),
|
|
425
|
+
name: text(check.constraint_name) ?? ""
|
|
426
|
+
})),
|
|
427
|
+
columns: built.map((entry) => entry.column),
|
|
428
|
+
...view ? { headerColor: VIEW_COLOR } : {},
|
|
429
|
+
indexes: [],
|
|
430
|
+
...Object.keys(metadata).length > 0 ? { metadata } : {},
|
|
431
|
+
name,
|
|
432
|
+
...notes.length > 0 ? { note: notes.join(`
|
|
433
|
+
`) } : {},
|
|
434
|
+
schema
|
|
435
|
+
};
|
|
436
|
+
[...indexes.entries()].filter(([indexKey]) => indexKey.startsWith(`${key}.`)).forEach(([, rows]) => placeIndex(table, indexOf(rows)));
|
|
437
|
+
tables.set(key, table);
|
|
438
|
+
});
|
|
439
|
+
model.enums.push(...enums);
|
|
440
|
+
model.tables.push(...tables.values());
|
|
441
|
+
model.refs.push(...foreignKeys(catalog, tables));
|
|
442
|
+
model.deps.push(...viewDeps(catalog, model.tables.filter((table) => table.headerColor), model.tables.filter((table) => !table.headerColor)));
|
|
443
|
+
model.notes.push(...routineNote(catalog), ...eventNote(catalog));
|
|
444
|
+
const defaultSchema = options.defaultSchema ?? schemas[0] ?? "";
|
|
445
|
+
const result = withDefaultSchema(model, defaultSchema);
|
|
446
|
+
if (options.groups === "schema")
|
|
447
|
+
result.tableGroups.push(...groupBySchema(result.tables, defaultSchema));
|
|
448
|
+
return result;
|
|
449
|
+
};
|
|
450
|
+
export {
|
|
451
|
+
fromMysql
|
|
452
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { DbmlMetadata, DbmlStickyNote } from '../model';
|
|
2
|
+
import type { ColumnRow, compositeRow, definitionRow, domainRow, eventTriggerRow, extensionRow, functionRow, IndexRow, partitionRow, RelationRow } from './rows';
|
|
3
|
+
type Definition = ReturnType<typeof definitionRow>;
|
|
4
|
+
/** A column's facts: its collation, compression, the domain it's of, and
|
|
5
|
+
* its identity sequence when it doesn't start at one and count by one. */
|
|
6
|
+
export declare const columnMetadata: (row: ColumnRow) => {
|
|
7
|
+
metadata: DbmlMetadata;
|
|
8
|
+
} | {
|
|
9
|
+
metadata?: undefined;
|
|
10
|
+
};
|
|
11
|
+
/** What an index's parts do that DBML has no setting for: nulls ordering,
|
|
12
|
+
* an operator class or collation that isn't the column's own, and
|
|
13
|
+
* unique with nulls equal. */
|
|
14
|
+
export declare const indexNotes: (row: IndexRow, partName: (part: number) => string) => string[];
|
|
15
|
+
/** A table's facts: owner, grants, persistence and storage options, a
|
|
16
|
+
* foreign table's server and options, what it inherits from, and (when
|
|
17
|
+
* asked) its estimated rows and size. */
|
|
18
|
+
export declare const tableMetadata: (relation: RelationRow, statistics: boolean) => {
|
|
19
|
+
metadata: DbmlMetadata;
|
|
20
|
+
} | {
|
|
21
|
+
metadata?: undefined;
|
|
22
|
+
};
|
|
23
|
+
/** Lines for a table's note: its triggers, rules, partitions and what it
|
|
24
|
+
* inherits from. */
|
|
25
|
+
export declare const tableNoteLines: (input: {
|
|
26
|
+
inherits?: string;
|
|
27
|
+
partitions: ReturnType<typeof partitionRow>[];
|
|
28
|
+
rules: Definition[];
|
|
29
|
+
triggers: Definition[];
|
|
30
|
+
}) => string[];
|
|
31
|
+
type Extras = {
|
|
32
|
+
composites: ReturnType<typeof compositeRow>[];
|
|
33
|
+
domains: ReturnType<typeof domainRow>[];
|
|
34
|
+
eventTriggers: ReturnType<typeof eventTriggerRow>[];
|
|
35
|
+
extensions: ReturnType<typeof extensionRow>[];
|
|
36
|
+
functions: ReturnType<typeof functionRow>[];
|
|
37
|
+
};
|
|
38
|
+
/** Sticky notes for what stands apart from the tables. */
|
|
39
|
+
export declare const extraNotes: (extras: Extras, defaultSchema: string) => DbmlStickyNote[];
|
|
40
|
+
export {};
|
package/dist/postgres/index.d.ts
CHANGED
|
@@ -13,9 +13,14 @@ export type FromPostgresOptions = {
|
|
|
13
13
|
/** The schema whose tables are named without it, as a query run there
|
|
14
14
|
* names them: `public` unless the app keeps its tables elsewhere. */
|
|
15
15
|
defaultSchema?: string;
|
|
16
|
+
/** `'schema'` groups tables by their schema. */
|
|
17
|
+
groups?: 'schema';
|
|
16
18
|
/** The schemas to read; every schema that isn't Postgres's own by
|
|
17
19
|
* default. */
|
|
18
20
|
schemas?: string[];
|
|
21
|
+
/** Each table's estimated rows and size on disk, as metadata. They
|
|
22
|
+
* change as the app runs, so they're left out unless asked for. */
|
|
23
|
+
statistics?: boolean;
|
|
19
24
|
};
|
|
20
25
|
/** A default as Postgres writes it, as a literal when it is one. */
|
|
21
26
|
export declare const parseDefault: (expression: string) => {
|