From 0ebd4cbd66a3a8e6c47145eeb1a58866410cdf2d Mon Sep 17 00:00:00 2001 From: Charles Lapierre Date: Sun, 8 Jun 2025 13:12:22 +0200 Subject: [PATCH 1/4] chore: add @adonisjs/lucid dependency to package.json for database management --- commands/make/factory.ts | 27 ++++++++++++++ commands/make/model.ts | 68 ++++++++++++++++++++++++++++++++++++ package.json | 1 + stubs/make/factory/main.stub | 19 ++++++++++ stubs/make/model/main.stub | 21 +++++++++++ 5 files changed, 136 insertions(+) create mode 100644 commands/make/factory.ts create mode 100644 commands/make/model.ts create mode 100644 stubs/make/factory/main.stub create mode 100644 stubs/make/model/main.stub diff --git a/commands/make/factory.ts b/commands/make/factory.ts new file mode 100644 index 0000000..8a405cb --- /dev/null +++ b/commands/make/factory.ts @@ -0,0 +1,27 @@ +import { flags } from '@adonisjs/core/ace' +import { stubsRoot } from '../../stubs/main.js' +import MakeFactory from '@adonisjs/lucid/commands/make_factory' +import { COMMAND_PREFIX, MODULE_FLAG } from '../../src/constants.js' +import { checkModule } from '../../src/utils.js' + +/** + * Command to make a new Factory + */ +export default class MMakeFactory extends MakeFactory { + @flags.string(MODULE_FLAG) + declare module: string + + override async run() { + if (!checkModule(this.app, this.module)) { + this.kernel.exec(`${COMMAND_PREFIX}:module`, [this.module]) + } + + const codemods = await this.createCodemods() + await codemods.makeUsingStub(stubsRoot, 'make/factory/main.stub', { + flags: this.parsed.flags, + entity: this.app.generators.createEntity(this.model), + model: this.app.generators.createEntity(this.model), + pathAlias: `#${this.module}/models`, + }) + } +} diff --git a/commands/make/model.ts b/commands/make/model.ts new file mode 100644 index 0000000..8f6b46b --- /dev/null +++ b/commands/make/model.ts @@ -0,0 +1,68 @@ +import { flags } from '@adonisjs/core/ace' +import { stubsRoot } from '../../stubs/main.js' +import MakeModel from '@adonisjs/lucid/commands/make_model' +import { COMMAND_PREFIX, MODULE_FLAG } from '../../src/constants.js' +import { checkModule } from '../../src/utils.js' + +export default class MMakeModel extends MakeModel { + @flags.string(MODULE_FLAG) + declare module: string + + /** + * Run migrations + */ + private async runMakeMigration() { + if (!this.migration || this.exitCode) { + return + } + + const makeMigration = await this.kernel.exec('make:migration', [this.name]) + this.exitCode = makeMigration.exitCode + this.error = makeMigration.error + } + + /** + * Make controller + */ + private async runMakeController() { + if (!this.controller || this.exitCode) { + return + } + + const makeController = await this.kernel.exec('make:controller', [this.name]) + this.exitCode = makeController.exitCode + this.error = makeController.error + } + + /** + * Make factory + */ + private async runMakeFactory() { + if (!this.factory || this.exitCode) { + return + } + + const makeFactory = await this.kernel.exec('make:factory', [this.name]) + this.exitCode = makeFactory.exitCode + this.error = makeFactory.error + } + + /** + * Execute command + */ + override async run(): Promise { + if (!checkModule(this.app, this.module)) { + this.kernel.exec(`${COMMAND_PREFIX}:module`, [this.module]) + } + + const codemods = await this.createCodemods() + await codemods.makeUsingStub(stubsRoot, 'make/model/main.stub', { + flags: this.parsed.flags, + entity: this.app.generators.createEntity(this.name), + }) + + await this.runMakeMigration() + await this.runMakeController() + await this.runMakeFactory() + } +} diff --git a/package.json b/package.json index 58b9b9f..b607679 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "@adonisjs/assembler": "^7.8.2", "@adonisjs/core": "^6.12.0", "@adonisjs/eslint-config": "2.0.0-beta.7", + "@adonisjs/lucid": "^21.6.1", "@adonisjs/prettier-config": "^1.4.0", "@adonisjs/tsconfig": "^1.3.0", "@japa/assert": "^3.0.0", diff --git a/stubs/make/factory/main.stub b/stubs/make/factory/main.stub new file mode 100644 index 0000000..69d16dd --- /dev/null +++ b/stubs/make/factory/main.stub @@ -0,0 +1,19 @@ +{{#var factoryName = generators.factoryName(entity.name)}} +{{#var factoryFileName = generators.factoryFileName(entity.name)}} +{{#var modelName = generators.modelName(model.name)}} +{{#var modelFileName = generators.modelFileName(model.name)}} +{{#var modelImportPath = generators.importPath(pathAlias, model.path, modelFileName.replace(/\.ts$/, ''))}} +{{#var moduleDirectoryName = flags.module}} +{{{ + exports({ + to: app.makePath('app', moduleDirectoryName, 'factories', factoryFileName) + }) +}}} +import factory from '@adonisjs/lucid/factories' +import {{ modelName }} from '{{ modelImportPath }}' + +export const {{ factoryName }} = factory + .define({{ modelName }}, async ({ faker }) => { + return {} + }) + .build() \ No newline at end of file diff --git a/stubs/make/model/main.stub b/stubs/make/model/main.stub new file mode 100644 index 0000000..a5062d4 --- /dev/null +++ b/stubs/make/model/main.stub @@ -0,0 +1,21 @@ +{{#var modelName = generators.modelName(entity.name)}} +{{#var modelFileName = generators.modelFileName(entity.name)}} +{{#var moduleDirectoryName = flags.module}} +{{{ + exports({ + to: app.makePath('app', moduleDirectoryName, 'models', modelFileName) + }) +}}} +import { DateTime } from 'luxon' +import { BaseModel, column } from '@adonisjs/lucid/orm' + +export default class {{ modelName }} extends BaseModel { + @column({ isPrimary: true }) + declare id: number + + @column.dateTime({ autoCreate: true }) + declare createdAt: DateTime + + @column.dateTime({ autoCreate: true, autoUpdate: true }) + declare updatedAt: DateTime +} \ No newline at end of file From 46db84dfef2f92a2cc992d0dee7a8170316db668 Mon Sep 17 00:00:00 2001 From: Charles Lapierre Date: Fri, 27 Jun 2025 13:32:18 +0200 Subject: [PATCH 2/4] chore: update @adonisjs/lucid to version 21.7.0 and refactor make commands to handle missing module cases --- commands/make/factory.ts | 4 ++++ commands/make/model.ts | 47 ++++------------------------------------ package.json | 2 +- 3 files changed, 9 insertions(+), 44 deletions(-) diff --git a/commands/make/factory.ts b/commands/make/factory.ts index 8a405cb..374c1ce 100644 --- a/commands/make/factory.ts +++ b/commands/make/factory.ts @@ -12,6 +12,10 @@ export default class MMakeFactory extends MakeFactory { declare module: string override async run() { + if (!this.model) { + return super.run() + } + if (!checkModule(this.app, this.module)) { this.kernel.exec(`${COMMAND_PREFIX}:module`, [this.module]) } diff --git a/commands/make/model.ts b/commands/make/model.ts index 8f6b46b..587e203 100644 --- a/commands/make/model.ts +++ b/commands/make/model.ts @@ -8,49 +8,14 @@ export default class MMakeModel extends MakeModel { @flags.string(MODULE_FLAG) declare module: string - /** - * Run migrations - */ - private async runMakeMigration() { - if (!this.migration || this.exitCode) { - return - } - - const makeMigration = await this.kernel.exec('make:migration', [this.name]) - this.exitCode = makeMigration.exitCode - this.error = makeMigration.error - } - - /** - * Make controller - */ - private async runMakeController() { - if (!this.controller || this.exitCode) { - return - } - - const makeController = await this.kernel.exec('make:controller', [this.name]) - this.exitCode = makeController.exitCode - this.error = makeController.error - } - - /** - * Make factory - */ - private async runMakeFactory() { - if (!this.factory || this.exitCode) { - return - } - - const makeFactory = await this.kernel.exec('make:factory', [this.name]) - this.exitCode = makeFactory.exitCode - this.error = makeFactory.error - } - /** * Execute command */ override async run(): Promise { + if (!this.module) { + return super.run() + } + if (!checkModule(this.app, this.module)) { this.kernel.exec(`${COMMAND_PREFIX}:module`, [this.module]) } @@ -60,9 +25,5 @@ export default class MMakeModel extends MakeModel { flags: this.parsed.flags, entity: this.app.generators.createEntity(this.name), }) - - await this.runMakeMigration() - await this.runMakeController() - await this.runMakeFactory() } } diff --git a/package.json b/package.json index b607679..7818f30 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "@adonisjs/assembler": "^7.8.2", "@adonisjs/core": "^6.12.0", "@adonisjs/eslint-config": "2.0.0-beta.7", - "@adonisjs/lucid": "^21.6.1", + "@adonisjs/lucid": "^21.7.0", "@adonisjs/prettier-config": "^1.4.0", "@adonisjs/tsconfig": "^1.3.0", "@japa/assert": "^3.0.0", From 9b59197028987bee734aa38f6af4bd7501d3f220 Mon Sep 17 00:00:00 2001 From: Charles Lapierre Date: Sun, 29 Jun 2025 16:10:19 +0200 Subject: [PATCH 3/4] fix: correct variable reference from model to module in MMakeFactory run method --- commands/make/factory.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/make/factory.ts b/commands/make/factory.ts index 374c1ce..c90daf1 100644 --- a/commands/make/factory.ts +++ b/commands/make/factory.ts @@ -12,7 +12,7 @@ export default class MMakeFactory extends MakeFactory { declare module: string override async run() { - if (!this.model) { + if (!this.module) { return super.run() } From 0176e5ad7fed0749479eb9d6142db8c2358e6d29 Mon Sep 17 00:00:00 2001 From: Charles Lapierre Date: Sun, 29 Jun 2025 16:18:05 +0200 Subject: [PATCH 4/4] feat: implement module-specific controller and factory creation in MMakeModel --- commands/make/model.ts | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/commands/make/model.ts b/commands/make/model.ts index 587e203..3d7871c 100644 --- a/commands/make/model.ts +++ b/commands/make/model.ts @@ -5,9 +5,36 @@ import { COMMAND_PREFIX, MODULE_FLAG } from '../../src/constants.js' import { checkModule } from '../../src/utils.js' export default class MMakeModel extends MakeModel { - @flags.string(MODULE_FLAG) + @flags.string({ + ...MODULE_FLAG, + alias: undefined, + }) declare module: string + private async runMakeControllerModule() { + if (!this.controller || this.exitCode) { + return + } + + const makeController = await this.kernel.exec('make:controller', [ + this.name, + '--module', + this.module, + ]) + this.exitCode = makeController.exitCode + this.error = makeController.error + } + + private async runMakeFactoryModule() { + if (!this.factory || this.exitCode) { + return + } + + const makeFactory = await this.kernel.exec('make:factory', [this.name, '--module', this.module]) + this.exitCode = makeFactory.exitCode + this.error = makeFactory.error + } + /** * Execute command */ @@ -25,5 +52,9 @@ export default class MMakeModel extends MakeModel { flags: this.parsed.flags, entity: this.app.generators.createEntity(this.name), }) + + await this['runMakeMigration']() + await this.runMakeControllerModule() + await this.runMakeFactoryModule() } }