@vinka/repo
v3.2.1
Published
Database and repo utilities
Downloads
2
Readme
Repo
Database and repo utilities
Usage example - SequelizeConnector
import SequelizeConnector from '@vinka/repo';
import { DbConfig, ModelMap, SequelizeConstructor } from '@vinka/repo/dist/types'
import { Pool } from 'pg';
import { Sequelize } from 'sequelize';
import Umzug = require('umzug');
// Set configuration used in
const dbConfig: DbConfig = {
user: 'username',
pass: 'password,
db: 'database which sequelize connects to',
ssl: false,
masterDb: 'name of database which can dreate the database to connect to',
options: {
logging: false,
dialect: 'postgres',
host: '127.0.0.1',
port: 5432,
pool: {
max: 8,
idle: 30,
},
},
};
// Initialize connector, only sets values to private fields
const connector = new SequelizeConnector(
Sequelize as unknown as SequelizeConstructor,
Pool,
dbConfig,
{ info: console.log, error: console.error, debug: console.log },
);
// Import module generated by pg-generator, includes init(Sequelize) method.
const models = require('/path/to/models') as ModelMap;
// Create Sequelize client, set to variable if using it for querying etc.
const orm = await connector.connect(models);
// Perform migrations
await connector.migrateDown(Umzug);
await connector.migrateUp(Umzug);