@biorate/sequelize 0.27.3 → 0.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts DELETED
@@ -1,129 +0,0 @@
1
- import { injectable } from '@biorate/inversion';
2
- import { Connector } from '@biorate/connector';
3
- import { Sequelize } from 'sequelize-typescript';
4
- import { QueryTypes, QueryOptions, QueryOptionsWithType } from 'sequelize';
5
- import { omit } from 'lodash';
6
- import { SequelizeCantConnectError } from './errors';
7
- import { ISequelizeConfig, ISequelizeConnection, IModels } from './interfaces';
8
-
9
- export * from './errors';
10
- export * from './interfaces';
11
- export * from 'sequelize-typescript';
12
-
13
- /**
14
- * @description Sequelize ORM connector
15
- *
16
- * ### Features:
17
- * - connector manager for sequelize
18
- *
19
- * @example
20
- * ```
21
- * import { join } from 'path';
22
- * import { tmpdir } from 'os';
23
- * import { container, Core, inject, Types } from '@biorate/inversion';
24
- * import { Config, IConfig } from '@biorate/config';
25
- * import { ISequelizeConnector, SequelizeConnector as BaseSequelizeConnector } from '@biorate/sequelize';
26
- * import { Table, Column, Model, DataType } from '@biorate/sequelize';
27
- *
28
- * const connectionName = 'db';
29
- *
30
- * // Create model
31
- * @Table({
32
- * tableName: 'test',
33
- * timestamps: false,
34
- * })
35
- * export class TestModel extends Model {
36
- * @Column({ type: DataType.CHAR, primaryKey: true })
37
- * key: string;
38
- *
39
- * @Column(DataType.INTEGER)
40
- * value: number;
41
- * }
42
- *
43
- * // Assign models with sequelize connector
44
- * class SequelizeConnector extends BaseSequelizeConnector {
45
- * protected readonly models = { [connectionName]: [TestModel] };
46
- * }
47
- *
48
- * // Create Root class
49
- * export class Root extends Core() {
50
- * @inject(SequelizeConnector) public connector: ISequelizeConnector;
51
- * }
52
- *
53
- * // Bind dependencies
54
- * container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
55
- * container.bind<ISequelizeConnector>(SequelizeConnector).toSelf().inSingletonScope();
56
- * container.bind<Root>(Root).toSelf().inSingletonScope();
57
- *
58
- * // Merge config
59
- * container.get<IConfig>(Types.Config).merge({
60
- * Sequelize: [
61
- * {
62
- * name: connectionName,
63
- * options: {
64
- * logging: false,
65
- * dialect: 'sqlite',
66
- * storage: join(tmpdir(), 'sqlite-test.db'),
67
- * },
68
- * },
69
- * ],
70
- * });
71
- *
72
- * // Example
73
- * (async () => {
74
- * await container.get<Root>(Root).$run();
75
- * // Drop table if exists
76
- * await TestModel.drop();
77
- * // Create table
78
- * await TestModel.sync();
79
- * // Create model item
80
- * await TestModel.create({ key: 'test', value: 1 });
81
- * // Create find model item by key
82
- * const data = await TestModel.findOne({ where: { key: 'test' } });
83
- * console.log(data.toJSON()); // { key: 'test', value: 1 }
84
- * })();
85
- * ```
86
- */
87
- @injectable()
88
- export class SequelizeConnector extends Connector<
89
- ISequelizeConfig,
90
- ISequelizeConnection
91
- > {
92
- /**
93
- * @description Namespace path for fetching configuration
94
- */
95
- protected readonly namespace = 'Sequelize';
96
- /**
97
- * @description Models list, key - connection name, value - array of models
98
- */
99
- protected readonly models: { [key: string]: IModels } = {};
100
- /**
101
- * @description Create connection
102
- */
103
- protected async connect(config: ISequelizeConfig) {
104
- let connection: ISequelizeConnection;
105
- try {
106
- config.options.models = this.models[config.name] ?? [];
107
- connection = new Sequelize(config.options);
108
- connection.authenticate();
109
- } catch (e) {
110
- throw new SequelizeCantConnectError(e);
111
- }
112
- return connection;
113
- }
114
- /**
115
- * @description Execute query on current connection
116
- */
117
- public async query<T = unknown>(
118
- sql: string | { query: string; values: unknown[] },
119
- options?: (QueryOptions | QueryOptionsWithType<QueryTypes.RAW>) & {
120
- connection?: string;
121
- },
122
- ): Promise<T[]> {
123
- const name = options?.connection;
124
- omit(options, 'connection');
125
- const connection = this.connection(name);
126
- const result = await connection.query(sql, options);
127
- return result[0] as T[];
128
- }
129
- }
package/src/interfaces.ts DELETED
@@ -1,21 +0,0 @@
1
- import { IConnectorConfig, IConnector } from '@biorate/connector';
2
- import { Sequelize, ModelCtor } from 'sequelize-typescript';
3
- import { Options, QueryOptions, QueryTypes, QueryOptionsWithType } from 'sequelize';
4
-
5
- export type IModels = string[] | ModelCtor[];
6
-
7
- export type ISequelizeConnection = Sequelize;
8
-
9
- export interface ISequelizeConfig extends IConnectorConfig {
10
- host: string;
11
- options: Options & { models?: IModels };
12
- }
13
-
14
- export type ISequelizeConnector = IConnector<ISequelizeConfig, ISequelizeConnection> & {
15
- query<T = unknown>(
16
- sql: string | { query: string; values: unknown[] },
17
- options?: (QueryOptions | QueryOptionsWithType<QueryTypes.RAW>) & {
18
- connection?: string;
19
- },
20
- ): Promise<T[]>;
21
- };
@@ -1,42 +0,0 @@
1
- import { tmpdir } from 'os';
2
- import { join } from 'path';
3
- import { use } from 'chai';
4
- import { jestSnapshotPlugin } from 'mocha-chai-jest-snapshot';
5
- import { inject, container, Types, Core } from '@biorate/inversion';
6
- import { IConfig, Config } from '@biorate/config';
7
- import {
8
- SequelizeConnector as BaseSequelizeConnector,
9
- ISequelizeConnector,
10
- } from '../../src';
11
- import { TestModel } from './models';
12
-
13
- export * from './models';
14
-
15
- use(jestSnapshotPlugin());
16
-
17
- export const name = 'connection';
18
-
19
- class SequelizeConnector extends BaseSequelizeConnector {
20
- protected readonly models = { [name]: [TestModel] };
21
- }
22
-
23
- export class Root extends Core() {
24
- @inject(SequelizeConnector) public connector: ISequelizeConnector;
25
- }
26
-
27
- container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
28
- container.bind<ISequelizeConnector>(SequelizeConnector).toSelf().inSingletonScope();
29
- container.bind<Root>(Root).toSelf().inSingletonScope();
30
-
31
- container.get<IConfig>(Types.Config).merge({
32
- Sequelize: [
33
- {
34
- name,
35
- options: {
36
- logging: false,
37
- dialect: 'sqlite',
38
- storage: join(tmpdir(), 'sqlite-test.db'),
39
- },
40
- },
41
- ],
42
- });
@@ -1 +0,0 @@
1
- export * from './test';