@hvedinich/db 0.0.7 → 0.0.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hvedinich/db",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "db",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  const mongoose = require('mongoose');
2
2
  const migration = require('./migration');
3
3
 
4
- const init = config => {
4
+ const init = ({ config, setStatus }) => {
5
5
  const db = mongoose.connection;
6
- const retry = 0;
6
+ let retry = 0;
7
+ const maxCount = 10;
8
+
7
9
  const state = { connected: false };
8
10
  const opt = {
9
11
  useNewUrlParser: true,
@@ -13,29 +15,49 @@ const init = config => {
13
15
  useUnifiedTopology: true,
14
16
  useFindAndModify: false,
15
17
  };
16
- mongoose.connect(config.mongo.url, opt);
18
+
19
+ mongoose.connect(config.mongo.url, opt).catch(err => {
20
+ if (retry >= maxCount) {
21
+ setStatus(false);
22
+ }
23
+ console.log(`consction error: ${err}`);
24
+ });
25
+
17
26
  db.on('disconnected', async () => {
27
+ console.log(`Can not connect to mongo retry: ${retry}`);
18
28
  state.connected = false;
19
- if (retry < 10) {
29
+ if (retry < maxCount) {
30
+ retry = retry + 1;
20
31
  await new Promise(res => setTimeout(res, 1000));
21
- await mongoose.connect(config.mongo.url, opt);
32
+ try {
33
+ await mongoose.connect(config.mongo.url, opt);
34
+ } catch (err) {
35
+ if (retry >= maxCount) {
36
+ setStatus(false);
37
+ }
38
+ console.log(`retry error: ${err?.message || JSON.stringify(err)}`);
39
+ }
40
+ }
41
+ if (retry >= maxCount) {
42
+ setStatus(false);
22
43
  }
23
- console.log('Can not connect to mongo');
24
44
  });
25
45
 
26
46
  db.on('error', () => {
27
47
  console.log('connection error:');
28
48
  mongoose.disconnect();
29
49
  });
30
- db.once('open', () => {
50
+ db.on('open', () => {
31
51
  console.log(`Mongo conected to ${config.mongo.url}`);
32
52
  state.connected = true;
53
+ setStatus(true);
54
+ retry = 0;
33
55
  });
34
56
 
35
57
  const createModel = ({ name, schema }) => mongoose.model(name, schema);
36
58
 
37
59
  const runMigrations = (migrations, log = console) =>
38
- migration.run(createModel, config.serviceName, migrations, log, state);
60
+ migration.run(createModel, config.serviceName, migrations, log, state, setStatus);
39
61
 
40
62
  return {
41
63
  createModel,
@@ -1,13 +1,18 @@
1
1
  /* eslint-disable no-await-in-loop */
2
2
  const schema = require('./schema');
3
3
 
4
- const run = async (createModel, serviceName, migrations = [], log, state) => {
4
+ const run = async (createModel, serviceName, migrations = [], log, state, setStatus) => {
5
5
  log.debug('Migration: waiting for connection');
6
+ let count = 0;
7
+ const maxCount = 60;
6
8
 
7
9
  while (!state.connected) {
10
+ count += 1;
11
+ if (count >= maxCount) setStatus(false);
12
+
8
13
  // eslint-disable-next-line no-await-in-loop
9
- await new Promise(res => setTimeout(res, 100));
10
- console.log(' while (!state.connected)');
14
+ await new Promise(res => setTimeout(res, 1000));
15
+ console.log(`while (!state.connected)${count}`);
11
16
  }
12
17
 
13
18
  log.debug('Migration: Start');
@@ -27,13 +32,14 @@ const run = async (createModel, serviceName, migrations = [], log, state) => {
27
32
 
28
33
  if (!currentMigration.inProgress || isPrevMigrationStuck) {
29
34
  try {
30
- log.debug(`Migration: last version ${currentMigration.version}`);
35
+ const currentVersion = currentMigration.version || 0;
36
+ log.debug(`Migration: last version ${currentVersion}`);
31
37
  await model.updateOne({ serviceName }, { $set: { inProgress: true } }).lean();
32
38
 
33
- const migrationToDo = migrations.filter(e => e.version > currentMigration.version);
39
+ const migrationToDo = migrations.filter(e => e.version > currentVersion);
34
40
 
35
41
  for (let i = 0; i < migrationToDo.length; i += 1) {
36
- log.debug(`Migration: update from ${currentMigration.version} to ${migrationToDo[i].version}`);
42
+ log.debug(`Migration: update from ${currentVersion} to ${migrationToDo[i].version}`);
37
43
  await migrationToDo[i].run();
38
44
 
39
45
  await model