-
Notifications
You must be signed in to change notification settings - Fork 3
/
wishlist-mongodb.js
121 lines (110 loc) · 3.91 KB
/
wishlist-mongodb.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
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
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;
WishlistProvider = function(host, port, dbname, username, password) {
var client = new Db(dbname, new Server(host, port, {auto_reconnect: true}, {}));
this.db = client;
client.open(function(err, p_client){
client.authenticate(username, password, function(err, p_client) {
if (err) console.log(err);
});
});
};
WishlistProvider.prototype.getCollection= function(callback) {
this.db.collection('Wishlists', function(error, Wishlist_collection) {
if( error ) callback(error);
else callback(null, Wishlist_collection);
});
};
WishlistProvider.prototype.find = function(criteria, debug, callback) {
this.getCollection(function(error, Wishlist_collection) {
if( error ) callback(error)
else {
if (!debug) {
criteria.removed_at = { $exists : false }
}
if (criteria._id) {
criteria._id = Wishlist_collection.db.bson_serializer.ObjectID.createFromHexString(criteria._id)
}
Wishlist_collection.find(criteria).toArray(function(error, results) {
if( error ) callback(error)
else callback(null, results)
});
}
});
};
WishlistProvider.prototype.save = function(wishes, callback) {
this.getCollection(function(error, Wishlist_collection) {
if( error ) callback(error)
else {
if( typeof(wishes.length)=="undefined")
wishes = [wishes];
for( var i =0;i< wishes.length;i++ ) {
wish = wishes[i];
wish.created_at = new Date();
}
Wishlist_collection.insert(wishes, function() {
callback(null, wishes);
});
}
});
};
WishlistProvider.prototype.update = function(id, wish, callback) {
this.getCollection(function(error, Wishlist_collection) {
if( error ) callback( error );
else {
Wishlist_collection.update(
{_id: Wishlist_collection.db.bson_serializer.ObjectID.createFromHexString(id)},
{"$set": wish},
{},
function(error){
if( error ) callback(error);
});
}
});
};
WishlistProvider.prototype.delete = function(id, callback) {
this.getCollection(function(error, Wishlist_collection) {
if( error ) callback( error );
else {
Wishlist_collection.remove(
{_id: Wishlist_collection.db.bson_serializer.ObjectID.createFromHexString(id)},
function(err) {
if(err) callback(err)
});
}
});
};
WishlistProvider.prototype.remove = function(id, reason, callback) {
this.getCollection(function(error, Wishlist_collection) {
if( error ) callback( error );
else {
Wishlist_collection.update(
{_id: Wishlist_collection.db.bson_serializer.ObjectID.createFromHexString(id)},
{"$set": {reason_removed: reason, removed_at: new Date()}},
{},
function(error, article){
if( error ) callback(error);
else callback(null, article)
});
}
});
};
WishlistProvider.prototype.restore = function(id, callback) {
this.getCollection(function(error, Wishlist_collection) {
if( error ) callback( error );
else {
Wishlist_collection.update(
{_id: Wishlist_collection.db.bson_serializer.ObjectID.createFromHexString(id)},
{"$unset": {reason_removed: 1, removed_at: 1}},
{},
function(error, article){
if( error ) callback(error);
else callback(null, article)
});
}
});
};
exports.WishlistProvider = WishlistProvider;