forked from silvercrow/cataloger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalogr.js
75 lines (68 loc) · 2.06 KB
/
catalogr.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
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;
Catalogr = function(host,port){
this.db = new Db('node-mongo-catalogr', new Server(host,port,{safe:false},{auto_reconnect:true},{}));
this.db.open(function(){});
};
Catalogr.prototype.getCollection = function(callback){
this.db.collection('catalogs', function(error, catalogr_collection){
if(error) callback(error);
else callback(null, catalogr_collection);
});
};
Catalogr.prototype.findAll = function(callback){
this.getCollection(function(error, catalogr_collection){
if(error) callback(error)
else{
catalogr_collection.find().toArray(function(error, results){
if(error) callback(error)
else callback(null, results)
});
}
});
};
Catalogr.prototype.save = function(catalogs, callback){
this.getCollection(function(error, catalogr_collection){
if(error) callback(error)
else{
if(typeof(catalogs.length)=="undefined")
catalogs = [catalogs];
for(var i=0;i<catalogs.length;i++){
catalog = catalogs[i];
}
catalogr_collection.insert(catalogs, function(){
callback(null,catalogs);
});
}
});
};
Catalogr.prototype.update = function(catalogId, catalogs, callback){
this.getCollection(function(error, catalogr_collection){
if(error) callback(error);
else{
catalogr_collection.update(
{_id: catalogr_collection.db.bson_serializer.ObjectID.createFromHexString(catalogId)},
catalogs,
function(error, catalogs){
if(error) callback(error);
else callback(null, catalogs)
});
}
});
}
Catalogr.prototype.findById = function(catalogId, callback){
this.getCollection(function(error, catalogr_collection){
if(error) callback(error);
else
catalogr_collection.findOne({_id:catalogr_collection.db.bson_serializer.ObjectID.createFromHexString(catalogId)}, function(error,result){
if(error) callback(error);
else{
callback(null, result);
}
});
})
}
exports.Catalogr = Catalogr;