@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
package/dist/drizzle/index.js
CHANGED
|
@@ -52,6 +52,33 @@ var withDefaultSchema = (model, schema) => {
|
|
|
52
52
|
};
|
|
53
53
|
return result;
|
|
54
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
|
+
};
|
|
55
82
|
|
|
56
83
|
// src/drizzle/dialects.ts
|
|
57
84
|
import { is, SQL, sql } from "drizzle-orm";
|
|
@@ -139,6 +166,20 @@ var isRecord = (value) => typeof value === "object" && value !== null;
|
|
|
139
166
|
var isTableRelations = (entry) => isRecord(entry) && isRecord(entry.relations) && ("table" in entry);
|
|
140
167
|
var isRelationsV2 = (value) => isRecord(value) && !is2(value, Relations) && Object.keys(value).length > 0 && Object.values(value).every(isTableRelations);
|
|
141
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
|
+
};
|
|
142
183
|
var v2Link = (field, relation) => {
|
|
143
184
|
const from = relation.sourceColumns;
|
|
144
185
|
const target = relation.targetColumns;
|
|
@@ -146,10 +187,19 @@ var v2Link = (field, relation) => {
|
|
|
146
187
|
return;
|
|
147
188
|
const owner = relation.sourceTable;
|
|
148
189
|
if (is2(relation, One)) {
|
|
149
|
-
const link = {
|
|
190
|
+
const link = {
|
|
191
|
+
field,
|
|
192
|
+
from,
|
|
193
|
+
kind: "one",
|
|
194
|
+
owner,
|
|
195
|
+
target,
|
|
196
|
+
...filterOf(relation)
|
|
197
|
+
};
|
|
150
198
|
return link;
|
|
151
199
|
}
|
|
152
200
|
const through = throughOf(relation);
|
|
201
|
+
if (is2(relation, Many) && !through)
|
|
202
|
+
return declaredMany(field, relation);
|
|
153
203
|
if (!is2(relation, Many) || !through)
|
|
154
204
|
return;
|
|
155
205
|
const link = {
|
|
@@ -222,7 +272,8 @@ var applyRelations = (options) => {
|
|
|
222
272
|
const target = endpointOf(options.lookup, link.target);
|
|
223
273
|
if (!from || !target)
|
|
224
274
|
continue;
|
|
225
|
-
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}`;
|
|
226
277
|
const key = refKey({ from, to: target });
|
|
227
278
|
const existing = byKey.get(key);
|
|
228
279
|
if (existing)
|
|
@@ -484,15 +535,15 @@ var tableOf = (config) => {
|
|
|
484
535
|
table
|
|
485
536
|
};
|
|
486
537
|
};
|
|
487
|
-
var
|
|
488
|
-
var
|
|
489
|
-
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) => {
|
|
490
541
|
const [only] = columns;
|
|
491
542
|
const single = columns.length === 1 ? table.columns.find((entry) => entry.name === only) : undefined;
|
|
492
543
|
if (single?.pk === true || single?.unique === true)
|
|
493
544
|
return true;
|
|
494
545
|
const wanted = [...columns].sort().join("\x00");
|
|
495
|
-
return table.indexes.some((index) =>
|
|
546
|
+
return table.indexes.some((index) => coversAll2(index) && index.parts.every((part) => part.kind === "column") && sortedColumns2(index) === wanted);
|
|
496
547
|
};
|
|
497
548
|
|
|
498
549
|
// src/drizzle/views.ts
|
|
@@ -655,7 +706,7 @@ var foreignKeyRefs = (entry, lookup, tables) => entry.config.foreignKeys.flatMap
|
|
|
655
706
|
...reference.name ? { name: reference.name } : {},
|
|
656
707
|
...onDelete ? { onDelete } : {},
|
|
657
708
|
...onUpdate ? { onUpdate } : {},
|
|
658
|
-
relation: table &&
|
|
709
|
+
relation: table && isUniqueSet2(table, columns) ? "-" : ">",
|
|
659
710
|
to: {
|
|
660
711
|
...target,
|
|
661
712
|
columns: reference.foreignColumns.map((column) => column.name)
|
|
@@ -782,7 +833,7 @@ var fromDrizzle = (schema, options = {}) => {
|
|
|
782
833
|
refs: model.refs,
|
|
783
834
|
isUnique: (end) => {
|
|
784
835
|
const table = byPlace.get(keyOf({ name: end.table, schema: end.schema }));
|
|
785
|
-
return table ?
|
|
836
|
+
return table ? isUniqueSet2(table, end.columns) : false;
|
|
786
837
|
}
|
|
787
838
|
});
|
|
788
839
|
addViews(model, found, lookup, (table) => byObject.get(table)?.columns.map((column) => column.name) ?? []);
|
package/dist/index.js
CHANGED
|
@@ -47,6 +47,33 @@ var withDefaultSchema = (model, schema) => {
|
|
|
47
47
|
};
|
|
48
48
|
return result;
|
|
49
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
|
+
};
|
|
50
77
|
// src/print.ts
|
|
51
78
|
var INDENT = " ";
|
|
52
79
|
var quoteName = (name) => `"${name.replaceAll("\\", "\\\\").replaceAll('"', "\\\"")}"`;
|
|
@@ -148,17 +175,27 @@ var printRef = (ref) => `Ref${ref.name === undefined ? "" : ` ${quoteName(ref.na
|
|
|
148
175
|
ref.inactive && "inactive",
|
|
149
176
|
ref.note !== undefined && `note: ${quoteText(ref.note)}`
|
|
150
177
|
])}`;
|
|
151
|
-
var printDepEnd = (end) => {
|
|
178
|
+
var printDepEnd = (end, column) => {
|
|
152
179
|
const table = qualified(end.schema, end.table);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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])}`);
|
|
156
190
|
};
|
|
157
191
|
var printDep = (dep) => {
|
|
158
|
-
const
|
|
159
|
-
if (dep.note === undefined)
|
|
160
|
-
return `Dep: ${
|
|
161
|
-
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
|
+
]);
|
|
162
199
|
};
|
|
163
200
|
var printGroup = (group) => block(`TableGroup ${quoteName(group.name)}${settings([
|
|
164
201
|
group.color !== undefined && `color: ${group.color}`
|
|
@@ -181,6 +218,23 @@ var printProject = (project) => block(`Project${project.name ? ` ${quoteName(pro
|
|
|
181
218
|
...project.databaseType === undefined ? [] : [`database_type: ${quoteText(project.databaseType)}`],
|
|
182
219
|
...project.note === undefined ? [] : [`Note: ${quoteText(project.note)}`]
|
|
183
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
|
+
});
|
|
184
238
|
var printDbml = (model) => {
|
|
185
239
|
const { project } = model;
|
|
186
240
|
const hasProject = project !== undefined && (project.name !== undefined || project.databaseType !== undefined || project.note !== undefined);
|
|
@@ -194,7 +248,8 @@ var printDbml = (model) => {
|
|
|
194
248
|
...model.deps.length > 0 ? [model.deps.map(printDep).join(`
|
|
195
249
|
`)] : [],
|
|
196
250
|
...model.notes.map(printNote),
|
|
197
|
-
...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)
|
|
198
253
|
];
|
|
199
254
|
return `${sections.join(`
|
|
200
255
|
|
|
@@ -203,6 +258,8 @@ var printDbml = (model) => {
|
|
|
203
258
|
};
|
|
204
259
|
export {
|
|
205
260
|
emptyModel,
|
|
261
|
+
groupBySchema,
|
|
262
|
+
isUniqueSet,
|
|
206
263
|
printDbml,
|
|
207
264
|
quoteExpression,
|
|
208
265
|
quoteName,
|
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;
|
|
@@ -162,3 +167,8 @@ export declare const emptyModel: (project?: DbmlProject) => DbmlModel;
|
|
|
162
167
|
* there would name it, and everything elsewhere keeps its own.
|
|
163
168
|
*/
|
|
164
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>;
|