-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
159 lines (133 loc) · 4.73 KB
/
database.js
File metadata and controls
159 lines (133 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const MongoClient = require('mongodb').MongoClient,
Migration = require('./migration'),
{askQuestion} = require('./utils');
class Database {
constructor(argsManager) {
this.argsManager = argsManager;
this.collectionName = this.argsManager.getCollectionName()
}
getConnection() {
let connectionString = this.argsManager.getConnectionString();
return this.mongoConnect(connectionString);
}
mongoConnect(uri) {
return new Promise((resolve, reject) => {
return MongoClient.connect(uri, function (err, db) {
if (err) {
return reject(err);
} else {
return resolve(db)
}
})
})
}
async cleanUpMigrationsCollection() {
let db = await this.getConnection();
return db.collection(this.collectionName)
.deleteMany({})
}
async fixMigrationsCollection() {
let message = `it looks like you ${this.collectionName} collection is damaged. \x1b[32mWould you like to fix it?\x1b[0m`;
await askQuestion(message, this.argsManager);
await this.cleanUpMigrationsCollection();
let migrations = Migration.getMigrationsFromFolder(this.argsManager);
for (let migration of migrations) {
try {
await askQuestion(`is that migration: \x1b[33m${migration.name}\x1b[0m already applied?`, this.argsManager);
await this._writeDownMigration(migration);
} catch (err) {
//TODO: make common-error module to throw and handle appropriate errors
if (err === 'declined by user') {
continue;
} else {
throw err;
}
}
}
}
async fetchAppliedMigrations() {
let db = await this.getConnection();
let result = await db.collection(this.collectionName)
.find({})
.toArray()
.then(migrations => {
try {
return migrations
.map(migration =>
new Migration(migration.name, undefined, migration.applied))
} catch (err) {
if (err === 'PARSERROR') { //TODO: error...
return this.fixMigrationsCollection()
.then(() => this.fetchAppliedMigrations())
} else {
throw err;
}
}
}
);
db.close();
return result;
}
/**
* make a note in database that specified migration applied
* @param migration {Migration}
* @returns {Promise.<void>}
*/
async _writeDownMigration(migration) {
let db = await this.getConnection();
return db.collection(this.collectionName)
.updateOne({
name: migration.name
}, {
name: migration.name,
applied: new Date()
}, {
upsert: true
})
}
async _cleanUpMigration(migration) {
let db = await this.getConnection();
return db.collection(this.collectionName)
.deleteMany({
name: migration.name
})
}
async upMigrations(migrations) {
return this._applyMigrations(migrations, "up");
}
async downMigrations(migrations) {
return this._applyMigrations(migrations, "down");
}
async _applyMigrations(migrations, method) {
for (let migration of migrations) {
try {
await migration[method]()
}
catch (err) {
throw (
`during ${method} migration [${migration.name}] an error occurred:
${JSON.stringify(err, null, 2)} migration process interrupted`
);
}
try {
switch (method) {
case "up":
await this._writeDownMigration(migration);
break;
case "down":
await this._cleanUpMigration(migration);
break;
}
}
catch (err) {
throw (
`migration ${this.collectionName} ${method} successfully, but
an error occurred during persisting migration in [${migration.name}] collection:
${JSON.stringify(err, null, 2)}
migration process interrupted`
);
}
}
}
}
module.exports = Database;