Created
July 10, 2018 09:18
-
-
Save thetutlage/007bc339f59b8db4f3fa964853d12646 to your computer and use it in GitHub Desktop.
Adonis Lucid Setup without Adonis framework
This file contains hidden or 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
const { registrar, ioc } = require('@adonisjs/fold') | |
const _ = require('lodash') | |
/** | |
* Very bare bones implementation of the config provider. | |
* If you run into issues, then make sure to check the | |
* implementation in adonis-framework repo | |
*/ | |
class Config { | |
constructor (config) { | |
this.config = config | |
} | |
get (key, defaultValue) { | |
return _.get(this.config, key, defaultValue) | |
} | |
} | |
module.exports = { | |
/** | |
* The setup process is to set the config for the database | |
* query builder and then boot the lucid provider | |
*/ | |
async setup (config) { | |
ioc.bind('Adonis/Src/Config', () => new Config({ | |
database: { | |
connection: 'default', | |
default: config | |
} | |
})) | |
await registrar.providers(['@adonisjs/lucid/providers/LucidProvider']).registerAndBoot() | |
}, | |
/** | |
* Get base model. This way you won't have to be dependent | |
* on the IoC container inside your regular app | |
*/ | |
getBaseModel () { | |
return ioc.use('Model') | |
}, | |
/** | |
* Get query builder to perform database queries. This can also be used | |
* for creating tables. `Database.schema.createTable` | |
*/ | |
getQueryBuilder () { | |
return ioc.use('Database') | |
}, | |
/** | |
* Define a model, so that you can later use it with it's name | |
* | |
* ``` | |
* const Model = getBaseModel() | |
* class User extends Model { | |
* } | |
* | |
* defineModel('name', User) | |
* ``` | |
*/ | |
defineModel (name, Model) { | |
Model._bootIfNotBooted() | |
ioc.singleton(name, () => Model) | |
}, | |
/** | |
* Get previously defined model | |
*/ | |
getModel (name) { | |
ioc.use(name) | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@thetutlage Could you please provide an updated setup according to the
adonis-lucid
package?Thanks!