diff --git a/CHANGELOG.md b/CHANGELOG.md index 98c275f5..3e8150a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,18 @@ + +## [4.0.5](https://github.com/adonisjs/adonis-lucid/compare/v4.0.4...v4.0.5) (2017-07-30) + + +### Bug Fixes + +* **hooks:** fix bad validation behavior ([1b3a0d5](https://github.com/adonisjs/adonis-lucid/commit/1b3a0d5)) + + +### Features + +* **commands:** add config:database command ([98a318a](https://github.com/adonisjs/adonis-lucid/commit/98a318a)) + + + ## [4.0.4](https://github.com/adonisjs/adonis-lucid/compare/v4.0.3...v4.0.4) (2017-07-30) diff --git a/commands/MakeConfig.js b/commands/MakeConfig.js new file mode 100644 index 00000000..d4b18390 --- /dev/null +++ b/commands/MakeConfig.js @@ -0,0 +1,82 @@ +'use strict' + +/* + * adonis-lucid + * + * (c) Harminder Virk + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. +*/ + +const path = require('path') +const { Command } = require('@adonisjs/ace') + +class MakeConfig extends Command { + constructor (Helpers) { + super() + this.Helpers = Helpers + } + + /** + * Ioc container injections + * + * @method inject + * + * @return {Array} + */ + static get inject () { + return ['Adonis/Src/Helpers'] + } + + /** + * Command signature required by ace + * + * @method signature + * + * @return {String} + */ + static get signature () { + return ` + config:database + { -c, --connection=@value: The database connection to use } + { -e, --echo=@value: Write config to console } + ` + } + + /** + * Command description + * + * @method description + * + * @return {String} + */ + static get description () { + return 'Setup configuration for database provider' + } + + async handle (args, { connection, echo }) { + connection = connection || 'sqlite' + const template = await this.readFile(path.join(__dirname, './templates/config.mustache'), 'utf-8') + + /** + * Echo template over creating the config file + */ + if (echo) { + return this.viaAce ? console.log(template) : 'echoed' + } + + /** + * Create config file + */ + const configPath = `${path.join(this.Helpers.configPath(), 'database.js')}` + await this.generateFile(configPath, template, { connection }) + + if (!this.viaAce) { + return configPath + } + this.completed('created', configPath.replace(this.Helpers.appRoot(), '').replace(path.sep, '')) + } +} + +module.exports = MakeConfig diff --git a/commands/templates/config.mustache b/commands/templates/config.mustache new file mode 100644 index 00000000..25f1e37f --- /dev/null +++ b/commands/templates/config.mustache @@ -0,0 +1,78 @@ +'use strict' + +const Env = use('Env') +const Helpers = use('Helpers') + +module.exports = { + /* + |-------------------------------------------------------------------------- + | Default Connection + |-------------------------------------------------------------------------- + | + | Connection defines the default connection settings to be used while + | interacting with SQL databases. + | + */ + connection: Env.get('DB_CONNECTION', '{{ connection }}'), + + /* + |-------------------------------------------------------------------------- + | Sqlite + |-------------------------------------------------------------------------- + | + | Sqlite is a flat file database and can be good choice under development + | environment. + | + | npm i --save sqlite3 + | + */ + sqlite: { + client: 'sqlite3', + connection: { + filename: Helpers.databasePath('development.sqlite') + }, + useNullAsDefault: true + }, + + /* + |-------------------------------------------------------------------------- + | MySQL + |-------------------------------------------------------------------------- + | + | Here we define connection settings for MySQL database. + | + | npm i --save mysql + | + */ + mysql: { + client: 'mysql', + connection: { + host: Env.get('DB_HOST', 'localhost'), + port: Env.get('DB_PORT', ''), + user: Env.get('DB_USER', 'root'), + password: Env.get('DB_PASSWORD', ''), + database: Env.get('DB_DATABASE', 'adonis') + } + }, + + /* + |-------------------------------------------------------------------------- + | PostgreSQL + |-------------------------------------------------------------------------- + | + | Here we define connection settings for PostgreSQL database. + | + | npm i --save pg + | + */ + pg: { + client: 'pg', + connection: { + host: Env.get('DB_HOST', 'localhost'), + port: Env.get('DB_PORT', ''), + user: Env.get('DB_USER', 'root'), + password: Env.get('DB_PASSWORD', ''), + database: Env.get('DB_DATABASE', 'adonis') + } + } +} diff --git a/package.json b/package.json index 1eeb35bc..d89be058 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@adonisjs/lucid", - "version": "4.0.4", + "version": "4.0.5", "description": "SQL ORM built on top of Active Record pattern", "main": "index.js", "scripts": { diff --git a/src/Lucid/Hooks/index.js b/src/Lucid/Hooks/index.js index da8a843d..069ace07 100644 --- a/src/Lucid/Hooks/index.js +++ b/src/Lucid/Hooks/index.js @@ -37,6 +37,14 @@ class Hooks { update: 'save' } + /** + * The events array of aliases, just required + * for validation purposes + * + * @type {Array} + */ + this._aliasEvents = _.values(this._aliases) + /** * A map of handlers to be called for each event * @@ -65,7 +73,7 @@ class Hooks { * ``` */ addHandler (event, handler, name) { - if (!this._events[event]) { + if (_.includes(this._events[event]) && !_.includes(this._aliasEvents, event)) { throw CE.InvalidArgumentException.invalidParameter(`${event} is not a valid hook event`) } this._handlers[event] = this._handlers[event] || [] diff --git a/test/functional/migration-make.spec.js b/test/functional/config-make.spec.js similarity index 63% rename from test/functional/migration-make.spec.js rename to test/functional/config-make.spec.js index ab07cb39..ce181f76 100644 --- a/test/functional/migration-make.spec.js +++ b/test/functional/config-make.spec.js @@ -15,9 +15,9 @@ const fs = require('fs-extra') const path = require('path') const { ioc } = require('@adonisjs/fold') const { setupResolver, Helpers } = require('@adonisjs/sink') -const MigrationMake = require('../../commands/MigrationMake') +const MakeConfig = require('../../commands/MakeConfig') -test.group('Migration Make', (group) => { +test.group('Make Config', (group) => { group.before(async () => { ioc.bind('Adonis/Src/Helpers', () => { return new Helpers(path.join(__dirname)) @@ -27,7 +27,7 @@ test.group('Migration Make', (group) => { group.after(async () => { try { - await fs.remove(path.join(__dirname, 'database')) + await fs.remove(path.join(__dirname, 'config')) } catch (error) { if (process.platform !== 'win32' || error.code !== 'EBUSY') { throw error @@ -35,10 +35,16 @@ test.group('Migration Make', (group) => { } }).timeout(0) - test('create migration file', async (assert) => { - ace.addCommand(MigrationMake) - const result = await ace.call('make:migration', { name: 'Users' }, { action: 'create' }) + test('create config file', async (assert) => { + ace.addCommand(MakeConfig) + const result = await ace.call('config:database') const exists = await fs.pathExists(result) assert.isTrue(exists) }) + + test('echo config file to console', async (assert) => { + ace.addCommand(MakeConfig) + const result = await ace.call('config:database', {}, { echo: true }) + assert.equal(result, 'echoed') + }) })