@biorate/mongodb 0.28.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/.nyc_output/66b86eb6-d677-4a13-a00c-6eaf7979fdfa.json +1 -0
- package/.nyc_output/processinfo/66b86eb6-d677-4a13-a00c-6eaf7979fdfa.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 +106 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +131 -0
- package/coverage/lcov-report/index.ts.html +541 -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 +61 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +7 -7
- package/dist/src/errors.js +0 -11
- package/dist/src/errors.js.map +0 -1
- package/dist/src/index.js +0 -83
- 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 -152
- package/src/interfaces.ts +0 -11
- package/tests/__mocks__/index.ts +0 -36
- package/tests/__mocks__/models/index.ts +0 -1
package/src/index.ts
DELETED
@@ -1,152 +0,0 @@
|
|
1
|
-
import { init, injectable } from '@biorate/inversion';
|
2
|
-
import { events } from '@biorate/tools';
|
3
|
-
import { Connector } from '@biorate/connector';
|
4
|
-
import { createConnection } from 'mongoose';
|
5
|
-
import { getModelForClass } from '@typegoose/typegoose';
|
6
|
-
import { MongoDBCantConnectError } from './errors';
|
7
|
-
import { IMongoDBConfig, IMongoDBConnection } from './interfaces';
|
8
|
-
|
9
|
-
export * from './errors';
|
10
|
-
export * from './interfaces';
|
11
|
-
|
12
|
-
/**
|
13
|
-
* @description Mongodb ORM connector based on mongoose and typegoose
|
14
|
-
*
|
15
|
-
* ### Features:
|
16
|
-
* - connector manager for mongodb
|
17
|
-
*
|
18
|
-
* @example
|
19
|
-
* ```
|
20
|
-
* import { inject, container, Types, Core } from '@biorate/inversion';
|
21
|
-
* import { IConfig, Config } from '@biorate/config';
|
22
|
-
* import {
|
23
|
-
* Severity,
|
24
|
-
* modelOptions,
|
25
|
-
* Prop,
|
26
|
-
* MongoDBConnector,
|
27
|
-
* IMongoDBConnector,
|
28
|
-
* model,
|
29
|
-
* ReturnModelType,
|
30
|
-
* } from '@biorate/mongodb';
|
31
|
-
*
|
32
|
-
* // Define models
|
33
|
-
* @modelOptions({
|
34
|
-
* options: {
|
35
|
-
* allowMixed: Severity.ALLOW,
|
36
|
-
* },
|
37
|
-
* schemaOptions: { collection: 'test', versionKey: false },
|
38
|
-
* })
|
39
|
-
* export class TestModel {
|
40
|
-
* @Prop()
|
41
|
-
* firstName: string;
|
42
|
-
*
|
43
|
-
* @Prop()
|
44
|
-
* lastName: string;
|
45
|
-
*
|
46
|
-
* @Prop()
|
47
|
-
* age: number;
|
48
|
-
* }
|
49
|
-
*
|
50
|
-
* // Define root
|
51
|
-
* export class Root extends Core() {
|
52
|
-
* @inject(MongoDBConnector) public connector: IMongoDBConnector;
|
53
|
-
* @model(TestModel) public test: ReturnModelType<typeof TestModel>;
|
54
|
-
* }
|
55
|
-
*
|
56
|
-
* // Bind dependencies
|
57
|
-
* container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
|
58
|
-
* container.bind<IMongoDBConnector>(MongoDBConnector).toSelf().inSingletonScope();
|
59
|
-
* container.bind<Root>(Root).toSelf().inSingletonScope();
|
60
|
-
*
|
61
|
-
* // Configure
|
62
|
-
* container.get<IConfig>(Types.Config).merge({
|
63
|
-
* MongoDB: [
|
64
|
-
* {
|
65
|
-
* name: 'connection',
|
66
|
-
* host: 'mongodb://localhost:27017/',
|
67
|
-
* options: {
|
68
|
-
* useNewUrlParser: true,
|
69
|
-
* useUnifiedTopology: true,
|
70
|
-
* dbName: 'test',
|
71
|
-
* },
|
72
|
-
* },
|
73
|
-
* ],
|
74
|
-
* });
|
75
|
-
*
|
76
|
-
* (async () => {
|
77
|
-
* const root = container.get<Root>(Root);
|
78
|
-
* await root.$run();
|
79
|
-
* await root.connector.connection().dropDatabase();
|
80
|
-
*
|
81
|
-
* const connection = root.connector.connection('connection'); // Get connection instance
|
82
|
-
* console.log(connection);
|
83
|
-
*
|
84
|
-
* await new root.test({
|
85
|
-
* firstName: 'Vasya',
|
86
|
-
* lastName: 'Pupkin',
|
87
|
-
* age: 36,
|
88
|
-
* }).save(); // insert data into test collection
|
89
|
-
*
|
90
|
-
* // Get data from database
|
91
|
-
* const data = await root.test.find({ firstName: 'Vasya' }, { _id: 0 });
|
92
|
-
* console.log(data); // {
|
93
|
-
* // firstName: 'Vasya',
|
94
|
-
* // lastName: 'Pupkin',
|
95
|
-
* // age: 36,
|
96
|
-
* // }
|
97
|
-
* })();
|
98
|
-
* ```
|
99
|
-
*/
|
100
|
-
@injectable()
|
101
|
-
export class MongoDBConnector extends Connector<IMongoDBConfig, IMongoDBConnection> {
|
102
|
-
/**
|
103
|
-
* @description Namespace path for fetching configuration
|
104
|
-
*/
|
105
|
-
protected readonly namespace = 'MongoDB';
|
106
|
-
/**
|
107
|
-
* @description Create connection
|
108
|
-
*/
|
109
|
-
protected async connect(config: IMongoDBConfig) {
|
110
|
-
let connection: IMongoDBConnection;
|
111
|
-
try {
|
112
|
-
connection = createConnection(config.host, config.options);
|
113
|
-
await events.once(connection, 'open');
|
114
|
-
} catch (e) {
|
115
|
-
throw new MongoDBCantConnectError(e);
|
116
|
-
}
|
117
|
-
return connection;
|
118
|
-
}
|
119
|
-
/**
|
120
|
-
* @description Initialize method
|
121
|
-
*/
|
122
|
-
@init() protected async initialize() {
|
123
|
-
connections = this.connections;
|
124
|
-
await super.initialize();
|
125
|
-
}
|
126
|
-
}
|
127
|
-
/**
|
128
|
-
* @description Private connections link
|
129
|
-
*/
|
130
|
-
let connections: Map<string, IMongoDBConnection> = null;
|
131
|
-
/**
|
132
|
-
* @description Model injection decorator
|
133
|
-
*/
|
134
|
-
export const model = <T = unknown>(
|
135
|
-
Model: new (...args: any) => T,
|
136
|
-
connection?: string,
|
137
|
-
options: Record<string, unknown> = {},
|
138
|
-
) => {
|
139
|
-
return (proto?: any, key?: string) => {
|
140
|
-
Object.defineProperty(proto, key, {
|
141
|
-
get() {
|
142
|
-
return getModelForClass(Model, {
|
143
|
-
existingConnection: connection
|
144
|
-
? connections.get(connection)
|
145
|
-
: [...connections][0][1],
|
146
|
-
options,
|
147
|
-
});
|
148
|
-
},
|
149
|
-
configurable: false,
|
150
|
-
});
|
151
|
-
};
|
152
|
-
};
|
package/src/interfaces.ts
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
import { IConnectorConfig, IConnector } from '@biorate/connector';
|
2
|
-
import { ConnectOptions, Connection } from 'mongoose';
|
3
|
-
|
4
|
-
export type IMongoDBConnection = Connection;
|
5
|
-
|
6
|
-
export interface IMongoDBConfig extends IConnectorConfig {
|
7
|
-
host: string;
|
8
|
-
options: ConnectOptions;
|
9
|
-
}
|
10
|
-
|
11
|
-
export type IMongoDBConnector = IConnector<IMongoDBConfig, IMongoDBConnection>;
|
package/tests/__mocks__/index.ts
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
import { use } from 'chai';
|
2
|
-
import { jestSnapshotPlugin } from 'mocha-chai-jest-snapshot';
|
3
|
-
import { inject, container, Types, Core } from '@biorate/inversion';
|
4
|
-
import { IConfig, Config } from '@biorate/config';
|
5
|
-
import { MongoDBConnector, IMongoDBConnector, model } from '../../src';
|
6
|
-
import { ReturnModelType } from '@typegoose/typegoose';
|
7
|
-
import { TestModel } from './models';
|
8
|
-
|
9
|
-
export * from './models';
|
10
|
-
|
11
|
-
use(jestSnapshotPlugin());
|
12
|
-
|
13
|
-
export const dbName = 'test';
|
14
|
-
|
15
|
-
export class Root extends Core() {
|
16
|
-
@inject(MongoDBConnector) public connector: IMongoDBConnector;
|
17
|
-
@model(TestModel) public test: ReturnModelType<typeof TestModel>;
|
18
|
-
}
|
19
|
-
|
20
|
-
container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
|
21
|
-
container.bind<IMongoDBConnector>(MongoDBConnector).toSelf().inSingletonScope();
|
22
|
-
container.bind<Root>(Root).toSelf().inSingletonScope();
|
23
|
-
|
24
|
-
container.get<IConfig>(Types.Config).merge({
|
25
|
-
MongoDB: [
|
26
|
-
{
|
27
|
-
name: 'connection',
|
28
|
-
host: 'mongodb://localhost:27017/',
|
29
|
-
options: {
|
30
|
-
useNewUrlParser: true,
|
31
|
-
useUnifiedTopology: true,
|
32
|
-
dbName,
|
33
|
-
},
|
34
|
-
},
|
35
|
-
],
|
36
|
-
});
|
@@ -1 +0,0 @@
|
|
1
|
-
export * from './test';
|