-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
2,089 additions
and
401 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.