Skip to content

Instantly share code, notes, and snippets.

@thetutlage
Created July 10, 2018 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thetutlage/007bc339f59b8db4f3fa964853d12646 to your computer and use it in GitHub Desktop.
Save thetutlage/007bc339f59b8db4f3fa964853d12646 to your computer and use it in GitHub Desktop.
Adonis Lucid Setup without Adonis framework
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)
},
}
@mehrancodes
Copy link

@thetutlage Could you please provide an updated setup according to the adonis-lucid package?
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment