-
Notifications
You must be signed in to change notification settings - Fork 1
Technical : Database
Damien BUTY edited this page May 20, 2020
·
2 revisions
Under the hood, aido uses Objection.js as its ORM, which itself uses knex as a query builder and SQL client.
You can use the persistentStorage init configuration option to specify either the path to an SQLite database, or the connection to a MySQL or PostgreSQL server. Please refer to the knex documentation for the exact configuration options to use there.
The database object is exposed :
- on the aido object as
aido.koaApp.context.database - on the Slash class as
this.database - as an argument when calling the plugin methods
extendDb,initPluginandgetHelpers
It contains the following :
- knex (Object) : The knex instance, connected to your database
- Model (Class) : The Objection Model class
-
createTable (function) : A convenience function to create a table in the database if it doesn't already exist. It takes 2 arguments :
- tableName (String) : The name of the table
- callback (function) : A knex-style schemaBuilder callback
- Oauth (Model) : The model for the Oauth database table
- Session (Model) : The model for the Session database table
While aido uses Objection/knex internally, you are free to use any ORM and database system you want in your application. You can also choose to extend the aido database with new tables related to your application.
In this case we recommend the following methodology :
// Create an Objection Model
// (https://vincit.github.io/objection.js/guide/models.html#examples)
class Test extends database.Model {
// ...
}
// Create the table in the database using the createTable helper
await database.createTable('test', (table) => {
// http://knexjs.org/#Schema
table.increments('id').primary()
})
// Bind the knex instance to your Model (this is necessary so that Objection knows what database driver to use for the model)
Test.knex(database.knex)
// Expose the new model on the database object
database.Test = Test
// You can now use this model in your Slash class
class SomeSlash extends Slash {
async someAction() {
await this.database.Test.findById(1)
}
}