Skip to content

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

The database object is exposed :

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 :
  • Oauth (Model) : The model for the Oauth database table
  • Session (Model) : The model for the Session database table

Extending the aido database

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

Clone this wiki locally