|
1 | | -"use strict"; |
2 | | -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
3 | | - if (k2 === undefined) k2 = k; |
4 | | - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); |
5 | | -}) : (function(o, m, k, k2) { |
6 | | - if (k2 === undefined) k2 = k; |
7 | | - o[k2] = m[k]; |
8 | | -})); |
9 | | -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
10 | | - Object.defineProperty(o, "default", { enumerable: true, value: v }); |
11 | | -}) : function(o, v) { |
12 | | - o["default"] = v; |
13 | | -}); |
14 | | -var __importStar = (this && this.__importStar) || function (mod) { |
15 | | - if (mod && mod.__esModule) return mod; |
16 | | - var result = {}; |
17 | | - if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); |
18 | | - __setModuleDefault(result, mod); |
19 | | - return result; |
20 | | -}; |
21 | | -var __importDefault = (this && this.__importDefault) || function (mod) { |
22 | | - return (mod && mod.__esModule) ? mod : { "default": mod }; |
23 | | -}; |
24 | | -Object.defineProperty(exports, "__esModule", { value: true }); |
25 | | -exports.disconnect = exports.connect = void 0; |
26 | | -const mongoose_1 = __importDefault(require("mongoose")); |
27 | | -const enums_1 = require("./common/enums"); |
28 | | -const Config = __importStar(require("./config")); |
29 | | -// Initialises the database connection using config settings |
30 | | -const connect = async (log) => { |
31 | | - // Set the db connection options from config settings |
32 | | - const options = { |
33 | | - connectTimeoutMS: Config.get().db.connTimeout, |
34 | | - keepAlive: true, |
35 | | - ssl: Config.get().db.useSRV || Config.get().db.ssl, |
36 | | - useFindAndModify: false, |
37 | | - useNewUrlParser: true, |
38 | | - useUnifiedTopology: true, |
39 | | - }; |
40 | | - // Configure db credentials |
41 | | - const username = Config.get().db.username || process.env.XBROWSERSYNC_DB_USER; |
42 | | - const password = Config.get().db.password || process.env.XBROWSERSYNC_DB_PWD; |
43 | | - const creds = username && password ? `${encodeURIComponent(username)}:${encodeURIComponent(password)}@` : ''; |
44 | | - // Create mongo connection uri using host and db name defined in config settings |
45 | | - let dbServerUrl = 'mongodb'; |
46 | | - if (Config.get().db.useSRV) { |
47 | | - dbServerUrl += `+srv://${creds}${Config.get().db.host}/${Config.get().db.name}`; |
48 | | - } |
49 | | - else { |
50 | | - dbServerUrl += `://${creds}${Config.get().db.host}:${Config.get().db.port}/${Config.get().db.name}`; |
51 | | - } |
52 | | - dbServerUrl += Config.get().db.authSource ? `?authSource=${Config.get().db.authSource}` : ''; |
53 | | - // Connect to the database |
54 | | - try { |
55 | | - await mongoose_1.default.connect(dbServerUrl, options); |
56 | | - } |
57 | | - catch (err) { |
58 | | - if ((log !== null && log !== void 0 ? log : undefined) !== undefined) { |
59 | | - log(enums_1.LogLevel.Error, 'Unable to connect to database', null, err); |
60 | | - } |
61 | | - process.exit(1); |
62 | | - } |
63 | | -}; |
64 | | -exports.connect = connect; |
65 | | -// Closes the database connection |
66 | | -const disconnect = async () => { |
67 | | - await mongoose_1.default.disconnect(); |
68 | | -}; |
69 | | -exports.disconnect = disconnect; |
| 1 | +"use strict"; |
| 2 | +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
| 3 | + if (k2 === undefined) k2 = k; |
| 4 | + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); |
| 5 | +}) : (function(o, m, k, k2) { |
| 6 | + if (k2 === undefined) k2 = k; |
| 7 | + o[k2] = m[k]; |
| 8 | +})); |
| 9 | +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
| 10 | + Object.defineProperty(o, "default", { enumerable: true, value: v }); |
| 11 | +}) : function(o, v) { |
| 12 | + o["default"] = v; |
| 13 | +}); |
| 14 | +var __importStar = (this && this.__importStar) || function (mod) { |
| 15 | + if (mod && mod.__esModule) return mod; |
| 16 | + var result = {}; |
| 17 | + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); |
| 18 | + __setModuleDefault(result, mod); |
| 19 | + return result; |
| 20 | +}; |
| 21 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 22 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 23 | +}; |
| 24 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 25 | +exports.disconnect = exports.connect = void 0; |
| 26 | +const mongoose_1 = __importDefault(require("mongoose")); |
| 27 | +const enums_1 = require("./common/enums"); |
| 28 | +const Config = __importStar(require("./config")); |
| 29 | +// Initialises the database connection using config settings |
| 30 | +const connect = async (log) => { |
| 31 | + // Set the db connection options from config settings |
| 32 | + const options = { |
| 33 | + connectTimeoutMS: Config.get().db.connTimeout, |
| 34 | + keepAlive: true, |
| 35 | + ssl: Config.get().db.useSRV || Config.get().db.ssl, |
| 36 | + useFindAndModify: false, |
| 37 | + useNewUrlParser: true, |
| 38 | + useUnifiedTopology: true, |
| 39 | + }; |
| 40 | + // Configure db credentials |
| 41 | + const username = Config.get().db.username || process.env.XBROWSERSYNC_DB_USER; |
| 42 | + const password = Config.get().db.password || process.env.XBROWSERSYNC_DB_PWD; |
| 43 | + const creds = username && password ? `${encodeURIComponent(username)}:${encodeURIComponent(password)}@` : ''; |
| 44 | + // Create mongo connection uri using host and db name defined in config settings |
| 45 | + let dbServerUrl = 'mongodb'; |
| 46 | + if (Config.get().db.useSRV) { |
| 47 | + dbServerUrl += `+srv://${creds}${Config.get().db.host}/${Config.get().db.name}`; |
| 48 | + } |
| 49 | + else { |
| 50 | + dbServerUrl += `://${creds}${Config.get().db.host}:${Config.get().db.port}/${Config.get().db.name}`; |
| 51 | + } |
| 52 | + dbServerUrl += Config.get().db.authSource ? `?authSource=${Config.get().db.authSource}` : ''; |
| 53 | + // Connect to the database |
| 54 | + try { |
| 55 | + await mongoose_1.default.connect(dbServerUrl, options); |
| 56 | + } |
| 57 | + catch (err) { |
| 58 | + if ((log !== null && log !== void 0 ? log : undefined) !== undefined) { |
| 59 | + log(enums_1.LogLevel.Error, 'Unable to connect to database', null, err); |
| 60 | + } |
| 61 | + process.exit(1); |
| 62 | + } |
| 63 | +}; |
| 64 | +exports.connect = connect; |
| 65 | +// Closes the database connection |
| 66 | +const disconnect = async () => { |
| 67 | + await mongoose_1.default.disconnect(); |
| 68 | +}; |
| 69 | +exports.disconnect = disconnect; |
0 commit comments