-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathindex.js
68 lines (60 loc) · 1.44 KB
/
index.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
const mongoose = require('mongoose');
// Map global promise - get rid of warning
mongoose.Promise = global.Promise;
// Connect to db
const db = mongoose.connect('mongodb://localhost:27017/customercli', {
useMongoClient: true
});
// Import model
const Customer = require('./models/customer');
// Add Customer
const addCustomer = (customer) => {
Customer.create(customer).then(customer => {
console.info('New Customer Added');
db.close();
});
}
// Find Customer
const findCustomer = (name) => {
// Make case insensitive
const search = new RegExp(name, 'i');
Customer.find({$or: [{firstname: search}, {lastname: search}]})
.then(customer => {
console.info(customer);
console.info(`${customer.length} matches`);
db.close();
});
}
// Update Customer
const updateCustomer = (_id, customer) => {
Customer.update({ _id }, customer)
.then(customer => {
console.info('Customer Updated');
db.close();
});
}
// Remove Customer
const removeCustomer = (_id) => {
Customer.remove({ _id })
.then(customer => {
console.info('Customer Removed');
db.close();
});
}
// List Customers
const listCustomers = () => {
Customer.find()
.then(customers => {
console.info(customers);
console.info(`${customers.length} customers`);
db.close();
});
}
// Export All Methods
module.exports = {
addCustomer,
findCustomer,
updateCustomer,
removeCustomer,
listCustomers
}