@hvedinich/db 0.0.5 → 0.0.6
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/.eslintrc.js +15 -0
- package/.prettierrc +6 -0
- package/package.json +9 -7
- package/src/index.js +6 -5
- package/src/migration/index.js +2 -3
- package/.DS_Store +0 -0
- package/.eslintrc.json +0 -14
- package/src/.DS_Store +0 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
extends: ["airbnb-base", "prettier"],
|
|
3
|
+
plugins: ["prettier"],
|
|
4
|
+
rules: {
|
|
5
|
+
"no-console": 0,
|
|
6
|
+
"no-underscore-dangle": [
|
|
7
|
+
"error",
|
|
8
|
+
{
|
|
9
|
+
allow: ["_id"],
|
|
10
|
+
},
|
|
11
|
+
],
|
|
12
|
+
"arrow-body-style": "off",
|
|
13
|
+
"prefer-arrow-callback": "off",
|
|
14
|
+
},
|
|
15
|
+
};
|
package/.prettierrc
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hvedinich/db",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "db",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -17,15 +17,17 @@
|
|
|
17
17
|
},
|
|
18
18
|
"homepage": "https://github.com/hvedinich/DB#readme",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"mongoose": "5.
|
|
20
|
+
"mongoose": "^5.13.15"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"eslint": "
|
|
24
|
-
"eslint-config-airbnb-base": "
|
|
25
|
-
"eslint-
|
|
26
|
-
"eslint-plugin-import": "2.
|
|
23
|
+
"eslint": "^7.32.0",
|
|
24
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
25
|
+
"eslint-config-prettier": "^8.3.0",
|
|
26
|
+
"eslint-plugin-import": "2.26.0",
|
|
27
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
28
|
+
"prettier": "^2.5.1"
|
|
27
29
|
},
|
|
28
30
|
"publishConfig": {
|
|
29
31
|
"access": "public"
|
|
30
32
|
}
|
|
31
|
-
}
|
|
33
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const mongoose = require('mongoose');
|
|
2
2
|
const migration = require('./migration');
|
|
3
3
|
|
|
4
|
-
const init =
|
|
4
|
+
const init = config => {
|
|
5
5
|
const db = mongoose.connection;
|
|
6
6
|
const retry = 0;
|
|
7
7
|
const state = { connected: false };
|
|
@@ -14,10 +14,11 @@ const init = (config) => {
|
|
|
14
14
|
useFindAndModify: false,
|
|
15
15
|
};
|
|
16
16
|
mongoose.connect(config.mongo.url, opt);
|
|
17
|
-
db.on('disconnected', () => {
|
|
17
|
+
db.on('disconnected', async () => {
|
|
18
18
|
state.connected = false;
|
|
19
19
|
if (retry < 10) {
|
|
20
|
-
|
|
20
|
+
await new Promise(res => setTimeout(res, 1000));
|
|
21
|
+
await mongoose.connect(config.mongo.url, opt);
|
|
21
22
|
}
|
|
22
23
|
console.log('Can not connect to mongo');
|
|
23
24
|
});
|
|
@@ -33,8 +34,8 @@ const init = (config) => {
|
|
|
33
34
|
|
|
34
35
|
const createModel = ({ name, schema }) => mongoose.model(name, schema);
|
|
35
36
|
|
|
36
|
-
const runMigrations = (migrations, log = console) =>
|
|
37
|
-
.run(createModel, config.serviceName, migrations, log, state);
|
|
37
|
+
const runMigrations = (migrations, log = console) =>
|
|
38
|
+
migration.run(createModel, config.serviceName, migrations, log, state);
|
|
38
39
|
|
|
39
40
|
return {
|
|
40
41
|
createModel,
|
package/src/migration/index.js
CHANGED
|
@@ -9,10 +9,9 @@ const getOrWait = async (model, serviceName, log) => {
|
|
|
9
9
|
log.debug('getOrWait: waiting for db');
|
|
10
10
|
const currentMigration = await model.findOne({ serviceName }).lean().exec();
|
|
11
11
|
if (currentMigration) {
|
|
12
|
-
if (!currentMigration.inProgress
|
|
13
|
-
|| new Date(currentMigration.updatedAt).getTime() + 60000 < Date.now()) {
|
|
12
|
+
if (!currentMigration.inProgress) {
|
|
14
13
|
const updated = await model.findOneAndUpdate(
|
|
15
|
-
{ serviceName },
|
|
14
|
+
{ serviceName, inProgress: false },
|
|
16
15
|
{ $set: { inProgress: true } },
|
|
17
16
|
{ new: true },
|
|
18
17
|
);
|
package/.DS_Store
DELETED
|
Binary file
|
package/.eslintrc.json
DELETED
package/src/.DS_Store
DELETED
|
Binary file
|