@absolutejs/dbml 0.1.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 +20 -0
- package/README.md +45 -7
- package/changelog.json +59 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +1313 -129
- package/dist/drizzle/index.js +96 -8
- package/dist/drizzle/relations.d.ts +1 -0
- package/dist/index.js +105 -10
- package/dist/model.d.ts +16 -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 +8 -0
- package/dist/postgres/index.js +413 -34
- 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
package/dist/drizzle/index.js
CHANGED
|
@@ -15,6 +15,70 @@ var emptyModel = (project) => ({
|
|
|
15
15
|
tableGroups: [],
|
|
16
16
|
tables: []
|
|
17
17
|
});
|
|
18
|
+
var unqualified = (place, schema) => {
|
|
19
|
+
if (place.schema !== schema)
|
|
20
|
+
return place;
|
|
21
|
+
const { schema: dropped, ...rest } = place;
|
|
22
|
+
return rest;
|
|
23
|
+
};
|
|
24
|
+
var withDefaultSchema = (model, schema) => {
|
|
25
|
+
const result = {
|
|
26
|
+
...model,
|
|
27
|
+
deps: model.deps.map((dep) => ({
|
|
28
|
+
...dep,
|
|
29
|
+
downstream: unqualified(dep.downstream, schema),
|
|
30
|
+
upstream: unqualified(dep.upstream, schema)
|
|
31
|
+
})),
|
|
32
|
+
enums: model.enums.map((entry) => unqualified(entry, schema)),
|
|
33
|
+
records: model.records.map((records) => unqualified(records, schema)),
|
|
34
|
+
refs: model.refs.map((ref) => ({
|
|
35
|
+
...ref,
|
|
36
|
+
from: unqualified(ref.from, schema),
|
|
37
|
+
to: unqualified(ref.to, schema)
|
|
38
|
+
})),
|
|
39
|
+
tableGroups: model.tableGroups.map((group) => ({
|
|
40
|
+
...group,
|
|
41
|
+
tables: group.tables.map((entry) => unqualified(entry, schema))
|
|
42
|
+
})),
|
|
43
|
+
tables: model.tables.map((table) => ({
|
|
44
|
+
...unqualified(table, schema),
|
|
45
|
+
columns: table.columns.map((column) => {
|
|
46
|
+
if (column.typeSchema !== schema)
|
|
47
|
+
return column;
|
|
48
|
+
const { typeSchema: dropped, ...rest } = column;
|
|
49
|
+
return rest;
|
|
50
|
+
})
|
|
51
|
+
}))
|
|
52
|
+
};
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
var groupBySchema = (tables, defaultSchema = "public") => {
|
|
56
|
+
const byName = new Map;
|
|
57
|
+
tables.forEach((table) => {
|
|
58
|
+
const name = table.schema ?? defaultSchema;
|
|
59
|
+
byName.set(name, [
|
|
60
|
+
...byName.get(name) ?? [],
|
|
61
|
+
{
|
|
62
|
+
...table.schema ? { schema: table.schema } : {},
|
|
63
|
+
table: table.name
|
|
64
|
+
}
|
|
65
|
+
]);
|
|
66
|
+
});
|
|
67
|
+
return [...byName].map(([name, members]) => {
|
|
68
|
+
const group = { name, tables: members };
|
|
69
|
+
return group;
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
var sortedColumns = (index) => index.parts.map((part) => part.kind === "column" ? part.column : "").sort().join("\x00");
|
|
73
|
+
var coversAll = (index) => index.pk === true || index.unique === true && index.note?.includes("Where ") !== true;
|
|
74
|
+
var isUniqueSet = (table, columns) => {
|
|
75
|
+
const [only] = columns;
|
|
76
|
+
const single = columns.length === 1 ? table.columns.find((column) => column.name === only) : undefined;
|
|
77
|
+
if (single?.pk === true || single?.unique === true)
|
|
78
|
+
return true;
|
|
79
|
+
const wanted = [...columns].sort().join("\x00");
|
|
80
|
+
return table.indexes.some((index) => coversAll(index) && index.parts.every((part) => part.kind === "column") && sortedColumns(index) === wanted);
|
|
81
|
+
};
|
|
18
82
|
|
|
19
83
|
// src/drizzle/dialects.ts
|
|
20
84
|
import { is, SQL, sql } from "drizzle-orm";
|
|
@@ -102,6 +166,20 @@ var isRecord = (value) => typeof value === "object" && value !== null;
|
|
|
102
166
|
var isTableRelations = (entry) => isRecord(entry) && isRecord(entry.relations) && ("table" in entry);
|
|
103
167
|
var isRelationsV2 = (value) => isRecord(value) && !is2(value, Relations) && Object.keys(value).length > 0 && Object.values(value).every(isTableRelations);
|
|
104
168
|
var throughOf = (relation) => ("throughTable" in relation) && isRecord(relation.throughTable) ? relation.throughTable : undefined;
|
|
169
|
+
var filterOf = (relation) => ("where" in relation) && relation.where !== undefined ? { where: relation.where } : {};
|
|
170
|
+
var declaredMany = (field, relation) => {
|
|
171
|
+
if ("isReversed" in relation && relation.isReversed === true)
|
|
172
|
+
return;
|
|
173
|
+
const link = {
|
|
174
|
+
field,
|
|
175
|
+
from: relation.targetColumns,
|
|
176
|
+
kind: "one",
|
|
177
|
+
owner: relation.sourceTable,
|
|
178
|
+
target: relation.sourceColumns,
|
|
179
|
+
...filterOf(relation)
|
|
180
|
+
};
|
|
181
|
+
return link;
|
|
182
|
+
};
|
|
105
183
|
var v2Link = (field, relation) => {
|
|
106
184
|
const from = relation.sourceColumns;
|
|
107
185
|
const target = relation.targetColumns;
|
|
@@ -109,10 +187,19 @@ var v2Link = (field, relation) => {
|
|
|
109
187
|
return;
|
|
110
188
|
const owner = relation.sourceTable;
|
|
111
189
|
if (is2(relation, One)) {
|
|
112
|
-
const link = {
|
|
190
|
+
const link = {
|
|
191
|
+
field,
|
|
192
|
+
from,
|
|
193
|
+
kind: "one",
|
|
194
|
+
owner,
|
|
195
|
+
target,
|
|
196
|
+
...filterOf(relation)
|
|
197
|
+
};
|
|
113
198
|
return link;
|
|
114
199
|
}
|
|
115
200
|
const through = throughOf(relation);
|
|
201
|
+
if (is2(relation, Many) && !through)
|
|
202
|
+
return declaredMany(field, relation);
|
|
116
203
|
if (!is2(relation, Many) || !through)
|
|
117
204
|
return;
|
|
118
205
|
const link = {
|
|
@@ -185,7 +272,8 @@ var applyRelations = (options) => {
|
|
|
185
272
|
const target = endpointOf(options.lookup, link.target);
|
|
186
273
|
if (!from || !target)
|
|
187
274
|
continue;
|
|
188
|
-
const
|
|
275
|
+
const filter = link.where === undefined ? "" : ` where ${JSON.stringify(link.where)}`;
|
|
276
|
+
const field = `${options.lookup(link.owner)?.table ?? from.table}.${link.field}${filter}`;
|
|
189
277
|
const key = refKey({ from, to: target });
|
|
190
278
|
const existing = byKey.get(key);
|
|
191
279
|
if (existing)
|
|
@@ -447,15 +535,15 @@ var tableOf = (config) => {
|
|
|
447
535
|
table
|
|
448
536
|
};
|
|
449
537
|
};
|
|
450
|
-
var
|
|
451
|
-
var
|
|
452
|
-
var
|
|
538
|
+
var sortedColumns2 = (index) => index.parts.map((part) => part.kind === "column" ? part.column : "").sort().join("\x00");
|
|
539
|
+
var coversAll2 = (index) => index.pk === true || index.unique === true && index.note?.startsWith("Where") !== true;
|
|
540
|
+
var isUniqueSet2 = (table, columns) => {
|
|
453
541
|
const [only] = columns;
|
|
454
542
|
const single = columns.length === 1 ? table.columns.find((entry) => entry.name === only) : undefined;
|
|
455
543
|
if (single?.pk === true || single?.unique === true)
|
|
456
544
|
return true;
|
|
457
545
|
const wanted = [...columns].sort().join("\x00");
|
|
458
|
-
return table.indexes.some((index) =>
|
|
546
|
+
return table.indexes.some((index) => coversAll2(index) && index.parts.every((part) => part.kind === "column") && sortedColumns2(index) === wanted);
|
|
459
547
|
};
|
|
460
548
|
|
|
461
549
|
// src/drizzle/views.ts
|
|
@@ -618,7 +706,7 @@ var foreignKeyRefs = (entry, lookup, tables) => entry.config.foreignKeys.flatMap
|
|
|
618
706
|
...reference.name ? { name: reference.name } : {},
|
|
619
707
|
...onDelete ? { onDelete } : {},
|
|
620
708
|
...onUpdate ? { onUpdate } : {},
|
|
621
|
-
relation: table &&
|
|
709
|
+
relation: table && isUniqueSet2(table, columns) ? "-" : ">",
|
|
622
710
|
to: {
|
|
623
711
|
...target,
|
|
624
712
|
columns: reference.foreignColumns.map((column) => column.name)
|
|
@@ -745,7 +833,7 @@ var fromDrizzle = (schema, options = {}) => {
|
|
|
745
833
|
refs: model.refs,
|
|
746
834
|
isUnique: (end) => {
|
|
747
835
|
const table = byPlace.get(keyOf({ name: end.table, schema: end.schema }));
|
|
748
|
-
return table ?
|
|
836
|
+
return table ? isUniqueSet2(table, end.columns) : false;
|
|
749
837
|
}
|
|
750
838
|
});
|
|
751
839
|
addViews(model, found, lookup, (table) => byObject.get(table)?.columns.map((column) => column.name) ?? []);
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,70 @@ var emptyModel = (project) => ({
|
|
|
10
10
|
tableGroups: [],
|
|
11
11
|
tables: []
|
|
12
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
|
+
};
|
|
13
77
|
// src/print.ts
|
|
14
78
|
var INDENT = " ";
|
|
15
79
|
var quoteName = (name) => `"${name.replaceAll("\\", "\\\\").replaceAll('"', "\\\"")}"`;
|
|
@@ -111,17 +175,27 @@ var printRef = (ref) => `Ref${ref.name === undefined ? "" : ` ${quoteName(ref.na
|
|
|
111
175
|
ref.inactive && "inactive",
|
|
112
176
|
ref.note !== undefined && `note: ${quoteText(ref.note)}`
|
|
113
177
|
])}`;
|
|
114
|
-
var printDepEnd = (end) => {
|
|
178
|
+
var printDepEnd = (end, column) => {
|
|
115
179
|
const table = qualified(end.schema, end.table);
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
180
|
+
return column === undefined ? table : `${table}.${quoteName(column)}`;
|
|
181
|
+
};
|
|
182
|
+
var depEdges = (dep) => {
|
|
183
|
+
const upstream = dep.upstream.columns ?? [];
|
|
184
|
+
const downstream = dep.downstream.columns ?? [];
|
|
185
|
+
if (upstream.length <= 1 && downstream.length <= 1)
|
|
186
|
+
return [
|
|
187
|
+
`${printDepEnd(dep.upstream, upstream[0])} -> ${printDepEnd(dep.downstream, downstream[0])}`
|
|
188
|
+
];
|
|
189
|
+
return upstream.map((column, index) => `${printDepEnd(dep.upstream, column)} -> ${printDepEnd(dep.downstream, downstream[index])}`);
|
|
119
190
|
};
|
|
120
191
|
var printDep = (dep) => {
|
|
121
|
-
const
|
|
122
|
-
if (dep.note === undefined)
|
|
123
|
-
return `Dep: ${
|
|
124
|
-
return block("Dep", [
|
|
192
|
+
const edges = depEdges(dep);
|
|
193
|
+
if (dep.note === undefined && edges.length === 1)
|
|
194
|
+
return `Dep: ${edges[0] ?? ""}`;
|
|
195
|
+
return block("Dep", [
|
|
196
|
+
...edges,
|
|
197
|
+
...dep.note === undefined ? [] : ["", `note: ${quoteText(dep.note)}`]
|
|
198
|
+
]);
|
|
125
199
|
};
|
|
126
200
|
var printGroup = (group) => block(`TableGroup ${quoteName(group.name)}${settings([
|
|
127
201
|
group.color !== undefined && `color: ${group.color}`
|
|
@@ -144,6 +218,23 @@ var printProject = (project) => block(`Project${project.name ? ` ${quoteName(pro
|
|
|
144
218
|
...project.databaseType === undefined ? [] : [`database_type: ${quoteText(project.databaseType)}`],
|
|
145
219
|
...project.note === undefined ? [] : [`Note: ${quoteText(project.note)}`]
|
|
146
220
|
]);
|
|
221
|
+
var printMetadataValue = (value) => typeof value === "string" ? quoteText(value) : `${value}`;
|
|
222
|
+
var metadataKey = (key) => key.replace(/[^A-Za-z0-9_]/gu, "_");
|
|
223
|
+
var metadataBlock = (title, metadata) => {
|
|
224
|
+
const entries = Object.entries(metadata ?? {});
|
|
225
|
+
if (entries.length === 0)
|
|
226
|
+
return [];
|
|
227
|
+
return [
|
|
228
|
+
block(`Metadata ${title}`, entries.map(([key, value]) => `${metadataKey(key)}: ${printMetadataValue(value)}`))
|
|
229
|
+
];
|
|
230
|
+
};
|
|
231
|
+
var printMetadata = (tables) => tables.flatMap((table) => {
|
|
232
|
+
const name = qualified(table.schema, table.name);
|
|
233
|
+
return [
|
|
234
|
+
...metadataBlock(`Table ${name}`, table.metadata),
|
|
235
|
+
...table.columns.flatMap((column) => metadataBlock(`Column ${name}.${quoteName(column.name)}`, column.metadata))
|
|
236
|
+
];
|
|
237
|
+
});
|
|
147
238
|
var printDbml = (model) => {
|
|
148
239
|
const { project } = model;
|
|
149
240
|
const hasProject = project !== undefined && (project.name !== undefined || project.databaseType !== undefined || project.note !== undefined);
|
|
@@ -157,7 +248,8 @@ var printDbml = (model) => {
|
|
|
157
248
|
...model.deps.length > 0 ? [model.deps.map(printDep).join(`
|
|
158
249
|
`)] : [],
|
|
159
250
|
...model.notes.map(printNote),
|
|
160
|
-
...model.records.filter((records) => records.rows.length > 0).map(printRecords)
|
|
251
|
+
...model.records.filter((records) => records.rows.length > 0).map(printRecords),
|
|
252
|
+
...printMetadata(model.tables)
|
|
161
253
|
];
|
|
162
254
|
return `${sections.join(`
|
|
163
255
|
|
|
@@ -166,8 +258,11 @@ var printDbml = (model) => {
|
|
|
166
258
|
};
|
|
167
259
|
export {
|
|
168
260
|
emptyModel,
|
|
261
|
+
groupBySchema,
|
|
262
|
+
isUniqueSet,
|
|
169
263
|
printDbml,
|
|
170
264
|
quoteExpression,
|
|
171
265
|
quoteName,
|
|
172
|
-
quoteText
|
|
266
|
+
quoteText,
|
|
267
|
+
withDefaultSchema
|
|
173
268
|
};
|
package/dist/model.d.ts
CHANGED
|
@@ -34,12 +34,16 @@ export type DbmlDefault = {
|
|
|
34
34
|
kind: 'string';
|
|
35
35
|
value: string;
|
|
36
36
|
};
|
|
37
|
+
/** Facts about a table or column as key and value, DBML's `Metadata`:
|
|
38
|
+
* an owner, a row count, a collation. Keys are bare words. */
|
|
39
|
+
export type DbmlMetadata = Record<string, boolean | number | string>;
|
|
37
40
|
export type DbmlColumn = {
|
|
38
41
|
/** Column-level checks, each a SQL expression. */
|
|
39
42
|
checks?: string[];
|
|
40
43
|
default?: DbmlDefault;
|
|
41
44
|
/** An identity, serial or auto-increment column. */
|
|
42
45
|
increment?: boolean;
|
|
46
|
+
metadata?: DbmlMetadata;
|
|
43
47
|
name: string;
|
|
44
48
|
note?: string;
|
|
45
49
|
notNull?: boolean;
|
|
@@ -80,6 +84,7 @@ export type DbmlTable = {
|
|
|
80
84
|
columns: DbmlColumn[];
|
|
81
85
|
headerColor?: DbmlColor;
|
|
82
86
|
indexes: DbmlIndex[];
|
|
87
|
+
metadata?: DbmlMetadata;
|
|
83
88
|
name: string;
|
|
84
89
|
note?: string;
|
|
85
90
|
schema?: string;
|
|
@@ -156,3 +161,14 @@ export type DbmlModel = {
|
|
|
156
161
|
};
|
|
157
162
|
/** A model with nothing in it, to build on. */
|
|
158
163
|
export declare const emptyModel: (project?: DbmlProject) => DbmlModel;
|
|
164
|
+
/**
|
|
165
|
+
* The model as read from inside one schema: everything in `schema` loses
|
|
166
|
+
* its qualifier (`"orders"`, not `"app"."orders"`), the way a query run
|
|
167
|
+
* there would name it, and everything elsewhere keeps its own.
|
|
168
|
+
*/
|
|
169
|
+
export declare const withDefaultSchema: (model: DbmlModel, schema: string) => DbmlModel;
|
|
170
|
+
/** Table groups, one per schema, in the order tables first appear. */
|
|
171
|
+
export declare const groupBySchema: (tables: DbmlTable[], defaultSchema?: string) => DbmlTableGroup[];
|
|
172
|
+
/** Whether these columns alone are unique in the table: a one-to-one link
|
|
173
|
+
* rather than many-to-one when they're what a foreign key holds. */
|
|
174
|
+
export declare const isUniqueSet: (table: DbmlTable, columns: string[]) => boolean;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type DbmlModel } from '../model';
|
|
2
|
+
/** Runs one statement with `?` parameters and returns its rows. */
|
|
3
|
+
export type MysqlQuery = (text: string, params: unknown[]) => Promise<Record<string, unknown>[]>;
|
|
4
|
+
export type FromMysqlOptions = {
|
|
5
|
+
/** The database (schema) named without qualifying it; the first one read
|
|
6
|
+
* by default. */
|
|
7
|
+
defaultSchema?: string;
|
|
8
|
+
/** `'schema'` groups tables by their database. */
|
|
9
|
+
groups?: 'schema';
|
|
10
|
+
project?: {
|
|
11
|
+
name?: string;
|
|
12
|
+
note?: string;
|
|
13
|
+
};
|
|
14
|
+
/** The databases to read; the connection's current one by default. */
|
|
15
|
+
schemas?: string[];
|
|
16
|
+
/** Each table's estimated rows and size, as metadata. */
|
|
17
|
+
statistics?: boolean;
|
|
18
|
+
};
|
|
19
|
+
/** The database's structure as a DBML model. */
|
|
20
|
+
export declare const fromMysql: (query: MysqlQuery, options?: FromMysqlOptions) => Promise<DbmlModel>;
|