ilana-orm 1.0.1 → 1.0.3

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.
Files changed (2) hide show
  1. package/cli/ilana.js +87 -11
  2. package/package.json +1 -1
package/cli/ilana.js CHANGED
@@ -64,6 +64,19 @@ function isTypeScriptProject() {
64
64
  return fs.existsSync(path.join(process.cwd(), 'tsconfig.json'));
65
65
  }
66
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
+
67
80
  function getFileExtension() {
68
81
  return isTypeScriptProject() ? '.ts' : '.js';
69
82
  }
@@ -118,8 +131,8 @@ function generateModel(name, options = {}) {
118
131
 
119
132
  function getModelTemplate(className, tableName) {
120
133
  if (isTypeScriptProject()) {
121
- return `import Model from 'ilana-orm/orm/Model';
122
- // import { MoneyCast, EncryptedCast } from 'ilana-orm/orm/CustomCasts';
134
+ return `import Model from 'ilana-orm/orm/Model.js';
135
+ // import { MoneyCast, EncryptedCast } from 'ilana-orm/orm/CustomCasts.js';
123
136
 
124
137
  export default class ${className} extends Model {
125
138
  protected static table = '${tableName}';
@@ -210,7 +223,7 @@ module.exports = ${className};
210
223
 
211
224
  function getPivotModelTemplate(className, tableName) {
212
225
  if (isTypeScriptProject()) {
213
- return `import Model from 'ilana-orm/orm/Model';
226
+ return `import Model from 'ilana-orm/orm/Model.js';
214
227
 
215
228
  export default class ${className} extends Model {
216
229
  protected static table = '${tableName}';
@@ -247,8 +260,8 @@ function generateFactory(className) {
247
260
  }
248
261
 
249
262
  const template = isTypeScriptProject() ?
250
- `import { defineFactory } from 'ilana-orm/orm/Factory';
251
- import ${className} from '../../models/${className}';
263
+ `import { defineFactory } from 'ilana-orm/orm/Factory.js';
264
+ import ${className} from '../../models/${className}.js';
252
265
 
253
266
  export default defineFactory(${className}, (faker) => ({
254
267
  // Define your factory attributes here
@@ -285,9 +298,9 @@ function generateSeeder(className) {
285
298
  }
286
299
 
287
300
  const template = isTypeScriptProject() ?
288
- `import Seeder from 'ilana-orm/orm/Seeder';
289
- import ${className} from '../../models/${className}';
290
- import '../factories/${className}Factory';
301
+ `import Seeder from 'ilana-orm/orm/Seeder.js';
302
+ import ${className} from '../../models/${className}.js';
303
+ import '../factories/${className}Factory.js';
291
304
 
292
305
  export default class ${className}Seeder extends Seeder {
293
306
  async run(): Promise<void> {
@@ -339,7 +352,69 @@ const commands = {
339
352
  // Create config file if it doesn't exist
340
353
  const configPath = 'ilana.config.js';
341
354
  if (!fs.existsSync(configPath)) {
342
- const configTemplate = `require('dotenv').config();
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.js';
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();
343
418
  const Database = require('ilana-orm/database/connection');
344
419
 
345
420
  const config = {
@@ -394,6 +469,7 @@ Database.configure(config);
394
469
 
395
470
  module.exports = config;
396
471
  `;
472
+ }
397
473
  fs.writeFileSync(configPath, configTemplate);
398
474
  console.log(`Created config file: ${configPath}`);
399
475
  }
@@ -787,7 +863,7 @@ DB_TIMEZONE=UTC
787
863
  }
788
864
 
789
865
  const template = isTypeScriptProject() ?
790
- `${modelName ? `import ${modelName} from '../models/${modelName}';\n\n` : ''}export default class ${className}Observer {
866
+ `${modelName ? `import ${modelName} from '../models/${modelName}.js';\n\n` : ''}export default class ${className}Observer {
791
867
  async creating(model: ${modelName || 'any'}): Promise<void> {}
792
868
  async created(model: ${modelName || 'any'}): Promise<void> {}
793
869
  async updating(model: ${modelName || 'any'}): Promise<void> {}
@@ -835,7 +911,7 @@ module.exports = ${className}Observer;
835
911
  }
836
912
 
837
913
  const template = isTypeScriptProject() ?
838
- `import { CustomCast } from 'ilana-orm/orm/Model';
914
+ `import { CustomCast } from 'ilana-orm/orm/Model.js';
839
915
 
840
916
  export default class ${className}Cast implements CustomCast {
841
917
  get(value: any): any {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ilana-orm",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "A fully-featured, Eloquent-style ORM for Node.js with TypeScript support",
5
5
  "main": "index.js",
6
6
  "bin": {