Overview
DataSources

DataSources are responsible for handling the creation of database connection like MySQL, PostgreSQL, Oracle


Create a DataSource Config

PostgresSQL

export const PostgresDatabaseConfiguration = {
    client: DatabaseClient.KNEX,
    config: <CommonDatabaseConfig>{
        host: process.env.POSTGRES_HOST || 'localhost',
        port: process.env.POSTGRES_PORT || 5432,
        user: process.env.POSTGRES_USER || 'root',
        password: process.env.POSTGRES_PASSWORD || 'root@123',
        database: process.env.POSTGRES_DATABASE || 'test',
        pooling: {min: 1, max: 10},
        driver: DatabaseDriver.POSTGRES,
        cluster: {
            secondaries: [
                {
                    host: localhost,
                    port: 5433
                }
            ]
        }
    }
};

MySQL & MariaDB

export const MySQLDatabaseConfiguration = {
    client: DatabaseClient.KNEX,
    config: <CommonDatabaseConfig>{
        host: process.env.MYSQL_HOST || 'localhost',
        port: process.env.MYSQL_PORT || 3306,
        user: process.env.MYSQL_USER || 'dev',
        password: process.env.MYSQL_PASSWORD || 'dev@123',
        database: process.env.MYSQL_DATABASE || 'dev',
        pooling: {min: 1, max: 10},
        driver: DatabaseDriver.MYSQL,
        cluster: {
             secondaries: [
                {
                    host: localhost,
                    port: 5433
                }
            ]
        }
    }
};

Create a DataSource

Supported DataSources

To create a database we need to create a class extends the AbstractDataSourceClient

@UseConfig(PostgresDatabaseConfiguration)
export class PostgresDatabase extends AbstractDataSourceClient<KnexClient> {
    constructor() {
        super();
    }
 
    context(): DatabaseContext<any> {
        return this;
    }
}

Custom Database

@Default()
export class CassandraDatabase extends AbstractDatabaseClient<Any> {
    init(): DatabaseConnector<Any> {
        return {
            connect: () => {
                //database connection behavior
            },
            get: () => {
                //datbase instance
            }
        };
    }
}
 

Multi-Tenancy

@UseConfig(PostgresDatabaseConfiguration)
export class PostgresDatabase extends AbstractTenancyDataSourceClient<KnexClient> {
    constructor() {
        super();
    }
}

Multi-Primary DataSource

export const PostgresDatabaseConfiguration = {
    client: DatabaseClient.KNEX,
    config: <CommonDatabaseConfig>{
        host: process.env.POSTGRES_HOST || 'localhost',
        port: process.env.POSTGRES_PORT || 5432,
        user: process.env.POSTGRES_USER || 'root',
        password: process.env.POSTGRES_PASSWORD || 'root@123',
        database: process.env.POSTGRES_DATABASE || 'test',
        pooling: {min: 1, max: 10},
        driver: DatabaseDriver.POSTGRES,
        cluster: {
            secondaries: [
                {
                    host: localhost,
                    port: 5433,
                    writable: true # default is false
                }
            ]
        }
    }
};

Set As Default

Using @Default will set the database as default for database lookup.

@UseConfig(PostgresDatabaseConfiguration)
@Default()
export class PostgresDatabase extends AbstractDataSourceClient<KnexClient> {
    constructor() {
        super();
    }
 
    context(): DatabaseContext<any> {
        return this;
    }
}

Register a DataSource

To register a datasource. We just need to import it to Root Module with @Databases

@Module({
    imports: [TodosModule],
})
@DataSources([MYSQLDatabase, PostgresDatabase])
export class AppModule {
}

Lookup a DataSource


The independent children module can use this primary store with @DatabaseLookup.

  • @DatabaseLookup find the default database.
  • @DatabaseLookup('name') will find the database with the provided name.
src/providers/todos.provider.ts

Supported Decorators

  • @UseConfig - set config for a database
  • @Default - set database as default
  • @DatabaseLookup - lookup for a database