-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsucursalprovider-mongodb.js
executable file
·61 lines (55 loc) · 1.77 KB
/
sucursalprovider-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
var Mongodb = require('mongodb');
var Db = Mongodb.Db;
var Connection = Mongodb.Connection;
var Server = Mongodb.Server
var BSON = Mongodb.BSON
var ObjectID = Mongodb.ObjectID;
SucursalProvider = function(host, port){
this.db = new Db('ComRap',new Server(host, port, {auto_reconnect: true}, {}));
this.db.open(function(){});
};
SucursalProvider.prototype.getCollection = function(callback){
this.db.collection('sucursal', function(error, sucursal_collection){
if( error ) callback( error );
else callback( null, sucursal_collection);
});
};
SucursalProvider.prototype.findAll = function( callback ){
this.getCollection(function( error, sucursal_collection){
if( error ) callback(error);
else{
sucursal_collection.find().toArray(function(error, results){
if(error) callback(error);
else callback(null, results);
});
}
});
};
SucursalProvider.prototype.findById = function( callback ){
this.getCollection(function(error, sucursal_collection){
if(error) callback(error)
else{
sucursal_collection.find({_id: sucursal_collection.db.bson_serializer.ObjectId.createFromHexString(id)},function(error, result){
if( error ) callback(error);
else callback(null, result);
});
}
});
};
SucursalProvider.prototype.save = function(sucursales, callback){
this.getCollection(function(error, sucursal_collection){
if( error ) callback( error )
else{
if( typeof(sucursales.length) == "undefined")
sucursales = [sucursales];
for(var i = 0; i < sucursales.length; i++){
sucursal = sucursales[i];
sucursal.created_at = new Date();
}
sucursal_collection.insert(sucursales, function(){
callback(null, sucursales);
});
}
});
};
exports.SucursalProvider = SucursalProvider;