-
Notifications
You must be signed in to change notification settings - Fork 63
/
migration-store.js
73 lines (68 loc) · 1.5 KB
/
migration-store.js
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
const AV = require('leanengine')
module.exports = LeanStorageStore
function LeanStorageStore() {
AV.init({
appId: process.env.LEANCLOUD_APP_ID,
appKey: process.env.LEANCLOUD_APP_KEY,
masterKey: process.env.LEANCLOUD_APP_MASTER_KEY,
})
AV.Cloud.useMasterKey()
}
/**
* Save the migration data.
*
* @api public
*/
LeanStorageStore.prototype.save = function (set, fn) {
return new AV.Query('Migration')
.first({ useMasterKey: true })
.catch((err) => {
if (err.code == 101) {
// Class or object doesn't exists.
return null
}
throw err
})
.then((migration) => {
if (!migration) {
migration = new AV.Object('Migration')
}
return migration.save({
lastRun: set.lastRun,
migrations: set.migrations,
ACL: new AV.ACL(),
})
})
.then(() => {
return fn()
})
.catch(fn)
}
/**
* Load the migration data and call `fn(err, obj)`.
*
* @param {Function} fn
* @return {Type}
* @api public
*/
LeanStorageStore.prototype.load = function (fn) {
return new AV.Query('Migration')
.first({ useMasterKey: true })
.catch((err) => {
if (err.code == 101) {
// Class or object doesn't exists.
return null
}
throw err
})
.then((migration) => {
if (!migration) {
return fn(null, {})
}
return fn(null, {
lastRun: migration.get('lastRun'),
migrations: migration.get('migrations'),
})
})
.catch(fn)
}