@bungres/kit 0.7.0 → 1.0.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/dist/cli.js +259 -88
- package/dist/index.js +252 -81
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -55,12 +55,12 @@ var require_src = __commonJS((exports, module) => {
|
|
|
55
55
|
ret += `${CSI2}${y}B`;
|
|
56
56
|
return ret;
|
|
57
57
|
},
|
|
58
|
-
up: (
|
|
59
|
-
down: (
|
|
60
|
-
forward: (
|
|
61
|
-
backward: (
|
|
62
|
-
nextLine: (
|
|
63
|
-
prevLine: (
|
|
58
|
+
up: (count = 1) => `${CSI2}${count}A`,
|
|
59
|
+
down: (count = 1) => `${CSI2}${count}B`,
|
|
60
|
+
forward: (count = 1) => `${CSI2}${count}C`,
|
|
61
|
+
backward: (count = 1) => `${CSI2}${count}D`,
|
|
62
|
+
nextLine: (count = 1) => `${CSI2}E`.repeat(count),
|
|
63
|
+
prevLine: (count = 1) => `${CSI2}F`.repeat(count),
|
|
64
64
|
left: `${CSI2}G`,
|
|
65
65
|
hide: `${CSI2}?25l`,
|
|
66
66
|
show: `${CSI2}?25h`,
|
|
@@ -68,21 +68,21 @@ var require_src = __commonJS((exports, module) => {
|
|
|
68
68
|
restore: `${ESC2}8`
|
|
69
69
|
};
|
|
70
70
|
var scroll = {
|
|
71
|
-
up: (
|
|
72
|
-
down: (
|
|
71
|
+
up: (count = 1) => `${CSI2}S`.repeat(count),
|
|
72
|
+
down: (count = 1) => `${CSI2}T`.repeat(count)
|
|
73
73
|
};
|
|
74
74
|
var erase = {
|
|
75
75
|
screen: `${CSI2}2J`,
|
|
76
|
-
up: (
|
|
77
|
-
down: (
|
|
76
|
+
up: (count = 1) => `${CSI2}1J`.repeat(count),
|
|
77
|
+
down: (count = 1) => `${CSI2}J`.repeat(count),
|
|
78
78
|
line: `${CSI2}2K`,
|
|
79
79
|
lineEnd: `${CSI2}K`,
|
|
80
80
|
lineStart: `${CSI2}1K`,
|
|
81
|
-
lines(
|
|
81
|
+
lines(count) {
|
|
82
82
|
let clear = "";
|
|
83
|
-
for (let i = 0;i <
|
|
84
|
-
clear += this.line + (i <
|
|
85
|
-
if (
|
|
83
|
+
for (let i = 0;i < count; i++)
|
|
84
|
+
clear += this.line + (i < count - 1 ? cursor.up() : "");
|
|
85
|
+
if (count)
|
|
86
86
|
clear += cursor.left;
|
|
87
87
|
return clear;
|
|
88
88
|
}
|
|
@@ -97,16 +97,16 @@ var require_picocolors = __commonJS((exports, module) => {
|
|
|
97
97
|
var env = p2.env || {};
|
|
98
98
|
var isColorSupported = !(!!env.NO_COLOR || argv.includes("--no-color")) && (!!env.FORCE_COLOR || argv.includes("--color") || p2.platform === "win32" || (p2.stdout || {}).isTTY && env.TERM !== "dumb" || !!env.CI);
|
|
99
99
|
var formatter = (open, close, replace = open) => (input) => {
|
|
100
|
-
let string = "" + input,
|
|
101
|
-
return ~
|
|
100
|
+
let string = "" + input, index = string.indexOf(close, open.length);
|
|
101
|
+
return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close;
|
|
102
102
|
};
|
|
103
|
-
var replaceClose = (string, close, replace,
|
|
103
|
+
var replaceClose = (string, close, replace, index) => {
|
|
104
104
|
let result = "", cursor3 = 0;
|
|
105
105
|
do {
|
|
106
|
-
result += string.substring(cursor3,
|
|
107
|
-
cursor3 =
|
|
108
|
-
|
|
109
|
-
} while (~
|
|
106
|
+
result += string.substring(cursor3, index) + replace;
|
|
107
|
+
cursor3 = index + close.length;
|
|
108
|
+
index = string.indexOf(close, cursor3);
|
|
109
|
+
} while (~index);
|
|
110
110
|
return result + string.substring(cursor3);
|
|
111
111
|
};
|
|
112
112
|
var createColors = (enabled = isColorSupported) => {
|
|
@@ -202,7 +202,7 @@ async function loadConfig(cwd = process.cwd()) {
|
|
|
202
202
|
// src/schema-loader.ts
|
|
203
203
|
import { resolve as resolve2, join as join2 } from "path";
|
|
204
204
|
|
|
205
|
-
// ../bungres-orm/
|
|
205
|
+
// ../bungres-orm/dist/index.js
|
|
206
206
|
var TableConfigSymbol = Symbol.for("BungresTableConfig");
|
|
207
207
|
function getTableConfig(table) {
|
|
208
208
|
return table[TableConfigSymbol];
|
|
@@ -272,7 +272,6 @@ var table = createTableFactory("snake");
|
|
|
272
272
|
var snakeCase = { table: createTableFactory("snake") };
|
|
273
273
|
var camelCase = { table: createTableFactory("camel") };
|
|
274
274
|
var noCasing = { table: createTableFactory("none") };
|
|
275
|
-
// ../bungres-orm/src/schema/columns.ts
|
|
276
275
|
function buildColumn(dataType, nameOrOpts, opts) {
|
|
277
276
|
let name = "";
|
|
278
277
|
let options = opts;
|
|
@@ -301,6 +300,13 @@ function buildColumn(dataType, nameOrOpts, opts) {
|
|
|
301
300
|
return Object.assign(config, {
|
|
302
301
|
as(alias) {
|
|
303
302
|
return Object.assign({}, this, { alias });
|
|
303
|
+
},
|
|
304
|
+
array() {
|
|
305
|
+
return Object.assign({}, this, { dataType: `${this.dataType}[]` });
|
|
306
|
+
},
|
|
307
|
+
generatedAlwaysAs(expr) {
|
|
308
|
+
const sqlStr = typeof expr === "string" ? expr : expr.sql;
|
|
309
|
+
return Object.assign({}, this, { generatedAs: sqlStr });
|
|
304
310
|
}
|
|
305
311
|
});
|
|
306
312
|
}
|
|
@@ -330,7 +336,6 @@ var textArray = col("text[]");
|
|
|
330
336
|
var integerArray = col("integer[]");
|
|
331
337
|
var varcharArray = col("varchar[]");
|
|
332
338
|
var uuidArray = col("uuid[]");
|
|
333
|
-
// ../bungres-orm/src/core/sql.ts
|
|
334
339
|
function sql(strings, ...values) {
|
|
335
340
|
let query = "";
|
|
336
341
|
const params = [];
|
|
@@ -373,7 +378,6 @@ function colName(c) {
|
|
|
373
378
|
return c.sql;
|
|
374
379
|
return c.tableName ? `${c.tableName}."${c.name}"` : `"${c.name}"`;
|
|
375
380
|
}
|
|
376
|
-
// ../bungres-orm/src/core/conditions.ts
|
|
377
381
|
function isColumnConfig(val) {
|
|
378
382
|
return val !== null && typeof val === "object" && "name" in val && "dataType" in val;
|
|
379
383
|
}
|
|
@@ -527,14 +531,13 @@ function parseOrderByObject(tableConfig, orderByObj) {
|
|
|
527
531
|
}
|
|
528
532
|
return parts;
|
|
529
533
|
}
|
|
530
|
-
|
|
531
|
-
// ../bungres-orm/src/builders/delete.ts
|
|
532
534
|
class DeleteBuilder {
|
|
533
535
|
_table;
|
|
534
536
|
_executor;
|
|
535
537
|
_where = [];
|
|
536
538
|
_returning;
|
|
537
539
|
_comment;
|
|
540
|
+
_with = [];
|
|
538
541
|
constructor(table2, executor) {
|
|
539
542
|
this._table = table2;
|
|
540
543
|
this._executor = executor;
|
|
@@ -553,6 +556,10 @@ class DeleteBuilder {
|
|
|
553
556
|
}
|
|
554
557
|
return this;
|
|
555
558
|
}
|
|
559
|
+
with(...ctes) {
|
|
560
|
+
this._with.push(...ctes);
|
|
561
|
+
return this;
|
|
562
|
+
}
|
|
556
563
|
returning(...columns) {
|
|
557
564
|
this._returning = columns.length > 0 ? columns : ["*"];
|
|
558
565
|
return this;
|
|
@@ -562,8 +569,20 @@ class DeleteBuilder {
|
|
|
562
569
|
return this;
|
|
563
570
|
}
|
|
564
571
|
toSQL() {
|
|
565
|
-
|
|
572
|
+
const tConfig = getTableConfig(this._table);
|
|
566
573
|
const params = [];
|
|
574
|
+
let prefix = "";
|
|
575
|
+
if (this._with.length > 0) {
|
|
576
|
+
const cteStrs = [];
|
|
577
|
+
for (const cte of this._with) {
|
|
578
|
+
const chunk = cte.query.toSQL();
|
|
579
|
+
const offset = params.length;
|
|
580
|
+
params.push(...chunk.params);
|
|
581
|
+
cteStrs.push(`"${cte.alias}" AS (${chunk.sql.replace(/\$(\d+)/g, (_, n) => `$${parseInt(n) + offset}`)})`);
|
|
582
|
+
}
|
|
583
|
+
prefix = `WITH ${cteStrs.join(", ")} `;
|
|
584
|
+
}
|
|
585
|
+
let query = `DELETE FROM ${tConfig.qualifiedName}`;
|
|
567
586
|
if (this._where.length > 0) {
|
|
568
587
|
const combined = sqlJoin(this._where, " AND ");
|
|
569
588
|
query += " WHERE " + combined.sql;
|
|
@@ -575,17 +594,19 @@ class DeleteBuilder {
|
|
|
575
594
|
if (this._comment) {
|
|
576
595
|
query += ` /* ${this._comment.replace(/\*\//g, "")} */`;
|
|
577
596
|
}
|
|
578
|
-
return { sql: query, params };
|
|
597
|
+
return { sql: prefix + query, params };
|
|
579
598
|
}
|
|
580
599
|
}
|
|
581
|
-
|
|
600
|
+
|
|
582
601
|
class InsertBuilder {
|
|
583
602
|
_table;
|
|
584
603
|
_executor;
|
|
585
604
|
_values = [];
|
|
586
605
|
_onConflict;
|
|
606
|
+
_onConflictUpdateConfig;
|
|
587
607
|
_returning;
|
|
588
608
|
_comment;
|
|
609
|
+
_with = [];
|
|
589
610
|
constructor(table2, executor) {
|
|
590
611
|
this._table = table2;
|
|
591
612
|
this._executor = executor;
|
|
@@ -612,6 +633,14 @@ class InsertBuilder {
|
|
|
612
633
|
this._onConflict = clause;
|
|
613
634
|
return this;
|
|
614
635
|
}
|
|
636
|
+
onConflictDoUpdate(config) {
|
|
637
|
+
this._onConflictUpdateConfig = config;
|
|
638
|
+
return this;
|
|
639
|
+
}
|
|
640
|
+
with(...ctes) {
|
|
641
|
+
this._with.push(...ctes);
|
|
642
|
+
return this;
|
|
643
|
+
}
|
|
615
644
|
returning(...columns) {
|
|
616
645
|
this._returning = columns.length > 0 ? columns : ["*"];
|
|
617
646
|
return this;
|
|
@@ -637,6 +666,17 @@ class InsertBuilder {
|
|
|
637
666
|
}
|
|
638
667
|
const columnsStr = keys.map((k) => `"${tConfig.columns[k]?.name ?? k}"`).join(", ");
|
|
639
668
|
const params = [];
|
|
669
|
+
let prefix = "";
|
|
670
|
+
if (this._with.length > 0) {
|
|
671
|
+
const cteStrs = [];
|
|
672
|
+
for (const cte of this._with) {
|
|
673
|
+
const chunk = cte.query.toSQL();
|
|
674
|
+
const offset = params.length;
|
|
675
|
+
params.push(...chunk.params);
|
|
676
|
+
cteStrs.push(`"${cte.alias}" AS (${chunk.sql.replace(/\$(\d+)/g, (_, n) => `$${parseInt(n) + offset}`)})`);
|
|
677
|
+
}
|
|
678
|
+
prefix = `WITH ${cteStrs.join(", ")} `;
|
|
679
|
+
}
|
|
640
680
|
const valuesStrs = this._values.map((v) => {
|
|
641
681
|
const vals = keys.map((k) => {
|
|
642
682
|
const val = v[k];
|
|
@@ -680,6 +720,52 @@ class InsertBuilder {
|
|
|
680
720
|
query += " " + this._onConflict.sql.replace(/\$(\d+)/g, (_, n) => `$${parseInt(n) + offset}`);
|
|
681
721
|
params.push(...this._onConflict.params);
|
|
682
722
|
}
|
|
723
|
+
} else if (this._onConflictUpdateConfig) {
|
|
724
|
+
const config = this._onConflictUpdateConfig;
|
|
725
|
+
const targets = Array.isArray(config.target) ? config.target : [config.target];
|
|
726
|
+
const targetStrs = [];
|
|
727
|
+
for (const t of targets) {
|
|
728
|
+
if (typeof t === "string") {
|
|
729
|
+
targetStrs.push(`"${tConfig.columns[t]?.name ?? t}"`);
|
|
730
|
+
} else {
|
|
731
|
+
const offset = params.length;
|
|
732
|
+
targetStrs.push(t.sql.replace(/\$(\d+)/g, (_, n) => `$${parseInt(n) + offset}`));
|
|
733
|
+
params.push(...t.params);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
const setEntries = Object.entries(config.set);
|
|
737
|
+
if (setEntries.length === 0)
|
|
738
|
+
throw new Error("InsertBuilder: onConflictDoUpdate requires 'set' fields");
|
|
739
|
+
const setClauses = setEntries.map(([key, value]) => {
|
|
740
|
+
const dbCol = tConfig.columns[key]?.name ?? key;
|
|
741
|
+
if (value && typeof value === "object" && "sql" in value && "params" in value) {
|
|
742
|
+
const chunk = value;
|
|
743
|
+
const offset = params.length;
|
|
744
|
+
params.push(...chunk.params);
|
|
745
|
+
return `"${dbCol}" = ${chunk.sql.replace(/\$(\d+)/g, (_, n) => `$${parseInt(n) + offset}`)}`;
|
|
746
|
+
}
|
|
747
|
+
if (value && typeof value === "object" && !(value instanceof Date)) {
|
|
748
|
+
const colType = tConfig.columns[key]?.dataType;
|
|
749
|
+
if (colType === "json" || colType === "jsonb") {
|
|
750
|
+
params.push(value);
|
|
751
|
+
} else if (Array.isArray(value)) {
|
|
752
|
+
const pgArray = "{" + value.map((item) => {
|
|
753
|
+
if (item === null || item === undefined)
|
|
754
|
+
return "NULL";
|
|
755
|
+
if (typeof item === "string")
|
|
756
|
+
return '"' + item.replace(/"/g, "\\\"") + '"';
|
|
757
|
+
return typeof item === "object" ? '"' + JSON.stringify(item).replace(/"/g, "\\\"") + '"' : String(item);
|
|
758
|
+
}).join(",") + "}";
|
|
759
|
+
params.push(pgArray);
|
|
760
|
+
} else {
|
|
761
|
+
params.push(JSON.stringify(value));
|
|
762
|
+
}
|
|
763
|
+
return `"${dbCol}" = $${params.length}`;
|
|
764
|
+
}
|
|
765
|
+
params.push(value);
|
|
766
|
+
return `"${dbCol}" = $${params.length}`;
|
|
767
|
+
});
|
|
768
|
+
query += ` ON CONFLICT (${targetStrs.join(", ")}) DO UPDATE SET ${setClauses.join(", ")}`;
|
|
683
769
|
}
|
|
684
770
|
if (this._returning) {
|
|
685
771
|
query += " RETURNING " + (this._returning[0] === "*" ? Object.keys(tConfig.columns).map((c) => `"${tConfig.columns[c].name}" AS "${c}"`).join(", ") : this._returning.map((c) => `"${tConfig.columns[c]?.name ?? c}" AS "${c}"`).join(", "));
|
|
@@ -687,10 +773,10 @@ class InsertBuilder {
|
|
|
687
773
|
if (this._comment) {
|
|
688
774
|
query += ` /* ${this._comment.replace(/\*\//g, "")} */`;
|
|
689
775
|
}
|
|
690
|
-
return { sql: query, params };
|
|
776
|
+
return { sql: prefix + query, params };
|
|
691
777
|
}
|
|
692
778
|
}
|
|
693
|
-
|
|
779
|
+
|
|
694
780
|
class SelectBuilder {
|
|
695
781
|
_table;
|
|
696
782
|
_executor;
|
|
@@ -703,6 +789,8 @@ class SelectBuilder {
|
|
|
703
789
|
_select;
|
|
704
790
|
_selection;
|
|
705
791
|
_joins = [];
|
|
792
|
+
_with = [];
|
|
793
|
+
_setOperations = [];
|
|
706
794
|
_comment;
|
|
707
795
|
constructor(table2, executor, selection) {
|
|
708
796
|
this._table = table2;
|
|
@@ -713,12 +801,35 @@ class SelectBuilder {
|
|
|
713
801
|
return this._executor.execute(this).then(onfulfilled, onrejected);
|
|
714
802
|
}
|
|
715
803
|
async single() {
|
|
804
|
+
if (this._limit === undefined) {
|
|
805
|
+
this.limit(1);
|
|
806
|
+
}
|
|
716
807
|
return this._executor.executeSingle(this);
|
|
717
808
|
}
|
|
718
809
|
select(...columns) {
|
|
719
810
|
this._select = columns;
|
|
720
811
|
return this;
|
|
721
812
|
}
|
|
813
|
+
with(...ctes) {
|
|
814
|
+
this._with.push(...ctes);
|
|
815
|
+
return this;
|
|
816
|
+
}
|
|
817
|
+
union(other) {
|
|
818
|
+
this._setOperations.push({ type: "UNION", builder: other });
|
|
819
|
+
return this;
|
|
820
|
+
}
|
|
821
|
+
unionAll(other) {
|
|
822
|
+
this._setOperations.push({ type: "UNION ALL", builder: other });
|
|
823
|
+
return this;
|
|
824
|
+
}
|
|
825
|
+
intersect(other) {
|
|
826
|
+
this._setOperations.push({ type: "INTERSECT", builder: other });
|
|
827
|
+
return this;
|
|
828
|
+
}
|
|
829
|
+
except(other) {
|
|
830
|
+
this._setOperations.push({ type: "EXCEPT", builder: other });
|
|
831
|
+
return this;
|
|
832
|
+
}
|
|
722
833
|
comment(tag) {
|
|
723
834
|
this._comment = tag;
|
|
724
835
|
return this;
|
|
@@ -821,7 +932,23 @@ class SelectBuilder {
|
|
|
821
932
|
}).join(", ");
|
|
822
933
|
} else {
|
|
823
934
|
const qName = getTableConfig(this._table).qualifiedName;
|
|
824
|
-
|
|
935
|
+
const colsKeys = Object.keys(getTableConfig(this._table).columns);
|
|
936
|
+
if (colsKeys.length === 0) {
|
|
937
|
+
cols = "*";
|
|
938
|
+
} else {
|
|
939
|
+
cols = colsKeys.map((c) => `${qName}."${getTableConfig(this._table).columns[c].name}" AS "${c}"`).join(", ");
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
let prefix = "";
|
|
943
|
+
if (this._with.length > 0) {
|
|
944
|
+
const cteStrs = [];
|
|
945
|
+
for (const cte of this._with) {
|
|
946
|
+
const chunk = cte.query.toSQL();
|
|
947
|
+
const offset = params.length;
|
|
948
|
+
params.push(...chunk.params);
|
|
949
|
+
cteStrs.push(`"${cte.alias}" AS (${chunk.sql.replace(/\$(\d+)/g, (_, n) => `$${parseInt(n) + offset}`)})`);
|
|
950
|
+
}
|
|
951
|
+
prefix = `WITH ${cteStrs.join(", ")} `;
|
|
825
952
|
}
|
|
826
953
|
let query = `SELECT ${cols} FROM ${getTableConfig(this._table).qualifiedName}`;
|
|
827
954
|
if (this._joins.length > 0) {
|
|
@@ -881,10 +1008,18 @@ class SelectBuilder {
|
|
|
881
1008
|
params.push(this._offset);
|
|
882
1009
|
query += ` OFFSET $${params.length}`;
|
|
883
1010
|
}
|
|
1011
|
+
if (this._setOperations.length > 0) {
|
|
1012
|
+
for (const op of this._setOperations) {
|
|
1013
|
+
const chunk = op.builder.toSQL();
|
|
1014
|
+
const offset = params.length;
|
|
1015
|
+
params.push(...chunk.params);
|
|
1016
|
+
query += ` ${op.type} ${chunk.sql.replace(/\$(\d+)/g, (_, n) => `$${parseInt(n) + offset}`)}`;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
884
1019
|
if (this._comment) {
|
|
885
1020
|
query += ` /* ${this._comment.replace(/\*\//g, "")} */`;
|
|
886
1021
|
}
|
|
887
|
-
return { sql: query, params };
|
|
1022
|
+
return { sql: prefix + query, params };
|
|
888
1023
|
}
|
|
889
1024
|
}
|
|
890
1025
|
|
|
@@ -899,7 +1034,7 @@ class SelectBuilderIntermediate {
|
|
|
899
1034
|
return new SelectBuilder(table2, this._executor, this._selection);
|
|
900
1035
|
}
|
|
901
1036
|
}
|
|
902
|
-
|
|
1037
|
+
|
|
903
1038
|
class UpdateBuilder {
|
|
904
1039
|
_table;
|
|
905
1040
|
_executor;
|
|
@@ -907,6 +1042,7 @@ class UpdateBuilder {
|
|
|
907
1042
|
_where = [];
|
|
908
1043
|
_returning;
|
|
909
1044
|
_comment;
|
|
1045
|
+
_with = [];
|
|
910
1046
|
constructor(table2, executor) {
|
|
911
1047
|
this._table = table2;
|
|
912
1048
|
this._executor = executor;
|
|
@@ -929,6 +1065,10 @@ class UpdateBuilder {
|
|
|
929
1065
|
}
|
|
930
1066
|
return this;
|
|
931
1067
|
}
|
|
1068
|
+
with(...ctes) {
|
|
1069
|
+
this._with.push(...ctes);
|
|
1070
|
+
return this;
|
|
1071
|
+
}
|
|
932
1072
|
returning(...columns) {
|
|
933
1073
|
this._returning = columns.length > 0 ? columns : ["*"];
|
|
934
1074
|
return this;
|
|
@@ -943,6 +1083,17 @@ class UpdateBuilder {
|
|
|
943
1083
|
throw new Error("UpdateBuilder: no fields to set");
|
|
944
1084
|
}
|
|
945
1085
|
const params = [];
|
|
1086
|
+
let prefix = "";
|
|
1087
|
+
if (this._with.length > 0) {
|
|
1088
|
+
const cteStrs = [];
|
|
1089
|
+
for (const cte of this._with) {
|
|
1090
|
+
const chunk = cte.query.toSQL();
|
|
1091
|
+
const offset = params.length;
|
|
1092
|
+
params.push(...chunk.params);
|
|
1093
|
+
cteStrs.push(`"${cte.alias}" AS (${chunk.sql.replace(/\$(\d+)/g, (_, n) => `$${parseInt(n) + offset}`)})`);
|
|
1094
|
+
}
|
|
1095
|
+
prefix = `WITH ${cteStrs.join(", ")} `;
|
|
1096
|
+
}
|
|
946
1097
|
const setClauses = entries.map(([key, value]) => {
|
|
947
1098
|
const dbCol = getTableConfig(this._table).columns[key]?.name ?? key;
|
|
948
1099
|
if (value && typeof value === "object" && "sql" in value && "params" in value) {
|
|
@@ -985,10 +1136,9 @@ class UpdateBuilder {
|
|
|
985
1136
|
if (this._comment) {
|
|
986
1137
|
query += ` /* ${this._comment.replace(/\*\//g, "")} */`;
|
|
987
1138
|
}
|
|
988
|
-
return { sql: query, params };
|
|
1139
|
+
return { sql: prefix + query, params };
|
|
989
1140
|
}
|
|
990
1141
|
}
|
|
991
|
-
// ../bungres-orm/src/builders/relational.ts
|
|
992
1142
|
function getPkColumn(tableConfig) {
|
|
993
1143
|
if (tableConfig.primaryKeys?.length > 0)
|
|
994
1144
|
return tableConfig.primaryKeys[0];
|
|
@@ -1246,8 +1396,6 @@ class RelationalQueryBuilder {
|
|
|
1246
1396
|
return { sql: sql2, params };
|
|
1247
1397
|
}
|
|
1248
1398
|
}
|
|
1249
|
-
|
|
1250
|
-
// ../bungres-orm/src/core/db.ts
|
|
1251
1399
|
function parseDBName(url) {
|
|
1252
1400
|
try {
|
|
1253
1401
|
return new URL(url).pathname.slice(1);
|
|
@@ -1356,6 +1504,7 @@ class BungresDB {
|
|
|
1356
1504
|
|
|
1357
1505
|
class BungresTransaction {
|
|
1358
1506
|
_sql;
|
|
1507
|
+
_savepointCounter = 0;
|
|
1359
1508
|
constructor(sql2) {
|
|
1360
1509
|
this._sql = sql2;
|
|
1361
1510
|
}
|
|
@@ -1390,6 +1539,19 @@ class BungresTransaction {
|
|
|
1390
1539
|
const result = await this._sql.unsafe(query, params);
|
|
1391
1540
|
return Array.from(result);
|
|
1392
1541
|
}
|
|
1542
|
+
async transaction(fn) {
|
|
1543
|
+
this._savepointCounter++;
|
|
1544
|
+
const spName = `sp_${this._savepointCounter}`;
|
|
1545
|
+
await this._sql.unsafe(`SAVEPOINT ${spName}`);
|
|
1546
|
+
try {
|
|
1547
|
+
const result = await fn(this);
|
|
1548
|
+
await this._sql.unsafe(`RELEASE SAVEPOINT ${spName}`);
|
|
1549
|
+
return result;
|
|
1550
|
+
} catch (e) {
|
|
1551
|
+
await this._sql.unsafe(`ROLLBACK TO SAVEPOINT ${spName}`);
|
|
1552
|
+
throw e;
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1393
1555
|
}
|
|
1394
1556
|
function bungres(config) {
|
|
1395
1557
|
const db = new BungresDB(config);
|
|
@@ -1413,7 +1575,6 @@ function bungres(config) {
|
|
|
1413
1575
|
}
|
|
1414
1576
|
return db;
|
|
1415
1577
|
}
|
|
1416
|
-
// ../bungres-orm/src/ddl.ts
|
|
1417
1578
|
function generateCreateTable(config, ifNotExists = true) {
|
|
1418
1579
|
const tableName = config.schema ? `"${config.schema}"."${config.name}"` : `"${config.name}"`;
|
|
1419
1580
|
const exists = ifNotExists ? " IF NOT EXISTS" : "";
|
|
@@ -1423,8 +1584,8 @@ function generateCreateTable(config, ifNotExists = true) {
|
|
|
1423
1584
|
columnDefs.push(`PRIMARY KEY (${pkCols})`);
|
|
1424
1585
|
}
|
|
1425
1586
|
if (config.checks) {
|
|
1426
|
-
for (const
|
|
1427
|
-
columnDefs.push(`CHECK (${
|
|
1587
|
+
for (const check2 of config.checks) {
|
|
1588
|
+
columnDefs.push(`CHECK (${check2})`);
|
|
1428
1589
|
}
|
|
1429
1590
|
}
|
|
1430
1591
|
let sql2 = `CREATE TABLE${exists} ${tableName} (
|
|
@@ -1462,7 +1623,9 @@ function buildColumnDDL(key, col2, tableName) {
|
|
|
1462
1623
|
if (col2.unique && !col2.primaryKey) {
|
|
1463
1624
|
parts.push("UNIQUE");
|
|
1464
1625
|
}
|
|
1465
|
-
if (col2.
|
|
1626
|
+
if (col2.generatedAs) {
|
|
1627
|
+
parts.push(`GENERATED ALWAYS AS (${col2.generatedAs}) STORED`);
|
|
1628
|
+
} else if (col2.defaultFn) {
|
|
1466
1629
|
parts.push(`DEFAULT ${col2.defaultFn}`);
|
|
1467
1630
|
} else if (col2.defaultValue !== undefined) {
|
|
1468
1631
|
parts.push(`DEFAULT ${formatDefaultValue(col2.defaultValue, col2.dataType)}`);
|
|
@@ -1483,6 +1646,13 @@ function buildColumnDDL(key, col2, tableName) {
|
|
|
1483
1646
|
return parts.join(" ");
|
|
1484
1647
|
}
|
|
1485
1648
|
function buildType(col2) {
|
|
1649
|
+
if (col2.enumConfig) {
|
|
1650
|
+
return `"${col2.enumConfig.enumName}"`;
|
|
1651
|
+
}
|
|
1652
|
+
if (col2.dataType.endsWith("[]")) {
|
|
1653
|
+
const baseType = buildType({ ...col2, dataType: col2.dataType.slice(0, -2) });
|
|
1654
|
+
return `${baseType}[]`;
|
|
1655
|
+
}
|
|
1486
1656
|
switch (col2.dataType) {
|
|
1487
1657
|
case "varchar":
|
|
1488
1658
|
return col2.length ? `VARCHAR(${col2.length})` : "VARCHAR";
|
|
@@ -1517,12 +1687,12 @@ function formatDefaultValue(value, dataType) {
|
|
|
1517
1687
|
}
|
|
1518
1688
|
function buildIndex(table2, idx) {
|
|
1519
1689
|
const tableName = table2.schema ? `"${table2.schema}"."${table2.name}"` : `"${table2.name}"`;
|
|
1520
|
-
const
|
|
1690
|
+
const unique2 = idx.unique ? "UNIQUE " : "";
|
|
1521
1691
|
const using = idx.using ? ` USING ${idx.using.toUpperCase()}` : "";
|
|
1522
1692
|
const cols = idx.columns.map((c) => `"${c}"`).join(", ");
|
|
1523
1693
|
const where = idx.where ? ` WHERE ${idx.where}` : "";
|
|
1524
1694
|
const idxName = idx.name ?? `idx_${table2.name}_${idx.columns.join("_")}`;
|
|
1525
|
-
return `CREATE ${
|
|
1695
|
+
return `CREATE ${unique2}INDEX IF NOT EXISTS "${idxName}" ON ${tableName}${using} (${cols})${where};`;
|
|
1526
1696
|
}
|
|
1527
1697
|
function generateAddColumn(tableName, schema, key, col2) {
|
|
1528
1698
|
const tbl = schema ? `"${schema}"."${tableName}"` : `"${tableName}"`;
|
|
@@ -1532,6 +1702,7 @@ function generateDropColumn(tableName, schema, columnName) {
|
|
|
1532
1702
|
const tbl = schema ? `"${schema}"."${tableName}"` : `"${tableName}"`;
|
|
1533
1703
|
return `ALTER TABLE ${tbl} DROP COLUMN IF EXISTS "${columnName}";`;
|
|
1534
1704
|
}
|
|
1705
|
+
|
|
1535
1706
|
// src/schema-loader.ts
|
|
1536
1707
|
async function loadSchemas(patterns, cwd = process.cwd()) {
|
|
1537
1708
|
const globs = Array.isArray(patterns) ? patterns : [patterns];
|
|
@@ -1643,11 +1814,11 @@ function diffSchemas(prev, next) {
|
|
|
1643
1814
|
const idxName = idx.name ?? `idx_${tableName}_${idx.columns.join("_")}`;
|
|
1644
1815
|
if (!prevIdxNames.has(idxName)) {
|
|
1645
1816
|
const tbl = nextConfig.schema ? `"${nextConfig.schema}"."${tableName}"` : `"${tableName}"`;
|
|
1646
|
-
const
|
|
1817
|
+
const unique = idx.unique ? "UNIQUE " : "";
|
|
1647
1818
|
const using = idx.using ? ` USING ${idx.using.toUpperCase()}` : "";
|
|
1648
1819
|
const cols = idx.columns.map((c) => `"${c}"`).join(", ");
|
|
1649
1820
|
const where = idx.where ? ` WHERE ${idx.where}` : "";
|
|
1650
|
-
statements.push(`CREATE ${
|
|
1821
|
+
statements.push(`CREATE ${unique}INDEX IF NOT EXISTS "${idxName}" ON ${tbl}${using} (${cols})${where};`);
|
|
1651
1822
|
summary.push(`CREATE INDEX ${idxName} ON ${tableName}`);
|
|
1652
1823
|
}
|
|
1653
1824
|
}
|
|
@@ -1785,7 +1956,7 @@ var getStringTruncatedWidth = (input, truncationOptions = {}, widthOptions = {})
|
|
|
1785
1956
|
[CJKT_WIDE_RE, WIDE_WIDTH]
|
|
1786
1957
|
];
|
|
1787
1958
|
let indexPrev = 0;
|
|
1788
|
-
let
|
|
1959
|
+
let index = 0;
|
|
1789
1960
|
let length = input.length;
|
|
1790
1961
|
let lengthExtra = 0;
|
|
1791
1962
|
let truncationEnabled = false;
|
|
@@ -1797,8 +1968,8 @@ var getStringTruncatedWidth = (input, truncationOptions = {}, widthOptions = {})
|
|
|
1797
1968
|
let widthExtra = 0;
|
|
1798
1969
|
outer:
|
|
1799
1970
|
while (true) {
|
|
1800
|
-
if (unmatchedEnd > unmatchedStart ||
|
|
1801
|
-
const unmatched = input.slice(unmatchedStart, unmatchedEnd) || input.slice(indexPrev,
|
|
1971
|
+
if (unmatchedEnd > unmatchedStart || index >= length && index > indexPrev) {
|
|
1972
|
+
const unmatched = input.slice(unmatchedStart, unmatchedEnd) || input.slice(indexPrev, index);
|
|
1802
1973
|
lengthExtra = 0;
|
|
1803
1974
|
for (const char of unmatched.replaceAll(MODIFIER_RE, "")) {
|
|
1804
1975
|
const codePoint = char.codePointAt(0) || 0;
|
|
@@ -1821,17 +1992,17 @@ var getStringTruncatedWidth = (input, truncationOptions = {}, widthOptions = {})
|
|
|
1821
1992
|
}
|
|
1822
1993
|
unmatchedStart = unmatchedEnd = 0;
|
|
1823
1994
|
}
|
|
1824
|
-
if (
|
|
1995
|
+
if (index >= length) {
|
|
1825
1996
|
break outer;
|
|
1826
1997
|
}
|
|
1827
1998
|
for (let i = 0, l = PARSE_BLOCKS.length;i < l; i++) {
|
|
1828
1999
|
const [BLOCK_RE, BLOCK_WIDTH] = PARSE_BLOCKS[i];
|
|
1829
|
-
BLOCK_RE.lastIndex =
|
|
2000
|
+
BLOCK_RE.lastIndex = index;
|
|
1830
2001
|
if (BLOCK_RE.test(input)) {
|
|
1831
|
-
lengthExtra = BLOCK_RE === CJKT_WIDE_RE ? getCodePointsLength(input.slice(
|
|
2002
|
+
lengthExtra = BLOCK_RE === CJKT_WIDE_RE ? getCodePointsLength(input.slice(index, BLOCK_RE.lastIndex)) : BLOCK_RE === EMOJI_RE ? 1 : BLOCK_RE.lastIndex - index;
|
|
1832
2003
|
widthExtra = lengthExtra * BLOCK_WIDTH;
|
|
1833
2004
|
if (width + widthExtra > truncationLimit) {
|
|
1834
|
-
truncationIndex = Math.min(truncationIndex,
|
|
2005
|
+
truncationIndex = Math.min(truncationIndex, index + Math.floor((truncationLimit - width) / BLOCK_WIDTH));
|
|
1835
2006
|
}
|
|
1836
2007
|
if (width + widthExtra > LIMIT) {
|
|
1837
2008
|
truncationEnabled = true;
|
|
@@ -1839,12 +2010,12 @@ var getStringTruncatedWidth = (input, truncationOptions = {}, widthOptions = {})
|
|
|
1839
2010
|
}
|
|
1840
2011
|
width += widthExtra;
|
|
1841
2012
|
unmatchedStart = indexPrev;
|
|
1842
|
-
unmatchedEnd =
|
|
1843
|
-
|
|
2013
|
+
unmatchedEnd = index;
|
|
2014
|
+
index = indexPrev = BLOCK_RE.lastIndex;
|
|
1844
2015
|
continue outer;
|
|
1845
2016
|
}
|
|
1846
2017
|
}
|
|
1847
|
-
|
|
2018
|
+
index += 1;
|
|
1848
2019
|
}
|
|
1849
2020
|
return {
|
|
1850
2021
|
width: truncationEnabled ? truncationLimit : width,
|
|
@@ -1903,7 +2074,7 @@ var getClosingCode = (openingCode) => {
|
|
|
1903
2074
|
};
|
|
1904
2075
|
var wrapAnsiCode = (code) => `${ESC}${ANSI_CSI}${code}${ANSI_SGR_TERMINATOR}`;
|
|
1905
2076
|
var wrapAnsiHyperlink = (url) => `${ESC}${ANSI_ESCAPE_LINK}${url}${ANSI_ESCAPE_BELL}`;
|
|
1906
|
-
var wrapWord = (rows, word,
|
|
2077
|
+
var wrapWord = (rows, word, columns) => {
|
|
1907
2078
|
const characters = word[Symbol.iterator]();
|
|
1908
2079
|
let isInsideEscape = false;
|
|
1909
2080
|
let isInsideLinkEscape = false;
|
|
@@ -1915,7 +2086,7 @@ var wrapWord = (rows, word, columns2) => {
|
|
|
1915
2086
|
while (!currentCharacter.done) {
|
|
1916
2087
|
const character = currentCharacter.value;
|
|
1917
2088
|
const characterLength = dist_default2(character);
|
|
1918
|
-
if (visible + characterLength <=
|
|
2089
|
+
if (visible + characterLength <= columns) {
|
|
1919
2090
|
rows[rows.length - 1] += character;
|
|
1920
2091
|
} else {
|
|
1921
2092
|
rows.push(character);
|
|
@@ -1936,7 +2107,7 @@ var wrapWord = (rows, word, columns2) => {
|
|
|
1936
2107
|
}
|
|
1937
2108
|
} else {
|
|
1938
2109
|
visible += characterLength;
|
|
1939
|
-
if (visible ===
|
|
2110
|
+
if (visible === columns && !nextCharacter.done) {
|
|
1940
2111
|
rows.push("");
|
|
1941
2112
|
visible = 0;
|
|
1942
2113
|
}
|
|
@@ -1964,7 +2135,7 @@ var stringVisibleTrimSpacesRight = (string) => {
|
|
|
1964
2135
|
}
|
|
1965
2136
|
return words.slice(0, last).join(" ") + words.slice(last).join("");
|
|
1966
2137
|
};
|
|
1967
|
-
var exec = (string,
|
|
2138
|
+
var exec = (string, columns, options = {}) => {
|
|
1968
2139
|
if (options.trim !== false && string.trim() === "") {
|
|
1969
2140
|
return "";
|
|
1970
2141
|
}
|
|
@@ -1974,8 +2145,8 @@ var exec = (string, columns2, options = {}) => {
|
|
|
1974
2145
|
const words = string.split(" ");
|
|
1975
2146
|
let rows = [""];
|
|
1976
2147
|
let rowLength = 0;
|
|
1977
|
-
for (let
|
|
1978
|
-
const word = words[
|
|
2148
|
+
for (let index = 0;index < words.length; index++) {
|
|
2149
|
+
const word = words[index];
|
|
1979
2150
|
if (options.trim !== false) {
|
|
1980
2151
|
const row = rows.at(-1) ?? "";
|
|
1981
2152
|
const trimmed = row.trimStart();
|
|
@@ -1984,8 +2155,8 @@ var exec = (string, columns2, options = {}) => {
|
|
|
1984
2155
|
rowLength = dist_default2(trimmed);
|
|
1985
2156
|
}
|
|
1986
2157
|
}
|
|
1987
|
-
if (
|
|
1988
|
-
if (rowLength >=
|
|
2158
|
+
if (index !== 0) {
|
|
2159
|
+
if (rowLength >= columns && (options.wordWrap === false || options.trim === false)) {
|
|
1989
2160
|
rows.push("");
|
|
1990
2161
|
rowLength = 0;
|
|
1991
2162
|
}
|
|
@@ -1995,28 +2166,28 @@ var exec = (string, columns2, options = {}) => {
|
|
|
1995
2166
|
}
|
|
1996
2167
|
}
|
|
1997
2168
|
const wordLength = dist_default2(word);
|
|
1998
|
-
if (options.hard && wordLength >
|
|
1999
|
-
const remainingColumns =
|
|
2000
|
-
const breaksStartingThisLine = 1 + Math.floor((wordLength - remainingColumns - 1) /
|
|
2001
|
-
const breaksStartingNextLine = Math.floor((wordLength - 1) /
|
|
2169
|
+
if (options.hard && wordLength > columns) {
|
|
2170
|
+
const remainingColumns = columns - rowLength;
|
|
2171
|
+
const breaksStartingThisLine = 1 + Math.floor((wordLength - remainingColumns - 1) / columns);
|
|
2172
|
+
const breaksStartingNextLine = Math.floor((wordLength - 1) / columns);
|
|
2002
2173
|
if (breaksStartingNextLine < breaksStartingThisLine) {
|
|
2003
2174
|
rows.push("");
|
|
2004
2175
|
}
|
|
2005
|
-
wrapWord(rows, word,
|
|
2176
|
+
wrapWord(rows, word, columns);
|
|
2006
2177
|
rowLength = dist_default2(rows.at(-1) ?? "");
|
|
2007
2178
|
continue;
|
|
2008
2179
|
}
|
|
2009
|
-
if (rowLength + wordLength >
|
|
2010
|
-
if (options.wordWrap === false && rowLength <
|
|
2011
|
-
wrapWord(rows, word,
|
|
2180
|
+
if (rowLength + wordLength > columns && rowLength && wordLength) {
|
|
2181
|
+
if (options.wordWrap === false && rowLength < columns) {
|
|
2182
|
+
wrapWord(rows, word, columns);
|
|
2012
2183
|
rowLength = dist_default2(rows.at(-1) ?? "");
|
|
2013
2184
|
continue;
|
|
2014
2185
|
}
|
|
2015
2186
|
rows.push("");
|
|
2016
2187
|
rowLength = 0;
|
|
2017
2188
|
}
|
|
2018
|
-
if (rowLength + wordLength >
|
|
2019
|
-
wrapWord(rows, word,
|
|
2189
|
+
if (rowLength + wordLength > columns && options.wordWrap === false) {
|
|
2190
|
+
wrapWord(rows, word, columns);
|
|
2020
2191
|
rowLength = dist_default2(rows.at(-1) ?? "");
|
|
2021
2192
|
continue;
|
|
2022
2193
|
}
|
|
@@ -2073,8 +2244,8 @@ var exec = (string, columns2, options = {}) => {
|
|
|
2073
2244
|
return returnValue;
|
|
2074
2245
|
};
|
|
2075
2246
|
var CRLF_OR_LF = /\r?\n/;
|
|
2076
|
-
function wrapAnsi(string,
|
|
2077
|
-
return String(string).normalize().split(CRLF_OR_LF).map((line) => exec(line,
|
|
2247
|
+
function wrapAnsi(string, columns, options) {
|
|
2248
|
+
return String(string).normalize().split(CRLF_OR_LF).map((line) => exec(line, columns, options)).join(`
|
|
2078
2249
|
`);
|
|
2079
2250
|
}
|
|
2080
2251
|
|
|
@@ -3398,7 +3569,7 @@ ${upContent}
|
|
|
3398
3569
|
// src/commands/pull.ts
|
|
3399
3570
|
import { resolve as resolve5, join as join5 } from "path";
|
|
3400
3571
|
async function introspectDb(sql2, dbSchema) {
|
|
3401
|
-
const
|
|
3572
|
+
const columns = await sql2.unsafe(`SELECT
|
|
3402
3573
|
c.table_name,
|
|
3403
3574
|
c.column_name,
|
|
3404
3575
|
c.data_type,
|
|
@@ -3432,7 +3603,7 @@ async function introspectDb(sql2, dbSchema) {
|
|
|
3432
3603
|
const indexes = await sql2.unsafe(`SELECT tablename, indexname, indexdef
|
|
3433
3604
|
FROM pg_indexes
|
|
3434
3605
|
WHERE schemaname = $1`, [dbSchema]);
|
|
3435
|
-
return groupByTable(
|
|
3606
|
+
return groupByTable(columns, constraints, indexes);
|
|
3436
3607
|
}
|
|
3437
3608
|
async function runPull(config) {
|
|
3438
3609
|
console.log("@bungres/kit pull: introspecting database...");
|
|
@@ -3455,9 +3626,9 @@ async function runPull(config) {
|
|
|
3455
3626
|
await sql2.end();
|
|
3456
3627
|
}
|
|
3457
3628
|
}
|
|
3458
|
-
function groupByTable(
|
|
3629
|
+
function groupByTable(columns, constraints, indexes) {
|
|
3459
3630
|
const map = new Map;
|
|
3460
|
-
for (const col2 of
|
|
3631
|
+
for (const col2 of columns) {
|
|
3461
3632
|
if (!map.has(col2.table_name)) {
|
|
3462
3633
|
map.set(col2.table_name, {
|
|
3463
3634
|
tableName: col2.table_name,
|