forked from nagypeter/nodejs-mongodb-crud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
employeeprovider.js
108 lines (96 loc) · 3.17 KB
/
employeeprovider.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
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');
EmployeeProvider = function() {
var that = this;
mongodbUri = process.env.MONGOLAB_URI || 'mongodb://mongodb';
MongoClient.connect(mongodbUri, function(err, db){
if(err) { return console.dir(err); }
that.db = db;
})
};
EmployeeProvider.prototype.getCollection= function(callback) {
this.db.collection('employees', function(error, employee_collection) {
if( error ) callback(error);
else callback(null, employee_collection);
});
};
//find all employees
EmployeeProvider.prototype.findAll = function(callback) {
this.getCollection(function(error, employee_collection) {
if( error ) callback(error)
else {
employee_collection.find().toArray(function(error, results) {
if( error ) callback(error)
else callback(null, results)
});
}
});
};
//find an employee by ID
EmployeeProvider.prototype.findById = function(id, callback) {
this.getCollection(function(error, employee_collection) {
if( error ) callback(error)
else {
employee_collection.findOne({_id: employee_collection.db.bson_serializer.ObjectID.createFromHexString(id)}, function(error, result) {
if( error ) callback(error)
else callback(null, result)
});
}
});
};
//save new employee
EmployeeProvider.prototype.save = function(employees, callback) {
this.getCollection(function(error, employee_collection) {
if( error ) callback(error)
else {
if( typeof(employees.length)=="undefined")
employees = [employees];
for( var i =0;i< employees.length;i++ ) {
employee = employees[i];
employee.created_at = new Date();
}
employee_collection.insert(employees, function() {
callback(null, employees);
});
}
});
};
// update an employee
EmployeeProvider.prototype.update = function(employeeId, employees, callback) {
this.getCollection(function(error, employee_collection) {
if( error ) callback(error);
else {
employee_collection.update(
{_id: employee_collection.db.bson_serializer.ObjectID.createFromHexString(employeeId)},
employees,
function(error, employees) {
if(error) callback(error);
else callback(null, employees)
});
}
});
};
//delete employee
EmployeeProvider.prototype.delete = function(employeeId, callback) {
this.getCollection(function(error, employee_collection) {
if(error) callback(error);
else {
employee_collection.remove(
{_id: employee_collection.db.bson_serializer.ObjectID.createFromHexString(employeeId)},
function(error, employee){
if(error) callback(error);
else callback(null, employee)
});
}
});
};
exports.EmployeeProvider = EmployeeProvider;