Installation
Install the package
npm install @fca.gg/orm
The package ships with Knex and the mysql and pg drivers.
Turn on decorators
Models use TypeScript's legacy decorators. Turn them on in your tsconfig.json:
tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
Configure the database
The ORM reads the Knex configuration from a knexfile.js file at the root of the project (the directory you start the application and the CLI from):
knexfile.js
module.exports = {
client: 'mysql2', // or 'pg' for PostgreSQL
connection: {
host: 'localhost',
user: 'username',
password: 'password',
database: 'database_name',
},
pool: {min: 2, max: 10},
migrations: {
tableName: 'knex_migrations',
directory: './migrations',
},
};
Keep credentials out of the repository, for example with environment variables read in this file.
Open the connection
import {KnexInstance} from '@fca.gg/orm';
await KnexInstance.init();
init()is asynchronous: await it before the first query. A second call does nothing.KnexInstance.get()returns the Knex instance, for your own queries. It throws untilinit()has been called.KnexInstance.destroy()closes the connection, for example when the bot shuts down.
Then describe your tables with models.