forked from mrvautin/expressCart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexing.js
140 lines (122 loc) · 4.29 KB
/
indexing.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const colors = require('colors');
const lunr = require('lunr');
const indexProducts = (app) => {
// index all products in lunr on startup
return new Promise((resolve, reject) => {
app.db.products.find({}).toArray((err, productsList) => {
if(err){
console.error(colors.red(err.stack));
reject(err);
}
// setup lunr indexing
const productsIndex = lunr(function(){
this.field('productTitle', { boost: 10 });
this.field('productTags', { boost: 5 });
this.field('productDescription');
const lunrIndex = this;
// add to lunr index
productsList.forEach((product) => {
const doc = {
productTitle: product.productTitle,
productTags: product.productTags,
productDescription: product.productDescription,
id: product._id
};
lunrIndex.add(doc);
});
});
app.productsIndex = productsIndex;
if(process.env.NODE_ENV !== 'test'){
console.log(colors.cyan('- Product indexing complete'));
}
resolve();
});
});
};
const indexCustomers = (app) => {
// index all products in lunr on startup
return new Promise((resolve, reject) => {
app.db.customers.find({}).toArray((err, customerList) => {
if(err){
console.error(colors.red(err.stack));
reject(err);
}
// setup lunr indexing
const customersIndex = lunr(function(){
this.field('email', { boost: 10 });
this.field('name', { boost: 5 });
this.field('phone');
const lunrIndex = this;
// add to lunr index
customerList.forEach((customer) => {
const doc = {
email: customer.email,
name: `${customer.firstName} ${customer.lastName}`,
phone: customer.phone,
id: customer._id
};
lunrIndex.add(doc);
});
});
app.customersIndex = customersIndex;
if(process.env.NODE_ENV !== 'test'){
console.log(colors.cyan('- Customer indexing complete'));
}
resolve();
});
});
};
const indexOrders = (app, cb) => {
// index all orders in lunr on startup
return new Promise((resolve, reject) => {
app.db.orders.find({}).toArray((err, ordersList) => {
if(err){
console.error(colors.red('Error setting up products index: ' + err));
reject(err);
}
// setup lunr indexing
const ordersIndex = lunr(function(){
this.field('orderEmail', { boost: 10 });
this.field('orderLastname', { boost: 5 });
this.field('orderPostcode');
const lunrIndex = this;
// add to lunr index
ordersList.forEach((order) => {
const doc = {
orderLastname: order.orderLastname,
orderEmail: order.orderEmail,
orderPostcode: order.orderPostcode,
id: order._id
};
lunrIndex.add(doc);
});
});
app.ordersIndex = ordersIndex;
if(process.env.NODE_ENV !== 'test'){
console.log(colors.cyan('- Order indexing complete'));
}
resolve();
});
});
};
// start indexing products and orders
const runIndexing = (app) => {
if(process.env.NODE_ENV !== 'test'){
console.info(colors.yellow('Setting up indexes..'));
}
return Promise.all([
indexProducts(app),
indexOrders(app),
indexCustomers(app)
])
.catch((err) => {
console.info(colors.yellow('Error setting up indexes', err));
process.exit(2);
});
};
module.exports = {
indexProducts,
indexCustomers,
indexOrders,
runIndexing
};