-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateUsersnNetwork.js
68 lines (68 loc) · 2.19 KB
/
createUsersnNetwork.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
var uuid = require('uuid');
var Promise = require('bluebird');
var ThinkTransaction = require('../thinkTransaction');
var transOptions = {
timeout: 30 //timeout the transaction if it couldnt complete within this time window
};
var johnsId = uuid.v4(), rajasId = uuid.v4();
ThinkTransaction.begin(transOptions)
.then(function(trans) {
//Create User's John and Raja into Profile Collection (in parallel)
Promise.all([
trans.op.insert({
Profile : {
id : johnsId, //REQUIRED : You will need to explicity pass id value along with other attributes of the document
type: "User",
firstName : "John",
lastName : "K"
}
}),
trans.op.insert({
Profile : {
id : rajasId, //REQUIRED : You will need to explicity pass id value along with other attributes of the document
type: "User",
firstName : "Raja",
lastName : "M"
}
}),
])
.then(function(results){
//after both John and Raja have been created in Profile, make them as connections in ProfileNetwork Collection
//Make Raja as Johns connection - John is now following Raja
return trans.op.insert({
ProfileNetwork : {
id : uuid.v4(), //REQUIRED : You will need to explicity pass id value along with other attributes of the document
profileId: johnsId,
referencesProfileId : rajasId
}
}).then(function(result){
//lets also make Raja follow John
return trans.op.insert({
ProfileNetwork : {
id : uuid.v4(), //REQUIRED : You will need to explicity pass id value along with other attributes of the document
profileId: rajasId,
referencesProfileId : johnsId
}
});
})
})
.then(function(results){
console.log('create Users commit handler');
trans.commit().then(function(result){
console.log('commit complete');
console.log(result);
})
})
.catch(function(err){
console.log('create Users catch handler');
console.log(err);
trans.rollback().then(function(result){
console.log('rollback complete');
console.log(result);
});
});
})
.catch(function(err){
console.log('couldn\'t begin transaction');
console.log(err);
});