@vinka/repo
Version:
Database and repo utilities
79 lines • 3.82 kB
TypeScript
import { Pool } from 'pg';
import { Sequelize } from 'sequelize';
import { Migration } from 'umzug';
import { DbConfig, Logger, ModelMap, PoolConstructor, SequelizeConstructor, UmzugConstructor } from './types';
export default class SequelizeConnector {
sequelizeClient?: Sequelize;
private readonly config;
private readonly logger;
private readonly PoolConstructor;
private readonly SequelizeConstructor;
/**
* Constructs a SequelizeConnector. Provides convenience for creating Sequelize clients and performing migrations.
* Use createClient to create a Sequelize client. After that you can use this connector's migration methods to
* perform migrations on a database.
*
* @param sequelizeConstructor a Sequelize constructor function.
* @param poolConstructor a Postgres Pool constructor function.
* @param config the database configuration adapted for Sequelize and Postgres.
* @param logger a Logger object.
*/
constructor(sequelizeConstructor: SequelizeConstructor, poolConstructor: PoolConstructor, config: DbConfig, logger: Logger);
/**
* Creates a Sequelize client connected to database specified in config,
* creates the database if it does not exist, and sets the created client to
* an internal member variable.
*
* @param [modelMap] the models to associate with the client. If not
* provided, the caller needs to initialize models separately.
*/
connect(modelMap?: ModelMap): Promise<Sequelize>;
/**
* Creates a new database with name from config.db. Uses database with name
* from config.masterDb to create the new database. The master database must
* already exist.
*
* @param config the configuration for the postgres pool used internally.
*/
createDatabase(config: DbConfig): Promise<void>;
/**
* Creates postgres connection pool, which can be used to connect to a database.
* @param config the configuration for the pool.
* @param dbName the name of the database the pool is connected to. Default is taken from config.db.
*/
createPgPool(config: DbConfig, dbName?: string): Pool;
/**
* Creates a Sequelize client and associates user defined models with the
* client.
*
* @param config the configuration for the client.
* @param [modelMap] the models to associate with the client. If not
* provided, the caller needs to initialize models separately.
*/
createSequelizeClient(config: DbConfig, modelMap?: ModelMap): Sequelize;
/**
* Migrates a database down with Umzug.
* @param umzugConstructor the constructor for Umzug.
* @param to 0 (default) to revert all migrations, or the name of the migration to migrate down to.
* @param client the sequelize client used for the migration. Optional if createClient has been called.
*/
migrateDown(umzugConstructor: UmzugConstructor, to?: 0 | string, client?: Sequelize | undefined): Promise<Migration[]>;
/**
* Migrates a database up with Umzug.
* @param umzugConstructor the constructor for Umzug.
* @param client the sequelize client used for the migration. Optional if createClient has been called.
*/
migrateUp(umzugConstructor: UmzugConstructor, client?: Sequelize | undefined): Promise<Migration[]>;
/**
* Tests that a client can be created from a Pool with config or throws.
* @param config the config to create the pool with. The pool provides the client.
*/
testDbExistsOrThrow(config: DbConfig): Promise<void>;
/**
* Creates an Umzug instance.
* @param umzugConstructor the Umzug constructor.
* @param client the sequelize client used in the Umzug.
*/
private createUmzug;
}
//# sourceMappingURL=index.d.ts.map