Skip to content

Commit

Permalink
Merge branch 'release/4.0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 30, 2017
2 parents 2322b8f + 5a8b6c8 commit 86a867d
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 8 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,18 @@
<a name="4.0.5"></a>
## [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))



<a name="4.0.4"></a>
## [4.0.4](https://github.com/adonisjs/adonis-lucid/compare/v4.0.3...v4.0.4) (2017-07-30)

Expand Down
82 changes: 82 additions & 0 deletions commands/MakeConfig.js
@@ -0,0 +1,82 @@
'use strict'

/*
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* 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
78 changes: 78 additions & 0 deletions 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')
}
}
}
2 changes: 1 addition & 1 deletion 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": {
Expand Down
10 changes: 9 additions & 1 deletion src/Lucid/Hooks/index.js
Expand Up @@ -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
*
Expand Down Expand Up @@ -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] || []
Expand Down
Expand Up @@ -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))
Expand All @@ -27,18 +27,24 @@ 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
}
}
}).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')
})
})

0 comments on commit 86a867d

Please sign in to comment.