Fixed indexing
parent
defad4e76f
commit
f0d501817c
105
app.js
105
app.js
|
@ -5,15 +5,14 @@ const cookieParser = require('cookie-parser');
|
|||
const bodyParser = require('body-parser');
|
||||
const session = require('express-session');
|
||||
const bcrypt = require('bcrypt-nodejs');
|
||||
const lunr = require('lunr');
|
||||
const moment = require('moment');
|
||||
const MongoStore = require('connect-mongodb-session')(session);
|
||||
const MongoClient = require('mongodb').MongoClient;
|
||||
const numeral = require('numeral');
|
||||
const helmet = require('helmet');
|
||||
const colors = require('colors');
|
||||
const config = require('./config/settings.json');
|
||||
const common = require('./routes/common');
|
||||
|
||||
let handlebars = require('express-handlebars');
|
||||
|
||||
// require the routes
|
||||
|
@ -155,6 +154,9 @@ handlebars = handlebars.create({
|
|||
}
|
||||
});
|
||||
|
||||
// get config
|
||||
let config = common.getConfig();
|
||||
|
||||
// var session store
|
||||
let store = new MongoStore({
|
||||
uri: config.databaseConnectionString,
|
||||
|
@ -248,7 +250,6 @@ MongoClient.connect(config.databaseConnectionString, {}, (err, client) => {
|
|||
|
||||
// setup the collections
|
||||
db.users = db.collection('users');
|
||||
db.config = db.collection('config');
|
||||
db.products = db.collection('products');
|
||||
db.orders = db.collection('orders');
|
||||
db.pages = db.collection('pages');
|
||||
|
@ -257,105 +258,17 @@ MongoClient.connect(config.databaseConnectionString, {}, (err, client) => {
|
|||
app.db = db;
|
||||
|
||||
// add indexing
|
||||
runIndexing(app, (err) => {
|
||||
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})
|
||||
common.runIndexing(app)
|
||||
.then(() => {
|
||||
// lift the app
|
||||
app.listen(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;
|
||||
|
|
|
@ -128,7 +128,6 @@ router.get('/orders/filter/:search', common.restrict, (req, res, next) => {
|
|||
// order product
|
||||
router.get('/order/delete/:id', common.restrict, (req, res) => {
|
||||
let db = req.app.db;
|
||||
let ordersIndex = req.app.ordersIndex;
|
||||
|
||||
// remove the article
|
||||
db.orders.remove({_id: common.getId(req.params.id)}, {}, (err, numRemoved) => {
|
||||
|
@ -136,14 +135,15 @@ router.get('/order/delete/:id', common.restrict, (req, res) => {
|
|||
console.info(err.stack);
|
||||
}
|
||||
// remove the index
|
||||
ordersIndex.remove({id: req.params.id}, false);
|
||||
|
||||
common.indexOrders(req.app)
|
||||
.then(() => {
|
||||
// redirect home
|
||||
req.session.message = 'Order successfully deleted';
|
||||
req.session.messageType = 'success';
|
||||
res.redirect('/admin/orders');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// update order status
|
||||
router.post('/order/statusupdate', common.restrict, (req, res) => {
|
||||
|
@ -371,7 +371,6 @@ router.get('/product/new', common.restrict, (req, res) => {
|
|||
router.post('/product/insert', common.restrict, (req, res) => {
|
||||
let db = req.app.db;
|
||||
let config = common.getConfig();
|
||||
let productsIndex = req.app.productsIndex;
|
||||
|
||||
let doc = {
|
||||
productPermalink: req.body.frmProductPermalink,
|
||||
|
@ -428,22 +427,15 @@ router.post('/product/insert', common.restrict, (req, res) => {
|
|||
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
|
||||
productsIndex.add(lunrDoc);
|
||||
|
||||
common.indexProducts(req.app)
|
||||
.then(() => {
|
||||
req.session.message = 'New product successfully created';
|
||||
req.session.messageType = 'success';
|
||||
|
||||
// redirect to new doc
|
||||
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
|
||||
router.post('/product/update', common.restrict, (req, res) => {
|
||||
let db = req.app.db;
|
||||
let productsIndex = req.app.productsIndex;
|
||||
|
||||
db.products.findOne({_id: common.getId(req.body.frmProductId)}, (err, product) => {
|
||||
if(err){
|
||||
|
@ -547,20 +538,13 @@ router.post('/product/update', common.restrict, (req, res) => {
|
|||
req.session.messageType = 'danger';
|
||||
res.redirect('/admin/product/edit/' + req.body.frmProductId);
|
||||
}else{
|
||||
// create lunr doc
|
||||
let lunrDoc = {
|
||||
productTitle: req.body.frmProductTitle,
|
||||
productTags: req.body.frmProductTags,
|
||||
productDescription: req.body.frmProductDescription,
|
||||
id: req.body.frmProductId
|
||||
};
|
||||
|
||||
// update the index
|
||||
productsIndex.update(lunrDoc, false);
|
||||
|
||||
// Update the index
|
||||
common.indexProducts(req.app)
|
||||
.then(() => {
|
||||
req.session.message = 'Successfully saved';
|
||||
req.session.messageType = 'success';
|
||||
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) => {
|
||||
let db = req.app.db;
|
||||
let rimraf = require('rimraf');
|
||||
let productsIndex = req.app.productsIndex;
|
||||
|
||||
// remove the article
|
||||
db.products.remove({_id: common.getId(req.params.id)}, {}, (err, numRemoved) => {
|
||||
|
@ -585,17 +568,10 @@ router.get('/product/delete/:id', common.restrict, (req, res) => {
|
|||
if(err){
|
||||
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
|
||||
productsIndex.remove(lunrDoc, false);
|
||||
|
||||
common.indexProducts(req.app)
|
||||
.then(() => {
|
||||
// redirect home
|
||||
req.session.message = 'Product successfully deleted';
|
||||
req.session.messageType = 'success';
|
||||
|
@ -603,6 +579,7 @@ router.get('/product/delete/:id', common.restrict, (req, res) => {
|
|||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// users
|
||||
router.get('/users', common.restrict, (req, res) => {
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
let _ = require('lodash');
|
||||
let uglifycss = require('uglifycss');
|
||||
let colors = require('colors');
|
||||
let escape = require('html-entities').AllHtmlEntities;
|
||||
const _ = require('lodash');
|
||||
const uglifycss = require('uglifycss');
|
||||
const colors = require('colors');
|
||||
const lunr = require('lunr');
|
||||
const escape = require('html-entities').AllHtmlEntities;
|
||||
|
||||
// common functions
|
||||
exports.checkLogin = function(req, res, next){
|
||||
|
@ -144,8 +145,6 @@ exports.getConfig = function(){
|
|||
let fs = require('fs');
|
||||
let path = require('path');
|
||||
|
||||
console.log('getting config');
|
||||
|
||||
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.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);
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue