@absolutejs/dbml 0.0.0-stage → 0.1.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 +14 -0
- package/LICENSE +21 -0
- package/README.md +72 -2
- package/changelog.json +17 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +1660 -0
- package/dist/drizzle/dialects.d.ts +121 -0
- package/dist/drizzle/index.d.ts +21 -0
- package/dist/drizzle/index.js +759 -0
- package/dist/drizzle/relations.d.ts +40 -0
- package/dist/drizzle/tables.d.ts +10 -0
- package/dist/drizzle/views.d.ts +13 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +173 -0
- package/dist/model.d.ts +158 -0
- package/dist/postgres/index.d.ts +35 -0
- package/dist/postgres/index.js +666 -0
- package/dist/postgres/queries.d.ts +10 -0
- package/dist/postgres/rows.d.ts +91 -0
- package/dist/print.d.ts +11 -0
- package/package.json +89 -5
|
@@ -0,0 +1,759 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/drizzle/index.ts
|
|
3
|
+
import { is as is4 } from "drizzle-orm";
|
|
4
|
+
import * as cockroach2 from "drizzle-orm/cockroach-core";
|
|
5
|
+
import * as pg2 from "drizzle-orm/pg-core";
|
|
6
|
+
|
|
7
|
+
// src/model.ts
|
|
8
|
+
var emptyModel = (project) => ({
|
|
9
|
+
deps: [],
|
|
10
|
+
enums: [],
|
|
11
|
+
notes: [],
|
|
12
|
+
...project ? { project } : {},
|
|
13
|
+
records: [],
|
|
14
|
+
refs: [],
|
|
15
|
+
tableGroups: [],
|
|
16
|
+
tables: []
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// src/drizzle/dialects.ts
|
|
20
|
+
import { is, SQL, sql } from "drizzle-orm";
|
|
21
|
+
import * as cockroach from "drizzle-orm/cockroach-core";
|
|
22
|
+
import * as mssql from "drizzle-orm/mssql-core";
|
|
23
|
+
import * as mysql from "drizzle-orm/mysql-core";
|
|
24
|
+
import * as pg from "drizzle-orm/pg-core";
|
|
25
|
+
import * as singlestore from "drizzle-orm/singlestore-core";
|
|
26
|
+
import * as sqlite from "drizzle-orm/sqlite-core";
|
|
27
|
+
var none = () => {
|
|
28
|
+
return;
|
|
29
|
+
};
|
|
30
|
+
var DIALECTS = [
|
|
31
|
+
{
|
|
32
|
+
databaseType: "PostgreSQL",
|
|
33
|
+
enumOf: (value) => pg.isPgEnum(value) ? value : undefined,
|
|
34
|
+
materializedViewOf: (value) => pg.isPgMaterializedView(value) ? pg.getMaterializedViewConfig(value) : undefined,
|
|
35
|
+
sequenceOf: (value) => pg.isPgSequence(value) ? value : undefined,
|
|
36
|
+
tableOf: (value) => is(value, pg.PgTable) ? pg.getTableConfig(value) : undefined,
|
|
37
|
+
viewOf: (value) => pg.isPgView(value) ? pg.getViewConfig(value) : undefined
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
databaseType: "CockroachDB",
|
|
41
|
+
enumOf: (value) => cockroach.isCockroachEnum(value) ? value : undefined,
|
|
42
|
+
materializedViewOf: (value) => cockroach.isCockroachMaterializedView(value) ? cockroach.getMaterializedViewConfig(value) : undefined,
|
|
43
|
+
sequenceOf: (value) => cockroach.isCockroachSequence(value) ? value : undefined,
|
|
44
|
+
tableOf: (value) => is(value, cockroach.CockroachTable) ? cockroach.getTableConfig(value) : undefined,
|
|
45
|
+
viewOf: (value) => cockroach.isCockroachView(value) ? cockroach.getViewConfig(value) : undefined
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
databaseType: "MySQL",
|
|
49
|
+
enumOf: none,
|
|
50
|
+
materializedViewOf: none,
|
|
51
|
+
sequenceOf: none,
|
|
52
|
+
tableOf: (value) => is(value, mysql.MySqlTable) ? mysql.getTableConfig(value) : undefined,
|
|
53
|
+
viewOf: (value) => is(value, mysql.MySqlView) ? mysql.getViewConfig(value) : undefined
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
databaseType: "SingleStore",
|
|
57
|
+
enumOf: none,
|
|
58
|
+
materializedViewOf: none,
|
|
59
|
+
sequenceOf: none,
|
|
60
|
+
viewOf: none,
|
|
61
|
+
tableOf: (value) => is(value, singlestore.SingleStoreTable) ? {
|
|
62
|
+
checks: [],
|
|
63
|
+
foreignKeys: [],
|
|
64
|
+
...singlestore.getTableConfig(value)
|
|
65
|
+
} : undefined
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
databaseType: "SQLite",
|
|
69
|
+
enumOf: none,
|
|
70
|
+
materializedViewOf: none,
|
|
71
|
+
sequenceOf: none,
|
|
72
|
+
tableOf: (value) => is(value, sqlite.SQLiteTable) ? sqlite.getTableConfig(value) : undefined,
|
|
73
|
+
viewOf: (value) => is(value, sqlite.SQLiteView) ? sqlite.getViewConfig(value) : undefined
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
databaseType: "SQL Server",
|
|
77
|
+
enumOf: none,
|
|
78
|
+
materializedViewOf: none,
|
|
79
|
+
sequenceOf: none,
|
|
80
|
+
tableOf: (value) => is(value, mssql.MsSqlTable) ? mssql.getTableConfig(value) : undefined,
|
|
81
|
+
viewOf: (value) => is(value, mssql.MsSqlView) ? mssql.getViewConfig(value) : undefined
|
|
82
|
+
}
|
|
83
|
+
];
|
|
84
|
+
var writer = new pg.PgDialect;
|
|
85
|
+
var sqlText = (fragment) => writer.sqlToQuery(sql`${fragment}`.inlineParams(), "indexes").sql;
|
|
86
|
+
var sqlTextQualified = (fragment) => writer.sqlToQuery(sql`${fragment}`.inlineParams()).sql;
|
|
87
|
+
var isSql = (value) => is(value, SQL);
|
|
88
|
+
|
|
89
|
+
// src/drizzle/relations.ts
|
|
90
|
+
import {
|
|
91
|
+
getColumnTable,
|
|
92
|
+
is as is2,
|
|
93
|
+
Many,
|
|
94
|
+
One
|
|
95
|
+
} from "drizzle-orm";
|
|
96
|
+
import {
|
|
97
|
+
createTableRelationsHelpers,
|
|
98
|
+
One as OneV1,
|
|
99
|
+
Relations
|
|
100
|
+
} from "drizzle-orm/_relations";
|
|
101
|
+
var isRecord = (value) => typeof value === "object" && value !== null;
|
|
102
|
+
var isTableRelations = (entry) => isRecord(entry) && isRecord(entry.relations) && ("table" in entry);
|
|
103
|
+
var isRelationsV2 = (value) => isRecord(value) && !is2(value, Relations) && Object.keys(value).length > 0 && Object.values(value).every(isTableRelations);
|
|
104
|
+
var throughOf = (relation) => ("throughTable" in relation) && isRecord(relation.throughTable) ? relation.throughTable : undefined;
|
|
105
|
+
var v2Link = (field, relation) => {
|
|
106
|
+
const from = relation.sourceColumns;
|
|
107
|
+
const target = relation.targetColumns;
|
|
108
|
+
if (from.length === 0 || target.length === 0)
|
|
109
|
+
return;
|
|
110
|
+
const owner = relation.sourceTable;
|
|
111
|
+
if (is2(relation, One)) {
|
|
112
|
+
const link = { field, from, kind: "one", owner, target };
|
|
113
|
+
return link;
|
|
114
|
+
}
|
|
115
|
+
const through = throughOf(relation);
|
|
116
|
+
if (!is2(relation, Many) || !through)
|
|
117
|
+
return;
|
|
118
|
+
const link = {
|
|
119
|
+
field,
|
|
120
|
+
from,
|
|
121
|
+
kind: "many-through",
|
|
122
|
+
owner,
|
|
123
|
+
target,
|
|
124
|
+
through
|
|
125
|
+
};
|
|
126
|
+
return link;
|
|
127
|
+
};
|
|
128
|
+
var linksFromV2 = (relations) => Object.values(relations).flatMap((config) => Object.entries(config.relations).map(([field, relation]) => v2Link(field, relation)).filter((link) => link !== undefined));
|
|
129
|
+
var isRelationsV1 = (value) => is2(value, Relations);
|
|
130
|
+
var linksFromV1 = (relations) => Object.entries(relations.config(createTableRelationsHelpers(relations.table))).map(([field, relation]) => {
|
|
131
|
+
if (!is2(relation, OneV1))
|
|
132
|
+
return;
|
|
133
|
+
const from = relation.config?.fields ?? [];
|
|
134
|
+
const target = relation.config?.references ?? [];
|
|
135
|
+
if (from.length === 0 || target.length === 0)
|
|
136
|
+
return;
|
|
137
|
+
const link = {
|
|
138
|
+
field,
|
|
139
|
+
from,
|
|
140
|
+
kind: "one",
|
|
141
|
+
owner: relations.table,
|
|
142
|
+
target
|
|
143
|
+
};
|
|
144
|
+
return link;
|
|
145
|
+
}).filter((link) => link !== undefined);
|
|
146
|
+
var endpointOf = (lookup, columns) => {
|
|
147
|
+
const [first] = columns;
|
|
148
|
+
if (!first)
|
|
149
|
+
return;
|
|
150
|
+
const place = lookup(getColumnTable(first));
|
|
151
|
+
if (!place)
|
|
152
|
+
return;
|
|
153
|
+
const endpoint = {
|
|
154
|
+
...place,
|
|
155
|
+
columns: columns.map((column) => column.name)
|
|
156
|
+
};
|
|
157
|
+
return endpoint;
|
|
158
|
+
};
|
|
159
|
+
var side = (end) => `${end.schema ?? ""}.${end.table}(${end.columns.join(",")})`;
|
|
160
|
+
var refKey = (ref) => [side(ref.from), side(ref.to)].sort().join("|");
|
|
161
|
+
var newRef = (link, ends, field, options) => {
|
|
162
|
+
if (link.kind === "many-through") {
|
|
163
|
+
const through = link.through ? options.lookup(link.through) : undefined;
|
|
164
|
+
const ref = {
|
|
165
|
+
...ends,
|
|
166
|
+
inactive: true,
|
|
167
|
+
note: `Many-to-many through ${through?.table ?? "a junction table"}, read as ${field}`,
|
|
168
|
+
relation: "<>"
|
|
169
|
+
};
|
|
170
|
+
return ref;
|
|
171
|
+
}
|
|
172
|
+
const ref = {
|
|
173
|
+
...ends,
|
|
174
|
+
inactive: true,
|
|
175
|
+
note: `Not a foreign key: the app's relation ${field}`,
|
|
176
|
+
relation: options.isUnique(ends.from) ? "-" : ">"
|
|
177
|
+
};
|
|
178
|
+
return ref;
|
|
179
|
+
};
|
|
180
|
+
var applyRelations = (options) => {
|
|
181
|
+
const byKey = new Map(options.refs.map((ref) => [refKey(ref), ref]));
|
|
182
|
+
const named = new Map;
|
|
183
|
+
for (const link of options.links) {
|
|
184
|
+
const from = endpointOf(options.lookup, link.from);
|
|
185
|
+
const target = endpointOf(options.lookup, link.target);
|
|
186
|
+
if (!from || !target)
|
|
187
|
+
continue;
|
|
188
|
+
const field = `${options.lookup(link.owner)?.table ?? from.table}.${link.field}`;
|
|
189
|
+
const key = refKey({ from, to: target });
|
|
190
|
+
const existing = byKey.get(key);
|
|
191
|
+
if (existing)
|
|
192
|
+
named.set(existing, [...named.get(existing) ?? [], field]);
|
|
193
|
+
if (existing)
|
|
194
|
+
continue;
|
|
195
|
+
const ref = newRef(link, { from, to: target }, field, options);
|
|
196
|
+
byKey.set(key, ref);
|
|
197
|
+
options.refs.push(ref);
|
|
198
|
+
}
|
|
199
|
+
named.forEach((fields, ref) => {
|
|
200
|
+
const line = `Read in the app as ${[...new Set(fields)].join(", ")}`;
|
|
201
|
+
ref.note = ref.note ? `${ref.note}
|
|
202
|
+
${line}` : line;
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
// src/drizzle/tables.ts
|
|
207
|
+
var SAFE_MAX = BigInt(Number.MAX_SAFE_INTEGER);
|
|
208
|
+
var SAFE_MIN = BigInt(Number.MIN_SAFE_INTEGER);
|
|
209
|
+
var bigintDefault = (value) => {
|
|
210
|
+
const safe = value <= SAFE_MAX && value >= SAFE_MIN;
|
|
211
|
+
const result = safe ? { kind: "number", value: `${value}` } : { kind: "expression", value: `${value}` };
|
|
212
|
+
return result;
|
|
213
|
+
};
|
|
214
|
+
var literalDefault = (value) => {
|
|
215
|
+
if (value === null)
|
|
216
|
+
return { kind: "null" };
|
|
217
|
+
if (typeof value === "boolean")
|
|
218
|
+
return { kind: "boolean", value };
|
|
219
|
+
if (typeof value === "number")
|
|
220
|
+
return { kind: "number", value };
|
|
221
|
+
if (typeof value === "bigint")
|
|
222
|
+
return bigintDefault(value);
|
|
223
|
+
if (typeof value === "string")
|
|
224
|
+
return { kind: "string", value };
|
|
225
|
+
if (value instanceof Date)
|
|
226
|
+
return {
|
|
227
|
+
kind: "string",
|
|
228
|
+
value: value.toISOString()
|
|
229
|
+
};
|
|
230
|
+
return {
|
|
231
|
+
kind: "string",
|
|
232
|
+
value: JSON.stringify(value)
|
|
233
|
+
};
|
|
234
|
+
};
|
|
235
|
+
var defaultOf = (column) => {
|
|
236
|
+
if (column.default === undefined)
|
|
237
|
+
return;
|
|
238
|
+
if (isSql(column.default))
|
|
239
|
+
return {
|
|
240
|
+
kind: "expression",
|
|
241
|
+
value: sqlText(column.default)
|
|
242
|
+
};
|
|
243
|
+
return literalDefault(column.default);
|
|
244
|
+
};
|
|
245
|
+
var SERIAL = /Serial/u;
|
|
246
|
+
var identityNote = (column) => {
|
|
247
|
+
const type = column.generatedIdentity?.type;
|
|
248
|
+
if (type === "always")
|
|
249
|
+
return "Identity, generated always";
|
|
250
|
+
if (type !== undefined)
|
|
251
|
+
return "Identity, generated by default";
|
|
252
|
+
if (column.identity !== undefined)
|
|
253
|
+
return "Identity";
|
|
254
|
+
return;
|
|
255
|
+
};
|
|
256
|
+
var isIncrement = (column) => column.generatedIdentity !== undefined || column.identity !== undefined || column.autoIncrement === true || SERIAL.test(column.columnType);
|
|
257
|
+
var generatedText = (value) => {
|
|
258
|
+
if (isSql(value))
|
|
259
|
+
return sqlText(value);
|
|
260
|
+
if (typeof value === "function")
|
|
261
|
+
return "a value the app computes";
|
|
262
|
+
return `${value}`;
|
|
263
|
+
};
|
|
264
|
+
var generatedNote = (column) => {
|
|
265
|
+
const { generated } = column;
|
|
266
|
+
if (!generated)
|
|
267
|
+
return;
|
|
268
|
+
const mode = generated.mode ? `, ${generated.mode}` : "";
|
|
269
|
+
return `Generated always as (${generatedText(generated.as)})${mode}`;
|
|
270
|
+
};
|
|
271
|
+
var present = (lines) => lines.filter((line) => line !== undefined);
|
|
272
|
+
var columnNotes = (column, storedAs) => present([
|
|
273
|
+
identityNote(column),
|
|
274
|
+
generatedNote(column),
|
|
275
|
+
column.default === undefined && column.defaultFn ? "The app sets it when a row is inserted" : undefined,
|
|
276
|
+
column.onUpdateFn ? "The app sets it whenever the row is updated" : undefined,
|
|
277
|
+
column.hasOnUpdateNow === true ? "Set to the current time on every update" : undefined,
|
|
278
|
+
column.uniqueType === "not distinct" ? "Unique, nulls not distinct" : undefined,
|
|
279
|
+
storedAs ? `Stored as ${storedAs}` : undefined
|
|
280
|
+
]);
|
|
281
|
+
var inSchema = (schema) => schema ? { schema } : {};
|
|
282
|
+
var arraySuffix = (dimensions) => {
|
|
283
|
+
if (dimensions === undefined || dimensions === 0)
|
|
284
|
+
return "";
|
|
285
|
+
return "[]".repeat(Number.isFinite(dimensions) ? dimensions : 1);
|
|
286
|
+
};
|
|
287
|
+
var valuesOf = (values) => (values ?? []).map((value) => ({ name: value }));
|
|
288
|
+
var typeOf = (column, table) => {
|
|
289
|
+
const sqlType = column.getSQLType();
|
|
290
|
+
const arrays = arraySuffix(column.dimensions);
|
|
291
|
+
if (column.enum?.enumName) {
|
|
292
|
+
const named = {
|
|
293
|
+
enum: {
|
|
294
|
+
name: column.enum.enumName,
|
|
295
|
+
...inSchema(column.enum.schema),
|
|
296
|
+
values: valuesOf(column.enumValues)
|
|
297
|
+
},
|
|
298
|
+
type: `${column.enum.enumName}${arrays}`,
|
|
299
|
+
...column.enum.schema ? { typeSchema: column.enum.schema } : {}
|
|
300
|
+
};
|
|
301
|
+
return named;
|
|
302
|
+
}
|
|
303
|
+
if (!column.enumValues || column.enumValues.length === 0) {
|
|
304
|
+
const plain = { type: `${sqlType}${arrays}` };
|
|
305
|
+
return plain;
|
|
306
|
+
}
|
|
307
|
+
const name = `${table.name}_${column.name}`;
|
|
308
|
+
const inline = {
|
|
309
|
+
enum: {
|
|
310
|
+
name,
|
|
311
|
+
...inSchema(table.schema),
|
|
312
|
+
values: valuesOf(column.enumValues)
|
|
313
|
+
},
|
|
314
|
+
storedAs: sqlType,
|
|
315
|
+
type: name,
|
|
316
|
+
...table.schema ? { typeSchema: table.schema } : {}
|
|
317
|
+
};
|
|
318
|
+
return inline;
|
|
319
|
+
};
|
|
320
|
+
var columnOf = (column, table) => {
|
|
321
|
+
const type = typeOf(column, table);
|
|
322
|
+
const notes = columnNotes(column, type.storedAs);
|
|
323
|
+
const fallback = defaultOf(column);
|
|
324
|
+
const result = {
|
|
325
|
+
name: column.name,
|
|
326
|
+
type: type.type,
|
|
327
|
+
...type.typeSchema ? { typeSchema: type.typeSchema } : {},
|
|
328
|
+
...column.primary ? { pk: true } : {},
|
|
329
|
+
...isIncrement(column) ? { increment: true } : {},
|
|
330
|
+
...column.isUnique && !column.primary ? { unique: true } : {},
|
|
331
|
+
...column.notNull && !column.primary ? { notNull: true } : {},
|
|
332
|
+
...fallback ? { default: fallback } : {},
|
|
333
|
+
...notes.length > 0 ? { note: notes.join(`
|
|
334
|
+
`) } : {}
|
|
335
|
+
};
|
|
336
|
+
return { column: result, enum: type.enum };
|
|
337
|
+
};
|
|
338
|
+
var isIndexedColumn = (value) => typeof value === "object" && value !== null && ("name" in value) && typeof value.name === "string";
|
|
339
|
+
var partOf = (value) => {
|
|
340
|
+
if (isSql(value))
|
|
341
|
+
return {
|
|
342
|
+
expression: sqlText(value),
|
|
343
|
+
kind: "expression"
|
|
344
|
+
};
|
|
345
|
+
if (!isIndexedColumn(value))
|
|
346
|
+
return {
|
|
347
|
+
expression: `${value}`,
|
|
348
|
+
kind: "expression"
|
|
349
|
+
};
|
|
350
|
+
return {
|
|
351
|
+
column: value.name,
|
|
352
|
+
kind: "column",
|
|
353
|
+
...value.indexConfig?.order === "desc" ? { desc: true } : {}
|
|
354
|
+
};
|
|
355
|
+
};
|
|
356
|
+
var indexNotes = (config) => {
|
|
357
|
+
const nulls = config.columns.filter(isIndexedColumn).filter((column) => column.indexConfig?.nulls === "first").map((column) => `${column.name} nulls first`);
|
|
358
|
+
const options = Object.entries(config.with ?? {}).map(([key, value]) => `${key} = ${value}`);
|
|
359
|
+
return present([
|
|
360
|
+
config.where ? `Where ${sqlText(config.where)}` : undefined,
|
|
361
|
+
nulls.length > 0 ? `Sorted with ${nulls.join(", ")}` : undefined,
|
|
362
|
+
options.length > 0 ? `With ${options.join(", ")}` : undefined,
|
|
363
|
+
config.concurrently === true ? "Built concurrently" : undefined,
|
|
364
|
+
config.algorithm ? `Algorithm ${config.algorithm}` : undefined,
|
|
365
|
+
config.lock ? `Lock ${config.lock}` : undefined
|
|
366
|
+
]);
|
|
367
|
+
};
|
|
368
|
+
var methodOf = (config) => {
|
|
369
|
+
if (config.method !== undefined)
|
|
370
|
+
return config.method === "btree" ? undefined : config.method;
|
|
371
|
+
if (config.using === undefined)
|
|
372
|
+
return;
|
|
373
|
+
return isSql(config.using) ? sqlText(config.using) : config.using;
|
|
374
|
+
};
|
|
375
|
+
var indexOf = (config) => {
|
|
376
|
+
const notes = indexNotes(config);
|
|
377
|
+
const method = methodOf(config);
|
|
378
|
+
const index = {
|
|
379
|
+
parts: config.columns.map(partOf),
|
|
380
|
+
...config.unique === true ? { unique: true } : {},
|
|
381
|
+
...method ? { type: method } : {},
|
|
382
|
+
...config.name ? { name: config.name } : {},
|
|
383
|
+
...notes.length > 0 ? { note: notes.join(`
|
|
384
|
+
`) } : {}
|
|
385
|
+
};
|
|
386
|
+
return index;
|
|
387
|
+
};
|
|
388
|
+
var roleName = (role) => typeof role === "object" && role !== null && ("name" in role) ? `${role.name}` : `${role}`;
|
|
389
|
+
var roleText = (roles) => (Array.isArray(roles) ? roles : [roles]).filter((role) => role !== undefined).map(roleName).join(", ");
|
|
390
|
+
var policyLine = (policy) => present([
|
|
391
|
+
`Policy ${policy.name}`,
|
|
392
|
+
policy.as && policy.as !== "permissive" ? policy.as : undefined,
|
|
393
|
+
`for ${policy.for ?? "all"}`,
|
|
394
|
+
policy.to === undefined ? undefined : `to ${roleText(policy.to)}`,
|
|
395
|
+
policy.using ? `using (${sqlText(policy.using)})` : undefined,
|
|
396
|
+
policy.withCheck ? `with check (${sqlText(policy.withCheck)})` : undefined
|
|
397
|
+
]).join(" ");
|
|
398
|
+
var tableNote = (config) => {
|
|
399
|
+
const policies = config.policies ?? [];
|
|
400
|
+
const secured = config.enableRLS === true || policies.length > 0;
|
|
401
|
+
const lines = present([
|
|
402
|
+
secured ? "Row-level security is on" : undefined,
|
|
403
|
+
...policies.map(policyLine)
|
|
404
|
+
]);
|
|
405
|
+
return lines.length > 0 ? lines.join(`
|
|
406
|
+
`) : undefined;
|
|
407
|
+
};
|
|
408
|
+
var columnParts = (columns) => columns.map((column) => ({ column: column.name, kind: "column" }));
|
|
409
|
+
var keyIndexes = (config) => [
|
|
410
|
+
...config.primaryKeys.map((key) => {
|
|
411
|
+
const index = {
|
|
412
|
+
parts: columnParts(key.columns),
|
|
413
|
+
pk: true,
|
|
414
|
+
...key.name ? { name: key.name } : {}
|
|
415
|
+
};
|
|
416
|
+
return index;
|
|
417
|
+
}),
|
|
418
|
+
...config.uniqueConstraints.map((constraint) => {
|
|
419
|
+
const index = {
|
|
420
|
+
parts: columnParts(constraint.columns),
|
|
421
|
+
unique: true,
|
|
422
|
+
...constraint.name ? { name: constraint.name } : {},
|
|
423
|
+
...constraint.nullsNotDistinct === true ? { note: "Nulls not distinct" } : {}
|
|
424
|
+
};
|
|
425
|
+
return index;
|
|
426
|
+
})
|
|
427
|
+
];
|
|
428
|
+
var tableOf = (config) => {
|
|
429
|
+
const columns = config.columns.map((column) => columnOf(column, config));
|
|
430
|
+
const note = tableNote(config);
|
|
431
|
+
const table = {
|
|
432
|
+
checks: config.checks.map((check) => ({
|
|
433
|
+
expression: sqlText(check.value),
|
|
434
|
+
name: check.name
|
|
435
|
+
})),
|
|
436
|
+
columns: columns.map((entry) => entry.column),
|
|
437
|
+
indexes: [
|
|
438
|
+
...keyIndexes(config),
|
|
439
|
+
...config.indexes.map((index) => indexOf(index.config))
|
|
440
|
+
],
|
|
441
|
+
name: config.name,
|
|
442
|
+
...inSchema(config.schema),
|
|
443
|
+
...note ? { note } : {}
|
|
444
|
+
};
|
|
445
|
+
return {
|
|
446
|
+
enums: columns.map((entry) => entry.enum).filter((entry) => entry !== undefined),
|
|
447
|
+
table
|
|
448
|
+
};
|
|
449
|
+
};
|
|
450
|
+
var sortedColumns = (index) => index.parts.map((part) => part.kind === "column" ? part.column : "").sort().join("\x00");
|
|
451
|
+
var coversAll = (index) => index.pk === true || index.unique === true && index.note?.startsWith("Where") !== true;
|
|
452
|
+
var isUniqueSet = (table, columns) => {
|
|
453
|
+
const [only] = columns;
|
|
454
|
+
const single = columns.length === 1 ? table.columns.find((entry) => entry.name === only) : undefined;
|
|
455
|
+
if (single?.pk === true || single?.unique === true)
|
|
456
|
+
return true;
|
|
457
|
+
const wanted = [...columns].sort().join("\x00");
|
|
458
|
+
return table.indexes.some((index) => coversAll(index) && index.parts.every((part) => part.kind === "column") && sortedColumns(index) === wanted);
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
// src/drizzle/views.ts
|
|
462
|
+
import {
|
|
463
|
+
Column,
|
|
464
|
+
getColumnTable as getColumnTable2,
|
|
465
|
+
is as is3,
|
|
466
|
+
SQL as SQL2,
|
|
467
|
+
Subquery,
|
|
468
|
+
Table,
|
|
469
|
+
View
|
|
470
|
+
} from "drizzle-orm";
|
|
471
|
+
var VIEW_COLOR = "#64748b";
|
|
472
|
+
var isAliased = (value) => typeof value === "object" && value !== null && ("fieldAlias" in value) && ("sql" in value) && is3(value.sql, SQL2);
|
|
473
|
+
var childrenOf = (chunk) => {
|
|
474
|
+
if (Array.isArray(chunk))
|
|
475
|
+
return chunk;
|
|
476
|
+
if (isAliased(chunk))
|
|
477
|
+
return [chunk.sql];
|
|
478
|
+
if (is3(chunk, SQL2))
|
|
479
|
+
return chunk.queryChunks;
|
|
480
|
+
if (is3(chunk, Subquery))
|
|
481
|
+
return [chunk._.sql];
|
|
482
|
+
return [];
|
|
483
|
+
};
|
|
484
|
+
var sourceOf = (chunk) => {
|
|
485
|
+
if (is3(chunk, Table) || is3(chunk, View))
|
|
486
|
+
return chunk;
|
|
487
|
+
if (is3(chunk, Column))
|
|
488
|
+
return getColumnTable2(chunk);
|
|
489
|
+
return;
|
|
490
|
+
};
|
|
491
|
+
var sourcesOf = (fragment) => {
|
|
492
|
+
const found = new Set;
|
|
493
|
+
const pending = [fragment];
|
|
494
|
+
while (pending.length > 0) {
|
|
495
|
+
const chunk = pending.pop();
|
|
496
|
+
const source = sourceOf(chunk);
|
|
497
|
+
if (source)
|
|
498
|
+
found.add(source);
|
|
499
|
+
else
|
|
500
|
+
pending.push(...childrenOf(chunk));
|
|
501
|
+
}
|
|
502
|
+
return found;
|
|
503
|
+
};
|
|
504
|
+
var SERIALS = {
|
|
505
|
+
bigserial: "bigint",
|
|
506
|
+
serial: "integer",
|
|
507
|
+
smallserial: "smallint"
|
|
508
|
+
};
|
|
509
|
+
var readType = (type) => SERIALS[type] ?? type;
|
|
510
|
+
var originOf = (column, sources) => {
|
|
511
|
+
const owners = sources.filter((source) => source.columns.includes(column));
|
|
512
|
+
const [owner] = owners;
|
|
513
|
+
return owners.length === 1 && owner ? `${owner.table}.${column}` : column;
|
|
514
|
+
};
|
|
515
|
+
var columnOfField = (name, field, sources) => {
|
|
516
|
+
if (is3(field, Column)) {
|
|
517
|
+
const origin = originOf(field.name, sources);
|
|
518
|
+
const picked = {
|
|
519
|
+
name,
|
|
520
|
+
...origin === name ? {} : { note: `From ${origin}` },
|
|
521
|
+
type: readType(field.getSQLType())
|
|
522
|
+
};
|
|
523
|
+
return picked;
|
|
524
|
+
}
|
|
525
|
+
const expression = isAliased(field) ? field.sql : field;
|
|
526
|
+
const computed = {
|
|
527
|
+
name,
|
|
528
|
+
...is3(expression, SQL2) ? { note: `Computed: ${sqlText(expression)}` } : {},
|
|
529
|
+
type: "unknown"
|
|
530
|
+
};
|
|
531
|
+
return computed;
|
|
532
|
+
};
|
|
533
|
+
var MAX_QUERY = 600;
|
|
534
|
+
var shortened = (query) => query.length > MAX_QUERY ? `${query.slice(0, MAX_QUERY)}\u2026` : query;
|
|
535
|
+
var viewNote = (config, kind) => {
|
|
536
|
+
if (config.isExisting)
|
|
537
|
+
return `${kind} that already exists in the database; its query lives there`;
|
|
538
|
+
if (!config.query)
|
|
539
|
+
return kind;
|
|
540
|
+
return `${kind}:
|
|
541
|
+
${shortened(sqlTextQualified(config.query))}`;
|
|
542
|
+
};
|
|
543
|
+
var viewOf = (config, materialized, sources) => {
|
|
544
|
+
const view = {
|
|
545
|
+
checks: [],
|
|
546
|
+
columns: Object.entries(config.selectedFields).map(([name, field]) => columnOfField(name, field, sources)),
|
|
547
|
+
headerColor: VIEW_COLOR,
|
|
548
|
+
indexes: [],
|
|
549
|
+
name: config.name,
|
|
550
|
+
note: viewNote(config, materialized ? "Materialized view" : "View"),
|
|
551
|
+
...config.schema ? { schema: config.schema } : {}
|
|
552
|
+
};
|
|
553
|
+
return view;
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
// src/drizzle/index.ts
|
|
557
|
+
var isRole = (value) => is4(value, pg2.PgRole) || is4(value, cockroach2.CockroachRole);
|
|
558
|
+
var isObject = (value) => typeof value === "object" && value !== null;
|
|
559
|
+
var classifyInDialects = (value, found) => DIALECTS.some((dialect) => {
|
|
560
|
+
const table = dialect.tableOf(value);
|
|
561
|
+
if (table && isObject(value))
|
|
562
|
+
found.tables.push({
|
|
563
|
+
config: table,
|
|
564
|
+
databaseType: dialect.databaseType,
|
|
565
|
+
object: value
|
|
566
|
+
});
|
|
567
|
+
const enumConfig = dialect.enumOf(value);
|
|
568
|
+
if (enumConfig)
|
|
569
|
+
found.enums.push(enumConfig);
|
|
570
|
+
const materialized = dialect.materializedViewOf(value);
|
|
571
|
+
const view = materialized ?? dialect.viewOf(value);
|
|
572
|
+
if (view && isObject(value))
|
|
573
|
+
found.views.push({
|
|
574
|
+
config: view,
|
|
575
|
+
materialized: materialized !== undefined,
|
|
576
|
+
object: value
|
|
577
|
+
});
|
|
578
|
+
const sequence = dialect.sequenceOf(value);
|
|
579
|
+
if (sequence)
|
|
580
|
+
found.sequences.push(sequence);
|
|
581
|
+
return (table ?? enumConfig ?? view ?? sequence) !== undefined;
|
|
582
|
+
});
|
|
583
|
+
var classify = (value, found) => {
|
|
584
|
+
if (isRelationsV2(value))
|
|
585
|
+
found.links.push(...linksFromV2(value));
|
|
586
|
+
else if (isRelationsV1(value))
|
|
587
|
+
found.links.push(...linksFromV1(value));
|
|
588
|
+
else if (isRole(value))
|
|
589
|
+
found.roles.push(value.name);
|
|
590
|
+
else
|
|
591
|
+
classifyInDialects(value, found);
|
|
592
|
+
};
|
|
593
|
+
var placeOf = (schema, table) => {
|
|
594
|
+
const place = { ...schema ? { schema } : {}, table };
|
|
595
|
+
return place;
|
|
596
|
+
};
|
|
597
|
+
var keyOf = (entry) => `${entry.schema ?? ""}.${entry.name}`;
|
|
598
|
+
var ACTIONS = {
|
|
599
|
+
cascade: "cascade",
|
|
600
|
+
"no action": undefined,
|
|
601
|
+
restrict: "restrict",
|
|
602
|
+
"set default": "set default",
|
|
603
|
+
"set null": "set null"
|
|
604
|
+
};
|
|
605
|
+
var actionOf = (action) => action === undefined ? undefined : ACTIONS[action.toLowerCase()];
|
|
606
|
+
var foreignKeyRefs = (entry, lookup, tables) => entry.config.foreignKeys.flatMap((key) => {
|
|
607
|
+
const reference = key.reference();
|
|
608
|
+
const target = lookup(reference.foreignTable);
|
|
609
|
+
if (!target)
|
|
610
|
+
return [];
|
|
611
|
+
const from = placeOf(entry.config.schema, entry.config.name);
|
|
612
|
+
const columns = reference.columns.map((column) => column.name);
|
|
613
|
+
const table = tables.get(keyOf({ name: from.table, schema: from.schema }));
|
|
614
|
+
const onDelete = actionOf(key.onDelete);
|
|
615
|
+
const onUpdate = actionOf(key.onUpdate);
|
|
616
|
+
const ref = {
|
|
617
|
+
from: { ...from, columns },
|
|
618
|
+
...reference.name ? { name: reference.name } : {},
|
|
619
|
+
...onDelete ? { onDelete } : {},
|
|
620
|
+
...onUpdate ? { onUpdate } : {},
|
|
621
|
+
relation: table && isUniqueSet(table, columns) ? "-" : ">",
|
|
622
|
+
to: {
|
|
623
|
+
...target,
|
|
624
|
+
columns: reference.foreignColumns.map((column) => column.name)
|
|
625
|
+
}
|
|
626
|
+
};
|
|
627
|
+
return [ref];
|
|
628
|
+
});
|
|
629
|
+
var sequenceLine = (sequence) => {
|
|
630
|
+
const options = Object.entries(sequence.seqOptions ?? {}).filter(([, value]) => value !== undefined).map(([key, value]) => `${key} ${value}`);
|
|
631
|
+
const name = `${sequence.schema ? `${sequence.schema}.` : ""}${sequence.seqName ?? "sequence"}`;
|
|
632
|
+
return options.length > 0 ? `${name}: ${options.join(", ")}` : name;
|
|
633
|
+
};
|
|
634
|
+
var groupNamer = (groups) => {
|
|
635
|
+
if (groups === "schema")
|
|
636
|
+
return (table) => table.schema ?? "public";
|
|
637
|
+
return (table) => groups({
|
|
638
|
+
name: table.name,
|
|
639
|
+
...table.schema ? { schema: table.schema } : {}
|
|
640
|
+
});
|
|
641
|
+
};
|
|
642
|
+
var groupsOf = (tables, groups) => {
|
|
643
|
+
const nameOf = groupNamer(groups);
|
|
644
|
+
const byName = new Map;
|
|
645
|
+
tables.forEach((table) => {
|
|
646
|
+
const name = nameOf(table);
|
|
647
|
+
if (name !== undefined)
|
|
648
|
+
byName.set(name, [
|
|
649
|
+
...byName.get(name) ?? [],
|
|
650
|
+
placeOf(table.schema, table.name)
|
|
651
|
+
]);
|
|
652
|
+
});
|
|
653
|
+
return [...byName].map(([name, members]) => {
|
|
654
|
+
const group = { name, tables: members };
|
|
655
|
+
return group;
|
|
656
|
+
});
|
|
657
|
+
};
|
|
658
|
+
var enumOf = (config) => {
|
|
659
|
+
const entry = {
|
|
660
|
+
name: config.enumName,
|
|
661
|
+
...config.schema ? { schema: config.schema } : {},
|
|
662
|
+
values: config.enumValues.map((name) => ({ name }))
|
|
663
|
+
};
|
|
664
|
+
return entry;
|
|
665
|
+
};
|
|
666
|
+
var viewSources = (view, lookup) => [
|
|
667
|
+
...sourcesOf([
|
|
668
|
+
view.config.query,
|
|
669
|
+
Object.values(view.config.selectedFields)
|
|
670
|
+
])
|
|
671
|
+
].filter((source) => source !== view.object && lookup(source) !== undefined);
|
|
672
|
+
var addViews = (model, found, lookup, columnsOf) => found.views.forEach((view) => {
|
|
673
|
+
const downstream = placeOf(view.config.schema, view.config.name);
|
|
674
|
+
const sources = viewSources(view, lookup);
|
|
675
|
+
model.tables.push(viewOf(view.config, view.materialized, sources.map((source) => ({
|
|
676
|
+
columns: columnsOf(source),
|
|
677
|
+
table: lookup(source)?.table ?? ""
|
|
678
|
+
}))));
|
|
679
|
+
sources.forEach((source) => {
|
|
680
|
+
const upstream = lookup(source);
|
|
681
|
+
if (upstream)
|
|
682
|
+
model.deps.push({ downstream, upstream });
|
|
683
|
+
});
|
|
684
|
+
});
|
|
685
|
+
var addNotes = (model, found) => {
|
|
686
|
+
if (found.sequences.length > 0)
|
|
687
|
+
model.notes.push({
|
|
688
|
+
content: `Sequences
|
|
689
|
+
${found.sequences.map(sequenceLine).join(`
|
|
690
|
+
`)}`,
|
|
691
|
+
name: "sequences"
|
|
692
|
+
});
|
|
693
|
+
if (found.roles.length > 0)
|
|
694
|
+
model.notes.push({
|
|
695
|
+
content: `Roles
|
|
696
|
+
${found.roles.join(`
|
|
697
|
+
`)}`,
|
|
698
|
+
name: "roles"
|
|
699
|
+
});
|
|
700
|
+
};
|
|
701
|
+
var collect = (schema, relations) => {
|
|
702
|
+
const found = {
|
|
703
|
+
enums: [],
|
|
704
|
+
links: [],
|
|
705
|
+
roles: [],
|
|
706
|
+
sequences: [],
|
|
707
|
+
tables: [],
|
|
708
|
+
views: []
|
|
709
|
+
};
|
|
710
|
+
const values = new Set(Array.isArray(schema) ? schema : Object.values(schema));
|
|
711
|
+
if (relations !== undefined)
|
|
712
|
+
values.add(relations);
|
|
713
|
+
values.forEach((value) => classify(value, found));
|
|
714
|
+
return found;
|
|
715
|
+
};
|
|
716
|
+
var fromDrizzle = (schema, options = {}) => {
|
|
717
|
+
const found = collect(schema, options.relations);
|
|
718
|
+
const places = new Map;
|
|
719
|
+
[...found.tables, ...found.views].forEach((entry) => places.set(entry.object, placeOf(entry.config.schema, entry.config.name)));
|
|
720
|
+
const lookup = (table) => places.get(table);
|
|
721
|
+
const enums = new Map(found.enums.map((entry) => [
|
|
722
|
+
keyOf({ name: entry.enumName, schema: entry.schema }),
|
|
723
|
+
enumOf(entry)
|
|
724
|
+
]));
|
|
725
|
+
const built = found.tables.map((entry) => tableOf(entry.config));
|
|
726
|
+
built.flatMap((result) => result.enums).forEach((entry) => {
|
|
727
|
+
if (!enums.has(keyOf(entry)))
|
|
728
|
+
enums.set(keyOf(entry), entry);
|
|
729
|
+
});
|
|
730
|
+
const tables = built.map((result) => result.table);
|
|
731
|
+
const byPlace = new Map(tables.map((table) => [keyOf(table), table]));
|
|
732
|
+
const byObject = new Map(found.tables.map((entry, index) => [entry.object, tables[index]]));
|
|
733
|
+
const types = new Set(found.tables.map((entry) => entry.databaseType));
|
|
734
|
+
const [databaseType] = types;
|
|
735
|
+
const model = emptyModel({
|
|
736
|
+
...options.project,
|
|
737
|
+
...types.size === 1 && databaseType ? { databaseType } : {}
|
|
738
|
+
});
|
|
739
|
+
model.enums.push(...enums.values());
|
|
740
|
+
model.tables.push(...tables);
|
|
741
|
+
model.refs.push(...found.tables.flatMap((entry) => foreignKeyRefs(entry, lookup, byPlace)));
|
|
742
|
+
applyRelations({
|
|
743
|
+
links: found.links,
|
|
744
|
+
lookup,
|
|
745
|
+
refs: model.refs,
|
|
746
|
+
isUnique: (end) => {
|
|
747
|
+
const table = byPlace.get(keyOf({ name: end.table, schema: end.schema }));
|
|
748
|
+
return table ? isUniqueSet(table, end.columns) : false;
|
|
749
|
+
}
|
|
750
|
+
});
|
|
751
|
+
addViews(model, found, lookup, (table) => byObject.get(table)?.columns.map((column) => column.name) ?? []);
|
|
752
|
+
addNotes(model, found);
|
|
753
|
+
if (options.groups)
|
|
754
|
+
model.tableGroups.push(...groupsOf(model.tables, options.groups));
|
|
755
|
+
return model;
|
|
756
|
+
};
|
|
757
|
+
export {
|
|
758
|
+
fromDrizzle
|
|
759
|
+
};
|