@bungres/kit 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +80 -54
- package/dist/index.js +80 -54
- package/package.json +2 -2
- package/src/commands/pull.ts +56 -56
package/dist/cli.js
CHANGED
|
@@ -41,7 +41,7 @@ async function loadConfig(cwd = process.cwd()) {
|
|
|
41
41
|
// src/commands/push.ts
|
|
42
42
|
import * as fs from "fs";
|
|
43
43
|
|
|
44
|
-
//
|
|
44
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/table.ts
|
|
45
45
|
var TableConfigSymbol = Symbol.for("BungresTableConfig");
|
|
46
46
|
function getTableConfig(table) {
|
|
47
47
|
return table[TableConfigSymbol];
|
|
@@ -81,7 +81,7 @@ function createTableFactory(casing) {
|
|
|
81
81
|
var table = createTableFactory("none");
|
|
82
82
|
var snakeCase = { table: createTableFactory("snake") };
|
|
83
83
|
var camelCase = { table: createTableFactory("camel") };
|
|
84
|
-
//
|
|
84
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/column.ts
|
|
85
85
|
function buildColumn(dataType, nameOrOpts, opts) {
|
|
86
86
|
let name = "";
|
|
87
87
|
let options = opts;
|
|
@@ -135,7 +135,7 @@ var interval = col("interval");
|
|
|
135
135
|
var inet = col("inet");
|
|
136
136
|
var cidr = col("cidr");
|
|
137
137
|
var macaddr = col("macaddr");
|
|
138
|
-
//
|
|
138
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/sql.ts
|
|
139
139
|
function sqlJoin(chunks, separator = ", ") {
|
|
140
140
|
const params = [];
|
|
141
141
|
const parts = [];
|
|
@@ -146,7 +146,7 @@ function sqlJoin(chunks, separator = ", ") {
|
|
|
146
146
|
}
|
|
147
147
|
return { sql: parts.join(separator), params };
|
|
148
148
|
}
|
|
149
|
-
//
|
|
149
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/query.ts
|
|
150
150
|
class SelectBuilder {
|
|
151
151
|
_table;
|
|
152
152
|
_executor;
|
|
@@ -166,6 +166,9 @@ class SelectBuilder {
|
|
|
166
166
|
then(onfulfilled, onrejected) {
|
|
167
167
|
return this._executor.execute(this).then(onfulfilled, onrejected);
|
|
168
168
|
}
|
|
169
|
+
async single() {
|
|
170
|
+
return this._executor.executeSingle(this);
|
|
171
|
+
}
|
|
169
172
|
select(...columns) {
|
|
170
173
|
this._select = columns;
|
|
171
174
|
return this;
|
|
@@ -266,6 +269,9 @@ class InsertBuilder {
|
|
|
266
269
|
then(onfulfilled, onrejected) {
|
|
267
270
|
return this._executor.execute(this).then(onfulfilled, onrejected);
|
|
268
271
|
}
|
|
272
|
+
async single() {
|
|
273
|
+
return this._executor.executeSingle(this);
|
|
274
|
+
}
|
|
269
275
|
values(data) {
|
|
270
276
|
const rows = Array.isArray(data) ? data : [data];
|
|
271
277
|
this._values.push(...rows);
|
|
@@ -332,6 +338,9 @@ class UpdateBuilder {
|
|
|
332
338
|
then(onfulfilled, onrejected) {
|
|
333
339
|
return this._executor.execute(this).then(onfulfilled, onrejected);
|
|
334
340
|
}
|
|
341
|
+
async single() {
|
|
342
|
+
return this._executor.executeSingle(this);
|
|
343
|
+
}
|
|
335
344
|
set(data) {
|
|
336
345
|
this._set = { ...this._set, ...data };
|
|
337
346
|
return this;
|
|
@@ -386,6 +395,9 @@ class DeleteBuilder {
|
|
|
386
395
|
then(onfulfilled, onrejected) {
|
|
387
396
|
return this._executor.execute(this).then(onfulfilled, onrejected);
|
|
388
397
|
}
|
|
398
|
+
async single() {
|
|
399
|
+
return this._executor.executeSingle(this);
|
|
400
|
+
}
|
|
389
401
|
where(condition) {
|
|
390
402
|
this._where.push(condition);
|
|
391
403
|
return this;
|
|
@@ -412,7 +424,7 @@ class DeleteBuilder {
|
|
|
412
424
|
return { sql: query, params };
|
|
413
425
|
}
|
|
414
426
|
}
|
|
415
|
-
//
|
|
427
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/relational.ts
|
|
416
428
|
var _relationsCache = new WeakMap;
|
|
417
429
|
|
|
418
430
|
class RelationalQueryBuilder {
|
|
@@ -639,7 +651,7 @@ class RelationalQueryBuilder {
|
|
|
639
651
|
}
|
|
640
652
|
}
|
|
641
653
|
|
|
642
|
-
//
|
|
654
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/db.ts
|
|
643
655
|
function parseDBName(url) {
|
|
644
656
|
try {
|
|
645
657
|
return new URL(url).pathname.slice(1);
|
|
@@ -805,7 +817,7 @@ function createDB(config2) {
|
|
|
805
817
|
}
|
|
806
818
|
return db2;
|
|
807
819
|
}
|
|
808
|
-
//
|
|
820
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/ddl.ts
|
|
809
821
|
function generateCreateTable(config2, ifNotExists = true) {
|
|
810
822
|
const tableName = config2.schema ? `"${config2.schema}"."${config2.name}"` : `"${config2.name}"`;
|
|
811
823
|
const exists = ifNotExists ? " IF NOT EXISTS" : "";
|
|
@@ -1455,21 +1467,22 @@ function generateSchemaTS(tableMap, dbSchema) {
|
|
|
1455
1467
|
`// Generated at: ${new Date().toISOString()}`,
|
|
1456
1468
|
``,
|
|
1457
1469
|
`import {`,
|
|
1458
|
-
`
|
|
1470
|
+
` snakeCase,`,
|
|
1459
1471
|
` text, varchar, char, integer, bigint, smallint,`,
|
|
1460
1472
|
` serial, bigserial, boolean, real, doublePrecision,`,
|
|
1461
1473
|
` numeric, decimal, json, jsonb,`,
|
|
1462
1474
|
` timestamp, timestamptz, date, time, uuid,`,
|
|
1463
1475
|
` bytea, inet,`,
|
|
1476
|
+
` textArray, integerArray, varcharArray, uuidArray,`,
|
|
1464
1477
|
`} from "@bungres/orm";`,
|
|
1465
1478
|
``
|
|
1466
1479
|
];
|
|
1467
1480
|
for (const [, table2] of tableMap) {
|
|
1468
1481
|
const varName = toCamelCase(table2.tableName);
|
|
1469
|
-
lines.push(`export const ${varName} = table("${table2.tableName}", {`);
|
|
1482
|
+
lines.push(`export const ${varName} = snakeCase.table("${table2.tableName}", {`);
|
|
1470
1483
|
for (const col2 of table2.columns) {
|
|
1471
1484
|
const colExpr = buildColumnExpression(col2);
|
|
1472
|
-
lines.push(` ${col2.name}: ${colExpr},`);
|
|
1485
|
+
lines.push(` ${toCamelCase(col2.name)}: ${colExpr},`);
|
|
1473
1486
|
}
|
|
1474
1487
|
const options = [];
|
|
1475
1488
|
if (dbSchema !== "public") {
|
|
@@ -1516,84 +1529,97 @@ function generateSchemaTS(tableMap, dbSchema) {
|
|
|
1516
1529
|
`);
|
|
1517
1530
|
}
|
|
1518
1531
|
function buildColumnExpression(col2) {
|
|
1519
|
-
|
|
1532
|
+
const opts = [];
|
|
1533
|
+
if (col2.dataType === "character varying" || col2.dataType === "character") {
|
|
1534
|
+
if (col2.maxLength)
|
|
1535
|
+
opts.push(`length: ${col2.maxLength}`);
|
|
1536
|
+
}
|
|
1520
1537
|
if (col2.isPrimary)
|
|
1521
|
-
|
|
1538
|
+
opts.push(`primaryKey: true`);
|
|
1522
1539
|
else if (!col2.isNullable)
|
|
1523
|
-
|
|
1540
|
+
opts.push(`notNull: true`);
|
|
1524
1541
|
if (col2.isUnique && !col2.isPrimary)
|
|
1525
|
-
|
|
1542
|
+
opts.push(`unique: true`);
|
|
1526
1543
|
if (col2.columnDefault !== null && !col2.isPrimary) {
|
|
1527
1544
|
if (col2.columnDefault.includes("(")) {
|
|
1528
|
-
|
|
1529
|
-
} else if (col2.dataType === "boolean") {
|
|
1530
|
-
|
|
1531
|
-
} else if (col2.dataType.includes("int") || col2.dataType.includes("numeric") || col2.dataType.includes("real") || col2.dataType.includes("double")) {
|
|
1532
|
-
expr += `.default(${col2.columnDefault})`;
|
|
1545
|
+
opts.push(`defaultRaw: "${col2.columnDefault}"`);
|
|
1546
|
+
} else if (col2.dataType === "boolean" || col2.dataType.includes("int") || col2.dataType.includes("numeric") || col2.dataType.includes("real") || col2.dataType.includes("double")) {
|
|
1547
|
+
opts.push(`default: ${col2.columnDefault}`);
|
|
1533
1548
|
} else {
|
|
1534
1549
|
const cleaned = col2.columnDefault.replace(/^'(.*)'::.*$/, "$1");
|
|
1535
|
-
|
|
1550
|
+
opts.push(`default: "${cleaned}"`);
|
|
1536
1551
|
}
|
|
1537
1552
|
}
|
|
1538
1553
|
if (col2.foreignTable && col2.foreignColumn) {
|
|
1539
1554
|
const deleteRule = col2.deleteRule?.toLowerCase().replace(" ", " ");
|
|
1540
1555
|
const updateRule = col2.updateRule?.toLowerCase().replace(" ", " ");
|
|
1541
|
-
let refOpts = ""
|
|
1542
|
-
if (deleteRule && deleteRule !== "no action")
|
|
1543
|
-
refOpts +=
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1556
|
+
let refOpts = `table: "${col2.foreignTable}", column: "${col2.foreignColumn}"`;
|
|
1557
|
+
if (deleteRule && deleteRule !== "no action")
|
|
1558
|
+
refOpts += `, onDelete: "${deleteRule}"`;
|
|
1559
|
+
if (updateRule && updateRule !== "no action")
|
|
1560
|
+
refOpts += `, onUpdate: "${updateRule}"`;
|
|
1561
|
+
opts.push(`references: { ${refOpts} }`);
|
|
1562
|
+
}
|
|
1563
|
+
let builderName = pgTypeToBungresBuilderName(col2);
|
|
1564
|
+
if (opts.length > 0) {
|
|
1565
|
+
return `${builderName}({ ${opts.join(", ")} })`;
|
|
1566
|
+
}
|
|
1567
|
+
return `${builderName}()`;
|
|
1552
1568
|
}
|
|
1553
|
-
function
|
|
1569
|
+
function pgTypeToBungresBuilderName(col2) {
|
|
1554
1570
|
const dt = col2.dataType;
|
|
1555
|
-
const name = col2.name;
|
|
1556
1571
|
if (dt === "uuid")
|
|
1557
|
-
return
|
|
1572
|
+
return "uuid";
|
|
1558
1573
|
if (dt === "text")
|
|
1559
|
-
return
|
|
1574
|
+
return "text";
|
|
1560
1575
|
if (dt === "character varying")
|
|
1561
|
-
return
|
|
1576
|
+
return "varchar";
|
|
1562
1577
|
if (dt === "character")
|
|
1563
|
-
return
|
|
1578
|
+
return "char";
|
|
1564
1579
|
if (dt === "integer")
|
|
1565
|
-
return
|
|
1580
|
+
return "integer";
|
|
1566
1581
|
if (dt === "bigint")
|
|
1567
|
-
return
|
|
1582
|
+
return "bigint";
|
|
1568
1583
|
if (dt === "smallint")
|
|
1569
|
-
return
|
|
1584
|
+
return "smallint";
|
|
1570
1585
|
if (dt === "boolean")
|
|
1571
|
-
return
|
|
1586
|
+
return "boolean";
|
|
1572
1587
|
if (dt === "real")
|
|
1573
|
-
return
|
|
1588
|
+
return "real";
|
|
1574
1589
|
if (dt === "double precision")
|
|
1575
|
-
return
|
|
1590
|
+
return "doublePrecision";
|
|
1576
1591
|
if (dt === "numeric" || dt === "decimal")
|
|
1577
|
-
return
|
|
1592
|
+
return "numeric";
|
|
1578
1593
|
if (dt === "json")
|
|
1579
|
-
return
|
|
1594
|
+
return "json";
|
|
1580
1595
|
if (dt === "jsonb")
|
|
1581
|
-
return
|
|
1596
|
+
return "jsonb";
|
|
1582
1597
|
if (dt === "timestamp without time zone")
|
|
1583
|
-
return
|
|
1598
|
+
return "timestamp";
|
|
1584
1599
|
if (dt === "timestamp with time zone")
|
|
1585
|
-
return
|
|
1600
|
+
return "timestamptz";
|
|
1586
1601
|
if (dt === "date")
|
|
1587
|
-
return
|
|
1602
|
+
return "date";
|
|
1588
1603
|
if (dt === "time without time zone")
|
|
1589
|
-
return
|
|
1604
|
+
return "time";
|
|
1590
1605
|
if (dt === "bytea")
|
|
1591
|
-
return
|
|
1606
|
+
return "bytea";
|
|
1592
1607
|
if (dt === "inet")
|
|
1593
|
-
return
|
|
1608
|
+
return "inet";
|
|
1594
1609
|
if (dt === "USER-DEFINED" && col2.udtName === "citext")
|
|
1595
|
-
return
|
|
1596
|
-
|
|
1610
|
+
return "text";
|
|
1611
|
+
if (dt === "ARRAY") {
|
|
1612
|
+
if (col2.udtName === "_text")
|
|
1613
|
+
return "textArray";
|
|
1614
|
+
if (col2.udtName === "_int4" || col2.udtName === "_int8")
|
|
1615
|
+
return "integerArray";
|
|
1616
|
+
if (col2.udtName === "_varchar")
|
|
1617
|
+
return "varcharArray";
|
|
1618
|
+
if (col2.udtName === "_uuid")
|
|
1619
|
+
return "uuidArray";
|
|
1620
|
+
return "textArray";
|
|
1621
|
+
}
|
|
1622
|
+
return "text";
|
|
1597
1623
|
}
|
|
1598
1624
|
function toCamelCase(str) {
|
|
1599
1625
|
return str.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
|
package/dist/index.js
CHANGED
|
@@ -38,7 +38,7 @@ async function loadConfig(cwd = process.cwd()) {
|
|
|
38
38
|
// src/schema-loader.ts
|
|
39
39
|
import { resolve as resolve2, join as join2 } from "path";
|
|
40
40
|
|
|
41
|
-
//
|
|
41
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/table.ts
|
|
42
42
|
var TableConfigSymbol = Symbol.for("BungresTableConfig");
|
|
43
43
|
function getTableConfig(table) {
|
|
44
44
|
return table[TableConfigSymbol];
|
|
@@ -78,7 +78,7 @@ function createTableFactory(casing) {
|
|
|
78
78
|
var table = createTableFactory("none");
|
|
79
79
|
var snakeCase = { table: createTableFactory("snake") };
|
|
80
80
|
var camelCase = { table: createTableFactory("camel") };
|
|
81
|
-
//
|
|
81
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/column.ts
|
|
82
82
|
function buildColumn(dataType, nameOrOpts, opts) {
|
|
83
83
|
let name = "";
|
|
84
84
|
let options = opts;
|
|
@@ -132,7 +132,7 @@ var interval = col("interval");
|
|
|
132
132
|
var inet = col("inet");
|
|
133
133
|
var cidr = col("cidr");
|
|
134
134
|
var macaddr = col("macaddr");
|
|
135
|
-
//
|
|
135
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/sql.ts
|
|
136
136
|
function sqlJoin(chunks, separator = ", ") {
|
|
137
137
|
const params = [];
|
|
138
138
|
const parts = [];
|
|
@@ -143,7 +143,7 @@ function sqlJoin(chunks, separator = ", ") {
|
|
|
143
143
|
}
|
|
144
144
|
return { sql: parts.join(separator), params };
|
|
145
145
|
}
|
|
146
|
-
//
|
|
146
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/query.ts
|
|
147
147
|
class SelectBuilder {
|
|
148
148
|
_table;
|
|
149
149
|
_executor;
|
|
@@ -163,6 +163,9 @@ class SelectBuilder {
|
|
|
163
163
|
then(onfulfilled, onrejected) {
|
|
164
164
|
return this._executor.execute(this).then(onfulfilled, onrejected);
|
|
165
165
|
}
|
|
166
|
+
async single() {
|
|
167
|
+
return this._executor.executeSingle(this);
|
|
168
|
+
}
|
|
166
169
|
select(...columns) {
|
|
167
170
|
this._select = columns;
|
|
168
171
|
return this;
|
|
@@ -263,6 +266,9 @@ class InsertBuilder {
|
|
|
263
266
|
then(onfulfilled, onrejected) {
|
|
264
267
|
return this._executor.execute(this).then(onfulfilled, onrejected);
|
|
265
268
|
}
|
|
269
|
+
async single() {
|
|
270
|
+
return this._executor.executeSingle(this);
|
|
271
|
+
}
|
|
266
272
|
values(data) {
|
|
267
273
|
const rows = Array.isArray(data) ? data : [data];
|
|
268
274
|
this._values.push(...rows);
|
|
@@ -329,6 +335,9 @@ class UpdateBuilder {
|
|
|
329
335
|
then(onfulfilled, onrejected) {
|
|
330
336
|
return this._executor.execute(this).then(onfulfilled, onrejected);
|
|
331
337
|
}
|
|
338
|
+
async single() {
|
|
339
|
+
return this._executor.executeSingle(this);
|
|
340
|
+
}
|
|
332
341
|
set(data) {
|
|
333
342
|
this._set = { ...this._set, ...data };
|
|
334
343
|
return this;
|
|
@@ -383,6 +392,9 @@ class DeleteBuilder {
|
|
|
383
392
|
then(onfulfilled, onrejected) {
|
|
384
393
|
return this._executor.execute(this).then(onfulfilled, onrejected);
|
|
385
394
|
}
|
|
395
|
+
async single() {
|
|
396
|
+
return this._executor.executeSingle(this);
|
|
397
|
+
}
|
|
386
398
|
where(condition) {
|
|
387
399
|
this._where.push(condition);
|
|
388
400
|
return this;
|
|
@@ -409,7 +421,7 @@ class DeleteBuilder {
|
|
|
409
421
|
return { sql: query, params };
|
|
410
422
|
}
|
|
411
423
|
}
|
|
412
|
-
//
|
|
424
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/relational.ts
|
|
413
425
|
var _relationsCache = new WeakMap;
|
|
414
426
|
|
|
415
427
|
class RelationalQueryBuilder {
|
|
@@ -636,7 +648,7 @@ class RelationalQueryBuilder {
|
|
|
636
648
|
}
|
|
637
649
|
}
|
|
638
650
|
|
|
639
|
-
//
|
|
651
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/db.ts
|
|
640
652
|
function parseDBName(url) {
|
|
641
653
|
try {
|
|
642
654
|
return new URL(url).pathname.slice(1);
|
|
@@ -802,7 +814,7 @@ function createDB(config) {
|
|
|
802
814
|
}
|
|
803
815
|
return db;
|
|
804
816
|
}
|
|
805
|
-
//
|
|
817
|
+
// ../../node_modules/.bun/@bungres+orm@0.2.0/node_modules/@bungres/orm/src/ddl.ts
|
|
806
818
|
function generateCreateTable(config, ifNotExists = true) {
|
|
807
819
|
const tableName = config.schema ? `"${config.schema}"."${config.name}"` : `"${config.name}"`;
|
|
808
820
|
const exists = ifNotExists ? " IF NOT EXISTS" : "";
|
|
@@ -1450,21 +1462,22 @@ function generateSchemaTS(tableMap, dbSchema) {
|
|
|
1450
1462
|
`// Generated at: ${new Date().toISOString()}`,
|
|
1451
1463
|
``,
|
|
1452
1464
|
`import {`,
|
|
1453
|
-
`
|
|
1465
|
+
` snakeCase,`,
|
|
1454
1466
|
` text, varchar, char, integer, bigint, smallint,`,
|
|
1455
1467
|
` serial, bigserial, boolean, real, doublePrecision,`,
|
|
1456
1468
|
` numeric, decimal, json, jsonb,`,
|
|
1457
1469
|
` timestamp, timestamptz, date, time, uuid,`,
|
|
1458
1470
|
` bytea, inet,`,
|
|
1471
|
+
` textArray, integerArray, varcharArray, uuidArray,`,
|
|
1459
1472
|
`} from "@bungres/orm";`,
|
|
1460
1473
|
``
|
|
1461
1474
|
];
|
|
1462
1475
|
for (const [, table2] of tableMap) {
|
|
1463
1476
|
const varName = toCamelCase(table2.tableName);
|
|
1464
|
-
lines.push(`export const ${varName} = table("${table2.tableName}", {`);
|
|
1477
|
+
lines.push(`export const ${varName} = snakeCase.table("${table2.tableName}", {`);
|
|
1465
1478
|
for (const col2 of table2.columns) {
|
|
1466
1479
|
const colExpr = buildColumnExpression(col2);
|
|
1467
|
-
lines.push(` ${col2.name}: ${colExpr},`);
|
|
1480
|
+
lines.push(` ${toCamelCase(col2.name)}: ${colExpr},`);
|
|
1468
1481
|
}
|
|
1469
1482
|
const options = [];
|
|
1470
1483
|
if (dbSchema !== "public") {
|
|
@@ -1511,84 +1524,97 @@ function generateSchemaTS(tableMap, dbSchema) {
|
|
|
1511
1524
|
`);
|
|
1512
1525
|
}
|
|
1513
1526
|
function buildColumnExpression(col2) {
|
|
1514
|
-
|
|
1527
|
+
const opts = [];
|
|
1528
|
+
if (col2.dataType === "character varying" || col2.dataType === "character") {
|
|
1529
|
+
if (col2.maxLength)
|
|
1530
|
+
opts.push(`length: ${col2.maxLength}`);
|
|
1531
|
+
}
|
|
1515
1532
|
if (col2.isPrimary)
|
|
1516
|
-
|
|
1533
|
+
opts.push(`primaryKey: true`);
|
|
1517
1534
|
else if (!col2.isNullable)
|
|
1518
|
-
|
|
1535
|
+
opts.push(`notNull: true`);
|
|
1519
1536
|
if (col2.isUnique && !col2.isPrimary)
|
|
1520
|
-
|
|
1537
|
+
opts.push(`unique: true`);
|
|
1521
1538
|
if (col2.columnDefault !== null && !col2.isPrimary) {
|
|
1522
1539
|
if (col2.columnDefault.includes("(")) {
|
|
1523
|
-
|
|
1524
|
-
} else if (col2.dataType === "boolean") {
|
|
1525
|
-
|
|
1526
|
-
} else if (col2.dataType.includes("int") || col2.dataType.includes("numeric") || col2.dataType.includes("real") || col2.dataType.includes("double")) {
|
|
1527
|
-
expr += `.default(${col2.columnDefault})`;
|
|
1540
|
+
opts.push(`defaultRaw: "${col2.columnDefault}"`);
|
|
1541
|
+
} else if (col2.dataType === "boolean" || col2.dataType.includes("int") || col2.dataType.includes("numeric") || col2.dataType.includes("real") || col2.dataType.includes("double")) {
|
|
1542
|
+
opts.push(`default: ${col2.columnDefault}`);
|
|
1528
1543
|
} else {
|
|
1529
1544
|
const cleaned = col2.columnDefault.replace(/^'(.*)'::.*$/, "$1");
|
|
1530
|
-
|
|
1545
|
+
opts.push(`default: "${cleaned}"`);
|
|
1531
1546
|
}
|
|
1532
1547
|
}
|
|
1533
1548
|
if (col2.foreignTable && col2.foreignColumn) {
|
|
1534
1549
|
const deleteRule = col2.deleteRule?.toLowerCase().replace(" ", " ");
|
|
1535
1550
|
const updateRule = col2.updateRule?.toLowerCase().replace(" ", " ");
|
|
1536
|
-
let refOpts = ""
|
|
1537
|
-
if (deleteRule && deleteRule !== "no action")
|
|
1538
|
-
refOpts +=
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1551
|
+
let refOpts = `table: "${col2.foreignTable}", column: "${col2.foreignColumn}"`;
|
|
1552
|
+
if (deleteRule && deleteRule !== "no action")
|
|
1553
|
+
refOpts += `, onDelete: "${deleteRule}"`;
|
|
1554
|
+
if (updateRule && updateRule !== "no action")
|
|
1555
|
+
refOpts += `, onUpdate: "${updateRule}"`;
|
|
1556
|
+
opts.push(`references: { ${refOpts} }`);
|
|
1557
|
+
}
|
|
1558
|
+
let builderName = pgTypeToBungresBuilderName(col2);
|
|
1559
|
+
if (opts.length > 0) {
|
|
1560
|
+
return `${builderName}({ ${opts.join(", ")} })`;
|
|
1561
|
+
}
|
|
1562
|
+
return `${builderName}()`;
|
|
1547
1563
|
}
|
|
1548
|
-
function
|
|
1564
|
+
function pgTypeToBungresBuilderName(col2) {
|
|
1549
1565
|
const dt = col2.dataType;
|
|
1550
|
-
const name = col2.name;
|
|
1551
1566
|
if (dt === "uuid")
|
|
1552
|
-
return
|
|
1567
|
+
return "uuid";
|
|
1553
1568
|
if (dt === "text")
|
|
1554
|
-
return
|
|
1569
|
+
return "text";
|
|
1555
1570
|
if (dt === "character varying")
|
|
1556
|
-
return
|
|
1571
|
+
return "varchar";
|
|
1557
1572
|
if (dt === "character")
|
|
1558
|
-
return
|
|
1573
|
+
return "char";
|
|
1559
1574
|
if (dt === "integer")
|
|
1560
|
-
return
|
|
1575
|
+
return "integer";
|
|
1561
1576
|
if (dt === "bigint")
|
|
1562
|
-
return
|
|
1577
|
+
return "bigint";
|
|
1563
1578
|
if (dt === "smallint")
|
|
1564
|
-
return
|
|
1579
|
+
return "smallint";
|
|
1565
1580
|
if (dt === "boolean")
|
|
1566
|
-
return
|
|
1581
|
+
return "boolean";
|
|
1567
1582
|
if (dt === "real")
|
|
1568
|
-
return
|
|
1583
|
+
return "real";
|
|
1569
1584
|
if (dt === "double precision")
|
|
1570
|
-
return
|
|
1585
|
+
return "doublePrecision";
|
|
1571
1586
|
if (dt === "numeric" || dt === "decimal")
|
|
1572
|
-
return
|
|
1587
|
+
return "numeric";
|
|
1573
1588
|
if (dt === "json")
|
|
1574
|
-
return
|
|
1589
|
+
return "json";
|
|
1575
1590
|
if (dt === "jsonb")
|
|
1576
|
-
return
|
|
1591
|
+
return "jsonb";
|
|
1577
1592
|
if (dt === "timestamp without time zone")
|
|
1578
|
-
return
|
|
1593
|
+
return "timestamp";
|
|
1579
1594
|
if (dt === "timestamp with time zone")
|
|
1580
|
-
return
|
|
1595
|
+
return "timestamptz";
|
|
1581
1596
|
if (dt === "date")
|
|
1582
|
-
return
|
|
1597
|
+
return "date";
|
|
1583
1598
|
if (dt === "time without time zone")
|
|
1584
|
-
return
|
|
1599
|
+
return "time";
|
|
1585
1600
|
if (dt === "bytea")
|
|
1586
|
-
return
|
|
1601
|
+
return "bytea";
|
|
1587
1602
|
if (dt === "inet")
|
|
1588
|
-
return
|
|
1603
|
+
return "inet";
|
|
1589
1604
|
if (dt === "USER-DEFINED" && col2.udtName === "citext")
|
|
1590
|
-
return
|
|
1591
|
-
|
|
1605
|
+
return "text";
|
|
1606
|
+
if (dt === "ARRAY") {
|
|
1607
|
+
if (col2.udtName === "_text")
|
|
1608
|
+
return "textArray";
|
|
1609
|
+
if (col2.udtName === "_int4" || col2.udtName === "_int8")
|
|
1610
|
+
return "integerArray";
|
|
1611
|
+
if (col2.udtName === "_varchar")
|
|
1612
|
+
return "varcharArray";
|
|
1613
|
+
if (col2.udtName === "_uuid")
|
|
1614
|
+
return "uuidArray";
|
|
1615
|
+
return "textArray";
|
|
1616
|
+
}
|
|
1617
|
+
return "text";
|
|
1592
1618
|
}
|
|
1593
1619
|
function toCamelCase(str) {
|
|
1594
1620
|
return str.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bungres/kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "CLI toolkit for @bungres/orm — migrate, push, generate, pull",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"test": "bun test"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@bungres/orm": "0.
|
|
37
|
+
"@bungres/orm": "0.2.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"bun-types": "latest",
|
package/src/commands/pull.ts
CHANGED
|
@@ -194,23 +194,24 @@ function generateSchemaTS(
|
|
|
194
194
|
`// Generated at: ${new Date().toISOString()}`,
|
|
195
195
|
``,
|
|
196
196
|
`import {`,
|
|
197
|
-
`
|
|
197
|
+
` snakeCase,`,
|
|
198
198
|
` text, varchar, char, integer, bigint, smallint,`,
|
|
199
199
|
` serial, bigserial, boolean, real, doublePrecision,`,
|
|
200
200
|
` numeric, decimal, json, jsonb,`,
|
|
201
201
|
` timestamp, timestamptz, date, time, uuid,`,
|
|
202
202
|
` bytea, inet,`,
|
|
203
|
+
` textArray, integerArray, varcharArray, uuidArray,`,
|
|
203
204
|
`} from "@bungres/orm";`,
|
|
204
205
|
``,
|
|
205
206
|
];
|
|
206
207
|
|
|
207
208
|
for (const [, table] of tableMap) {
|
|
208
209
|
const varName = toCamelCase(table.tableName);
|
|
209
|
-
lines.push(`export const ${varName} = table("${table.tableName}", {`);
|
|
210
|
+
lines.push(`export const ${varName} = snakeCase.table("${table.tableName}", {`);
|
|
210
211
|
|
|
211
212
|
for (const col of table.columns) {
|
|
212
213
|
const colExpr = buildColumnExpression(col);
|
|
213
|
-
lines.push(` ${col.name}: ${colExpr},`);
|
|
214
|
+
lines.push(` ${toCamelCase(col.name)}: ${colExpr},`);
|
|
214
215
|
}
|
|
215
216
|
|
|
216
217
|
// Third argument: Table options (schema, indexes)
|
|
@@ -260,78 +261,77 @@ function generateSchemaTS(
|
|
|
260
261
|
}
|
|
261
262
|
|
|
262
263
|
function buildColumnExpression(col: TableInfo["columns"][number]): string {
|
|
263
|
-
|
|
264
|
+
const opts: string[] = [];
|
|
264
265
|
|
|
265
|
-
if (col.
|
|
266
|
-
|
|
266
|
+
if (col.dataType === "character varying" || col.dataType === "character") {
|
|
267
|
+
if (col.maxLength) opts.push(`length: ${col.maxLength}`);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (col.isPrimary) opts.push(`primaryKey: true`);
|
|
271
|
+
else if (!col.isNullable) opts.push(`notNull: true`);
|
|
267
272
|
|
|
268
|
-
if (col.isUnique && !col.isPrimary)
|
|
273
|
+
if (col.isUnique && !col.isPrimary) opts.push(`unique: true`);
|
|
269
274
|
|
|
270
275
|
if (col.columnDefault !== null && !col.isPrimary) {
|
|
271
276
|
if (col.columnDefault.includes("(")) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
expr += `.default(${col.columnDefault})`;
|
|
276
|
-
} else if (
|
|
277
|
-
col.dataType.includes("int") ||
|
|
278
|
-
col.dataType.includes("numeric") ||
|
|
279
|
-
col.dataType.includes("real") ||
|
|
280
|
-
col.dataType.includes("double")
|
|
281
|
-
) {
|
|
282
|
-
expr += `.default(${col.columnDefault})`;
|
|
277
|
+
opts.push(`defaultRaw: "${col.columnDefault}"`);
|
|
278
|
+
} else if (col.dataType === "boolean" || col.dataType.includes("int") || col.dataType.includes("numeric") || col.dataType.includes("real") || col.dataType.includes("double")) {
|
|
279
|
+
opts.push(`default: ${col.columnDefault}`);
|
|
283
280
|
} else {
|
|
284
|
-
// Strip surrounding quotes for string defaults
|
|
285
281
|
const cleaned = col.columnDefault.replace(/^'(.*)'::.*$/, "$1");
|
|
286
|
-
|
|
282
|
+
opts.push(`default: "${cleaned}"`);
|
|
287
283
|
}
|
|
288
284
|
}
|
|
289
285
|
|
|
290
286
|
if (col.foreignTable && col.foreignColumn) {
|
|
291
287
|
const deleteRule = col.deleteRule?.toLowerCase().replace(" ", " ");
|
|
292
288
|
const updateRule = col.updateRule?.toLowerCase().replace(" ", " ");
|
|
293
|
-
let refOpts = ""
|
|
294
|
-
if (deleteRule && deleteRule !== "no action") {
|
|
295
|
-
|
|
296
|
-
}
|
|
297
|
-
if (updateRule && updateRule !== "no action") {
|
|
298
|
-
refOpts += `onUpdate: "${updateRule}"`;
|
|
299
|
-
}
|
|
300
|
-
const opts = refOpts ? `, { ${refOpts.trim().replace(/,$/, "")} }` : "";
|
|
301
|
-
expr += `.references("${col.foreignTable}", "${col.foreignColumn}"${opts})`;
|
|
289
|
+
let refOpts = `table: "${col.foreignTable}", column: "${col.foreignColumn}"`;
|
|
290
|
+
if (deleteRule && deleteRule !== "no action") refOpts += `, onDelete: "${deleteRule}"`;
|
|
291
|
+
if (updateRule && updateRule !== "no action") refOpts += `, onUpdate: "${updateRule}"`;
|
|
292
|
+
opts.push(`references: { ${refOpts} }`);
|
|
302
293
|
}
|
|
303
294
|
|
|
304
|
-
|
|
295
|
+
let builderName = pgTypeToBungresBuilderName(col);
|
|
296
|
+
|
|
297
|
+
if (opts.length > 0) {
|
|
298
|
+
return `${builderName}({ ${opts.join(", ")} })`;
|
|
299
|
+
}
|
|
300
|
+
return `${builderName}()`;
|
|
305
301
|
}
|
|
306
302
|
|
|
307
|
-
function
|
|
303
|
+
function pgTypeToBungresBuilderName(col: TableInfo["columns"][number]): string {
|
|
308
304
|
const dt = col.dataType;
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
if (dt === "
|
|
312
|
-
if (dt === "
|
|
313
|
-
if (dt === "character
|
|
314
|
-
|
|
315
|
-
if (dt === "
|
|
316
|
-
|
|
317
|
-
if (dt === "
|
|
318
|
-
if (dt === "
|
|
319
|
-
if (dt === "
|
|
320
|
-
if (dt === "
|
|
321
|
-
if (dt === "
|
|
322
|
-
if (dt === "
|
|
323
|
-
if (dt === "
|
|
324
|
-
if (dt === "
|
|
325
|
-
if (dt === "
|
|
326
|
-
if (dt === "
|
|
327
|
-
if (dt === "
|
|
328
|
-
if (dt === "
|
|
329
|
-
if (dt === "
|
|
330
|
-
if (dt === "
|
|
331
|
-
|
|
332
|
-
|
|
305
|
+
|
|
306
|
+
if (dt === "uuid") return "uuid";
|
|
307
|
+
if (dt === "text") return "text";
|
|
308
|
+
if (dt === "character varying") return "varchar";
|
|
309
|
+
if (dt === "character") return "char";
|
|
310
|
+
if (dt === "integer") return "integer";
|
|
311
|
+
if (dt === "bigint") return "bigint";
|
|
312
|
+
if (dt === "smallint") return "smallint";
|
|
313
|
+
if (dt === "boolean") return "boolean";
|
|
314
|
+
if (dt === "real") return "real";
|
|
315
|
+
if (dt === "double precision") return "doublePrecision";
|
|
316
|
+
if (dt === "numeric" || dt === "decimal") return "numeric";
|
|
317
|
+
if (dt === "json") return "json";
|
|
318
|
+
if (dt === "jsonb") return "jsonb";
|
|
319
|
+
if (dt === "timestamp without time zone") return "timestamp";
|
|
320
|
+
if (dt === "timestamp with time zone") return "timestamptz";
|
|
321
|
+
if (dt === "date") return "date";
|
|
322
|
+
if (dt === "time without time zone") return "time";
|
|
323
|
+
if (dt === "bytea") return "bytea";
|
|
324
|
+
if (dt === "inet") return "inet";
|
|
325
|
+
if (dt === "USER-DEFINED" && col.udtName === "citext") return "text";
|
|
326
|
+
if (dt === "ARRAY") {
|
|
327
|
+
if (col.udtName === "_text") return "textArray";
|
|
328
|
+
if (col.udtName === "_int4" || col.udtName === "_int8") return "integerArray";
|
|
329
|
+
if (col.udtName === "_varchar") return "varcharArray";
|
|
330
|
+
if (col.udtName === "_uuid") return "uuidArray";
|
|
331
|
+
return "textArray";
|
|
332
|
+
}
|
|
333
333
|
// Fallback
|
|
334
|
-
return
|
|
334
|
+
return "text";
|
|
335
335
|
}
|
|
336
336
|
|
|
337
337
|
function toCamelCase(str: string): string {
|