1 | "use strict";
|
2 | const Mongoose = require("mongoose");
|
3 | const CoreUtils = require('../core/utils');
|
4 | const winstonLog_1 = require('../logging/winstonLog');
|
5 | const principalContext_1 = require('../security/auth/principalContext');
|
6 | const Q = require('q');
|
7 |
|
8 | exports.mainConnection = {};
|
9 | var allConnections = {};
|
10 | var connectionOptions;
|
11 | var emitters = new Array();
|
12 | function connect() {
|
13 | let dbLoc = CoreUtils.config().Config.DbConnection;
|
14 | connectionOptions = CoreUtils.config().Config.DbConnectionOptions || {};
|
15 | getConnection(dbLoc, connectionOptions);
|
16 | exports.mainConnection = allConnections[dbLoc];
|
17 | }
|
18 | exports.connect = connect;
|
19 | function getDbSpecifcModel(schemaName, schema) {
|
20 | var database = principalContext_1.PrincipalContext.get(CoreUtils.resources.userDatabase);
|
21 | if (database && allConnections[database]) {
|
22 | return allConnections[database].model(schemaName, schema);
|
23 | }
|
24 | else {
|
25 | return exports.mainConnection.model(schemaName, schema);
|
26 | }
|
27 | }
|
28 | exports.getDbSpecifcModel = getDbSpecifcModel;
|
29 | function addEmitter(msg) {
|
30 | emitters.push(msg);
|
31 | }
|
32 | exports.addEmitter = addEmitter;
|
33 | function removeEmitter(msg) {
|
34 | emitters = emitters.filter((mem) => { return mem != msg; });
|
35 | }
|
36 | exports.removeEmitter = removeEmitter;
|
37 | function updateConnection(connectionString, connectionOption) {
|
38 | principalContext_1.PrincipalContext.save(CoreUtils.resources.userDatabase, connectionString);
|
39 | return getConnection(connectionString, connectionOption);
|
40 | }
|
41 | exports.updateConnection = updateConnection;
|
42 | function getConnection(connectionString, connectionOption) {
|
43 | if (!connectionString)
|
44 | return Q.when(false);
|
45 | if (!allConnections[connectionString]) {
|
46 | var conn = Mongoose.createConnection(connectionString, defaultDomainOptions(connectionOption));
|
47 | allConnections[connectionString] = conn;
|
48 | return connectDataBase(conn, connectionString);
|
49 | }
|
50 | else {
|
51 | return Q.when(true);
|
52 | }
|
53 | }
|
54 | const emitMesseageToALL = (event, message) => {
|
55 | try {
|
56 | if (emitters && emitters.length) {
|
57 | emitters.forEach((emitter) => {
|
58 | emitter.emit(event, message);
|
59 | });
|
60 | }
|
61 | }
|
62 | catch (ex) { }
|
63 | };
|
64 | function connectDataBase(conn, connectionString) {
|
65 | let defer = Q.defer();
|
66 | conn.on('connecting', () => {
|
67 | winstonLog_1.winstonLog.logInfo(`trying to establish connection for ${connectionString}`);
|
68 | });
|
69 | conn.on('connected', () => {
|
70 | winstonLog_1.winstonLog.logInfo(`connection established successfully ${connectionString}`);
|
71 | defer.resolve(true);
|
72 | emitMesseageToALL('databaseconnected', conn);
|
73 | });
|
74 | conn.on('error', (err) => {
|
75 | winstonLog_1.winstonLog.logInfo(`connection to mongo failed for ${connectionString} with error ${err}`);
|
76 | defer.resolve(false);
|
77 | emitMesseageToALL('error', conn);
|
78 | });
|
79 | conn.on('disconnected', () => {
|
80 | winstonLog_1.winstonLog.logInfo(`connection closed successfully ${connectionString}`);
|
81 | defer.resolve(false);
|
82 | emitMesseageToALL('disconnected', conn);
|
83 | });
|
84 | return defer.promise;
|
85 | }
|
86 | function defaultDomainOptions(connectionOption) {
|
87 | if (!connectionOption) {
|
88 | connectionOption = {};
|
89 | }
|
90 | if (!connectionOption['server']) {
|
91 | connectionOption['server'] = {};
|
92 | }
|
93 | if (!connectionOption['replset']) {
|
94 | connectionOption['replset'] = {};
|
95 | }
|
96 | connectionOption['server']['domainsEnabled'] = true;
|
97 | connectionOption['replset']['domainsEnabled'] = true;
|
98 | return connectionOption;
|
99 | }
|
100 |
|
101 |
|