-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
145 lines (143 loc) · 4.25 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { injectable } from '@biorate/inversion';
import { Connector } from '@biorate/connector';
import { Sequelize } from 'sequelize-typescript';
import { QueryTypes, QueryOptions, QueryOptionsWithType } from 'sequelize';
import { omit } from 'lodash';
import { SequelizeCantConnectError, UndefinedConnectionError } from './errors';
import { ISequelizeConfig, ISequelizeConnection, IModels } from './interfaces';
export * from './errors';
export * from './interfaces';
export * from 'sequelize-typescript';
export {
QueryTypes,
QueryOptions,
QueryOptionsWithType,
Transaction,
TransactionOptions,
} from 'sequelize';
/**
* @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 }
* })();
* ```
*/
@injectable()
export class SequelizeConnector extends Connector<
ISequelizeConfig,
ISequelizeConnection
> {
/**
* @description Private connections storage
*/
private '#connections': Map<string, ISequelizeConnection>;
/**
* @description Private link to selected (used) connection
*/
private '#current': ISequelizeConnection | undefined;
/**
* @description Namespace path for fetching configuration
*/
protected readonly namespace = 'Sequelize';
/**
* @description Models list, key - connection name, value - array of models
*/
protected readonly models: { [key: string]: IModels } = {};
/**
* @description Create connection
*/
protected async connect(config: ISequelizeConfig) {
let connection: ISequelizeConnection;
try {
config.options.models = this.models[config.name] ?? [];
connection = new Sequelize(config.options);
await connection.authenticate();
} catch (e: unknown) {
throw new SequelizeCantConnectError(<Error>e);
}
return connection;
}
/**
* @description Execute query on current connection
*/
public async query<T = unknown>(
sql: string | { query: string; values: unknown[] },
options: (QueryOptions | QueryOptionsWithType<QueryTypes.RAW>) & {
connection?: string;
} = {},
): Promise<T[]> {
const name = options?.connection;
omit(options, 'connection');
const connection = this.connection(name);
if (!connection) throw new UndefinedConnectionError(name);
const result = await connection.query(sql, options);
return result[0] as T[];
}
}