@stacksjs/database 0.61.18 → 0.61.19
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/index.js +132 -129
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -52701,7 +52701,7 @@ var require_helper = __commonJS((exports, module) => {
|
|
|
52701
52701
|
warnStream.write(util.format.apply(util, args));
|
|
52702
52702
|
}
|
|
52703
52703
|
};
|
|
52704
|
-
var
|
|
52704
|
+
var path14 = import.meta.require("path");
|
|
52705
52705
|
var Stream = import.meta.require("stream").Stream;
|
|
52706
52706
|
var split = require_split2();
|
|
52707
52707
|
var util = import.meta.require("util");
|
|
@@ -52730,7 +52730,7 @@ var require_helper = __commonJS((exports, module) => {
|
|
|
52730
52730
|
};
|
|
52731
52731
|
exports.getFileName = function(rawEnv) {
|
|
52732
52732
|
var env3 = rawEnv || process.env;
|
|
52733
|
-
var file = env3.PGPASSFILE || (isWin ?
|
|
52733
|
+
var file = env3.PGPASSFILE || (isWin ? path14.join(env3.APPDATA || "./", "postgresql", "pgpass.conf") : path14.join(env3.HOME || "./", ".pgpass"));
|
|
52734
52734
|
return file;
|
|
52735
52735
|
};
|
|
52736
52736
|
exports.usePgPass = function(stats, fname) {
|
|
@@ -52854,7 +52854,7 @@ var require_helper = __commonJS((exports, module) => {
|
|
|
52854
52854
|
|
|
52855
52855
|
// ../../../../node_modules/pgpass/lib/index.js
|
|
52856
52856
|
var require_lib15 = __commonJS((exports, module) => {
|
|
52857
|
-
var
|
|
52857
|
+
var path14 = import.meta.require("path");
|
|
52858
52858
|
var fs7 = import.meta.require("fs");
|
|
52859
52859
|
var helper = require_helper();
|
|
52860
52860
|
module.exports = function(connInfo, cb) {
|
|
@@ -54235,7 +54235,7 @@ var Schema = {
|
|
|
54235
54235
|
}
|
|
54236
54236
|
};
|
|
54237
54237
|
// src/migrations.ts
|
|
54238
|
-
import {log as
|
|
54238
|
+
import {log as log10} from "@stacksjs/cli";
|
|
54239
54239
|
import {database as database7} from "@stacksjs/config";
|
|
54240
54240
|
|
|
54241
54241
|
// ../error-handling/src/handler.ts
|
|
@@ -54658,14 +54658,14 @@ async function extractFieldsFromModel(filePath) {
|
|
|
54658
54658
|
return fields;
|
|
54659
54659
|
}
|
|
54660
54660
|
// src/migrations.ts
|
|
54661
|
-
import {path as
|
|
54661
|
+
import {path as path15} from "@stacksjs/path";
|
|
54662
54662
|
import {fs as fs7, glob as glob6} from "@stacksjs/storage";
|
|
54663
54663
|
var {$: $2 } = globalThis.Bun;
|
|
54664
54664
|
import {FileMigrationProvider, Migrator} from "kysely";
|
|
54665
54665
|
|
|
54666
54666
|
// src/drivers/index.ts
|
|
54667
|
-
import {log as
|
|
54668
|
-
import {path as
|
|
54667
|
+
import {log as log8} from "@stacksjs/cli";
|
|
54668
|
+
import {path as path13} from "@stacksjs/path";
|
|
54669
54669
|
import {fs as fs6, glob as glob5} from "@stacksjs/storage";
|
|
54670
54670
|
import {plural as plural2, snakeCase as snakeCase2} from "@stacksjs/strings";
|
|
54671
54671
|
|
|
@@ -58329,9 +58329,12 @@ var isMoney = vine_default.createRule((value, _2, field) => {
|
|
|
58329
58329
|
const amount = dinero({ amount: numericValue, currency: USD });
|
|
58330
58330
|
field.mutate(amount, field);
|
|
58331
58331
|
});
|
|
58332
|
-
// src/
|
|
58333
|
-
import {
|
|
58332
|
+
// ../validation/src/validator.ts
|
|
58333
|
+
import {log as log4} from "@stacksjs/cli";
|
|
58334
58334
|
import {path as path5} from "@stacksjs/path";
|
|
58335
|
+
// src/drivers/mysql.ts
|
|
58336
|
+
import {italic as italic2, log as log5} from "@stacksjs/cli";
|
|
58337
|
+
import {path as path7} from "@stacksjs/path";
|
|
58335
58338
|
import {fs as fs3, glob as glob2} from "@stacksjs/storage";
|
|
58336
58339
|
async function resetMysqlDatabase() {
|
|
58337
58340
|
const tables2 = await fetchMysqlTables();
|
|
@@ -58339,9 +58342,9 @@ async function resetMysqlDatabase() {
|
|
|
58339
58342
|
await db.schema.dropTable(table2).ifExists().execute();
|
|
58340
58343
|
await db.schema.dropTable("migrations").ifExists().execute();
|
|
58341
58344
|
await db.schema.dropTable("migration_locks").ifExists().execute();
|
|
58342
|
-
const files = await fs3.readdir(
|
|
58343
|
-
const modelFiles = await fs3.readdir(
|
|
58344
|
-
const userModelFiles = glob2.sync(
|
|
58345
|
+
const files = await fs3.readdir(path7.userMigrationsPath());
|
|
58346
|
+
const modelFiles = await fs3.readdir(path7.frameworkPath("database/models"));
|
|
58347
|
+
const userModelFiles = glob2.sync(path7.userModelsPath("*.ts"));
|
|
58345
58348
|
for (const userModel of userModelFiles) {
|
|
58346
58349
|
const model2 = (await import(userModel)).default;
|
|
58347
58350
|
const pivotTables = await getPivotTables(model2);
|
|
@@ -58351,7 +58354,7 @@ async function resetMysqlDatabase() {
|
|
|
58351
58354
|
if (modelFiles.length) {
|
|
58352
58355
|
for (const modelFile of modelFiles) {
|
|
58353
58356
|
if (modelFile.endsWith(".ts")) {
|
|
58354
|
-
const modelPath =
|
|
58357
|
+
const modelPath = path7.frameworkPath(`database/models/${modelFile}`);
|
|
58355
58358
|
if (fs3.existsSync(modelPath))
|
|
58356
58359
|
await Bun.$`rm ${modelPath}`;
|
|
58357
58360
|
}
|
|
@@ -58360,7 +58363,7 @@ async function resetMysqlDatabase() {
|
|
|
58360
58363
|
if (files.length) {
|
|
58361
58364
|
for (const file of files) {
|
|
58362
58365
|
if (file.endsWith(".ts")) {
|
|
58363
|
-
const migrationPath =
|
|
58366
|
+
const migrationPath = path7.userMigrationsPath(`${file}`);
|
|
58364
58367
|
if (fs3.existsSync(migrationPath))
|
|
58365
58368
|
await Bun.$`rm ${migrationPath}`;
|
|
58366
58369
|
}
|
|
@@ -58369,47 +58372,47 @@ async function resetMysqlDatabase() {
|
|
|
58369
58372
|
return ok("All tables dropped successfully!");
|
|
58370
58373
|
}
|
|
58371
58374
|
async function generateMysqlMigration(modelPath) {
|
|
58372
|
-
const files = await fs3.readdir(
|
|
58375
|
+
const files = await fs3.readdir(path7.userMigrationsPath());
|
|
58373
58376
|
if (files.length === 0) {
|
|
58374
|
-
|
|
58375
|
-
const modelFiles = await fs3.readdir(
|
|
58377
|
+
log5.debug("No migrations found in the database folder, deleting all framework/database/*.json files...");
|
|
58378
|
+
const modelFiles = await fs3.readdir(path7.frameworkPath("database/models"));
|
|
58376
58379
|
if (modelFiles.length) {
|
|
58377
|
-
|
|
58380
|
+
log5.debug("No existing model files in framework path...");
|
|
58378
58381
|
for (const file of modelFiles) {
|
|
58379
58382
|
if (file.endsWith(".ts"))
|
|
58380
|
-
await fs3.unlink(
|
|
58383
|
+
await fs3.unlink(path7.frameworkPath(`database/models/${file}`));
|
|
58381
58384
|
}
|
|
58382
58385
|
}
|
|
58383
58386
|
}
|
|
58384
58387
|
const model2 = (await import(modelPath)).default;
|
|
58385
|
-
const fileName =
|
|
58388
|
+
const fileName = path7.basename(modelPath);
|
|
58386
58389
|
const tableName = await modelTableName2(model2);
|
|
58387
58390
|
const fieldsString = JSON.stringify(model2.attributes, null, 2);
|
|
58388
|
-
const copiedModelPath =
|
|
58391
|
+
const copiedModelPath = path7.frameworkPath(`database/models/${fileName}`);
|
|
58389
58392
|
let haveFieldsChanged = false;
|
|
58390
58393
|
if (fs3.existsSync(copiedModelPath)) {
|
|
58391
|
-
|
|
58394
|
+
log5.info(`Fields have already been generated for ${tableName}`);
|
|
58392
58395
|
const previousFields = await getLastMigrationFields(fileName);
|
|
58393
58396
|
const previousFieldsString = JSON.stringify(previousFields, null, 2);
|
|
58394
58397
|
if (previousFieldsString === fieldsString) {
|
|
58395
|
-
|
|
58398
|
+
log5.debug(`Fields have not changed for ${tableName}`);
|
|
58396
58399
|
return;
|
|
58397
58400
|
}
|
|
58398
58401
|
haveFieldsChanged = true;
|
|
58399
|
-
|
|
58402
|
+
log5.debug(`Fields have changed for ${tableName}`);
|
|
58400
58403
|
} else {
|
|
58401
|
-
|
|
58404
|
+
log5.debug(`Fields have not been generated for ${tableName}`);
|
|
58402
58405
|
}
|
|
58403
58406
|
await Bun.$`cp ${modelPath} ${copiedModelPath}`;
|
|
58404
58407
|
const hasBeenMigrated = await hasTableBeenMigrated(tableName);
|
|
58405
|
-
|
|
58408
|
+
log5.debug(`Has ${tableName} been migrated? ${hasBeenMigrated}`);
|
|
58406
58409
|
if (haveFieldsChanged)
|
|
58407
58410
|
await createAlterTableMigration(modelPath);
|
|
58408
58411
|
else
|
|
58409
58412
|
await createTableMigration(modelPath);
|
|
58410
58413
|
}
|
|
58411
58414
|
async function createTableMigration(modelPath) {
|
|
58412
|
-
|
|
58415
|
+
log5.debug("createTableMigration modelPath:", modelPath);
|
|
58413
58416
|
const model2 = (await import(modelPath)).default;
|
|
58414
58417
|
const tableName = await modelTableName2(model2);
|
|
58415
58418
|
await createPivotTableMigration(model2);
|
|
@@ -58454,9 +58457,9 @@ async function createTableMigration(modelPath) {
|
|
|
58454
58457
|
migrationContent += `}\n`;
|
|
58455
58458
|
const timestamp = new Date().getTime().toString();
|
|
58456
58459
|
const migrationFileName = `${timestamp}-create-${tableName}-table.ts`;
|
|
58457
|
-
const migrationFilePath =
|
|
58460
|
+
const migrationFilePath = path7.userMigrationsPath(migrationFileName);
|
|
58458
58461
|
Bun.write(migrationFilePath, migrationContent);
|
|
58459
|
-
|
|
58462
|
+
log5.success(`Created migration: ${italic2(migrationFileName)}`);
|
|
58460
58463
|
}
|
|
58461
58464
|
async function createPivotTableMigration(model2) {
|
|
58462
58465
|
const pivotTables = await getPivotTables(model2);
|
|
@@ -58477,15 +58480,15 @@ async function createPivotTableMigration(model2) {
|
|
|
58477
58480
|
migrationContent += ` }\n`;
|
|
58478
58481
|
const timestamp = new Date().getTime().toString();
|
|
58479
58482
|
const migrationFileName = `${timestamp}-create-${pivotTable.table}-table.ts`;
|
|
58480
|
-
const migrationFilePath =
|
|
58483
|
+
const migrationFilePath = path7.userMigrationsPath(migrationFileName);
|
|
58481
58484
|
Bun.write(migrationFilePath, migrationContent);
|
|
58482
|
-
|
|
58485
|
+
log5.success(`Created pivot migration: ${migrationFileName}`);
|
|
58483
58486
|
}
|
|
58484
58487
|
}
|
|
58485
58488
|
async function createAlterTableMigration(modelPath) {
|
|
58486
58489
|
console.log("createAlterTableMigration");
|
|
58487
58490
|
const model2 = (await import(modelPath)).default;
|
|
58488
|
-
const modelName =
|
|
58491
|
+
const modelName = path7.basename(modelPath);
|
|
58489
58492
|
const tableName = await modelTableName2(model2);
|
|
58490
58493
|
const lastMigrationFields = await getLastMigrationFields(modelName);
|
|
58491
58494
|
const lastFields = lastMigrationFields ?? {};
|
|
@@ -58507,12 +58510,12 @@ async function createAlterTableMigration(modelPath) {
|
|
|
58507
58510
|
migrationContent += `}\n`;
|
|
58508
58511
|
const timestamp = new Date().getTime().toString();
|
|
58509
58512
|
const migrationFileName = `${timestamp}-update-${tableName}-table.ts`;
|
|
58510
|
-
const migrationFilePath =
|
|
58513
|
+
const migrationFilePath = path7.userMigrationsPath(migrationFileName);
|
|
58511
58514
|
Bun.write(migrationFilePath, migrationContent);
|
|
58512
|
-
|
|
58515
|
+
log5.success(`Created migration: ${italic2(migrationFileName)}`);
|
|
58513
58516
|
}
|
|
58514
58517
|
async function fetchMysqlTables() {
|
|
58515
|
-
const modelFiles = glob2.sync(
|
|
58518
|
+
const modelFiles = glob2.sync(path7.userModelsPath("*.ts"));
|
|
58516
58519
|
const tables2 = [];
|
|
58517
58520
|
for (const modelPath of modelFiles) {
|
|
58518
58521
|
const model2 = (await import(modelPath)).default;
|
|
@@ -58522,8 +58525,8 @@ async function fetchMysqlTables() {
|
|
|
58522
58525
|
return tables2;
|
|
58523
58526
|
}
|
|
58524
58527
|
// src/drivers/postgres.ts
|
|
58525
|
-
import {italic as italic3, log as
|
|
58526
|
-
import {path as
|
|
58528
|
+
import {italic as italic3, log as log6} from "@stacksjs/cli";
|
|
58529
|
+
import {path as path9} from "@stacksjs/path";
|
|
58527
58530
|
import {fs as fs4, glob as glob3} from "@stacksjs/storage";
|
|
58528
58531
|
async function resetPostgresDatabase() {
|
|
58529
58532
|
const tables2 = await fetchMysqlTables2();
|
|
@@ -58531,9 +58534,9 @@ async function resetPostgresDatabase() {
|
|
|
58531
58534
|
await db.schema.dropTable(table2).ifExists().execute();
|
|
58532
58535
|
await db.schema.dropTable("migrations").ifExists().execute();
|
|
58533
58536
|
await db.schema.dropTable("migration_locks").ifExists().execute();
|
|
58534
|
-
const files = await fs4.readdir(
|
|
58535
|
-
const modelFiles = await fs4.readdir(
|
|
58536
|
-
const userModelFiles = glob3.sync(
|
|
58537
|
+
const files = await fs4.readdir(path9.userMigrationsPath());
|
|
58538
|
+
const modelFiles = await fs4.readdir(path9.frameworkPath("database/models"));
|
|
58539
|
+
const userModelFiles = glob3.sync(path9.userModelsPath("*.ts"));
|
|
58537
58540
|
for (const userModel of userModelFiles) {
|
|
58538
58541
|
const userModelPath = await import(userModel);
|
|
58539
58542
|
const pivotTables = await getPivotTables(userModelPath);
|
|
@@ -58543,7 +58546,7 @@ async function resetPostgresDatabase() {
|
|
|
58543
58546
|
if (modelFiles.length) {
|
|
58544
58547
|
for (const modelFile of modelFiles) {
|
|
58545
58548
|
if (modelFile.endsWith(".ts")) {
|
|
58546
|
-
const modelPath =
|
|
58549
|
+
const modelPath = path9.frameworkPath(`database/models/${modelFile}`);
|
|
58547
58550
|
if (fs4.existsSync(modelPath))
|
|
58548
58551
|
await Bun.$`rm ${modelPath}`;
|
|
58549
58552
|
}
|
|
@@ -58552,7 +58555,7 @@ async function resetPostgresDatabase() {
|
|
|
58552
58555
|
if (files.length) {
|
|
58553
58556
|
for (const file of files) {
|
|
58554
58557
|
if (file.endsWith(".ts")) {
|
|
58555
|
-
const migrationPath =
|
|
58558
|
+
const migrationPath = path9.userMigrationsPath(`${file}`);
|
|
58556
58559
|
if (fs4.existsSync(migrationPath))
|
|
58557
58560
|
await Bun.$`rm ${migrationPath}`;
|
|
58558
58561
|
}
|
|
@@ -58561,47 +58564,47 @@ async function resetPostgresDatabase() {
|
|
|
58561
58564
|
return ok("All tables dropped successfully!");
|
|
58562
58565
|
}
|
|
58563
58566
|
async function generatePostgresMigration(modelPath) {
|
|
58564
|
-
const files = await fs4.readdir(
|
|
58567
|
+
const files = await fs4.readdir(path9.userMigrationsPath());
|
|
58565
58568
|
if (files.length === 0) {
|
|
58566
|
-
|
|
58567
|
-
const modelFiles = await fs4.readdir(
|
|
58569
|
+
log6.debug("No migrations found in the database folder, deleting all framework/database/*.json files...");
|
|
58570
|
+
const modelFiles = await fs4.readdir(path9.frameworkPath("database/models"));
|
|
58568
58571
|
if (modelFiles.length) {
|
|
58569
|
-
|
|
58572
|
+
log6.debug("No existing model files in framework path...");
|
|
58570
58573
|
for (const file of modelFiles) {
|
|
58571
58574
|
if (file.endsWith(".ts"))
|
|
58572
|
-
await fs4.unlink(
|
|
58575
|
+
await fs4.unlink(path9.frameworkPath(`database/models/${file}`));
|
|
58573
58576
|
}
|
|
58574
58577
|
}
|
|
58575
58578
|
}
|
|
58576
58579
|
const model2 = (await import(modelPath)).default;
|
|
58577
|
-
const fileName =
|
|
58580
|
+
const fileName = path9.basename(modelPath);
|
|
58578
58581
|
const tableName = await modelTableName(model2);
|
|
58579
58582
|
const fieldsString = JSON.stringify(model2.attributes, null, 2);
|
|
58580
|
-
const copiedModelPath =
|
|
58583
|
+
const copiedModelPath = path9.frameworkPath(`database/models/${fileName}`);
|
|
58581
58584
|
let haveFieldsChanged = false;
|
|
58582
58585
|
if (fs4.existsSync(copiedModelPath)) {
|
|
58583
|
-
|
|
58586
|
+
log6.info(`Fields have already been generated for ${tableName}`);
|
|
58584
58587
|
const previousFields = await getLastMigrationFields(fileName);
|
|
58585
58588
|
const previousFieldsString = JSON.stringify(previousFields, null, 2);
|
|
58586
58589
|
if (previousFieldsString === fieldsString) {
|
|
58587
|
-
|
|
58590
|
+
log6.debug(`Fields have not changed for ${tableName}`);
|
|
58588
58591
|
return;
|
|
58589
58592
|
}
|
|
58590
58593
|
haveFieldsChanged = true;
|
|
58591
|
-
|
|
58594
|
+
log6.debug(`Fields have changed for ${tableName}`);
|
|
58592
58595
|
} else {
|
|
58593
|
-
|
|
58596
|
+
log6.debug(`Fields have not been generated for ${tableName}`);
|
|
58594
58597
|
}
|
|
58595
58598
|
await Bun.$`cp ${modelPath} ${copiedModelPath}`;
|
|
58596
58599
|
const hasBeenMigrated = await hasTableBeenMigrated(tableName);
|
|
58597
|
-
|
|
58600
|
+
log6.debug(`Has ${tableName} been migrated? ${hasBeenMigrated}`);
|
|
58598
58601
|
if (haveFieldsChanged)
|
|
58599
58602
|
await createAlterTableMigration2(modelPath);
|
|
58600
58603
|
else
|
|
58601
58604
|
await createTableMigration2(modelPath);
|
|
58602
58605
|
}
|
|
58603
58606
|
async function createTableMigration2(modelPath) {
|
|
58604
|
-
|
|
58607
|
+
log6.debug("createTableMigration modelPath:", modelPath);
|
|
58605
58608
|
const model2 = (await import(modelPath)).default;
|
|
58606
58609
|
const tableName = await modelTableName(model2);
|
|
58607
58610
|
await createPivotTableMigration2(model2);
|
|
@@ -58646,9 +58649,9 @@ async function createTableMigration2(modelPath) {
|
|
|
58646
58649
|
migrationContent += `}\n`;
|
|
58647
58650
|
const timestamp = new Date().getTime().toString();
|
|
58648
58651
|
const migrationFileName = `${timestamp}-create-${tableName}-table.ts`;
|
|
58649
|
-
const migrationFilePath =
|
|
58652
|
+
const migrationFilePath = path9.userMigrationsPath(migrationFileName);
|
|
58650
58653
|
Bun.write(migrationFilePath, migrationContent);
|
|
58651
|
-
|
|
58654
|
+
log6.success(`Created migration: ${italic3(migrationFileName)}`);
|
|
58652
58655
|
}
|
|
58653
58656
|
async function createPivotTableMigration2(model2) {
|
|
58654
58657
|
const pivotTables = await getPivotTables(model2);
|
|
@@ -58670,15 +58673,15 @@ async function createPivotTableMigration2(model2) {
|
|
|
58670
58673
|
migrationContent += ` }\n`;
|
|
58671
58674
|
const timestamp = new Date().getTime().toString();
|
|
58672
58675
|
const migrationFileName = `${timestamp}-create-${pivotTable.table}-table.ts`;
|
|
58673
|
-
const migrationFilePath =
|
|
58676
|
+
const migrationFilePath = path9.userMigrationsPath(migrationFileName);
|
|
58674
58677
|
Bun.write(migrationFilePath, migrationContent);
|
|
58675
|
-
|
|
58678
|
+
log6.success(`Created migration: ${italic3(migrationFileName)}`);
|
|
58676
58679
|
}
|
|
58677
58680
|
}
|
|
58678
58681
|
async function createAlterTableMigration2(modelPath) {
|
|
58679
58682
|
console.log("createAlterTableMigration");
|
|
58680
58683
|
const model2 = (await import(modelPath)).default;
|
|
58681
|
-
const modelName =
|
|
58684
|
+
const modelName = path9.basename(modelPath);
|
|
58682
58685
|
const tableName = await modelTableName(model2);
|
|
58683
58686
|
const lastMigrationFields = await getLastMigrationFields(modelName);
|
|
58684
58687
|
const lastFields = lastMigrationFields ?? {};
|
|
@@ -58700,12 +58703,12 @@ async function createAlterTableMigration2(modelPath) {
|
|
|
58700
58703
|
migrationContent += `}\n`;
|
|
58701
58704
|
const timestamp = new Date().getTime().toString();
|
|
58702
58705
|
const migrationFileName = `${timestamp}-update-${tableName}-table.ts`;
|
|
58703
|
-
const migrationFilePath =
|
|
58706
|
+
const migrationFilePath = path9.userMigrationsPath(migrationFileName);
|
|
58704
58707
|
Bun.write(migrationFilePath, migrationContent);
|
|
58705
|
-
|
|
58708
|
+
log6.success(`Created migration: ${italic3(migrationFileName)}`);
|
|
58706
58709
|
}
|
|
58707
58710
|
async function fetchMysqlTables2() {
|
|
58708
|
-
const modelFiles = glob3.sync(
|
|
58711
|
+
const modelFiles = glob3.sync(path9.userModelsPath("*.ts"));
|
|
58709
58712
|
const tables2 = [];
|
|
58710
58713
|
for (const modelPath of modelFiles) {
|
|
58711
58714
|
const model2 = await import(modelPath);
|
|
@@ -58715,16 +58718,16 @@ async function fetchMysqlTables2() {
|
|
|
58715
58718
|
return tables2;
|
|
58716
58719
|
}
|
|
58717
58720
|
// src/drivers/sqlite.ts
|
|
58718
|
-
import {italic as italic4, log as
|
|
58719
|
-
import {path as
|
|
58721
|
+
import {italic as italic4, log as log7} from "@stacksjs/cli";
|
|
58722
|
+
import {path as path11} from "@stacksjs/path";
|
|
58720
58723
|
import {fs as fs5, glob as glob4} from "@stacksjs/storage";
|
|
58721
58724
|
async function resetSqliteDatabase() {
|
|
58722
|
-
const dbPath =
|
|
58725
|
+
const dbPath = path11.userDatabasePath("stacks.sqlite");
|
|
58723
58726
|
if (fs5.existsSync(dbPath))
|
|
58724
58727
|
await Bun.$`rm ${dbPath}`;
|
|
58725
|
-
const files = await fs5.readdir(
|
|
58726
|
-
const modelFiles = await fs5.readdir(
|
|
58727
|
-
const userModelFiles = glob4.sync(
|
|
58728
|
+
const files = await fs5.readdir(path11.userMigrationsPath());
|
|
58729
|
+
const modelFiles = await fs5.readdir(path11.frameworkPath("database/models"));
|
|
58730
|
+
const userModelFiles = glob4.sync(path11.userModelsPath("*.ts"));
|
|
58728
58731
|
for (const userModel of userModelFiles) {
|
|
58729
58732
|
const userModelPath = await import(userModel);
|
|
58730
58733
|
const pivotTables = await getPivotTables(userModelPath);
|
|
@@ -58734,7 +58737,7 @@ async function resetSqliteDatabase() {
|
|
|
58734
58737
|
if (modelFiles.length) {
|
|
58735
58738
|
for (const modelFile of modelFiles) {
|
|
58736
58739
|
if (modelFile.endsWith(".ts")) {
|
|
58737
|
-
const modelPath =
|
|
58740
|
+
const modelPath = path11.frameworkPath(`database/models/${modelFile}`);
|
|
58738
58741
|
if (fs5.existsSync(modelPath))
|
|
58739
58742
|
await Bun.$`rm ${modelPath}`;
|
|
58740
58743
|
}
|
|
@@ -58743,7 +58746,7 @@ async function resetSqliteDatabase() {
|
|
|
58743
58746
|
if (files.length) {
|
|
58744
58747
|
for (const file of files) {
|
|
58745
58748
|
if (file.endsWith(".ts")) {
|
|
58746
|
-
const migrationPath =
|
|
58749
|
+
const migrationPath = path11.userMigrationsPath(`${file}`);
|
|
58747
58750
|
if (fs5.existsSync(migrationPath))
|
|
58748
58751
|
await Bun.$`rm ${migrationPath}`;
|
|
58749
58752
|
}
|
|
@@ -58752,46 +58755,46 @@ async function resetSqliteDatabase() {
|
|
|
58752
58755
|
return ok("All tables dropped successfully!");
|
|
58753
58756
|
}
|
|
58754
58757
|
async function generateSqliteMigration(modelPath) {
|
|
58755
|
-
const files = await fs5.readdir(
|
|
58758
|
+
const files = await fs5.readdir(path11.userMigrationsPath());
|
|
58756
58759
|
if (files.length === 0) {
|
|
58757
|
-
|
|
58758
|
-
const modelFiles = await fs5.readdir(
|
|
58760
|
+
log7.debug("No migrations found in the database folder, deleting all framework/database/*.json files...");
|
|
58761
|
+
const modelFiles = await fs5.readdir(path11.frameworkPath("database/models"));
|
|
58759
58762
|
if (modelFiles.length) {
|
|
58760
|
-
|
|
58763
|
+
log7.debug("No existing model files in framework path...");
|
|
58761
58764
|
for (const file of modelFiles)
|
|
58762
58765
|
if (file.endsWith(".ts"))
|
|
58763
|
-
await fs5.unlink(
|
|
58766
|
+
await fs5.unlink(path11.frameworkPath(`database/models/${file}`));
|
|
58764
58767
|
}
|
|
58765
58768
|
}
|
|
58766
58769
|
const model2 = (await import(modelPath)).default;
|
|
58767
|
-
const fileName =
|
|
58770
|
+
const fileName = path11.basename(modelPath);
|
|
58768
58771
|
const tableName = await modelTableName(model2);
|
|
58769
58772
|
const fieldsString = JSON.stringify(model2.attributes, null, 2);
|
|
58770
|
-
const copiedModelPath =
|
|
58773
|
+
const copiedModelPath = path11.frameworkPath(`database/models/${fileName}`);
|
|
58771
58774
|
let haveFieldsChanged = false;
|
|
58772
58775
|
if (fs5.existsSync(copiedModelPath)) {
|
|
58773
|
-
|
|
58776
|
+
log7.debug(`Fields have already been generated for ${tableName}`);
|
|
58774
58777
|
const previousFields = await getLastMigrationFields(fileName);
|
|
58775
58778
|
const previousFieldsString = JSON.stringify(previousFields, null, 2);
|
|
58776
58779
|
if (previousFieldsString === fieldsString) {
|
|
58777
|
-
|
|
58780
|
+
log7.debug(`Fields have not changed for ${tableName}`);
|
|
58778
58781
|
return;
|
|
58779
58782
|
}
|
|
58780
58783
|
haveFieldsChanged = true;
|
|
58781
|
-
|
|
58784
|
+
log7.debug(`Fields have changed for ${tableName}`);
|
|
58782
58785
|
} else {
|
|
58783
|
-
|
|
58786
|
+
log7.debug(`Fields have not been generated for ${tableName}`);
|
|
58784
58787
|
}
|
|
58785
58788
|
await Bun.$`cp ${modelPath} ${copiedModelPath}`;
|
|
58786
58789
|
const hasBeenMigrated = await hasTableBeenMigrated(tableName);
|
|
58787
|
-
|
|
58790
|
+
log7.debug(`Has ${tableName} been migrated? ${hasBeenMigrated}`);
|
|
58788
58791
|
if (haveFieldsChanged)
|
|
58789
58792
|
await createAlterTableMigration3(modelPath);
|
|
58790
58793
|
else
|
|
58791
58794
|
await createTableMigration3(modelPath);
|
|
58792
58795
|
}
|
|
58793
58796
|
async function createTableMigration3(modelPath) {
|
|
58794
|
-
|
|
58797
|
+
log7.debug("createTableMigration modelPath:", modelPath);
|
|
58795
58798
|
const model2 = (await import(modelPath)).default;
|
|
58796
58799
|
const tableName = await modelTableName(model2);
|
|
58797
58800
|
await createPivotTableMigration3(model2);
|
|
@@ -58836,9 +58839,9 @@ async function createTableMigration3(modelPath) {
|
|
|
58836
58839
|
migrationContent += `}\n`;
|
|
58837
58840
|
const timestamp = new Date().getTime().toString();
|
|
58838
58841
|
const migrationFileName = `${timestamp}-create-${tableName}-table.ts`;
|
|
58839
|
-
const migrationFilePath =
|
|
58842
|
+
const migrationFilePath = path11.userMigrationsPath(migrationFileName);
|
|
58840
58843
|
Bun.write(migrationFilePath, migrationContent);
|
|
58841
|
-
|
|
58844
|
+
log7.success(`Created migration: ${italic4(migrationFileName)}`);
|
|
58842
58845
|
}
|
|
58843
58846
|
async function createPivotTableMigration3(model2) {
|
|
58844
58847
|
const pivotTables = await getPivotTables(model2);
|
|
@@ -58859,15 +58862,15 @@ async function createPivotTableMigration3(model2) {
|
|
|
58859
58862
|
migrationContent += ` }\n`;
|
|
58860
58863
|
const timestamp = new Date().getTime().toString();
|
|
58861
58864
|
const migrationFileName = `${timestamp}-create-${pivotTable.table}-table.ts`;
|
|
58862
|
-
const migrationFilePath =
|
|
58865
|
+
const migrationFilePath = path11.userMigrationsPath(migrationFileName);
|
|
58863
58866
|
Bun.write(migrationFilePath, migrationContent);
|
|
58864
|
-
|
|
58867
|
+
log7.success(`Created pivot migration: ${migrationFileName}`);
|
|
58865
58868
|
}
|
|
58866
58869
|
}
|
|
58867
58870
|
async function createAlterTableMigration3(modelPath) {
|
|
58868
58871
|
console.log("createAlterTableMigration");
|
|
58869
58872
|
const model2 = (await import(modelPath)).default;
|
|
58870
|
-
const modelName =
|
|
58873
|
+
const modelName = path11.basename(modelPath);
|
|
58871
58874
|
const tableName = await modelTableName(model2);
|
|
58872
58875
|
const lastMigrationFields = await getLastMigrationFields(modelName);
|
|
58873
58876
|
const lastFields = lastMigrationFields ?? {};
|
|
@@ -58889,14 +58892,14 @@ async function createAlterTableMigration3(modelPath) {
|
|
|
58889
58892
|
migrationContent += `}\n`;
|
|
58890
58893
|
const timestamp = new Date().getTime().toString();
|
|
58891
58894
|
const migrationFileName = `${timestamp}-update-${tableName}-table.ts`;
|
|
58892
|
-
const migrationFilePath =
|
|
58895
|
+
const migrationFilePath = path11.userMigrationsPath(migrationFileName);
|
|
58893
58896
|
Bun.write(migrationFilePath, migrationContent);
|
|
58894
|
-
|
|
58897
|
+
log7.success(`Created migration: ${italic4(migrationFileName)}`);
|
|
58895
58898
|
}
|
|
58896
58899
|
|
|
58897
58900
|
// src/drivers/index.ts
|
|
58898
58901
|
async function getLastMigrationFields(modelName) {
|
|
58899
|
-
const oldModelPath =
|
|
58902
|
+
const oldModelPath = path13.frameworkPath(`database/models/${modelName}`);
|
|
58900
58903
|
const model2 = (await import(oldModelPath)).default;
|
|
58901
58904
|
let fields2 = {};
|
|
58902
58905
|
if (typeof model2.attributes === "object")
|
|
@@ -58912,7 +58915,7 @@ async function modelTableName2(model2) {
|
|
|
58912
58915
|
return model2.table ?? snakeCase2(plural2(model2?.name || ""));
|
|
58913
58916
|
}
|
|
58914
58917
|
async function hasTableBeenMigrated(tableName) {
|
|
58915
|
-
|
|
58918
|
+
log8.debug(`hasTableBeenMigrated for table: ${tableName}`);
|
|
58916
58919
|
const results = await getExecutedMigrations();
|
|
58917
58920
|
return results.some((migration) => migration.name.includes(tableName));
|
|
58918
58921
|
}
|
|
@@ -58971,7 +58974,7 @@ function prepareTextColumnType(rule) {
|
|
|
58971
58974
|
return `'${columnType}'`;
|
|
58972
58975
|
}
|
|
58973
58976
|
async function checkPivotMigration(dynamicPart) {
|
|
58974
|
-
const files = await fs6.readdir(
|
|
58977
|
+
const files = await fs6.readdir(path13.userMigrationsPath());
|
|
58975
58978
|
return files.some((migrationFile) => {
|
|
58976
58979
|
const escapedDynamicPart = dynamicPart.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
58977
58980
|
const pattern = new RegExp(`(-${escapedDynamicPart}-)`);
|
|
@@ -58988,7 +58991,7 @@ async function getRelations(model2) {
|
|
|
58988
58991
|
if (isString(relationInstance)) {
|
|
58989
58992
|
relationModel = relationInstance;
|
|
58990
58993
|
}
|
|
58991
|
-
const modelRelationPath =
|
|
58994
|
+
const modelRelationPath = path13.userModelsPath(`${relationModel}.ts`);
|
|
58992
58995
|
const modelRelation = (await import(modelRelationPath)).default;
|
|
58993
58996
|
const formattedModelName = model2.name?.toLowerCase();
|
|
58994
58997
|
relationships.push({
|
|
@@ -59009,7 +59012,7 @@ async function getRelations(model2) {
|
|
|
59009
59012
|
return relationships;
|
|
59010
59013
|
}
|
|
59011
59014
|
async function fetchOtherModelRelations(model2) {
|
|
59012
|
-
const modelFiles = glob5.sync(
|
|
59015
|
+
const modelFiles = glob5.sync(path13.userModelsPath("*.ts"));
|
|
59013
59016
|
const modelRelations = [];
|
|
59014
59017
|
for (let i = 0;i < modelFiles.length; i++) {
|
|
59015
59018
|
const modelFileElement = modelFiles[i];
|
|
@@ -59030,7 +59033,7 @@ async function getPivotTables(model2) {
|
|
|
59030
59033
|
if (model2.belongsToMany && model2.name) {
|
|
59031
59034
|
if ("belongsToMany" in model2) {
|
|
59032
59035
|
for (const belongsToManyRelation of model2.belongsToMany) {
|
|
59033
|
-
const modelRelationPath =
|
|
59036
|
+
const modelRelationPath = path13.userModelsPath(`${belongsToManyRelation}.ts`);
|
|
59034
59037
|
const modelRelation = (await import(modelRelationPath)).default;
|
|
59035
59038
|
const formattedModelName = model2.name.toLowerCase();
|
|
59036
59039
|
const firstForeignKey = belongsToManyRelation.firstForeignKey || `${model2.name?.toLowerCase()}_${model2.primaryKey}`;
|
|
@@ -59052,7 +59055,7 @@ var hasRelations = function(obj, key) {
|
|
|
59052
59055
|
|
|
59053
59056
|
// src/utils.ts
|
|
59054
59057
|
import {database as database6} from "@stacksjs/config";
|
|
59055
|
-
import {log as
|
|
59058
|
+
import {log as log9} from "@stacksjs/logging";
|
|
59056
59059
|
import {Kysely, MysqlDialect, PostgresDialect, sql} from "kysely";
|
|
59057
59060
|
|
|
59058
59061
|
// ../../../../node_modules/kysely-bun-worker/dist/index.mjs
|
|
@@ -59185,11 +59188,11 @@ var import_pg = __toESM(require_lib16(), 1);
|
|
|
59185
59188
|
import {createPool} from "mysql2";
|
|
59186
59189
|
function getDialect() {
|
|
59187
59190
|
const driver = database6.default ?? "sqlite";
|
|
59188
|
-
|
|
59191
|
+
log9.debug(`Using database driver: ${driver}`);
|
|
59189
59192
|
if (driver === "sqlite") {
|
|
59190
|
-
const
|
|
59193
|
+
const path14 = database6.connections?.sqlite.database ?? "database/stacks.sqlite";
|
|
59191
59194
|
return new BunWorkerDialect({
|
|
59192
|
-
url:
|
|
59195
|
+
url: path14
|
|
59193
59196
|
});
|
|
59194
59197
|
}
|
|
59195
59198
|
if (driver === "mysql") {
|
|
@@ -59224,19 +59227,19 @@ var db = new Kysely({
|
|
|
59224
59227
|
// src/migrations.ts
|
|
59225
59228
|
async function runDatabaseMigration() {
|
|
59226
59229
|
try {
|
|
59227
|
-
|
|
59230
|
+
log10.info("Migrating database...");
|
|
59228
59231
|
const { error, results } = await migrator.migrateToLatest();
|
|
59229
59232
|
if (error) {
|
|
59230
|
-
|
|
59233
|
+
log10.error(error);
|
|
59231
59234
|
return err(error);
|
|
59232
59235
|
}
|
|
59233
59236
|
if (results?.length === 0) {
|
|
59234
|
-
|
|
59237
|
+
log10.success("No new migrations were executed");
|
|
59235
59238
|
return ok("No new migrations were executed");
|
|
59236
59239
|
}
|
|
59237
59240
|
if (results)
|
|
59238
59241
|
return ok(results);
|
|
59239
|
-
|
|
59242
|
+
log10.success("Database migration completed with no new migrations.");
|
|
59240
59243
|
return ok("Database migration completed with no new migrations.");
|
|
59241
59244
|
} catch (error) {
|
|
59242
59245
|
console.error("Migration failed:", error);
|
|
@@ -59254,13 +59257,13 @@ async function resetDatabase() {
|
|
|
59254
59257
|
}
|
|
59255
59258
|
async function generateMigrations() {
|
|
59256
59259
|
try {
|
|
59257
|
-
|
|
59258
|
-
const modelFiles = glob6.sync(
|
|
59260
|
+
log10.info("Generating migrations...");
|
|
59261
|
+
const modelFiles = glob6.sync(path15.userModelsPath("*.ts"));
|
|
59259
59262
|
for (const file of modelFiles) {
|
|
59260
|
-
|
|
59263
|
+
log10.debug("Generating migration for:", file);
|
|
59261
59264
|
await generateMigration(file);
|
|
59262
59265
|
}
|
|
59263
|
-
|
|
59266
|
+
log10.success("Migrations generated");
|
|
59264
59267
|
return ok("Migrations generated");
|
|
59265
59268
|
} catch (error) {
|
|
59266
59269
|
return err(error);
|
|
@@ -59282,7 +59285,7 @@ async function getExecutedMigrations2() {
|
|
|
59282
59285
|
}
|
|
59283
59286
|
}
|
|
59284
59287
|
async function haveModelFieldsChangedSinceLastMigration(modelPath) {
|
|
59285
|
-
|
|
59288
|
+
log10.debug(`haveModelFieldsChangedSinceLastMigration for model: ${modelPath}`);
|
|
59286
59289
|
const cmd = ``;
|
|
59287
59290
|
const gitHistory = await $2`${cmd}`.text();
|
|
59288
59291
|
return !!gitHistory;
|
|
@@ -59304,7 +59307,7 @@ async function lastMigrationDate() {
|
|
|
59304
59307
|
}
|
|
59305
59308
|
}
|
|
59306
59309
|
async function getLastMigrationFields2(modelName) {
|
|
59307
|
-
const oldModelPath =
|
|
59310
|
+
const oldModelPath = path15.frameworkPath(`database/models/${modelName}`);
|
|
59308
59311
|
const model2 = (await import(oldModelPath)).default;
|
|
59309
59312
|
let fields2 = {};
|
|
59310
59313
|
if (typeof model2.attributes === "object")
|
|
@@ -59321,29 +59324,29 @@ var migrator = new Migrator({
|
|
|
59321
59324
|
db,
|
|
59322
59325
|
provider: new FileMigrationProvider({
|
|
59323
59326
|
fs: fs7,
|
|
59324
|
-
path:
|
|
59325
|
-
migrationFolder:
|
|
59327
|
+
path: path15,
|
|
59328
|
+
migrationFolder: path15.userMigrationsPath()
|
|
59326
59329
|
}),
|
|
59327
59330
|
migrationTableName: database7.migrations,
|
|
59328
59331
|
migrationLockTableName: database7.migrationLocks
|
|
59329
59332
|
});
|
|
59330
59333
|
// src/seeder.ts
|
|
59331
|
-
import {italic as italic6, log as
|
|
59332
|
-
import {path as
|
|
59334
|
+
import {italic as italic6, log as log11} from "@stacksjs/cli";
|
|
59335
|
+
import {path as path17} from "@stacksjs/path";
|
|
59333
59336
|
import {fs as fs8, glob as glob7} from "@stacksjs/storage";
|
|
59334
59337
|
async function seedModel(name, model2) {
|
|
59335
59338
|
if (model2?.traits?.useSeeder === false || model2?.traits?.seedable === false) {
|
|
59336
|
-
|
|
59339
|
+
log11.info(`Skipping seeding for ${italic6(name)}`);
|
|
59337
59340
|
return;
|
|
59338
59341
|
}
|
|
59339
59342
|
if (!model2)
|
|
59340
|
-
model2 = await import(
|
|
59343
|
+
model2 = await import(path17.userModelsPath(name));
|
|
59341
59344
|
const tableName = await modelTableName(model2);
|
|
59342
59345
|
const seedCount = typeof model2.traits?.useSeeder === "object" && model2.traits?.useSeeder?.count ? model2.traits.useSeeder.count : 10;
|
|
59343
|
-
|
|
59346
|
+
log11.info(`Seeding ${seedCount} records into ${italic6(tableName)}`);
|
|
59344
59347
|
const records = [];
|
|
59345
59348
|
const otherRelations = await fetchOtherModelRelations2(model2);
|
|
59346
|
-
|
|
59349
|
+
log11.debug(otherRelations);
|
|
59347
59350
|
for (let i = 0;i < seedCount; i++) {
|
|
59348
59351
|
const record = {};
|
|
59349
59352
|
for (const fieldName in model2.attributes) {
|
|
@@ -59361,7 +59364,7 @@ async function seedModel(name, model2) {
|
|
|
59361
59364
|
await db.insertInto(tableName).values(records).execute();
|
|
59362
59365
|
}
|
|
59363
59366
|
async function seedModelRelation(modelName) {
|
|
59364
|
-
const modelInstance = (await import(
|
|
59367
|
+
const modelInstance = (await import(path17.userModelsPath(modelName))).default;
|
|
59365
59368
|
if (!modelInstance)
|
|
59366
59369
|
return 1;
|
|
59367
59370
|
const record = {};
|
|
@@ -59383,7 +59386,7 @@ async function getRelations2(model2) {
|
|
|
59383
59386
|
if (isString(relationInstance)) {
|
|
59384
59387
|
relationModel = relationInstance;
|
|
59385
59388
|
}
|
|
59386
|
-
const modelRelationPath =
|
|
59389
|
+
const modelRelationPath = path17.userModelsPath(`${relationModel}.ts`);
|
|
59387
59390
|
const modelRelation = (await import(modelRelationPath)).default;
|
|
59388
59391
|
const formattedModelName = model2.name?.toLowerCase();
|
|
59389
59392
|
relationships.push({
|
|
@@ -59404,7 +59407,7 @@ async function getRelations2(model2) {
|
|
|
59404
59407
|
return relationships;
|
|
59405
59408
|
}
|
|
59406
59409
|
async function fetchOtherModelRelations2(model2) {
|
|
59407
|
-
const modelFiles = glob7.sync(
|
|
59410
|
+
const modelFiles = glob7.sync(path17.userModelsPath("*.ts"));
|
|
59408
59411
|
const modelRelations = [];
|
|
59409
59412
|
for (let i = 0;i < modelFiles.length; i++) {
|
|
59410
59413
|
const modelFileElement = modelFiles[i];
|
|
@@ -59424,24 +59427,24 @@ var hasRelations2 = function(obj, key) {
|
|
|
59424
59427
|
return key in obj;
|
|
59425
59428
|
};
|
|
59426
59429
|
async function seed() {
|
|
59427
|
-
const dbPath =
|
|
59430
|
+
const dbPath = path17.userDatabasePath("stacks.sqlite");
|
|
59428
59431
|
if (!fs8.existsSync(dbPath)) {
|
|
59429
|
-
|
|
59432
|
+
log11.warn("No database found, configuring it...");
|
|
59430
59433
|
await resetDatabase();
|
|
59431
59434
|
await generateMigrations();
|
|
59432
59435
|
await runDatabaseMigration();
|
|
59433
59436
|
} else {
|
|
59434
|
-
|
|
59437
|
+
log11.debug("Database configured...");
|
|
59435
59438
|
}
|
|
59436
|
-
const customSeederPath =
|
|
59439
|
+
const customSeederPath = path17.userDatabasePath("seeder.ts");
|
|
59437
59440
|
if (fs8.existsSync(customSeederPath)) {
|
|
59438
|
-
|
|
59441
|
+
log11.info("Custom seeder found");
|
|
59439
59442
|
await import(customSeederPath);
|
|
59440
59443
|
}
|
|
59441
|
-
const modelsDir =
|
|
59444
|
+
const modelsDir = path17.userModelsPath();
|
|
59442
59445
|
const modelFiles = fs8.readdirSync(modelsDir).filter((file) => file.endsWith(".ts"));
|
|
59443
59446
|
for (const file of modelFiles) {
|
|
59444
|
-
const modelPath =
|
|
59447
|
+
const modelPath = path17.join(modelsDir, file);
|
|
59445
59448
|
const model2 = await import(modelPath);
|
|
59446
59449
|
await seedModel(file, model2.default);
|
|
59447
59450
|
}
|