Fixed indexing

react_convert
Mark Moffat 2018-01-06 21:07:10 +01:00
parent defad4e76f
commit f0d501817c
3 changed files with 131 additions and 157 deletions

105
app.js
View File

@ -5,15 +5,14 @@ const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const session = require('express-session'); const session = require('express-session');
const bcrypt = require('bcrypt-nodejs'); const bcrypt = require('bcrypt-nodejs');
const lunr = require('lunr');
const moment = require('moment'); const moment = require('moment');
const MongoStore = require('connect-mongodb-session')(session); const MongoStore = require('connect-mongodb-session')(session);
const MongoClient = require('mongodb').MongoClient; const MongoClient = require('mongodb').MongoClient;
const numeral = require('numeral'); const numeral = require('numeral');
const helmet = require('helmet'); const helmet = require('helmet');
const colors = require('colors'); const colors = require('colors');
const config = require('./config/settings.json');
const common = require('./routes/common'); const common = require('./routes/common');
let handlebars = require('express-handlebars'); let handlebars = require('express-handlebars');
// require the routes // require the routes
@ -155,6 +154,9 @@ handlebars = handlebars.create({
} }
}); });
// get config
let config = common.getConfig();
// var session store // var session store
let store = new MongoStore({ let store = new MongoStore({
uri: config.databaseConnectionString, uri: config.databaseConnectionString,
@ -248,7 +250,6 @@ MongoClient.connect(config.databaseConnectionString, {}, (err, client) => {
// setup the collections // setup the collections
db.users = db.collection('users'); db.users = db.collection('users');
db.config = db.collection('config');
db.products = db.collection('products'); db.products = db.collection('products');
db.orders = db.collection('orders'); db.orders = db.collection('orders');
db.pages = db.collection('pages'); db.pages = db.collection('pages');
@ -257,105 +258,17 @@ MongoClient.connect(config.databaseConnectionString, {}, (err, client) => {
app.db = db; app.db = db;
// add indexing // add indexing
runIndexing(app, (err) => { common.runIndexing(app)
if(err){
console.error(colors.red('Error setting up indexes:' + err));
process.exit();
}
// Loads the config file into the DB
db.config.update({}, config, {upsert: true})
.then(() => { .then(() => {
// lift the app // lift the app
app.listen(app.get('port'), () => { app.listen(app.get('port'), () => {
console.log(colors.green('expressCart running on host: http://localhost:' + app.get('port'))); console.log(colors.green('expressCart running on host: http://localhost:' + app.get('port')));
}); });
}); })
.catch(() => {
console.error(colors.red('Error setting up indexes:' + err));
process.exit();
}); });
}); });
function indexProducts(app, cb){
// index all products in lunr on startup
common.dbQuery(app.db.products, {}, null, null, (err, productsList) => {
if(err){
console.error(colors.red(err.stack));
}
// 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) => {
let doc = {
'productTitle': product.productTitle,
'productTags': product.productTags,
'productDescription': product.productDescription,
'id': product._id
};
lunrIndex.add(doc);
});
});
app.productsIndex = productsIndex;
cb(null);
});
}
function indexOrders(app, cb){
// index all orders in lunr on startup
common.dbQuery(app.db.orders, {}, null, null, (err, ordersList) => {
if(err){
console.error(colors.red(err.stack));
}
// 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) => {
let doc = {
'orderLastname': order.orderLastname,
'orderEmail': order.orderEmail,
'orderPostcode': order.orderPostcode,
'id': order._id
};
lunrIndex.add(doc);
});
});
app.ordersIndex = ordersIndex;
cb(null);
});
}
// start indexing products and orders
function runIndexing(app, cb){
console.info(colors.yellow('Setting up indexes..'));
indexProducts(app, (err) => {
if(err){
console.error(colors.red('Error setting up products index: ' + err));
cb(err);
}
console.log(colors.cyan('- Product indexing complete'));
indexOrders(app, (err) => {
if(err){
console.error(colors.red('Error setting up products index: ' + err));
cb(err);
}
console.log(colors.cyan('- Order indexing complete'));
cb(null);
});
});
}
module.exports = app; module.exports = app;

