-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
119 lines (102 loc) · 2.81 KB
/
logic.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
const mongoose = require('mongoose');
const assert = require('assert'); // N.B: Assert module comes bundled with NodeJS.
mongoose.Promise = global.Promise; // Allows us to use Native promises without throwing error.
// Connect to a single MongoDB instance. The connection string could be that of remote server
// We assign the connection instance to a constant to be used later in closing the connection
const db = mongoose.connect('mongodb://localhost:27017/contact-manager');
// Convert value to to lowercase
function toLower(v) {
return v.toLowerCase();
}
// Define a contact Schema
const contactSchema = mongoose.Schema({
firstname: { type: String, set: toLower },
lastname: { type: String, set: toLower },
phone: { type: String, set: toLower },
email: { type: String, set: toLower }
});
// Define model as an interface with the database
const Contact = mongoose.model('Contact', contactSchema);
/**
* @function [addContact]
* @returns {String} Status
*/
const addContact = (contact) => {
Contact.create(contact, (err) => {
assert.equal(null, err);
console.info('New contact added');
db.disconnect();
});
};
/**
* @function [getContact]
* @returns {Json} contacts
*/
const getContact = (name) => {
const search = new RegExp(name, 'i');
Contact.find({$or: [{firstname: search }, {lastname: search }]})
.exec((err, contact) => {
assert.equal(null, err);
console.info(contact);
console.info(`${contact.length} matches`);
db.disconnect();
});
};
// This is not a BULK insert - the underlying mongoose implementation does loops through all of the elements and commits them one by one
/**
* @function [addMultipleContacts]
* @return {Array} contacts
*/
const addMultipleContacts = (contacts) => {
Contact.create(contacts, (err, contacts) => {
assert.equal(null, err);
console.info(contacts, 'contacts')
db.disconnect();
})
}
/**
* @function [getContactList]
* @returns {Sting} status
*/
const updateContact = (_id, contact) => {
Contact.update({ _id }, contact)
.exec((err, status) => {
assert.equal(null, err);
console.info('Updated successfully');
db.disconnect();
});
};
/**
* @function [deleteContact]
* @returns {String} status
*/
const deleteContact = (_id) => {
Contact.remove({ _id })
.exec((err, status) => {
assert.equal(null, err);
console.info('Deleted successfully');
db.disconnect();
})
}
/**
* @function [getContactList]
* @returns [contactlist] contacts
*/
const getContactList = () => {
Contact.find()
.exec((err, contacts) => {
assert.equal(null, err);
console.info(contacts);
console.info(`${contacts.length} matches`);
db.disconnect();
})
}
// Export all methods
module.exports = {
addContact,
getContact,
addMultipleContacts,
getContactList,
updateContact,
deleteContact
};