@biorate/sequelize 0.28.3 → 0.29.1
Sign up to get free protection for your applications and to get access to all the features.
- package/.nyc_output/026195fc-3d49-46e3-9e17-cae0ea21d742.json +1 -0
- package/.nyc_output/processinfo/026195fc-3d49-46e3-9e17-cae0ea21d742.json +1 -0
- package/.nyc_output/processinfo/index.json +1 -0
- package/CHANGELOG.md +19 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/errors.ts.html +124 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +131 -0
- package/coverage/lcov-report/index.ts.html +475 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov.info +66 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +6 -6
- package/dist/src/errors.js +0 -11
- package/dist/src/errors.js.map +0 -1
- package/dist/src/index.js +0 -63
- package/dist/src/index.js.map +0 -1
- package/dist/src/interfaces.js +0 -3
- package/dist/src/interfaces.js.map +0 -1
- package/index.ts +0 -1
- package/src/errors.ts +0 -7
- package/src/index.ts +0 -129
- package/src/interfaces.ts +0 -21
- package/tests/__mocks__/index.ts +0 -42
- package/tests/__mocks__/models/index.ts +0 -1
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
|
-
};
|
package/tests/__mocks__/index.ts
DELETED
@@ -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';
|