/
promises.js
62 lines (59 loc) · 2.16 KB
/
promises.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
var when = require('when'),
fn = require('when/function'),
p = require('../lib/promiseSupport.js');
require('../lib/fakesP');
// Assume Version = objectLift(version), File = objectLift(file) etc.
module.exports = function upload(stream, idOrPath, tag, done) {
var blob = blobManager.create(account);
var tx = db.begin();
var blobIdP = blob.put(stream);
var fileP = self.byUuidOrPath(idOrPath).get();
var previousIdP = p.ternary(fileP, p.get(fileP, 'version'), null);
var versionP = p.allObject({
userAccountId: userAccount.id,
date: new Date(),
blobId: blobIdP,
creatorId: userAccount.id,
previousId: previousIdP,
});
versionP = p.set(versionP, p.allObject({
id: fn.call(Version.createHash, versionP)
}));
// Even if Version.insert has been lifted, it returns a promise and
// therefore we cannot call execWithin. We have to wait for the promise
// to resolve
var versionInsert = p.eventuallyCall(
Version.insert(versionP), 'execWithin', tx);
var versionIdP = p.get(versionP, 'id');
var fileIdP = p.if (p.not(fileP), function () {
var splitPath = idOrPath.split('/');
var fileName = splitPath[splitPath.length - 1];
var newId = uuid.v1();
p.eventuallyCall(self.createQuery(idOrPath, p.allObject({
id: newId,
userAccountId: userAccount.id,
name: fileName,
version: versionIdP
}), 'execWithin', tx)).then(function() {
return newId;
});
}, function() {
return p.get(fileP, 'id');
});
var addFileVersion = p.eventuallyCall(
FileVersion.insert(p.allObject({
fileId: fileIdP,
versionId: versionIdP
})), 'execWithin', tx);
var fileUpdate = p.eventuallyCall(File.whereUpdate(
p.allObject({ id: fileIdP }),
p.allObject({ version: p.get(versionP, 'id') })),
'execWithin', tx);
when.all([versionInsert, addFileVersion, fileUpdate]).then(function() {
tx.commit();
return done();
}, function(err) {
tx.rollback();
return done(new Error(err));
});
}