Skip to main content

Define Connection

Before define models, you should create a Connection to the database.

import * as cormo from 'cormo';

const connection = new cormo.MySQLConnection({ database: 'test' });

You can see allowed settings for each database system at MySQLConnectionSettings, MongoDBConnectionSettings, SQLite3ConnectionSettings and PostgreSQLConnectionSettings.

You must install a driver(mysql2 or mysql for MySQL. mongodb for MongoDB. sqlite3 for SQLite3. pg for PostgreSQL.) for the database system. Otherwise you will see an error like Install mysql module to use this adapter and process will be terminated.

If you want to select different database system at runtime according to the environment, use Connection class directly.

let Config;
if (process.env.NODE_ENV === 'test') {
Config = {
database_type: 'sqlite3',
database_settings: {
database: __dirname + '/test.sqlite3',
},
};
} else {
Config = {
database_type: 'mysql',
database_settings: {
host: 'db.example.com',
database: 'cormo',
},
};
}
const connection = new cormo.Connection(Config.database_type, Config.database_settings);

If you define models, they will be connected the last Connection instance automatically. If you don't want this behavior (e.g use multiple connections), set is_default to false.

const mysql = new cormo.MySQLConnection({ database: 'test', is_default: false });
const mongodb = new cormo.MongoDBConnection({ database: 'test', is_default: false });

@cormo.Model({ connection: mongodb })
class User extends cormo.BaseModel {
@cormo.Column(String)
public name?: string | null;

@cormo.Column(Number)
public age?: number | null;
}

@cormo.Model({ connection: mysql })
class Post extends cormo.BaseModel {
@cormo.Column(String)
public title?: string | null;

@cormo.Column(String)
public body?: string | null;
}

The Connection instance will connect the database server automatically without any method call.