@brimble/models 1.4.18 → 1.4.19

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/dist/index.d.ts CHANGED
@@ -12,7 +12,7 @@ export { default as Log } from "./logs";
12
12
  export { IUser, IGit, IProject, IFollowing, IIntegration, IEnv, IDomain, IToken, IMember, ITeam, IInstalledIntegration, ILog, } from "./types";
13
13
  export { GIT_TYPE, INTEGRATION_TYPE, OAUTH_PERMISSIONS, ENVIRONMENT, ROLES, PROJECT_STATUS, } from "./enum";
14
14
  import mongoose from "mongoose";
15
- export declare const connectToMongo: (mongoUrl: string) => Promise<void>;
15
+ export declare const connectToMongo: (mongoUrl: string, retryCount?: number) => Promise<void>;
16
16
  export declare const db: mongoose.Connection;
17
17
  export declare const closeMongo: (options?: {
18
18
  restart: boolean;
package/dist/index.js CHANGED
@@ -45,8 +45,9 @@ Object.defineProperty(exports, "PROJECT_STATUS", { enumerable: true, get: functi
45
45
  const mongoose_1 = __importDefault(require("mongoose"));
46
46
  const utils_1 = require("@brimble/utils");
47
47
  // Connection to Mongo
48
- const connectToMongo = (mongoUrl) => __awaiter(void 0, void 0, void 0, function* () {
48
+ const connectToMongo = (mongoUrl, retryCount) => __awaiter(void 0, void 0, void 0, function* () {
49
49
  const options = {
50
+ autoReconnect: true,
50
51
  useNewUrlParser: true,
51
52
  useUnifiedTopology: true,
52
53
  poolSize: 10,
@@ -55,7 +56,17 @@ const connectToMongo = (mongoUrl) => __awaiter(void 0, void 0, void 0, function*
55
56
  mongoose_1.default.set("useFindAndModify", false);
56
57
  mongoose_1.default.set("useCreateIndex", true);
57
58
  // connect to mongo
58
- mongoose_1.default.connect(mongoUrl, options);
59
+ mongoose_1.default.connect(mongoUrl, options).catch((err) => {
60
+ if (retryCount && retryCount > 0) {
61
+ setTimeout(() => {
62
+ (0, exports.connectToMongo)(mongoUrl, retryCount - 1);
63
+ }, 5000);
64
+ }
65
+ else {
66
+ console.log("No more retries, exiting...");
67
+ process.exit(1);
68
+ }
69
+ });
59
70
  // listen for connection
60
71
  mongoose_1.default.connection.on("connected", () => {
61
72
  console.log("Database connection successful 🚀");
@@ -69,8 +80,12 @@ const connectToMongo = (mongoUrl) => __awaiter(void 0, void 0, void 0, function*
69
80
  exports.connectToMongo = connectToMongo;
70
81
  exports.db = mongoose_1.default.connection;
71
82
  const closeMongo = (options) => {
72
- if (options === null || options === void 0 ? void 0 : options.restart)
73
- mongoose_1.default.connection.close(() => { (0, exports.connectToMongo)(options.url); });
83
+ if (options === null || options === void 0 ? void 0 : options.restart) {
84
+ mongoose_1.default.connection.close(() => {
85
+ utils_1.log.info("Mongoose connection closed, restarting...");
86
+ (0, exports.connectToMongo)(options.url, 3);
87
+ });
88
+ }
74
89
  else
75
90
  mongoose_1.default.connection.close(true);
76
91
  };
package/index.ts CHANGED
@@ -36,8 +36,12 @@ import mongoose from "mongoose";
36
36
  import { log } from "@brimble/utils";
37
37
 
38
38
  // Connection to Mongo
39
- export const connectToMongo = async (mongoUrl: string): Promise<void> => {
39
+ export const connectToMongo = async (
40
+ mongoUrl: string,
41
+ retryCount?: number,
42
+ ): Promise<void> => {
40
43
  const options = {
44
+ autoReconnect: true,
41
45
  useNewUrlParser: true,
42
46
  useUnifiedTopology: true,
43
47
  poolSize: 10,
@@ -47,7 +51,16 @@ export const connectToMongo = async (mongoUrl: string): Promise<void> => {
47
51
  mongoose.set("useCreateIndex", true);
48
52
 
49
53
  // connect to mongo
50
- mongoose.connect(mongoUrl, options);
54
+ mongoose.connect(mongoUrl, options).catch((err) => {
55
+ if (retryCount && retryCount > 0) {
56
+ setTimeout(() => {
57
+ connectToMongo(mongoUrl, retryCount - 1);
58
+ }, 5000);
59
+ } else {
60
+ console.log("No more retries, exiting...");
61
+ process.exit(1);
62
+ }
63
+ });
51
64
 
52
65
  // listen for connection
53
66
  mongoose.connection.on("connected", () => {
@@ -63,7 +76,11 @@ export const connectToMongo = async (mongoUrl: string): Promise<void> => {
63
76
 
64
77
  export const db = mongoose.connection;
65
78
 
66
- export const closeMongo = (options?: { restart: boolean, url:string }) => {
67
- if (options?.restart) mongoose.connection.close(() => { connectToMongo(options.url); });
68
- else mongoose.connection.close(true);
79
+ export const closeMongo = (options?: { restart: boolean; url: string }) => {
80
+ if (options?.restart) {
81
+ mongoose.connection.close(() => {
82
+ log.info("Mongoose connection closed, restarting...");
83
+ connectToMongo(options.url, 3);
84
+ });
85
+ } else mongoose.connection.close(true);
69
86
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brimble/models",
3
- "version": "1.4.18",
3
+ "version": "1.4.19",
4
4
  "description": "Brimble models",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",