View File

@ -128,7 +128,6 @@ router.get('/orders/filter/:search', common.restrict, (req, res, next) => {
// order product // order product
router.get('/order/delete/:id', common.restrict, (req, res) => { router.get('/order/delete/:id', common.restrict, (req, res) => {
let db = req.app.db; let db = req.app.db;
let ordersIndex = req.app.ordersIndex;
// remove the article // remove the article
db.orders.remove({_id: common.getId(req.params.id)}, {}, (err, numRemoved) => { db.orders.remove({_id: common.getId(req.params.id)}, {}, (err, numRemoved) => {
@ -136,13 +135,14 @@ router.get('/order/delete/:id', common.restrict, (req, res) => {
console.info(err.stack); console.info(err.stack);
} }
// remove the index // remove the index
ordersIndex.remove({id: req.params.id}, false); common.indexOrders(req.app)
.then(() => {
// redirect home // redirect home
req.session.message = 'Order successfully deleted'; req.session.message = 'Order successfully deleted';
req.session.messageType = 'success'; req.session.messageType = 'success';
res.redirect('/admin/orders'); res.redirect('/admin/orders');
}); });
});
}); });
// update order status // update order status
@ -371,7 +371,6 @@ router.get('/product/new', common.restrict, (req, res) => {
router.post('/product/insert', common.restrict, (req, res) => { router.post('/product/insert', common.restrict, (req, res) => {
let db = req.app.db; let db = req.app.db;
let config = common.getConfig(); let config = common.getConfig();
let productsIndex = req.app.productsIndex;
let doc = { let doc = {
productPermalink: req.body.frmProductPermalink, productPermalink: req.body.frmProductPermalink,
@ -428,22 +427,15 @@ router.post('/product/insert', common.restrict, (req, res) => {
newId = newDoc.insertedIds; newId = newDoc.insertedIds;
} }
// create lunr doc
let lunrDoc = {
productTitle: req.body.frmProductTitle,
productTags: req.body.frmProductTags,
productDescription: req.body.frmProductDescription,
id: newId
};
// add to lunr index // add to lunr index
productsIndex.add(lunrDoc); common.indexProducts(req.app)
.then(() => {
req.session.message = 'New product successfully created'; req.session.message = 'New product successfully created';
req.session.messageType = 'success'; req.session.messageType = 'success';
// redirect to new doc // redirect to new doc
res.redirect('/admin/product/edit/' + newId); res.redirect('/admin/product/edit/' + newId);
});
} }
}); });
} }
@ -484,7 +476,6 @@ router.get('/product/edit/:id', common.restrict, (req, res) => {
// Update an existing product form action // Update an existing product form action
router.post('/product/update', common.restrict, (req, res) => { router.post('/product/update', common.restrict, (req, res) => {
let db = req.app.db; let db = req.app.db;
let productsIndex = req.app.productsIndex;
db.products.findOne({_id: common.getId(req.body.frmProductId)}, (err, product) => { db.products.findOne({_id: common.getId(req.body.frmProductId)}, (err, product) => {
if(err){ if(err){
@ -547,20 +538,13 @@ router.post('/product/update', common.restrict, (req, res) => {
req.session.messageType = 'danger'; req.session.messageType = 'danger';
res.redirect('/admin/product/edit/' + req.body.frmProductId); res.redirect('/admin/product/edit/' + req.body.frmProductId);
}else{ }else{
// create lunr doc // Update the index
let lunrDoc = { common.indexProducts(req.app)
productTitle: req.body.frmProductTitle, .then(() => {
productTags: req.body.frmProductTags,
productDescription: req.body.frmProductDescription,
id: req.body.frmProductId
};
// update the index
productsIndex.update(lunrDoc, false);
req.session.message = 'Successfully saved'; req.session.message = 'Successfully saved';
req.session.messageType = 'success'; req.session.messageType = 'success';
res.redirect('/admin/product/edit/' + req.body.frmProductId); res.redirect('/admin/product/edit/' + req.body.frmProductId);
});
} }
}); });
}); });
@ -573,7 +557,6 @@ router.post('/product/update', common.restrict, (req, res) => {
router.get('/product/delete/:id', common.restrict, (req, res) => { router.get('/product/delete/:id', common.restrict, (req, res) => {
let db = req.app.db; let db = req.app.db;
let rimraf = require('rimraf'); let rimraf = require('rimraf');
let productsIndex = req.app.productsIndex;
// remove the article // remove the article
db.products.remove({_id: common.getId(req.params.id)}, {}, (err, numRemoved) => { db.products.remove({_id: common.getId(req.params.id)}, {}, (err, numRemoved) => {
@ -585,23 +568,17 @@ router.get('/product/delete/:id', common.restrict, (req, res) => {
if(err){ if(err){
console.info(err.stack); console.info(err.stack);
} }
// create lunr doc
let lunrDoc = {
productTitle: req.body.frmProductTitle,
productTags: req.body.frmProductTags,
productDescription: req.body.frmProductDescription,
id: req.body.frmProductId
};
// remove the index // remove the index
productsIndex.remove(lunrDoc, false); common.indexProducts(req.app)
.then(() => {
// redirect home // redirect home
req.session.message = 'Product successfully deleted'; req.session.message = 'Product successfully deleted';
req.session.messageType = 'success'; req.session.messageType = 'success';
res.redirect('/admin/products'); res.redirect('/admin/products');
}); });
}); });
});
}); });
// users // users

View File

@ -1,7 +1,8 @@
let _ = require('lodash'); const _ = require('lodash');
let uglifycss = require('uglifycss'); const uglifycss = require('uglifycss');
let colors = require('colors'); const colors = require('colors');
let escape = require('html-entities').AllHtmlEntities; const lunr = require('lunr');
const escape = require('html-entities').AllHtmlEntities;
// common functions // common functions
exports.checkLogin = function(req, res, next){ exports.checkLogin = function(req, res, next){
@ -144,8 +145,6 @@ exports.getConfig = function(){
let fs = require('fs'); let fs = require('fs');
let path = require('path'); let path = require('path');
console.log('getting config');
let config = JSON.parse(fs.readFileSync(path.join(__dirname, '../config', 'settings.json'), 'utf8')); let config = JSON.parse(fs.readFileSync(path.join(__dirname, '../config', 'settings.json'), 'utf8'));
config.customCss = typeof config.customCss !== 'undefined' ? escape.decode(config.customCss) : null; config.customCss = typeof config.customCss !== 'undefined' ? escape.decode(config.customCss) : null;
config.footerHtml = typeof config.footerHtml !== 'undefined' ? escape.decode(config.footerHtml) : null; config.footerHtml = typeof config.footerHtml !== 'undefined' ? escape.decode(config.footerHtml) : null;
@ -413,3 +412,88 @@ exports.dbQuery = function(db, query, sort, limit, callback){
}); });
} }
}; };
exports.indexProducts = (app) => {
// index all products in lunr on startup
return new Promise((resolve, reject) => {
exports.dbQuery(app.db.products, {}, null, null, (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) => {
let doc = {
'productTitle': product.productTitle,
'productTags': product.productTags,
'productDescription': product.productDescription,
'id': product._id
};
lunrIndex.add(doc);
});
});
app.productsIndex = productsIndex;
console.log(colors.cyan('- Product indexing complete'));
resolve();
});
});
};
exports.indexOrders = (app, cb) => {
// index all orders in lunr on startup
return new Promise((resolve, reject) => {
exports.dbQuery(app.db.orders, {}, null, null, (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) => {
let doc = {
'orderLastname': order.orderLastname,
'orderEmail': order.orderEmail,
'orderPostcode': order.orderPostcode,
'id': order._id
};
lunrIndex.add(doc);
});
});
app.ordersIndex = ordersIndex;
console.log(colors.cyan('- Order indexing complete'));
resolve();
});
});
};
// start indexing products and orders
exports.runIndexing = (app) => {
console.info(colors.yellow('Setting up indexes..'));
return Promise.all([
exports.indexProducts(app),
exports.indexOrders(app)
])
.catch((err) => {
process.exit(2);
});
};