Class SequelizeConnector

Description

Sequelize ORM connector

Features:

  • connector manager for sequelize

Example

import { join } from 'path';
import { tmpdir } from 'os';
import { container, Core, inject, Types } from '@biorate/inversion';
import { Config, IConfig } from '@biorate/config';
import { ISequelizeConnector, SequelizeConnector as BaseSequelizeConnector } from '@biorate/sequelize';
import { Table, Column, Model, DataType } from '@biorate/sequelize';

const connectionName = 'db';

// Create model
@Table({
tableName: 'test',
timestamps: false,
})
export class TestModel extends Model {
@Column({ type: DataType.CHAR, primaryKey: true })
key: string;

@Column(DataType.INTEGER)
value: number;
}

// Assign models with sequelize connector
class SequelizeConnector extends BaseSequelizeConnector {
protected readonly models = { [connectionName]: [TestModel] };
}

// Create Root class
export class Root extends Core() {
@inject(SequelizeConnector) public connector: ISequelizeConnector;
}

// Bind dependencies
container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
container.bind<ISequelizeConnector>(SequelizeConnector).toSelf().inSingletonScope();
container.bind<Root>(Root).toSelf().inSingletonScope();

// Merge config
container.get<IConfig>(Types.Config).merge({
Sequelize: [
{
name: connectionName,
options: {
logging: false,
dialect: 'sqlite',
storage: join(tmpdir(), 'sqlite-test.db'),
},
},
],
});

// Example
(async () => {
await container.get<Root>(Root).$run();
// Drop table if exists
await TestModel.drop();
// Create table
await TestModel.sync();
// Create model item
await TestModel.create({ key: 'test', value: 1 });
// Create find model item by key
const data = await TestModel.findOne({ where: { key: 'test' } });
console.log(data.toJSON()); // { key: 'test', value: 1 }
})();

Hierarchy

Constructors

Properties

#connections: Map<string, Sequelize>

Description

Private connections storage

#connections: Map<string, Sequelize> = ...

Description

Private connections storage

#current: Sequelize

Description

Private link to selected (used) connection

#current: Sequelize = undefined

Description

Private link to selected (used) connection

config: IConfig

Description

Config dependency

models: {
    [key: string]: IModels;
} = {}

Type declaration

Description

Models list, key - connection name, value - array of models

namespace: "Sequelize" = 'Sequelize'

Description

Namespace path for fetching configuration

Accessors

Methods

  • Type Parameters

    • T = unknown

    Parameters

    • sql: string | {
          query: string;
          values: unknown[];
      }
    • options: Object = {}

    Returns Promise<T[]>

    Description

    Execute query on current connection

Generated using TypeDoc