@hvedinich/db 0.0.5 → 0.0.7
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 +50 -57
- 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.7",
|
|
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
|
@@ -1,72 +1,65 @@
|
|
|
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) => {
|
|
5
|
+
log.debug('Migration: waiting for connection');
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
log.debug('getOrWait: waiting for db');
|
|
10
|
-
const currentMigration = await model.findOne({ serviceName }).lean().exec();
|
|
11
|
-
if (currentMigration) {
|
|
12
|
-
if (!currentMigration.inProgress
|
|
13
|
-
|| new Date(currentMigration.updatedAt).getTime() + 60000 < Date.now()) {
|
|
14
|
-
const updated = await model.findOneAndUpdate(
|
|
15
|
-
{ serviceName },
|
|
16
|
-
{ $set: { inProgress: true } },
|
|
17
|
-
{ new: true },
|
|
18
|
-
);
|
|
19
|
-
if (updated) {
|
|
20
|
-
waiting = false;
|
|
21
|
-
result = currentMigration;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
await new Promise(res => setTimeout(res, 1000));
|
|
25
|
-
} else {
|
|
26
|
-
waiting = false;
|
|
27
|
-
}
|
|
7
|
+
while (!state.connected) {
|
|
8
|
+
// eslint-disable-next-line no-await-in-loop
|
|
9
|
+
await new Promise(res => setTimeout(res, 100));
|
|
10
|
+
console.log(' while (!state.connected)');
|
|
28
11
|
}
|
|
29
|
-
return result;
|
|
30
|
-
};
|
|
31
12
|
|
|
32
|
-
const run = async (createModel, serviceName, migrations = [], log, state) => {
|
|
33
|
-
log.debug('Migration: waiting for connection');
|
|
34
|
-
await new Promise(async (done) => {
|
|
35
|
-
while (!state.connected) {
|
|
36
|
-
// eslint-disable-next-line no-await-in-loop
|
|
37
|
-
await new Promise(res => setTimeout(res, 100));
|
|
38
|
-
}
|
|
39
|
-
done();
|
|
40
|
-
});
|
|
41
13
|
log.debug('Migration: Start');
|
|
42
14
|
const model = createModel({ name: 'Migration', schema });
|
|
43
|
-
try {
|
|
44
|
-
const currentMigration = await getOrWait(model, serviceName, log);
|
|
45
15
|
|
|
46
|
-
|
|
47
|
-
|
|
16
|
+
const migrationList = await model.find({}).lean();
|
|
17
|
+
if (migrationList.length > 1) {
|
|
18
|
+
throw new Error('The migration fails due to an incorrect number of migration objects.');
|
|
19
|
+
}
|
|
20
|
+
const currentMigration =
|
|
21
|
+
migrationList[0] || (await model.create({ serviceName, startTime: Date.now() + 60000 * 10 }));
|
|
22
|
+
const isPrevMigrationStuck = currentMigration.inProgress && (currentMigration.startTime || 0) < Date.now();
|
|
23
|
+
|
|
24
|
+
if (isPrevMigrationStuck) {
|
|
25
|
+
log.error(`Previous migration stuck`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!currentMigration.inProgress || isPrevMigrationStuck) {
|
|
29
|
+
try {
|
|
30
|
+
log.debug(`Migration: last version ${currentMigration.version}`);
|
|
31
|
+
await model.updateOne({ serviceName }, { $set: { inProgress: true } }).lean();
|
|
32
|
+
|
|
33
|
+
const migrationToDo = migrations.filter(e => e.version > currentMigration.version);
|
|
48
34
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
35
|
+
for (let i = 0; i < migrationToDo.length; i += 1) {
|
|
36
|
+
log.debug(`Migration: update from ${currentMigration.version} to ${migrationToDo[i].version}`);
|
|
37
|
+
await migrationToDo[i].run();
|
|
52
38
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
39
|
+
await model
|
|
40
|
+
.updateOne(
|
|
41
|
+
{ serviceName },
|
|
42
|
+
{
|
|
43
|
+
$set: { version: migrationToDo[i].version },
|
|
44
|
+
$push: { history: { version: migrationToDo[i].version } },
|
|
45
|
+
},
|
|
46
|
+
{ upsert: true },
|
|
47
|
+
)
|
|
48
|
+
.lean();
|
|
49
|
+
}
|
|
50
|
+
log.debug('Migration: migrations are done');
|
|
51
|
+
} finally {
|
|
52
|
+
await model
|
|
53
|
+
.updateOne(
|
|
54
|
+
{ serviceName },
|
|
55
|
+
{
|
|
56
|
+
$set: { inProgress: false },
|
|
57
|
+
},
|
|
58
|
+
)
|
|
59
|
+
.lean();
|
|
61
60
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
await model.updateOne(
|
|
65
|
-
{ serviceName, inProgress: true },
|
|
66
|
-
{
|
|
67
|
-
$set: { inProgress: false },
|
|
68
|
-
},
|
|
69
|
-
).lean();
|
|
61
|
+
} else if (!isPrevMigrationStuck) {
|
|
62
|
+
log.debug('Migration is being performed by another instance');
|
|
70
63
|
}
|
|
71
64
|
};
|
|
72
65
|
|
package/.DS_Store
DELETED
|
Binary file
|
package/.eslintrc.json
DELETED
package/src/.DS_Store
DELETED
|
Binary file
|