drizzle-pg-extras 0.5.0 → 0.6.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/README.md +14 -4
- package/dist/cli.js +120 -39
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +140 -59
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +136 -55
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.cts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { SQL, Column, Table, ColumnBuilderBase
|
|
1
|
+
import { SQL, Column, Table, ColumnBuilderBase } from 'drizzle-orm';
|
|
2
2
|
import { AnyPgColumn, PgTable, PgView, PgMaterializedView, PgEnum, PgEnumObject, IndexBuilder, UniqueConstraintBuilder, CheckBuilder, ForeignKeyBuilder, PrimaryKeyBuilder } from 'drizzle-orm/pg-core';
|
|
3
3
|
|
|
4
4
|
declare class PgExtrasError extends Error {
|
|
5
5
|
constructor(message: string);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
declare const VERSION = "0.
|
|
8
|
+
declare const VERSION = "0.6.0";
|
|
9
|
+
|
|
10
|
+
type Casing = "snake_case" | "camelCase";
|
|
9
11
|
|
|
10
12
|
declare const KIND: unique symbol;
|
|
11
13
|
type PgLanguage = "plpgsql" | "sql";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { SQL, Column, Table, ColumnBuilderBase
|
|
1
|
+
import { SQL, Column, Table, ColumnBuilderBase } from 'drizzle-orm';
|
|
2
2
|
import { AnyPgColumn, PgTable, PgView, PgMaterializedView, PgEnum, PgEnumObject, IndexBuilder, UniqueConstraintBuilder, CheckBuilder, ForeignKeyBuilder, PrimaryKeyBuilder } from 'drizzle-orm/pg-core';
|
|
3
3
|
|
|
4
4
|
declare class PgExtrasError extends Error {
|
|
5
5
|
constructor(message: string);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
declare const VERSION = "0.
|
|
8
|
+
declare const VERSION = "0.6.0";
|
|
9
|
+
|
|
10
|
+
type Casing = "snake_case" | "camelCase";
|
|
9
11
|
|
|
10
12
|
declare const KIND: unique symbol;
|
|
11
13
|
type PgLanguage = "plpgsql" | "sql";
|
package/dist/index.js
CHANGED
|
@@ -7,23 +7,58 @@ var PgExtrasError = class extends Error {
|
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
// src/version.ts
|
|
10
|
-
var VERSION = "0.
|
|
10
|
+
var VERSION = "0.6.0";
|
|
11
11
|
|
|
12
12
|
// src/defs.ts
|
|
13
13
|
import {
|
|
14
14
|
Column,
|
|
15
15
|
ColumnBuilder,
|
|
16
16
|
getTableColumns,
|
|
17
|
-
is as
|
|
17
|
+
is as is3,
|
|
18
18
|
sql,
|
|
19
19
|
Table
|
|
20
20
|
} from "drizzle-orm";
|
|
21
|
-
|
|
21
|
+
|
|
22
|
+
// src/compat.ts
|
|
23
|
+
import { is } from "drizzle-orm";
|
|
24
|
+
import * as casingModule from "drizzle-orm/casing";
|
|
25
|
+
import * as pgCore from "drizzle-orm/pg-core";
|
|
26
|
+
var toSnakeCase2 = casingModule.toSnakeCase;
|
|
27
|
+
var toCamelCase2 = casingModule.toCamelCase;
|
|
28
|
+
function columnDbName(column, casing) {
|
|
29
|
+
if (!column.keyAsName || !casing) return column.name;
|
|
30
|
+
return casing === "snake_case" ? toSnakeCase2(column.name) : toCamelCase2(column.name);
|
|
31
|
+
}
|
|
32
|
+
var ColumnCasing = class {
|
|
33
|
+
constructor(casing) {
|
|
34
|
+
this.casing = casing;
|
|
35
|
+
}
|
|
36
|
+
casing;
|
|
37
|
+
getColumnCasing(column) {
|
|
38
|
+
return columnDbName(column, this.casing);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
function columnTable(column) {
|
|
42
|
+
return column.table;
|
|
43
|
+
}
|
|
44
|
+
var PgArrayClass = pgCore["PgArray"];
|
|
45
|
+
function unwrapArrayColumn(column) {
|
|
46
|
+
let current = column;
|
|
47
|
+
if (!PgArrayClass) return current;
|
|
48
|
+
while (is(current, PgArrayClass)) current = current.baseColumn;
|
|
49
|
+
return current;
|
|
50
|
+
}
|
|
51
|
+
function dialectConfig(casing) {
|
|
52
|
+
return casing ? { casing } : void 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/defs.ts
|
|
22
56
|
import {
|
|
23
57
|
CheckBuilder,
|
|
24
58
|
ForeignKeyBuilder,
|
|
25
59
|
IndexBuilder,
|
|
26
60
|
IndexBuilderOn,
|
|
61
|
+
PgColumnBuilder,
|
|
27
62
|
ManualMaterializedViewBuilder,
|
|
28
63
|
ManualViewBuilder,
|
|
29
64
|
MaterializedViewBuilder,
|
|
@@ -106,20 +141,20 @@ $${tag}$`;
|
|
|
106
141
|
}
|
|
107
142
|
|
|
108
143
|
// src/render-sql.ts
|
|
109
|
-
import { is, SQL } from "drizzle-orm";
|
|
144
|
+
import { is as is2, SQL } from "drizzle-orm";
|
|
110
145
|
import { PgDialect } from "drizzle-orm/pg-core";
|
|
111
146
|
var dialects = /* @__PURE__ */ new Map();
|
|
112
147
|
function dialectFor(casing) {
|
|
113
148
|
const key = casing ?? "";
|
|
114
149
|
let dialect = dialects.get(key);
|
|
115
150
|
if (!dialect) {
|
|
116
|
-
dialect = new PgDialect(casing
|
|
151
|
+
dialect = new PgDialect(dialectConfig(casing));
|
|
117
152
|
dialects.set(key, dialect);
|
|
118
153
|
}
|
|
119
154
|
return dialect;
|
|
120
155
|
}
|
|
121
156
|
function isSqlFragment(value) {
|
|
122
|
-
return
|
|
157
|
+
return is2(value, SQL);
|
|
123
158
|
}
|
|
124
159
|
function renderSql(query, casing) {
|
|
125
160
|
return dialectFor(casing).sqlToQuery(query.inlineParams()).sql;
|
|
@@ -159,11 +194,11 @@ function stampConfig(target, comment) {
|
|
|
159
194
|
}
|
|
160
195
|
function withComment(target, comment) {
|
|
161
196
|
const text = requireComment(comment, "withComment()");
|
|
162
|
-
if (
|
|
197
|
+
if (is3(target, Table)) {
|
|
163
198
|
stampDirect(target, text);
|
|
164
199
|
return target;
|
|
165
200
|
}
|
|
166
|
-
if (
|
|
201
|
+
if (is3(target, Column) || is3(target, ColumnBuilder) || is3(target, PgColumnBuilder)) {
|
|
167
202
|
stampConfig(target, text);
|
|
168
203
|
return target;
|
|
169
204
|
}
|
|
@@ -176,14 +211,14 @@ function withComment(target, comment) {
|
|
|
176
211
|
stampDirect(target, text);
|
|
177
212
|
return target;
|
|
178
213
|
}
|
|
179
|
-
if (
|
|
214
|
+
if (is3(target, IndexBuilder) || is3(target, UniqueConstraintBuilder) || is3(target, CheckBuilder) || is3(target, ForeignKeyBuilder) || is3(target, PrimaryKeyBuilder)) {
|
|
180
215
|
stampDirect(target, text);
|
|
181
216
|
return target;
|
|
182
217
|
}
|
|
183
|
-
if (
|
|
218
|
+
if (is3(target, IndexBuilderOn) || is3(target, UniqueOnConstraintBuilder)) {
|
|
184
219
|
throw new PgExtrasError("withComment() on an index or unique constraint needs .on(...) first");
|
|
185
220
|
}
|
|
186
|
-
if (
|
|
221
|
+
if (is3(target, ViewBuilder) || is3(target, ManualViewBuilder) || is3(target, MaterializedViewBuilder) || is3(target, ManualMaterializedViewBuilder)) {
|
|
187
222
|
throw new PgExtrasError("withComment() on a view needs .as() or .existing() first");
|
|
188
223
|
}
|
|
189
224
|
throw new PgExtrasError(
|
|
@@ -267,7 +302,7 @@ function pgTrigger(name, config) {
|
|
|
267
302
|
});
|
|
268
303
|
}
|
|
269
304
|
function updatedAtTrigger(table, column, options) {
|
|
270
|
-
if (!
|
|
305
|
+
if (!is3(table, PgTable)) {
|
|
271
306
|
throw new PgExtrasError("updatedAtTrigger() table must be a Drizzle Postgres table");
|
|
272
307
|
}
|
|
273
308
|
assertColumnOfTable(table, column);
|
|
@@ -418,7 +453,7 @@ function triggerError(label, message) {
|
|
|
418
453
|
throw new PgExtrasError(`pgTrigger("${label}"): ${message}`);
|
|
419
454
|
}
|
|
420
455
|
function relationIdentity(relation2) {
|
|
421
|
-
if (
|
|
456
|
+
if (is3(relation2, PgTable)) {
|
|
422
457
|
const config = getTableConfig(relation2);
|
|
423
458
|
return { schema: schemaName(config.schema), name: config.name, kind: "table" };
|
|
424
459
|
}
|
|
@@ -438,16 +473,16 @@ function relationIdentity(relation2) {
|
|
|
438
473
|
function viewOwnsColumn(view, column, viewName) {
|
|
439
474
|
const entries = relationColumnEntries(view);
|
|
440
475
|
if (entries.some(([, owned]) => owned === column)) return true;
|
|
441
|
-
const parent = column
|
|
442
|
-
if (!
|
|
476
|
+
const parent = columnTable(column);
|
|
477
|
+
if (!is3(parent, PgTable) || getTableConfig(parent).name !== viewName) return false;
|
|
443
478
|
return entries.some(([, owned]) => owned.name === column.name);
|
|
444
479
|
}
|
|
445
480
|
function relationColumnEntries(relation2) {
|
|
446
|
-
if (
|
|
481
|
+
if (is3(relation2, PgTable)) return Object.entries(getTableColumns(relation2));
|
|
447
482
|
const config = isPgMaterializedView(relation2) ? getMaterializedViewConfig(relation2) : getViewConfig(relation2);
|
|
448
483
|
const entries = [];
|
|
449
484
|
for (const [key, field] of Object.entries(config.selectedFields ?? {})) {
|
|
450
|
-
if (
|
|
485
|
+
if (is3(field, Column)) entries.push([key, field]);
|
|
451
486
|
}
|
|
452
487
|
return entries;
|
|
453
488
|
}
|
|
@@ -479,7 +514,7 @@ function readReferencedTable(label, from) {
|
|
|
479
514
|
if (isPgView(from) || isPgMaterializedView(from)) {
|
|
480
515
|
triggerError(label, "FROM must reference a table");
|
|
481
516
|
}
|
|
482
|
-
if (
|
|
517
|
+
if (is3(from, PgTable)) return from;
|
|
483
518
|
if (typeof from === "object" && from !== null && "name" in from && typeof from.name === "string") {
|
|
484
519
|
assertIdentifier("referenced table", from.name);
|
|
485
520
|
if (from.schema !== void 0) {
|
|
@@ -532,7 +567,7 @@ function renderWhenInput(label, table, when, casing) {
|
|
|
532
567
|
if (!when) return null;
|
|
533
568
|
let value;
|
|
534
569
|
if (typeof when === "function") {
|
|
535
|
-
if (!
|
|
570
|
+
if (!is3(table, PgTable) && !isPgView(table) && !isPgMaterializedView(table)) {
|
|
536
571
|
triggerError(label, "WHEN callback needs a Drizzle table or view so NEW and OLD can be typed");
|
|
537
572
|
}
|
|
538
573
|
const entries = relationColumnEntries(table);
|
|
@@ -547,7 +582,7 @@ function renderWhenInput(label, table, when, casing) {
|
|
|
547
582
|
return text.length === 0 ? null : text;
|
|
548
583
|
}
|
|
549
584
|
function rowProxy(columns, alias, casing) {
|
|
550
|
-
const cache = new
|
|
585
|
+
const cache = new ColumnCasing(casing);
|
|
551
586
|
const refs = {};
|
|
552
587
|
for (const [key, column] of columns) {
|
|
553
588
|
refs[key] = sql.raw(`${alias}.${quoteIdent(cache.getColumnCasing(column))}`);
|
|
@@ -651,7 +686,7 @@ function commentOn(target, text) {
|
|
|
651
686
|
if (isCommentTarget(target)) {
|
|
652
687
|
return brand({ target, text: comment }, "comment");
|
|
653
688
|
}
|
|
654
|
-
if (
|
|
689
|
+
if (is3(target, IndexBuilder) || is3(target, IndexBuilderOn) || is3(target, UniqueConstraintBuilder) || is3(target, UniqueOnConstraintBuilder) || is3(target, CheckBuilder) || is3(target, ForeignKeyBuilder) || is3(target, PrimaryKeyBuilder)) {
|
|
655
690
|
throw new PgExtrasError(
|
|
656
691
|
"commentOn() does not take an index or constraint. Wrap the builder with withComment() inside the table extra config."
|
|
657
692
|
);
|
|
@@ -659,7 +694,7 @@ function commentOn(target, text) {
|
|
|
659
694
|
throw new PgExtrasError("commentOn() takes a table, column, enum, view, materialized view, function, trigger, or extension");
|
|
660
695
|
}
|
|
661
696
|
function isCommentTarget(target) {
|
|
662
|
-
if (
|
|
697
|
+
if (is3(target, PgTable) || is3(target, Column)) return true;
|
|
663
698
|
if (isEnumTarget(target)) return true;
|
|
664
699
|
if (isPgView(target) || isPgMaterializedView(target)) return true;
|
|
665
700
|
const kind = extrasKind(target);
|
|
@@ -674,7 +709,7 @@ function assertColumnOfTable(table, column) {
|
|
|
674
709
|
}
|
|
675
710
|
}
|
|
676
711
|
function tableIdentity(table) {
|
|
677
|
-
if (
|
|
712
|
+
if (is3(table, PgTable)) {
|
|
678
713
|
const config = getTableConfig(table);
|
|
679
714
|
return { schema: config.schema && config.schema.length > 0 ? config.schema : "public", name: config.name };
|
|
680
715
|
}
|
|
@@ -688,7 +723,7 @@ function partitionedTable(table, config) {
|
|
|
688
723
|
if (!config.columns || config.columns.length === 0) {
|
|
689
724
|
throw new PgExtrasError("partitionedTable(): columns must include the partition key");
|
|
690
725
|
}
|
|
691
|
-
if (
|
|
726
|
+
if (is3(table, PgTable)) {
|
|
692
727
|
const seen = /* @__PURE__ */ new Set();
|
|
693
728
|
const columns = [];
|
|
694
729
|
for (const column of config.columns) {
|
|
@@ -770,7 +805,7 @@ function pgDetachPartition(table, config) {
|
|
|
770
805
|
}
|
|
771
806
|
function requireParent(parent, where) {
|
|
772
807
|
if (extrasKind(parent) === "partitionedTable") return parent;
|
|
773
|
-
if (
|
|
808
|
+
if (is3(parent, PgTable) || isPlainTableRef(parent)) return parent;
|
|
774
809
|
throw new PgExtrasError(`${where}: parent must be a Drizzle table, partitionedTable(), or { name, schema }`);
|
|
775
810
|
}
|
|
776
811
|
function isPlainTableRef(value) {
|
|
@@ -849,7 +884,7 @@ function assertKeyCoveredByUniques(table, keyColumns, options) {
|
|
|
849
884
|
if (!index.config.unique) continue;
|
|
850
885
|
const names = [];
|
|
851
886
|
for (const column of index.config.columns) {
|
|
852
|
-
if (!
|
|
887
|
+
if (!is3(column, Column)) {
|
|
853
888
|
const indexName = index.config.name ? ` "${index.config.name}"` : "";
|
|
854
889
|
throw new PgExtrasError(`${where}: unique index${indexName} includes an expression. ${expressionRequirement}`);
|
|
855
890
|
}
|
|
@@ -873,7 +908,7 @@ var INTERVAL_PATTERN = new RegExp(`^${INTERVAL_PART}(?:\\s+${INTERVAL_PART})*$`,
|
|
|
873
908
|
function hypertable(table, config) {
|
|
874
909
|
const identity = tableIdentity(table);
|
|
875
910
|
const where = `hypertable(${identity.schema}.${identity.name})`;
|
|
876
|
-
const drizzleTable =
|
|
911
|
+
const drizzleTable = is3(table, PgTable) ? table : null;
|
|
877
912
|
const timeColumn = resolveHypertableColumn(drizzleTable, config.timeColumn, `${where} timeColumn`);
|
|
878
913
|
if (drizzleTable && typeof timeColumn !== "string") {
|
|
879
914
|
if (!isHypertableTimeType(timeColumn.getSQLType())) {
|
|
@@ -941,7 +976,7 @@ function readCompression(table, compression, where) {
|
|
|
941
976
|
return { segmentBy, orderBy };
|
|
942
977
|
}
|
|
943
978
|
function readOrder(table, entry, where) {
|
|
944
|
-
if (typeof entry === "string" ||
|
|
979
|
+
if (typeof entry === "string" || is3(entry, Column)) {
|
|
945
980
|
return { column: resolveHypertableColumn(table, entry, where), direction: null, nulls: null };
|
|
946
981
|
}
|
|
947
982
|
if (!entry || typeof entry !== "object" || !("column" in entry)) {
|
|
@@ -1035,7 +1070,7 @@ function isPlainRecord(value) {
|
|
|
1035
1070
|
}
|
|
1036
1071
|
|
|
1037
1072
|
// src/collect.ts
|
|
1038
|
-
import { is as
|
|
1073
|
+
import { is as is4 } from "drizzle-orm";
|
|
1039
1074
|
import { PgTable as PgTable2, isPgEnum as isPgEnum2, isPgMaterializedView as isPgMaterializedView2, isPgView as isPgView2 } from "drizzle-orm/pg-core";
|
|
1040
1075
|
function collectExtras(modules) {
|
|
1041
1076
|
const extras = {
|
|
@@ -1106,7 +1141,7 @@ function walk(value, extras, seen) {
|
|
|
1106
1141
|
if (kind === "partitionedTable") {
|
|
1107
1142
|
const partitioned = value;
|
|
1108
1143
|
extras.partitionedTables.push(partitioned);
|
|
1109
|
-
if (
|
|
1144
|
+
if (is4(partitioned.table, PgTable2)) walk(partitioned.table, extras, seen);
|
|
1110
1145
|
return;
|
|
1111
1146
|
}
|
|
1112
1147
|
if (kind === "partition") {
|
|
@@ -1120,14 +1155,14 @@ function walk(value, extras, seen) {
|
|
|
1120
1155
|
if (kind === "hypertable") {
|
|
1121
1156
|
const hypertable2 = value;
|
|
1122
1157
|
extras.hypertables.push(hypertable2);
|
|
1123
|
-
if (
|
|
1158
|
+
if (is4(hypertable2.table, PgTable2)) walk(hypertable2.table, extras, seen);
|
|
1124
1159
|
return;
|
|
1125
1160
|
}
|
|
1126
1161
|
if (isPgView2(value) || isPgMaterializedView2(value)) {
|
|
1127
1162
|
pushView(extras, value);
|
|
1128
1163
|
return;
|
|
1129
1164
|
}
|
|
1130
|
-
if (
|
|
1165
|
+
if (is4(value, PgTable2)) {
|
|
1131
1166
|
if (!extras.tables.includes(value)) extras.tables.push(value);
|
|
1132
1167
|
return;
|
|
1133
1168
|
}
|
|
@@ -1148,13 +1183,12 @@ function pushView(extras, value) {
|
|
|
1148
1183
|
}
|
|
1149
1184
|
|
|
1150
1185
|
// src/snapshot.ts
|
|
1151
|
-
import { Column as Column2, SQL as SQL2, getTableColumns as getTableColumns2, is as
|
|
1152
|
-
import {
|
|
1186
|
+
import { Column as Column2, SQL as SQL2, getTableColumns as getTableColumns2, is as is5 } from "drizzle-orm";
|
|
1187
|
+
import { toCamelCase as toCamelCase3, toSnakeCase as toSnakeCase3 } from "drizzle-orm/casing";
|
|
1153
1188
|
import {
|
|
1154
1189
|
CheckBuilder as CheckBuilder2,
|
|
1155
1190
|
ForeignKeyBuilder as ForeignKeyBuilder2,
|
|
1156
1191
|
IndexBuilder as IndexBuilder2,
|
|
1157
|
-
PgArray,
|
|
1158
1192
|
PgEnumColumn,
|
|
1159
1193
|
PgEnumObjectColumn,
|
|
1160
1194
|
PgTable as PgTable3,
|
|
@@ -1185,7 +1219,7 @@ function functionSignature(schema, name, argTypes) {
|
|
|
1185
1219
|
}
|
|
1186
1220
|
function snapshotExtras(extras, options) {
|
|
1187
1221
|
const casing = options?.casing;
|
|
1188
|
-
const cache = new
|
|
1222
|
+
const cache = new ColumnCasing(casing);
|
|
1189
1223
|
const nameOf = (column) => cache.getColumnCasing(column);
|
|
1190
1224
|
const functions = [];
|
|
1191
1225
|
const fnByObject = /* @__PURE__ */ new Map();
|
|
@@ -1487,11 +1521,11 @@ function harvest(table, nameOf) {
|
|
|
1487
1521
|
}
|
|
1488
1522
|
function snapshotComment(comment, nameOf, casing, fnByObject, triggerByObject, extras) {
|
|
1489
1523
|
const target = comment.target;
|
|
1490
|
-
if (
|
|
1524
|
+
if (is5(target, PgTable3)) {
|
|
1491
1525
|
const identity = tableIdentity(target);
|
|
1492
1526
|
return tableComment(identity.schema, identity.name, comment.text);
|
|
1493
1527
|
}
|
|
1494
|
-
if (
|
|
1528
|
+
if (is5(target, Column2)) {
|
|
1495
1529
|
return columnCommentFrom(target, comment.text, nameOf, extras);
|
|
1496
1530
|
}
|
|
1497
1531
|
if (isPgEnum3(target)) {
|
|
@@ -1778,10 +1812,11 @@ function commentLabel(comment) {
|
|
|
1778
1812
|
return `${comment.kind} ${comment.schema}.${comment.name}`;
|
|
1779
1813
|
}
|
|
1780
1814
|
function columnCommentFrom(column, text, nameOf, extras) {
|
|
1781
|
-
|
|
1815
|
+
const owner = columnTable(column);
|
|
1816
|
+
if (!owner || !is5(owner, PgTable3)) {
|
|
1782
1817
|
throw new PgExtrasError(`commentOn() column "${column.name}" is not attached to a Postgres table or view`);
|
|
1783
1818
|
}
|
|
1784
|
-
const identity = tableIdentity(
|
|
1819
|
+
const identity = tableIdentity(owner);
|
|
1785
1820
|
const dbName = nameOf(column);
|
|
1786
1821
|
const knownTable = extras.tables.find((table) => {
|
|
1787
1822
|
const id = tableIdentity(table);
|
|
@@ -1796,7 +1831,7 @@ function columnCommentFrom(column, text, nameOf, extras) {
|
|
|
1796
1831
|
const viewId = viewIdentity(view);
|
|
1797
1832
|
return columnComment(viewId.schema, viewId.name, dbName, text);
|
|
1798
1833
|
}
|
|
1799
|
-
assertColumnOfTable(
|
|
1834
|
+
assertColumnOfTable(owner, column);
|
|
1800
1835
|
return columnComment(identity.schema, identity.name, dbName, text);
|
|
1801
1836
|
}
|
|
1802
1837
|
var ExtraConfigBuilder = /* @__PURE__ */ Symbol.for("drizzle:ExtraConfigBuilder");
|
|
@@ -1820,7 +1855,7 @@ function harvestRelations(table, nameOf, casing) {
|
|
|
1820
1855
|
"Return the index or constraint from the table extra config wrapped with withComment(). commentOn() is for tables, columns, enums, views, functions, triggers, and extensions."
|
|
1821
1856
|
);
|
|
1822
1857
|
}
|
|
1823
|
-
if (
|
|
1858
|
+
if (is5(value, IndexBuilder2)) {
|
|
1824
1859
|
const built = value.build(table);
|
|
1825
1860
|
const text = readComment(value);
|
|
1826
1861
|
let name;
|
|
@@ -1834,14 +1869,14 @@ function harvestRelations(table, nameOf, casing) {
|
|
|
1834
1869
|
if (text) comments.push(indexComment(identity.schema, name, text));
|
|
1835
1870
|
continue;
|
|
1836
1871
|
}
|
|
1837
|
-
if (
|
|
1872
|
+
if (is5(value, CheckBuilder2)) {
|
|
1838
1873
|
const name = value.name;
|
|
1839
1874
|
constraints.push({ schema: identity.schema, table: identity.name, name });
|
|
1840
1875
|
const text = readComment(value);
|
|
1841
1876
|
if (text) comments.push(constraintComment(identity.schema, identity.name, name, text));
|
|
1842
1877
|
continue;
|
|
1843
1878
|
}
|
|
1844
|
-
if (
|
|
1879
|
+
if (is5(value, UniqueConstraintBuilder2)) {
|
|
1845
1880
|
const built = buildOf(value, table);
|
|
1846
1881
|
const name = constraintName(built);
|
|
1847
1882
|
constraints.push({ schema: identity.schema, table: identity.name, name });
|
|
@@ -1849,7 +1884,7 @@ function harvestRelations(table, nameOf, casing) {
|
|
|
1849
1884
|
if (text) comments.push(constraintComment(identity.schema, identity.name, name, text));
|
|
1850
1885
|
continue;
|
|
1851
1886
|
}
|
|
1852
|
-
if (
|
|
1887
|
+
if (is5(value, ForeignKeyBuilder2)) {
|
|
1853
1888
|
const built = buildOf(value, table);
|
|
1854
1889
|
const name = applyCasing(built.getName(), [...built.reference().columns, ...built.reference().foreignColumns], nameOf, casing);
|
|
1855
1890
|
constraints.push({ schema: identity.schema, table: identity.name, name });
|
|
@@ -1857,7 +1892,7 @@ function harvestRelations(table, nameOf, casing) {
|
|
|
1857
1892
|
if (text) comments.push(constraintComment(identity.schema, identity.name, name, text));
|
|
1858
1893
|
continue;
|
|
1859
1894
|
}
|
|
1860
|
-
if (
|
|
1895
|
+
if (is5(value, PrimaryKeyBuilder2)) {
|
|
1861
1896
|
const built = buildOf(value, table);
|
|
1862
1897
|
const name = applyCasing(built.getName(), built.columns, nameOf, casing);
|
|
1863
1898
|
constraints.push({ schema: identity.schema, table: identity.name, name });
|
|
@@ -1888,7 +1923,7 @@ function resolveIndexName(index, tableName, casing) {
|
|
|
1888
1923
|
if (index.config.name && index.config.name.length > 0) return index.config.name;
|
|
1889
1924
|
const columns = [];
|
|
1890
1925
|
for (const column of index.config.columns) {
|
|
1891
|
-
if (
|
|
1926
|
+
if (is5(column, SQL2)) {
|
|
1892
1927
|
throw new PgExtrasError(
|
|
1893
1928
|
`index comment on "${tableName}" needs an index name. Drizzle names indexes made of columns only; name an index whose key is a SQL expression.`
|
|
1894
1929
|
);
|
|
@@ -1903,7 +1938,7 @@ function resolveIndexName(index, tableName, casing) {
|
|
|
1903
1938
|
}
|
|
1904
1939
|
function casedIndexColumn(name, keyAsName, casing) {
|
|
1905
1940
|
if (!keyAsName || !casing) return name;
|
|
1906
|
-
return casing === "camelCase" ?
|
|
1941
|
+
return casing === "camelCase" ? toCamelCase3(name) : toSnakeCase3(name);
|
|
1907
1942
|
}
|
|
1908
1943
|
function harvestView(view, nameOf) {
|
|
1909
1944
|
const identity = viewIdentity(view);
|
|
@@ -1922,7 +1957,7 @@ function viewIdentity(view) {
|
|
|
1922
1957
|
const config = materialized ? getMaterializedViewConfig2(view) : getViewConfig2(view);
|
|
1923
1958
|
const columns = [];
|
|
1924
1959
|
for (const field of Object.values(config.selectedFields ?? {})) {
|
|
1925
|
-
if (
|
|
1960
|
+
if (is5(field, Column2)) columns.push(field);
|
|
1926
1961
|
}
|
|
1927
1962
|
return {
|
|
1928
1963
|
schema: config.schema && config.schema.length > 0 ? config.schema : "public",
|
|
@@ -1949,8 +1984,8 @@ function enumObjectsIn(extras) {
|
|
|
1949
1984
|
}
|
|
1950
1985
|
function enumFromColumn(column) {
|
|
1951
1986
|
let current = column;
|
|
1952
|
-
|
|
1953
|
-
if (
|
|
1987
|
+
current = unwrapArrayColumn(current);
|
|
1988
|
+
if (is5(current, PgEnumColumn) || is5(current, PgEnumObjectColumn)) return current.enum;
|
|
1954
1989
|
return null;
|
|
1955
1990
|
}
|
|
1956
1991
|
function enumIdentity(value) {
|
|
@@ -3315,6 +3350,7 @@ function viewColumnGone(from, to, schema, relation2, column) {
|
|
|
3315
3350
|
}
|
|
3316
3351
|
|
|
3317
3352
|
// src/journal.ts
|
|
3353
|
+
import { createRequire } from "module";
|
|
3318
3354
|
import { randomUUID } from "crypto";
|
|
3319
3355
|
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "fs";
|
|
3320
3356
|
import path from "path";
|
|
@@ -3368,7 +3404,27 @@ function detectLayout(out) {
|
|
|
3368
3404
|
`migrations folder ${out} has both meta/_journal.json and v1 migration folders. Keep one layout.`
|
|
3369
3405
|
);
|
|
3370
3406
|
}
|
|
3371
|
-
|
|
3407
|
+
if (v1.length > 0) return "v1";
|
|
3408
|
+
if (journal) return "journal";
|
|
3409
|
+
return installedKitMajor() >= 1 ? "v1" : "journal";
|
|
3410
|
+
}
|
|
3411
|
+
function installedKitMajor() {
|
|
3412
|
+
for (const name of ["drizzle-kit", "drizzle-orm"]) {
|
|
3413
|
+
try {
|
|
3414
|
+
const require2 = createRequire(path.join(process.cwd(), "noop.js"));
|
|
3415
|
+
let dir = path.dirname(require2.resolve(name));
|
|
3416
|
+
for (let i = 0; i < 6; i += 1) {
|
|
3417
|
+
const pkgPath = path.join(dir, "package.json");
|
|
3418
|
+
if (existsSync(pkgPath)) {
|
|
3419
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
3420
|
+
if (pkg.name === name && pkg.version) return Number.parseInt(pkg.version, 10);
|
|
3421
|
+
}
|
|
3422
|
+
dir = path.dirname(dir);
|
|
3423
|
+
}
|
|
3424
|
+
} catch {
|
|
3425
|
+
}
|
|
3426
|
+
}
|
|
3427
|
+
return 0;
|
|
3372
3428
|
}
|
|
3373
3429
|
function writeJournal(out, input) {
|
|
3374
3430
|
const prefixMode = input.prefix ?? "index";
|
|
@@ -3408,9 +3464,11 @@ function writeV1(out, input) {
|
|
|
3408
3464
|
const folders = readdirSync(out, { withFileTypes: true }).filter((entry) => entry.isDirectory() && /^\d{14}_/.test(entry.name)).map((entry) => entry.name).sort();
|
|
3409
3465
|
const suffix = sanitizeMigrationName(input.name);
|
|
3410
3466
|
let stamp = utcStamp(/* @__PURE__ */ new Date());
|
|
3467
|
+
const lastStamp = folders.length > 0 ? folders[folders.length - 1].slice(0, 14) : null;
|
|
3468
|
+
if (lastStamp && stamp <= lastStamp) stamp = bumpStamp(lastStamp);
|
|
3411
3469
|
let folder = `${stamp}_${suffix}`;
|
|
3412
3470
|
while (existsSync(path.join(out, folder))) {
|
|
3413
|
-
stamp =
|
|
3471
|
+
stamp = bumpStamp(stamp);
|
|
3414
3472
|
folder = `${stamp}_${suffix}`;
|
|
3415
3473
|
}
|
|
3416
3474
|
const dir = path.join(out, folder);
|
|
@@ -3424,8 +3482,20 @@ function writeV1(out, input) {
|
|
|
3424
3482
|
const parsed = JSON.parse(readFileSync(previous, "utf8"));
|
|
3425
3483
|
let next = parsed;
|
|
3426
3484
|
if (typeof parsed.id === "string") {
|
|
3427
|
-
|
|
3428
|
-
|
|
3485
|
+
if (parsed.type === "base" || parsed.type === "diff") {
|
|
3486
|
+
next = {
|
|
3487
|
+
type: "diff",
|
|
3488
|
+
version: parsed.version,
|
|
3489
|
+
dialect: parsed.dialect,
|
|
3490
|
+
id: randomUUID(),
|
|
3491
|
+
prevIds: [parsed.id],
|
|
3492
|
+
footprints: [],
|
|
3493
|
+
deltas: []
|
|
3494
|
+
};
|
|
3495
|
+
} else {
|
|
3496
|
+
const { id, prevId: _previous, ...rest } = parsed;
|
|
3497
|
+
next = { id: randomUUID(), prevId: id, ...rest };
|
|
3498
|
+
}
|
|
3429
3499
|
}
|
|
3430
3500
|
writeFileSync(path.join(dir, "snapshot.json"), `${JSON.stringify(next, null, 2)}
|
|
3431
3501
|
`);
|
|
@@ -3483,6 +3553,17 @@ function utcStamp(date) {
|
|
|
3483
3553
|
const pad = (value) => String(value).padStart(2, "0");
|
|
3484
3554
|
return `${date.getUTCFullYear()}${pad(date.getUTCMonth() + 1)}${pad(date.getUTCDate())}${pad(date.getUTCHours())}${pad(date.getUTCMinutes())}${pad(date.getUTCSeconds())}`;
|
|
3485
3555
|
}
|
|
3556
|
+
function bumpStamp(stamp) {
|
|
3557
|
+
const at = Date.UTC(
|
|
3558
|
+
Number(stamp.slice(0, 4)),
|
|
3559
|
+
Number(stamp.slice(4, 6)) - 1,
|
|
3560
|
+
Number(stamp.slice(6, 8)),
|
|
3561
|
+
Number(stamp.slice(8, 10)),
|
|
3562
|
+
Number(stamp.slice(10, 12)),
|
|
3563
|
+
Number(stamp.slice(12, 14))
|
|
3564
|
+
);
|
|
3565
|
+
return utcStamp(new Date(at + 1e3));
|
|
3566
|
+
}
|
|
3486
3567
|
|
|
3487
3568
|
// src/generate.ts
|
|
3488
3569
|
var UP_HEADER = `-- Generated by drizzle-pg-extras ${VERSION}. Review before applying.`;
|