ilana-orm 1.0.0 → 1.0.2
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/cli/ilana.js +119 -20
- package/orm/MigrationRunner.js +27 -3
- package/package.json +3 -3
package/cli/ilana.js
CHANGED
|
@@ -30,13 +30,24 @@ const defaultConfig = {
|
|
|
30
30
|
};
|
|
31
31
|
|
|
32
32
|
// Load configuration and auto-initialize
|
|
33
|
-
function loadConfig() {
|
|
33
|
+
async function loadConfig() {
|
|
34
34
|
const configPath = path.join(process.cwd(), 'ilana.config.js');
|
|
35
35
|
if (fs.existsSync(configPath)) {
|
|
36
36
|
delete require.cache[configPath];
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
try {
|
|
38
|
+
const config = require(configPath);
|
|
39
|
+
// Config file already initializes database
|
|
40
|
+
return config;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (error.code === 'ERR_REQUIRE_ESM') {
|
|
43
|
+
// Handle ES modules
|
|
44
|
+
const configModule = await import(configPath);
|
|
45
|
+
const config = configModule.default || configModule;
|
|
46
|
+
return config;
|
|
47
|
+
} else {
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
40
51
|
}
|
|
41
52
|
// No config file found - don't initialize with hardcoded default
|
|
42
53
|
console.error('No ilana.config.js found. Run "npx ilana setup" first.');
|
|
@@ -44,8 +55,8 @@ function loadConfig() {
|
|
|
44
55
|
}
|
|
45
56
|
|
|
46
57
|
// Initialize database
|
|
47
|
-
function initializeDatabase() {
|
|
48
|
-
loadConfig(); // Config handles initialization
|
|
58
|
+
async function initializeDatabase() {
|
|
59
|
+
await loadConfig(); // Config handles initialization
|
|
49
60
|
}
|
|
50
61
|
|
|
51
62
|
// Helper functions
|
|
@@ -53,6 +64,19 @@ function isTypeScriptProject() {
|
|
|
53
64
|
return fs.existsSync(path.join(process.cwd(), 'tsconfig.json'));
|
|
54
65
|
}
|
|
55
66
|
|
|
67
|
+
function isESModuleProject() {
|
|
68
|
+
const packageJsonPath = path.join(process.cwd(), 'package.json');
|
|
69
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
70
|
+
try {
|
|
71
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
72
|
+
return packageJson.type === 'module';
|
|
73
|
+
} catch (error) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
56
80
|
function getFileExtension() {
|
|
57
81
|
return isTypeScriptProject() ? '.ts' : '.js';
|
|
58
82
|
}
|
|
@@ -328,7 +352,69 @@ const commands = {
|
|
|
328
352
|
// Create config file if it doesn't exist
|
|
329
353
|
const configPath = 'ilana.config.js';
|
|
330
354
|
if (!fs.existsSync(configPath)) {
|
|
331
|
-
const
|
|
355
|
+
const isESModule = isESModuleProject();
|
|
356
|
+
const configTemplate = isESModule ? getESModuleConfigTemplate() : getCommonJSConfigTemplate();
|
|
357
|
+
|
|
358
|
+
function getESModuleConfigTemplate() {
|
|
359
|
+
return `import 'dotenv/config';
|
|
360
|
+
import Database from 'ilana-orm/database/connection';
|
|
361
|
+
|
|
362
|
+
const config = {
|
|
363
|
+
default: process.env.DB_CONNECTION || 'mysql',
|
|
364
|
+
timezone: process.env.DB_TIMEZONE || 'UTC',
|
|
365
|
+
|
|
366
|
+
connections: {
|
|
367
|
+
sqlite: {
|
|
368
|
+
client: 'sqlite3',
|
|
369
|
+
connection: {
|
|
370
|
+
filename: process.env.DB_FILENAME || './database.sqlite'
|
|
371
|
+
},
|
|
372
|
+
useNullAsDefault: true
|
|
373
|
+
},
|
|
374
|
+
|
|
375
|
+
mysql: {
|
|
376
|
+
client: 'mysql2',
|
|
377
|
+
connection: {
|
|
378
|
+
host: process.env.DB_HOST || 'localhost',
|
|
379
|
+
port: process.env.DB_PORT || 3306,
|
|
380
|
+
user: process.env.DB_USERNAME || 'root',
|
|
381
|
+
password: process.env.DB_PASSWORD || '',
|
|
382
|
+
database: process.env.DB_DATABASE || 'your_database',
|
|
383
|
+
timezone: process.env.DB_TIMEZONE || 'UTC'
|
|
384
|
+
}
|
|
385
|
+
},
|
|
386
|
+
|
|
387
|
+
postgres: {
|
|
388
|
+
client: 'pg',
|
|
389
|
+
connection: {
|
|
390
|
+
host: process.env.DB_HOST || 'localhost',
|
|
391
|
+
port: process.env.DB_PORT || 5432,
|
|
392
|
+
user: process.env.DB_USERNAME || 'postgres',
|
|
393
|
+
password: process.env.DB_PASSWORD || '',
|
|
394
|
+
database: process.env.DB_DATABASE || 'your_database'
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
},
|
|
398
|
+
|
|
399
|
+
migrations: {
|
|
400
|
+
directory: './database/migrations',
|
|
401
|
+
tableName: 'migrations'
|
|
402
|
+
},
|
|
403
|
+
|
|
404
|
+
seeds: {
|
|
405
|
+
directory: './database/seeds'
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
// Auto-initialize database connections
|
|
410
|
+
Database.configure(config);
|
|
411
|
+
|
|
412
|
+
export default config;
|
|
413
|
+
`;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function getCommonJSConfigTemplate() {
|
|
417
|
+
return `require('dotenv').config();
|
|
332
418
|
const Database = require('ilana-orm/database/connection');
|
|
333
419
|
|
|
334
420
|
const config = {
|
|
@@ -383,6 +469,7 @@ Database.configure(config);
|
|
|
383
469
|
|
|
384
470
|
module.exports = config;
|
|
385
471
|
`;
|
|
472
|
+
}
|
|
386
473
|
fs.writeFileSync(configPath, configTemplate);
|
|
387
474
|
console.log(`Created config file: ${configPath}`);
|
|
388
475
|
}
|
|
@@ -449,7 +536,7 @@ DB_TIMEZONE=UTC
|
|
|
449
536
|
}
|
|
450
537
|
}
|
|
451
538
|
|
|
452
|
-
initializeDatabase();
|
|
539
|
+
await initializeDatabase();
|
|
453
540
|
const runner = new MigrationRunner();
|
|
454
541
|
runner.generateMigration(name, tableName, isCreate);
|
|
455
542
|
},
|
|
@@ -524,14 +611,14 @@ DB_TIMEZONE=UTC
|
|
|
524
611
|
}
|
|
525
612
|
|
|
526
613
|
if (options.migration || options.all) {
|
|
527
|
-
initializeDatabase();
|
|
614
|
+
await initializeDatabase();
|
|
528
615
|
}
|
|
529
616
|
|
|
530
617
|
generateModel(name, options);
|
|
531
618
|
},
|
|
532
619
|
|
|
533
620
|
async migrate(...args) {
|
|
534
|
-
initializeDatabase();
|
|
621
|
+
await initializeDatabase();
|
|
535
622
|
const runner = new MigrationRunner();
|
|
536
623
|
|
|
537
624
|
let connection;
|
|
@@ -565,7 +652,7 @@ DB_TIMEZONE=UTC
|
|
|
565
652
|
},
|
|
566
653
|
|
|
567
654
|
async 'migrate:fresh'(...args) {
|
|
568
|
-
initializeDatabase();
|
|
655
|
+
await initializeDatabase();
|
|
569
656
|
const runner = new MigrationRunner();
|
|
570
657
|
|
|
571
658
|
let connection;
|
|
@@ -591,21 +678,21 @@ DB_TIMEZONE=UTC
|
|
|
591
678
|
},
|
|
592
679
|
|
|
593
680
|
async 'migrate:list'(connection) {
|
|
594
|
-
initializeDatabase();
|
|
681
|
+
await initializeDatabase();
|
|
595
682
|
const runner = new MigrationRunner();
|
|
596
683
|
await runner.list(connection);
|
|
597
684
|
process.exit(0);
|
|
598
685
|
},
|
|
599
686
|
|
|
600
687
|
async 'migrate:unlock'(connection) {
|
|
601
|
-
initializeDatabase();
|
|
688
|
+
await initializeDatabase();
|
|
602
689
|
const runner = new MigrationRunner();
|
|
603
690
|
await runner.unlock(connection);
|
|
604
691
|
process.exit(0);
|
|
605
692
|
},
|
|
606
693
|
|
|
607
694
|
async 'migrate:rollback'(...args) {
|
|
608
|
-
initializeDatabase();
|
|
695
|
+
await initializeDatabase();
|
|
609
696
|
const runner = new MigrationRunner();
|
|
610
697
|
|
|
611
698
|
let steps = 1;
|
|
@@ -641,28 +728,28 @@ DB_TIMEZONE=UTC
|
|
|
641
728
|
},
|
|
642
729
|
|
|
643
730
|
async 'migrate:reset'(connection) {
|
|
644
|
-
initializeDatabase();
|
|
731
|
+
await initializeDatabase();
|
|
645
732
|
const runner = new MigrationRunner();
|
|
646
733
|
await runner.reset(connection);
|
|
647
734
|
process.exit(0);
|
|
648
735
|
},
|
|
649
736
|
|
|
650
737
|
async 'migrate:refresh'(connection) {
|
|
651
|
-
initializeDatabase();
|
|
738
|
+
await initializeDatabase();
|
|
652
739
|
const runner = new MigrationRunner();
|
|
653
740
|
await runner.refresh(connection);
|
|
654
741
|
process.exit(0);
|
|
655
742
|
},
|
|
656
743
|
|
|
657
744
|
async 'migrate:status'(connection) {
|
|
658
|
-
initializeDatabase();
|
|
745
|
+
await initializeDatabase();
|
|
659
746
|
const runner = new MigrationRunner();
|
|
660
747
|
await runner.status(connection);
|
|
661
748
|
process.exit(0);
|
|
662
749
|
},
|
|
663
750
|
|
|
664
751
|
async seed(...args) {
|
|
665
|
-
initializeDatabase();
|
|
752
|
+
await initializeDatabase();
|
|
666
753
|
|
|
667
754
|
let seederName;
|
|
668
755
|
let connection;
|
|
@@ -710,7 +797,19 @@ DB_TIMEZONE=UTC
|
|
|
710
797
|
console.log(`Seeding: ${file}`);
|
|
711
798
|
const filepath = path.join(seedsPath, file);
|
|
712
799
|
delete require.cache[filepath];
|
|
713
|
-
|
|
800
|
+
|
|
801
|
+
let seederModule;
|
|
802
|
+
try {
|
|
803
|
+
seederModule = require(filepath);
|
|
804
|
+
} catch (error) {
|
|
805
|
+
if (error.code === 'ERR_REQUIRE_ESM') {
|
|
806
|
+
// Handle ES modules
|
|
807
|
+
seederModule = await import(filepath);
|
|
808
|
+
} else {
|
|
809
|
+
throw error;
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
|
|
714
813
|
const SeederClass = seederModule.default || seederModule;
|
|
715
814
|
const seeder = new SeederClass();
|
|
716
815
|
|
|
@@ -734,7 +833,7 @@ DB_TIMEZONE=UTC
|
|
|
734
833
|
},
|
|
735
834
|
|
|
736
835
|
async 'db:wipe'(connection) {
|
|
737
|
-
initializeDatabase();
|
|
836
|
+
await initializeDatabase();
|
|
738
837
|
const runner = new MigrationRunner();
|
|
739
838
|
await runner.wipe(connection);
|
|
740
839
|
process.exit(0);
|
package/orm/MigrationRunner.js
CHANGED
|
@@ -6,11 +6,24 @@ const SchemaBuilder = require('../database/schema-builder');
|
|
|
6
6
|
let config = {};
|
|
7
7
|
|
|
8
8
|
// Auto-load configuration on first import
|
|
9
|
-
(function autoLoadConfig() {
|
|
9
|
+
(async function autoLoadConfig() {
|
|
10
10
|
const configPath = path.join(process.cwd(), 'ilana.config.js');
|
|
11
11
|
if (fs.existsSync(configPath)) {
|
|
12
12
|
delete require.cache[configPath];
|
|
13
|
-
|
|
13
|
+
try {
|
|
14
|
+
config = require(configPath) || {}; // Config file handles Database.configure()
|
|
15
|
+
} catch (error) {
|
|
16
|
+
if (error.code === 'ERR_REQUIRE_ESM') {
|
|
17
|
+
// Handle ES modules
|
|
18
|
+
const configModule = await import(configPath);
|
|
19
|
+
config = configModule.default || configModule;
|
|
20
|
+
if (typeof config.configure === 'function') {
|
|
21
|
+
config.configure(config);
|
|
22
|
+
}
|
|
23
|
+
} else {
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
14
27
|
}
|
|
15
28
|
})();
|
|
16
29
|
|
|
@@ -293,7 +306,18 @@ class MigrationRunner {
|
|
|
293
306
|
async loadMigration(filename) {
|
|
294
307
|
const filepath = path.resolve(this.migrationsPath, filename);
|
|
295
308
|
delete require.cache[filepath];
|
|
296
|
-
|
|
309
|
+
|
|
310
|
+
let migrationModule;
|
|
311
|
+
try {
|
|
312
|
+
migrationModule = require(filepath);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
if (error.code === 'ERR_REQUIRE_ESM') {
|
|
315
|
+
// Handle ES modules
|
|
316
|
+
migrationModule = await import(filepath);
|
|
317
|
+
} else {
|
|
318
|
+
throw error;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
297
321
|
|
|
298
322
|
const MigrationClass = migrationModule.default || migrationModule;
|
|
299
323
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ilana-orm",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A fully-featured, Eloquent-style ORM for Node.js with TypeScript support",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"type": "git",
|
|
33
33
|
"url": "https://github.com/raphyabak/ilana-orm.git"
|
|
34
34
|
},
|
|
35
|
-
"homepage": "https://
|
|
35
|
+
"homepage": "https://raphwebb.mintlify.com",
|
|
36
36
|
"bugs": {
|
|
37
37
|
"url": "https://github.com/raphyabak/ilana-orm/issues"
|
|
38
38
|
},
|
|
@@ -68,4 +68,4 @@
|
|
|
68
68
|
"engines": {
|
|
69
69
|
"node": ">=16.0.0"
|
|
70
70
|
}
|
|
71
|
-
}
|
|
71
|
+
}
|