@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.
@@ -0,0 +1,666 @@
1
+ // @bun
2
+ // src/postgres/index.ts
3
+ import { Buffer } from "buffer";
4
+
5
+ // src/model.ts
6
+ var emptyModel = (project) => ({
7
+ deps: [],
8
+ enums: [],
9
+ notes: [],
10
+ ...project ? { project } : {},
11
+ records: [],
12
+ refs: [],
13
+ tableGroups: [],
14
+ tables: []
15
+ });
16
+
17
+ // src/postgres/queries.ts
18
+ var SCHEMAS = `
19
+ select nspname as name
20
+ from pg_namespace
21
+ where nspname not in ('pg_catalog', 'information_schema', 'pg_toast')
22
+ and nspname not like 'pg_temp_%' and nspname not like 'pg_toast_temp_%'
23
+ order by nspname`;
24
+ var ENUMS = `
25
+ select n.nspname as schema, t.typname as name,
26
+ array_agg(e.enumlabel order by e.enumsortorder) as "values"
27
+ from pg_type t
28
+ join pg_namespace n on n.oid = t.typnamespace
29
+ join pg_enum e on e.enumtypid = t.oid
30
+ where n.nspname = any($1)
31
+ group by n.nspname, t.typname
32
+ order by n.nspname, t.typname`;
33
+ var RELATIONS = `
34
+ select c.oid::int as id, n.nspname as schema, c.relname as name, c.relkind as kind,
35
+ obj_description(c.oid, 'pg_class') as comment,
36
+ c.relrowsecurity as rls, c.relforcerowsecurity as force_rls,
37
+ case when c.relkind = 'p' then pg_get_partkeydef(c.oid) end as partition_key,
38
+ case when c.relkind in ('v', 'm') then pg_get_viewdef(c.oid, true) end as definition,
39
+ (select count(*)::int from pg_inherits i where i.inhparent = c.oid) as partitions
40
+ from pg_class c
41
+ join pg_namespace n on n.oid = c.relnamespace
42
+ where n.nspname = any($1) and c.relkind in ('r', 'p', 'v', 'm', 'f') and not c.relispartition
43
+ order by n.nspname, c.relname`;
44
+ var COLUMNS = `
45
+ select a.attrelid::int as table_id, a.attnum::int as position, a.attname as name,
46
+ format_type(a.atttypid, a.atttypmod) as type,
47
+ a.attnotnull as not_null, a.attidentity as identity, a.attgenerated as generated,
48
+ pg_get_expr(d.adbin, d.adrelid) as "default",
49
+ col_description(a.attrelid, a.attnum) as comment,
50
+ case when t.typtype = 'e' then tn.nspname
51
+ when et.typtype = 'e' then etn.nspname end as enum_schema,
52
+ coalesce(case when t.typtype = 'e' then t.typname end,
53
+ case when et.typtype = 'e' then et.typname || '[]' end) as enum_type,
54
+ pg_get_serial_sequence(quote_ident(n.nspname) || '.' || quote_ident(c.relname), a.attname) as serial_sequence
55
+ from pg_attribute a
56
+ join pg_class c on c.oid = a.attrelid
57
+ join pg_namespace n on n.oid = c.relnamespace
58
+ join pg_type t on t.oid = a.atttypid
59
+ join pg_namespace tn on tn.oid = t.typnamespace
60
+ left join pg_type et on et.oid = t.typelem and t.typcategory = 'A'
61
+ left join pg_namespace etn on etn.oid = et.typnamespace
62
+ left join pg_attrdef d on d.adrelid = a.attrelid and d.adnum = a.attnum
63
+ where n.nspname = any($1) and c.relkind in ('r', 'p', 'v', 'm', 'f') and not c.relispartition
64
+ and a.attnum > 0 and not a.attisdropped
65
+ order by a.attrelid, a.attnum`;
66
+ var CONSTRAINTS = `
67
+ select con.conrelid::int as table_id, con.conname as name, con.contype as kind,
68
+ (select array_agg(k::int order by o) from unnest(con.conkey) with ordinality as u(k, o)) as columns,
69
+ con.confrelid::int as foreign_table_id,
70
+ (select array_agg(k::int order by o) from unnest(con.confkey) with ordinality as u(k, o)) as foreign_columns,
71
+ con.confdeltype as on_delete, con.confupdtype as on_update,
72
+ pg_get_constraintdef(con.oid, true) as definition,
73
+ case when con.contype = 'c' then pg_get_expr(con.conbin, con.conrelid, true) end as check_expression,
74
+ obj_description(con.oid, 'pg_constraint') as comment,
75
+ con.condeferrable as deferrable, con.condeferred as deferred,
76
+ /* Read through jsonb so a Postgres older than 15, without the column,
77
+ * reads null instead of failing. */
78
+ (to_jsonb(con) ->> 'connullsnotdistinct')::boolean as nulls_not_distinct
79
+ from pg_constraint con
80
+ join pg_namespace n on n.oid = con.connamespace
81
+ where n.nspname = any($1) and con.contype in ('p', 'u', 'f', 'c', 'x') and con.conrelid <> 0
82
+ order by con.conrelid, con.contype, con.conname`;
83
+ var INDEXES = `
84
+ select i.indrelid::int as table_id, ic.relname as name, am.amname as method,
85
+ i.indisunique as is_unique, i.indnkeyatts::int as key_count, i.indnatts::int as count,
86
+ (select array_agg(pg_get_indexdef(i.indexrelid, k, true) order by k)
87
+ from generate_series(1, i.indnatts) as k) as parts,
88
+ (select array_agg(i.indkey[k - 1]::int order by k)
89
+ from generate_series(1, i.indnatts) as k) as attnums,
90
+ (select array_agg((i.indoption[k - 1] & 1) = 1 order by k)
91
+ from generate_series(1, i.indnkeyatts) as k) as descending,
92
+ pg_get_expr(i.indpred, i.indrelid, true) as predicate,
93
+ obj_description(i.indexrelid, 'pg_class') as comment
94
+ from pg_index i
95
+ join pg_class ic on ic.oid = i.indexrelid
96
+ join pg_class tc on tc.oid = i.indrelid
97
+ join pg_namespace n on n.oid = tc.relnamespace
98
+ join pg_am am on am.oid = ic.relam
99
+ where n.nspname = any($1)
100
+ and not exists (select 1 from pg_constraint con where con.conindid = i.indexrelid and con.contype in ('p', 'u', 'x'))
101
+ order by i.indrelid, ic.relname`;
102
+ var VIEW_SOURCES = `
103
+ select distinct v.oid::int as view_id, d.refobjid::int as source_id
104
+ from pg_class v
105
+ join pg_namespace n on n.oid = v.relnamespace
106
+ join pg_rewrite r on r.ev_class = v.oid
107
+ join pg_depend d on d.objid = r.oid and d.classid = 'pg_rewrite'::regclass
108
+ and d.refclassid = 'pg_class'::regclass and d.refobjid <> v.oid
109
+ where n.nspname = any($1) and v.relkind in ('v', 'm')`;
110
+ var POLICIES = `
111
+ select p.polrelid::int as table_id, p.polname as name, p.polpermissive as permissive,
112
+ p.polcmd as command,
113
+ (select array_agg(case when r = 0 then 'public' else pg_get_userbyid(r) end) from unnest(p.polroles) as r) as roles,
114
+ pg_get_expr(p.polqual, p.polrelid, true) as "using",
115
+ pg_get_expr(p.polwithcheck, p.polrelid, true) as with_check
116
+ from pg_policy p
117
+ join pg_class c on c.oid = p.polrelid
118
+ join pg_namespace n on n.oid = c.relnamespace
119
+ where n.nspname = any($1)
120
+ order by p.polrelid, p.polname`;
121
+ var SEQUENCES = `
122
+ select n.nspname as schema, c.relname as name, s.seqstart::text as start,
123
+ s.seqincrement::text as increment
124
+ from pg_sequence s
125
+ join pg_class c on c.oid = s.seqrelid
126
+ join pg_namespace n on n.oid = c.relnamespace
127
+ where n.nspname = any($1)
128
+ and not exists (select 1 from pg_depend d where d.objid = c.oid and d.deptype in ('a', 'i'))
129
+ order by n.nspname, c.relname`;
130
+ var SCHEMA_COMMENTS = `
131
+ select nspname as name, obj_description(oid, 'pg_namespace') as comment
132
+ from pg_namespace where nspname = any($1)`;
133
+
134
+ // src/postgres/rows.ts
135
+ var optional = (key, value) => {
136
+ const entry = {};
137
+ if (value !== undefined)
138
+ entry[key] = value;
139
+ return entry;
140
+ };
141
+ var text = (value) => {
142
+ if (value === null || value === undefined)
143
+ return;
144
+ return typeof value === "string" ? value : `${value}`;
145
+ };
146
+ var integer = (value) => {
147
+ if (typeof value === "number")
148
+ return value;
149
+ const parsed = Number.parseInt(text(value) ?? "", 10);
150
+ return Number.isNaN(parsed) ? 0 : parsed;
151
+ };
152
+ var flag = (value) => value === true || value === "t" || value === "true";
153
+ var ARRAY_ITEM = /"((?:[^"\\]|\\.)*)"|([^,{}]+)/gu;
154
+ var list = (value) => {
155
+ if (Array.isArray(value))
156
+ return value.map((entry) => text(entry) ?? "");
157
+ const raw = text(value);
158
+ if (raw === undefined)
159
+ return [];
160
+ return [...raw.matchAll(ARRAY_ITEM)].map((match) => match[1] === undefined ? match[2] ?? "" : match[1].replace(/\\(.)/gu, "$1"));
161
+ };
162
+ var integers = (value) => list(value).map(integer);
163
+ var flags = (value) => list(value).map(flag);
164
+ var relationRow = (row) => {
165
+ const result = {
166
+ forceRls: flag(row.force_rls),
167
+ id: integer(row.id),
168
+ kind: text(row.kind) ?? "",
169
+ name: text(row.name) ?? "",
170
+ partitions: integer(row.partitions),
171
+ rls: flag(row.rls),
172
+ schema: text(row.schema) ?? "",
173
+ ...optional("comment", text(row.comment)),
174
+ ...optional("definition", text(row.definition)),
175
+ ...optional("partitionKey", text(row.partition_key))
176
+ };
177
+ return result;
178
+ };
179
+ var setting = (value) => {
180
+ const raw = text(value);
181
+ return raw === "" ? undefined : raw;
182
+ };
183
+ var columnRow = (row) => {
184
+ const result = {
185
+ name: text(row.name) ?? "",
186
+ notNull: flag(row.not_null),
187
+ position: integer(row.position),
188
+ tableId: integer(row.table_id),
189
+ type: text(row.type) ?? "",
190
+ ...optional("comment", text(row.comment)),
191
+ ...optional("default", text(row.default)),
192
+ ...optional("enumSchema", text(row.enum_schema)),
193
+ ...optional("enumType", text(row.enum_type)),
194
+ ...optional("generated", setting(row.generated)),
195
+ ...optional("identity", setting(row.identity)),
196
+ ...optional("serialSequence", text(row.serial_sequence))
197
+ };
198
+ return result;
199
+ };
200
+ var constraintRow = (row) => {
201
+ const result = {
202
+ columns: integers(row.columns),
203
+ deferrable: flag(row.deferrable),
204
+ deferred: flag(row.deferred),
205
+ definition: text(row.definition) ?? "",
206
+ foreignColumns: integers(row.foreign_columns),
207
+ foreignTableId: integer(row.foreign_table_id),
208
+ kind: text(row.kind) ?? "",
209
+ name: text(row.name) ?? "",
210
+ nullsNotDistinct: flag(row.nulls_not_distinct),
211
+ onDelete: text(row.on_delete) ?? "a",
212
+ onUpdate: text(row.on_update) ?? "a",
213
+ tableId: integer(row.table_id),
214
+ ...optional("checkExpression", text(row.check_expression)),
215
+ ...optional("comment", text(row.comment))
216
+ };
217
+ return result;
218
+ };
219
+ var indexRow = (row) => {
220
+ const result = {
221
+ attnums: integers(row.attnums),
222
+ descending: flags(row.descending),
223
+ keyCount: integer(row.key_count),
224
+ method: text(row.method) ?? "btree",
225
+ name: text(row.name) ?? "",
226
+ parts: list(row.parts),
227
+ tableId: integer(row.table_id),
228
+ unique: flag(row.is_unique),
229
+ ...optional("comment", text(row.comment)),
230
+ ...optional("predicate", text(row.predicate))
231
+ };
232
+ return result;
233
+ };
234
+ var policyRow = (row) => {
235
+ const result = {
236
+ command: text(row.command) ?? "*",
237
+ name: text(row.name) ?? "",
238
+ permissive: flag(row.permissive),
239
+ roles: list(row.roles),
240
+ tableId: integer(row.table_id),
241
+ ...optional("using", text(row.using)),
242
+ ...optional("withCheck", text(row.with_check))
243
+ };
244
+ return result;
245
+ };
246
+ var namedRow = (row) => {
247
+ const result = {
248
+ name: text(row.name) ?? "",
249
+ schema: text(row.schema) ?? "",
250
+ ...optional("comment", text(row.comment))
251
+ };
252
+ return result;
253
+ };
254
+ var enumRow = (row) => ({
255
+ name: text(row.name) ?? "",
256
+ schema: text(row.schema) ?? "",
257
+ values: list(row.values)
258
+ });
259
+ var sequenceRow = (row) => ({
260
+ increment: text(row.increment) ?? "1",
261
+ name: text(row.name) ?? "",
262
+ schema: text(row.schema) ?? "",
263
+ start: text(row.start) ?? "1"
264
+ });
265
+ var viewSourceRow = (row) => ({
266
+ sourceId: integer(row.source_id),
267
+ viewId: integer(row.view_id)
268
+ });
269
+
270
+ // src/postgres/index.ts
271
+ var DEFAULT_SCHEMA = "public";
272
+ var inSchema = (schema) => schema === DEFAULT_SCHEMA ? {} : { schema };
273
+ var ACTIONS = {
274
+ a: undefined,
275
+ c: "cascade",
276
+ d: "set default",
277
+ n: "set null",
278
+ r: "restrict"
279
+ };
280
+ var QUOTED = /^'((?:[^']|'')*)'(?:::([\w\s."[\]()]+))?$/u;
281
+ var NUMBER = /^-?\d+(\.\d+)?$/u;
282
+ var NUMBER_CAST = /^\(?(-?\d+(?:\.\d+)?)\)?::[\w\s."]+$/u;
283
+ var NULL = /^NULL(::[\w\s."[\]()]+)?$/u;
284
+ var NUMERIC_TYPE = /^(smallint|integer|bigint|int[248]?|numeric(\(.*\))?|decimal(\(.*\))?|real|double precision|float[48]?)$/u;
285
+ var exactNumber = (digits) => {
286
+ const value = Number.parseFloat(digits);
287
+ const exact = digits.includes(".") || Number.isSafeInteger(value);
288
+ return exact ? value : undefined;
289
+ };
290
+ var numberDefault = (digits, expression) => {
291
+ const value = exactNumber(digits);
292
+ const result = value === undefined ? { kind: "expression", value: expression } : { kind: "number", value };
293
+ return result;
294
+ };
295
+ var quotedDefault = (inner, type, expression) => {
296
+ if (NUMERIC_TYPE.test(type) && NUMBER.test(inner))
297
+ return numberDefault(inner, expression);
298
+ const result = {
299
+ kind: "string",
300
+ value: inner.replaceAll("''", "'")
301
+ };
302
+ return result;
303
+ };
304
+ var parseDefault = (expression) => {
305
+ if (NULL.test(expression))
306
+ return { kind: "null" };
307
+ if (expression === "true" || expression === "false")
308
+ return {
309
+ kind: "boolean",
310
+ value: expression === "true"
311
+ };
312
+ if (NUMBER.test(expression))
313
+ return numberDefault(expression, expression);
314
+ const [, castDigits] = NUMBER_CAST.exec(expression) ?? [];
315
+ if (castDigits !== undefined)
316
+ return numberDefault(castDigits, expression);
317
+ const [, inner, type] = QUOTED.exec(expression) ?? [];
318
+ if (inner !== undefined)
319
+ return quotedDefault(inner, type ?? "", expression);
320
+ return { kind: "expression", value: expression };
321
+ };
322
+ var KINDS = {
323
+ f: "Foreign table",
324
+ m: "Materialized view",
325
+ p: "Partitioned table",
326
+ v: "View"
327
+ };
328
+ var VIEW_COLOR = "#64748b";
329
+ var POLICY_COMMANDS = {
330
+ "*": "all",
331
+ a: "insert",
332
+ d: "delete",
333
+ r: "select",
334
+ w: "update"
335
+ };
336
+ var joinNotes = (lines) => {
337
+ const present = lines.filter((line) => line !== undefined && line !== "");
338
+ return present.length > 0 ? present.join(`
339
+ `) : undefined;
340
+ };
341
+ var withNote = (note) => note === undefined ? {} : { note };
342
+ var identityNote = (identity) => {
343
+ if (identity === "a")
344
+ return "Identity, generated always";
345
+ if (identity === "d")
346
+ return "Identity, generated by default";
347
+ return;
348
+ };
349
+ var generatedNote = (row) => {
350
+ if (!row.generated || !row.default)
351
+ return;
352
+ const mode = row.generated === "v" ? "virtual" : "stored";
353
+ return `Generated always as (${row.default}), ${mode}`;
354
+ };
355
+ var isSerial = (row) => row.serialSequence !== undefined && row.default?.startsWith("nextval(") === true;
356
+ var enumSchema = (row) => row.enumType && row.enumSchema && row.enumSchema !== DEFAULT_SCHEMA ? { typeSchema: row.enumSchema } : {};
357
+ var columnOf = (row) => {
358
+ const serial = isSerial(row);
359
+ const fallback = row.default !== undefined && !serial && !row.generated ? parseDefault(row.default) : undefined;
360
+ const column = {
361
+ name: row.name,
362
+ type: row.enumType ?? row.type,
363
+ ...enumSchema(row),
364
+ ...serial || row.identity ? { increment: true } : {},
365
+ ...row.notNull ? { notNull: true } : {},
366
+ ...fallback ? { default: fallback } : {},
367
+ ...withNote(joinNotes([
368
+ row.comment,
369
+ identityNote(row.identity),
370
+ generatedNote(row)
371
+ ]))
372
+ };
373
+ return column;
374
+ };
375
+ var constraintNote = (row) => joinNotes([
376
+ row.comment,
377
+ row.deferrable ? `Deferrable${row.deferred ? ", initially deferred" : ""}` : undefined,
378
+ row.nullsNotDistinct ? "Nulls not distinct" : undefined
379
+ ]);
380
+ var defaultName = (row, table, columns) => {
381
+ if (row.kind === "p")
382
+ return `${table}_pkey`;
383
+ if (row.kind === "f")
384
+ return `${table}_${columns.join("_")}_fkey`;
385
+ return `${table}_${columns.join("_")}_key`;
386
+ };
387
+ var columnParts = (columns) => columns.map((column) => ({ column, kind: "column" }));
388
+ var markColumn = (column, primary) => {
389
+ if (!primary) {
390
+ column.unique = true;
391
+ return;
392
+ }
393
+ column.pk = true;
394
+ delete column.notNull;
395
+ };
396
+ var applyKey = (table, row, columns) => {
397
+ const primary = row.kind === "p";
398
+ const plain = row.name === defaultName(row, table.name, columns);
399
+ const note = constraintNote(row);
400
+ const [only] = columns;
401
+ const single = columns.length === 1 ? table.columns.find((column) => column.name === only) : undefined;
402
+ if (single && plain && note === undefined) {
403
+ markColumn(single, primary);
404
+ return;
405
+ }
406
+ const index = {
407
+ parts: columnParts(columns),
408
+ ...primary ? { pk: true } : { unique: true },
409
+ ...plain ? {} : { name: row.name },
410
+ ...withNote(note)
411
+ };
412
+ table.indexes.push(index);
413
+ };
414
+ var sortedColumns = (index) => index.parts.map((part) => part.kind === "column" ? part.column : "").sort().join("\x00");
415
+ var coversAll = (index) => index.pk === true || index.unique === true && index.note?.includes("Where ") !== true;
416
+ var isUniqueSet = (table, columns) => {
417
+ const [only] = columns;
418
+ const single = columns.length === 1 ? table.columns.find((column) => column.name === only) : undefined;
419
+ if (single?.pk === true || single?.unique === true)
420
+ return true;
421
+ const wanted = [...columns].sort().join("\x00");
422
+ return table.indexes.some((index) => coversAll(index) && index.parts.every((part) => part.kind === "column") && sortedColumns(index) === wanted);
423
+ };
424
+ var refOf = (row, context) => {
425
+ const source = context.relations.get(row.tableId);
426
+ const target = context.relations.get(row.foreignTableId);
427
+ const table = context.tables.get(row.tableId);
428
+ if (!source || !target || !table)
429
+ return;
430
+ const columns = row.columns.map((position) => context.columnName(source.id, position));
431
+ const onDelete = ACTIONS[row.onDelete];
432
+ const onUpdate = ACTIONS[row.onUpdate];
433
+ const ref = {
434
+ from: { columns, ...inSchema(source.schema), table: source.name },
435
+ ...row.name === defaultName(row, source.name, columns) ? {} : { name: row.name },
436
+ ...onDelete ? { onDelete } : {},
437
+ ...onUpdate ? { onUpdate } : {},
438
+ relation: isUniqueSet(table, columns) ? "-" : ">",
439
+ to: {
440
+ columns: row.foreignColumns.map((position) => context.columnName(target.id, position)),
441
+ ...inSchema(target.schema),
442
+ table: target.name
443
+ },
444
+ ...withNote(constraintNote(row))
445
+ };
446
+ return ref;
447
+ };
448
+ var partOf = (row, index, context) => {
449
+ const desc = row.descending[index] === true ? { desc: true } : {};
450
+ const position = row.attnums[index] ?? 0;
451
+ if (position > 0)
452
+ return {
453
+ column: context.columnName(row.tableId, position),
454
+ kind: "column",
455
+ ...desc
456
+ };
457
+ return {
458
+ expression: row.parts[index] ?? "",
459
+ kind: "expression",
460
+ ...desc
461
+ };
462
+ };
463
+ var indexOf = (row, context) => {
464
+ const included = row.parts.slice(row.keyCount);
465
+ const index = {
466
+ name: row.name,
467
+ parts: row.parts.slice(0, row.keyCount).map((_, position) => partOf(row, position, context)),
468
+ ...row.method === "btree" ? {} : { type: row.method },
469
+ ...row.unique ? { unique: true } : {},
470
+ ...withNote(joinNotes([
471
+ row.comment,
472
+ row.predicate ? `Where ${row.predicate}` : undefined,
473
+ included.length > 0 ? `Includes ${included.join(", ")}` : undefined
474
+ ]))
475
+ };
476
+ return index;
477
+ };
478
+ var policyLine = (row) => [
479
+ `Policy ${row.name}`,
480
+ row.permissive ? undefined : "restrictive",
481
+ `for ${POLICY_COMMANDS[row.command] ?? "all"}`,
482
+ `to ${row.roles.join(", ")}`,
483
+ row.using ? `using (${row.using})` : undefined,
484
+ row.withCheck ? `with check (${row.withCheck})` : undefined
485
+ ].filter((part) => part !== undefined).join(" ");
486
+ var MAX_DEFINITION = 600;
487
+ var kindLine = (relation) => {
488
+ const kind = KINDS[relation.kind];
489
+ const definition = relation.definition?.trim();
490
+ if (!kind || !definition)
491
+ return kind;
492
+ const shown = definition.length > MAX_DEFINITION ? `${definition.slice(0, MAX_DEFINITION)}\u2026` : definition;
493
+ return `${kind}:
494
+ ${shown}`;
495
+ };
496
+ var tableNote = (relation, policies) => joinNotes([
497
+ relation.comment,
498
+ kindLine(relation),
499
+ relation.partitionKey ? `Partitioned by ${relation.partitionKey}, ${relation.partitions} partitions` : undefined,
500
+ relation.rls ? `Row-level security is on${relation.forceRls ? ", for the owner too" : ""}` : undefined,
501
+ ...policies.map(policyLine)
502
+ ]);
503
+ var recordValue = (value) => {
504
+ if (value === null || value === undefined)
505
+ return null;
506
+ if (typeof value === "boolean" || typeof value === "string")
507
+ return value;
508
+ if (typeof value === "number")
509
+ return Number.isFinite(value) ? value : `${value}`;
510
+ if (typeof value === "bigint")
511
+ return `${value}`;
512
+ if (value instanceof Date)
513
+ return value.toISOString();
514
+ if (value instanceof Uint8Array)
515
+ return `\\x${Buffer.from(value).toString("hex")}`;
516
+ return JSON.stringify(value);
517
+ };
518
+ var quoteIdent = (name) => `"${name.replaceAll('"', '""')}"`;
519
+ var readRecords = (query, tables, limit) => Promise.all(tables.map(async (table) => {
520
+ const columns = table.columns.map((column) => column.name);
521
+ const from = `${quoteIdent(table.schema ?? DEFAULT_SCHEMA)}.${quoteIdent(table.name)}`;
522
+ const rows = await query(`select ${columns.map(quoteIdent).join(", ")} from ${from} limit ${Math.max(0, Math.floor(limit))}`, []);
523
+ const records = {
524
+ columns,
525
+ rows: rows.map((row) => columns.map((column) => recordValue(row[column]))),
526
+ ...table.schema ? { schema: table.schema } : {},
527
+ table: table.name
528
+ };
529
+ return records;
530
+ }));
531
+ var tableOf = (relation, columns, policies) => {
532
+ const view = relation.kind === "v" || relation.kind === "m";
533
+ const table = {
534
+ checks: [],
535
+ columns: columns.filter((row) => row.tableId === relation.id).map(columnOf),
536
+ ...view ? { headerColor: VIEW_COLOR } : {},
537
+ indexes: [],
538
+ name: relation.name,
539
+ ...inSchema(relation.schema),
540
+ ...withNote(tableNote(relation, policies.filter((row) => row.tableId === relation.id)))
541
+ };
542
+ return table;
543
+ };
544
+ var applyConstraints = (rows, context) => rows.filter((row) => {
545
+ const table = context.tables.get(row.tableId);
546
+ if (!table || row.kind === "f")
547
+ return table !== undefined;
548
+ const columns = row.columns.map((position) => context.columnName(row.tableId, position));
549
+ if (row.kind === "p" || row.kind === "u")
550
+ applyKey(table, row, columns);
551
+ if (row.kind === "c")
552
+ table.checks.push({
553
+ expression: (row.checkExpression ?? row.definition).replace(/^\((.*)\)$/su, "$1"),
554
+ name: row.name
555
+ });
556
+ if (row.kind === "x")
557
+ table.indexes.push({
558
+ name: row.name,
559
+ note: `Exclusion constraint: ${row.definition}`,
560
+ parts: columnParts(columns)
561
+ });
562
+ return false;
563
+ });
564
+ var sequenceNote = (rows) => ({
565
+ content: `Sequences
566
+ ${rows.map((row) => `${row.schema === DEFAULT_SCHEMA ? "" : `${row.schema}.`}${row.name}: start ${row.start}, increment ${row.increment}`).join(`
567
+ `)}`,
568
+ name: "sequences"
569
+ });
570
+ var readCatalogs = async (query, schemas) => {
571
+ const read = (sql) => query(sql, [schemas]);
572
+ const [
573
+ enums,
574
+ relations,
575
+ columns,
576
+ constraints,
577
+ indexes,
578
+ sources,
579
+ policies,
580
+ sequences,
581
+ comments
582
+ ] = await Promise.all([
583
+ read(ENUMS),
584
+ read(RELATIONS),
585
+ read(COLUMNS),
586
+ read(CONSTRAINTS),
587
+ read(INDEXES),
588
+ read(VIEW_SOURCES),
589
+ read(POLICIES),
590
+ read(SEQUENCES),
591
+ read(SCHEMA_COMMENTS)
592
+ ]);
593
+ return {
594
+ columns: columns.map(columnRow),
595
+ comments: comments.map(namedRow),
596
+ constraints: constraints.map(constraintRow),
597
+ enums: enums.map(enumRow),
598
+ indexes: indexes.map(indexRow),
599
+ policies: policies.map(policyRow),
600
+ relations: relations.map(relationRow),
601
+ sequences: sequences.map(sequenceRow),
602
+ sources: sources.map(viewSourceRow)
603
+ };
604
+ };
605
+ var schemasToRead = async (query, schemas) => schemas ?? (await query(SCHEMAS, [])).map((row) => namedRow(row).name);
606
+ var addDeps = (model, catalogs, relations) => catalogs.sources.forEach((row) => {
607
+ const view = relations.get(row.viewId);
608
+ const source = relations.get(row.sourceId);
609
+ if (view && source)
610
+ model.deps.push({
611
+ downstream: { ...inSchema(view.schema), table: view.name },
612
+ upstream: { ...inSchema(source.schema), table: source.name }
613
+ });
614
+ });
615
+ var addNotes = (model, catalogs) => {
616
+ if (catalogs.sequences.length > 0)
617
+ model.notes.push(sequenceNote(catalogs.sequences));
618
+ const described = catalogs.comments.filter((row) => row.comment !== undefined && row.name !== DEFAULT_SCHEMA);
619
+ if (described.length > 0)
620
+ model.notes.push({
621
+ content: `Schemas
622
+ ${described.map((row) => `${row.name}: ${row.comment}`).join(`
623
+ `)}`,
624
+ name: "schemas"
625
+ });
626
+ };
627
+ var fromPostgres = async (query, options = {}) => {
628
+ const catalogs = await readCatalogs(query, await schemasToRead(query, options.schemas));
629
+ const model = emptyModel({
630
+ databaseType: "PostgreSQL",
631
+ ...options.project
632
+ });
633
+ model.enums.push(...catalogs.enums.map((row) => ({
634
+ name: row.name,
635
+ ...inSchema(row.schema),
636
+ values: row.values.map((name) => ({ name }))
637
+ })));
638
+ const relations = new Map(catalogs.relations.map((row) => [row.id, row]));
639
+ const names = new Map(catalogs.columns.map((row) => [
640
+ `${row.tableId}:${row.position}`,
641
+ row.name
642
+ ]));
643
+ const tables = new Map(catalogs.relations.map((row) => [
644
+ row.id,
645
+ tableOf(row, catalogs.columns, catalogs.policies)
646
+ ]));
647
+ const context = {
648
+ relations,
649
+ tables,
650
+ columnName: (tableId, position) => names.get(`${tableId}:${position}`) ?? `${position}`
651
+ };
652
+ const foreign = applyConstraints(catalogs.constraints, context);
653
+ catalogs.indexes.forEach((row) => tables.get(row.tableId)?.indexes.push(indexOf(row, context)));
654
+ model.tables.push(...tables.values());
655
+ model.refs.push(...foreign.map((row) => refOf(row, context)).filter((ref) => ref !== undefined));
656
+ addDeps(model, catalogs, relations);
657
+ addNotes(model, catalogs);
658
+ const limit = options.records?.limit ?? 0;
659
+ if (limit > 0)
660
+ model.records.push(...await readRecords(query, model.tables.filter((table) => !table.headerColor), limit));
661
+ return model;
662
+ };
663
+ export {
664
+ fromPostgres,
665
+ parseDefault
666
+ };
@@ -0,0 +1,10 @@
1
+ export declare const SCHEMAS = "\nselect nspname as name\nfrom pg_namespace\nwhere nspname not in ('pg_catalog', 'information_schema', 'pg_toast')\n\tand nspname not like 'pg_temp_%' and nspname not like 'pg_toast_temp_%'\norder by nspname";
2
+ export declare const ENUMS = "\nselect n.nspname as schema, t.typname as name,\n\tarray_agg(e.enumlabel order by e.enumsortorder) as \"values\"\nfrom pg_type t\njoin pg_namespace n on n.oid = t.typnamespace\njoin pg_enum e on e.enumtypid = t.oid\nwhere n.nspname = any($1)\ngroup by n.nspname, t.typname\norder by n.nspname, t.typname";
3
+ export declare const RELATIONS = "\nselect c.oid::int as id, n.nspname as schema, c.relname as name, c.relkind as kind,\n\tobj_description(c.oid, 'pg_class') as comment,\n\tc.relrowsecurity as rls, c.relforcerowsecurity as force_rls,\n\tcase when c.relkind = 'p' then pg_get_partkeydef(c.oid) end as partition_key,\n\tcase when c.relkind in ('v', 'm') then pg_get_viewdef(c.oid, true) end as definition,\n\t(select count(*)::int from pg_inherits i where i.inhparent = c.oid) as partitions\nfrom pg_class c\njoin pg_namespace n on n.oid = c.relnamespace\nwhere n.nspname = any($1) and c.relkind in ('r', 'p', 'v', 'm', 'f') and not c.relispartition\norder by n.nspname, c.relname";
4
+ export declare const COLUMNS = "\nselect a.attrelid::int as table_id, a.attnum::int as position, a.attname as name,\n\tformat_type(a.atttypid, a.atttypmod) as type,\n\ta.attnotnull as not_null, a.attidentity as identity, a.attgenerated as generated,\n\tpg_get_expr(d.adbin, d.adrelid) as \"default\",\n\tcol_description(a.attrelid, a.attnum) as comment,\n\tcase when t.typtype = 'e' then tn.nspname\n\t\twhen et.typtype = 'e' then etn.nspname end as enum_schema,\n\tcoalesce(case when t.typtype = 'e' then t.typname end,\n\t\tcase when et.typtype = 'e' then et.typname || '[]' end) as enum_type,\n\tpg_get_serial_sequence(quote_ident(n.nspname) || '.' || quote_ident(c.relname), a.attname) as serial_sequence\nfrom pg_attribute a\njoin pg_class c on c.oid = a.attrelid\njoin pg_namespace n on n.oid = c.relnamespace\njoin pg_type t on t.oid = a.atttypid\njoin pg_namespace tn on tn.oid = t.typnamespace\nleft join pg_type et on et.oid = t.typelem and t.typcategory = 'A'\nleft join pg_namespace etn on etn.oid = et.typnamespace\nleft join pg_attrdef d on d.adrelid = a.attrelid and d.adnum = a.attnum\nwhere n.nspname = any($1) and c.relkind in ('r', 'p', 'v', 'm', 'f') and not c.relispartition\n\tand a.attnum > 0 and not a.attisdropped\norder by a.attrelid, a.attnum";
5
+ export declare const CONSTRAINTS = "\nselect con.conrelid::int as table_id, con.conname as name, con.contype as kind,\n\t(select array_agg(k::int order by o) from unnest(con.conkey) with ordinality as u(k, o)) as columns,\n\tcon.confrelid::int as foreign_table_id,\n\t(select array_agg(k::int order by o) from unnest(con.confkey) with ordinality as u(k, o)) as foreign_columns,\n\tcon.confdeltype as on_delete, con.confupdtype as on_update,\n\tpg_get_constraintdef(con.oid, true) as definition,\n\tcase when con.contype = 'c' then pg_get_expr(con.conbin, con.conrelid, true) end as check_expression,\n\tobj_description(con.oid, 'pg_constraint') as comment,\n\tcon.condeferrable as deferrable, con.condeferred as deferred,\n\t/* Read through jsonb so a Postgres older than 15, without the column,\n\t * reads null instead of failing. */\n\t(to_jsonb(con) ->> 'connullsnotdistinct')::boolean as nulls_not_distinct\nfrom pg_constraint con\njoin pg_namespace n on n.oid = con.connamespace\nwhere n.nspname = any($1) and con.contype in ('p', 'u', 'f', 'c', 'x') and con.conrelid <> 0\norder by con.conrelid, con.contype, con.conname";
6
+ export declare const INDEXES = "\nselect i.indrelid::int as table_id, ic.relname as name, am.amname as method,\n\ti.indisunique as is_unique, i.indnkeyatts::int as key_count, i.indnatts::int as count,\n\t(select array_agg(pg_get_indexdef(i.indexrelid, k, true) order by k)\n\t\tfrom generate_series(1, i.indnatts) as k) as parts,\n\t(select array_agg(i.indkey[k - 1]::int order by k)\n\t\tfrom generate_series(1, i.indnatts) as k) as attnums,\n\t(select array_agg((i.indoption[k - 1] & 1) = 1 order by k)\n\t\tfrom generate_series(1, i.indnkeyatts) as k) as descending,\n\tpg_get_expr(i.indpred, i.indrelid, true) as predicate,\n\tobj_description(i.indexrelid, 'pg_class') as comment\nfrom pg_index i\njoin pg_class ic on ic.oid = i.indexrelid\njoin pg_class tc on tc.oid = i.indrelid\njoin pg_namespace n on n.oid = tc.relnamespace\njoin pg_am am on am.oid = ic.relam\nwhere n.nspname = any($1)\n\tand not exists (select 1 from pg_constraint con where con.conindid = i.indexrelid and con.contype in ('p', 'u', 'x'))\norder by i.indrelid, ic.relname";
7
+ export declare const VIEW_SOURCES = "\nselect distinct v.oid::int as view_id, d.refobjid::int as source_id\nfrom pg_class v\njoin pg_namespace n on n.oid = v.relnamespace\njoin pg_rewrite r on r.ev_class = v.oid\njoin pg_depend d on d.objid = r.oid and d.classid = 'pg_rewrite'::regclass\n\tand d.refclassid = 'pg_class'::regclass and d.refobjid <> v.oid\nwhere n.nspname = any($1) and v.relkind in ('v', 'm')";
8
+ export declare const POLICIES = "\nselect p.polrelid::int as table_id, p.polname as name, p.polpermissive as permissive,\n\tp.polcmd as command,\n\t(select array_agg(case when r = 0 then 'public' else pg_get_userbyid(r) end) from unnest(p.polroles) as r) as roles,\n\tpg_get_expr(p.polqual, p.polrelid, true) as \"using\",\n\tpg_get_expr(p.polwithcheck, p.polrelid, true) as with_check\nfrom pg_policy p\njoin pg_class c on c.oid = p.polrelid\njoin pg_namespace n on n.oid = c.relnamespace\nwhere n.nspname = any($1)\norder by p.polrelid, p.polname";
9
+ export declare const SEQUENCES = "\nselect n.nspname as schema, c.relname as name, s.seqstart::text as start,\n\ts.seqincrement::text as increment\nfrom pg_sequence s\njoin pg_class c on c.oid = s.seqrelid\njoin pg_namespace n on n.oid = c.relnamespace\nwhere n.nspname = any($1)\n\tand not exists (select 1 from pg_depend d where d.objid = c.oid and d.deptype in ('a', 'i'))\norder by n.nspname, c.relname";
10
+ export declare const SCHEMA_COMMENTS = "\nselect nspname as name, obj_description(oid, 'pg_namespace') as comment\nfrom pg_namespace where nspname = any($1)";