Skip to content

Commit

Permalink
Merge branch 'release/4.0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 1, 2017
2 parents 86a867d + 44f60f1 commit c0f3cd0
Show file tree
Hide file tree
Showing 11 changed files with 2,089 additions and 401 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
<a name="4.0.6"></a>
## [4.0.6](https://github.com/adonisjs/adonis-lucid/compare/v4.0.5...v4.0.6) (2017-08-01)


### Features

* **commands:** add seed command ([dd56409](https://github.com/adonisjs/adonis-lucid/commit/dd56409))
* **instructions:** add instructions file ([a1086d9](https://github.com/adonisjs/adonis-lucid/commit/a1086d9))


### Reverts

* **commands:** remove config:database command ([0671c56](https://github.com/adonisjs/adonis-lucid/commit/0671c56))



<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)

Expand Down
82 changes: 0 additions & 82 deletions commands/MakeConfig.js

This file was deleted.

136 changes: 136 additions & 0 deletions commands/Seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
'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 { Command } = require('@adonisjs/ace')
const requireAll = require('require-all')
const _ = require('lodash')
const { ioc } = require('@adonisjs/fold')

class SeedDatabase extends Command {
constructor (Helpers) {
super()
this._seedsPath = Helpers.seedsPath()
}

/**
* IoC container injections
*
* @method inject
*
* @return {Array}
*/
static get inject () {
return ['Adonis/Src/Helpers']
}

/**
* Returns an object of all schema files
*
* @method _getSeedFiles
*
* @return {Object}
*
* @private
*/
_getSeedFiles (selectedFiles) {
return requireAll({
dirname: this._seedsPath,
filters: /(.*)\.js$/,
filter: (fileName) => {
if (!selectedFiles) {
return fileName
}
return _.find(selectedFiles, (file) => file.trim().endsWith(fileName))
}
})
}

/**
* Throws exception when trying to run migrations are
* executed in production and not using force flag.
*
* @method _validateState
*
* @param {Boolean} force
*
* @return {void}
*
* @private
*
* @throws {Error} If NODE_ENV is production
*/
_validateState (force) {
if (process.env.NODE_ENV === 'production' && !force) {
throw new Error('Cannot run migrations in production. Use --force flag to continue')
}
}

/**
* Command signature required by ace
*
* @method signature
*
* @return {String}
*/
static get signature () {
return `
seed
{ -f, --force: Forcefully seed database in production }
{ --files=@value: Run only selected files }
`
}

/**
* Command description
*
* @method description
*
* @return {String}
*/
static get description () {
return 'Seed database using seed files'
}

/**
* Method called when command is executed. This method will
* require all files from the migrations directory
* and execute all pending schema files
*
* @method handle
*
* @param {Object} args
* @param {Boolean} options.force
* @param {String} options.files
*
* @return {void|Array}
*/
async handle (args, { force, files }) {
this._validateState(force)

files = typeof (files) === 'string' ? files.split(',') : null
const allFiles = this._getSeedFiles(files)

if (!_.size(allFiles)) {
return this.viaAce ? this.info('Nothing to seed') : 'Nothing to seed'
}

for (const file of _.keys(allFiles)) {
const seedInstance = ioc.make(allFiles[file])
if (typeof (seedInstance.run) === 'function') {
await seedInstance.run()
} else {
this.warn(`${seedInstance.constructor.name} does not have a run method`)
}
}
}
}

module.exports = SeedDatabase
20 changes: 20 additions & 0 deletions instructions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'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')

module.exports = async function (cli) {
try {
await cli.makeConfig('database.js', path.join(__dirname, './templates/config.mustache'))
} catch (error) {
// ignore errors
}
}
35 changes: 35 additions & 0 deletions instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Registering provider

Make sure to register the lucid provider to make use of `Database` and `Lucid` models. The providers are registered inside `start/app.js`

```js
const providers = [
'@adonisjs/lucid/providers/LucidProvider'
]
```


## Usage

Once done you can access `Database` provider and run mysql queries as follows.

```js
const Database = use('Database')

await Database.table('users').select('*')
await Database.table('users').paginate()
```

## Migrations Provider

This repo also comes with a migrations and seeds provider to run to migrate your database using incremental migrations.

Make sure to register migrations provider under `aceProviders` array.

```js
const aceProviders = [
'@adonisjs/lucid/providers/MigrationsProvider'
]
```

After this running `adonis --help` will list a set of commands under `migration` namespace.
Loading

0 comments on commit c0f3cd0

Please sign in to comment.