mirror of
https://github.com/stolksdorf/homebrewery.git
synced 2025-12-22 23:31:31 +00:00
Adding a new script from populating the DB with a bunch of random brews
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
"quick": "node scripts/quick.js",
|
"quick": "node scripts/quick.js",
|
||||||
"build": "node scripts/build.js",
|
"build": "node scripts/build.js",
|
||||||
"phb": "node scripts/phb.js",
|
"phb": "node scripts/phb.js",
|
||||||
|
"genbrews": "node scripts/genbrews.js",
|
||||||
"prod": "set NODE_ENV=production&& npm run build",
|
"prod": "set NODE_ENV=production&& npm run build",
|
||||||
"postinstall": "npm run build",
|
"postinstall": "npm run build",
|
||||||
"start": "node server.js",
|
"start": "node server.js",
|
||||||
|
|||||||
51
scripts/genbrews.js
Normal file
51
scripts/genbrews.js
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
//Populates the DB with a bunch of brews for UI testing
|
||||||
|
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
|
const DB = require('../server/db.js');
|
||||||
|
const BrewData = require('../server/brew.data.js');
|
||||||
|
|
||||||
|
|
||||||
|
//TODO: pull in snippets and randomly add them
|
||||||
|
|
||||||
|
const genBrew = () => {
|
||||||
|
return {
|
||||||
|
title : 'BrewA',
|
||||||
|
description : '',
|
||||||
|
text : '',
|
||||||
|
authors : _.sampleSize(['userA','userB','userC','userD'], _.random(0, 3)),
|
||||||
|
systems : _.sampleSize(['5e', '4e', '3.5e', 'Pathfinder'], _.random(0,2)),
|
||||||
|
views : _.random(0,1000),
|
||||||
|
published : !!_.random(0,1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const randomBrews = _.times(20, genBrew);
|
||||||
|
|
||||||
|
const specificBrews = [
|
||||||
|
{
|
||||||
|
text : 'Cool test',
|
||||||
|
authors : ['test']
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
return Promise.resolve()
|
||||||
|
.then(DB.connect)
|
||||||
|
.then(BrewData.removeAll)
|
||||||
|
.then(() => {
|
||||||
|
console.log('Adding random brews...');
|
||||||
|
return Promise.all(_.map(randomBrews, (brew) => {
|
||||||
|
return BrewData.create(brew);
|
||||||
|
}));
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
console.log('Adding specific brews...');
|
||||||
|
return Promise.all(_.map(specificBrews, (brew) => {
|
||||||
|
return BrewData.create(brew);
|
||||||
|
}));
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
console.log(`\n Added ${randomBrews.length + specificBrews.length} brews.`);
|
||||||
|
})
|
||||||
|
.catch(console.error);
|
||||||
@@ -33,6 +33,7 @@ const BrewSchema = mongoose.Schema({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Index these fields for fast text searching
|
||||||
BrewSchema.index({ title: "text", description: "text" });
|
BrewSchema.index({ title: "text", description: "text" });
|
||||||
|
|
||||||
BrewSchema.methods.increaseView = function(){
|
BrewSchema.methods.increaseView = function(){
|
||||||
@@ -42,9 +43,6 @@ BrewSchema.methods.increaseView = function(){
|
|||||||
|
|
||||||
|
|
||||||
const Brew = mongoose.model('Brew', BrewSchema);
|
const Brew = mongoose.model('Brew', BrewSchema);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const BrewData = {
|
const BrewData = {
|
||||||
schema : BrewSchema,
|
schema : BrewSchema,
|
||||||
model : Brew,
|
model : Brew,
|
||||||
@@ -96,6 +94,12 @@ const BrewData = {
|
|||||||
return BrewData.get({ editId });
|
return BrewData.get({ editId });
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
//TODO: Add a 'core search' which just takes a search object
|
||||||
|
//TODO: extend the core search with a user search and a term search
|
||||||
|
//TODO: break these functions off into a 'brew.search.js' file
|
||||||
|
//TODO: pagniation, sorting and full access should be an 'opts' param
|
||||||
|
|
||||||
search : (searchTerms, pagination, sorting, fullAccess = true) => {
|
search : (searchTerms, pagination, sorting, fullAccess = true) => {
|
||||||
let query = {};
|
let query = {};
|
||||||
if(searchTerms){
|
if(searchTerms){
|
||||||
|
|||||||
@@ -8,14 +8,14 @@ module.exports = {
|
|||||||
connect : ()=>{
|
connect : ()=>{
|
||||||
return new Promise((resolve, reject)=>{
|
return new Promise((resolve, reject)=>{
|
||||||
if(mongoose.connection.readyState == 1){
|
if(mongoose.connection.readyState == 1){
|
||||||
log.info('DB already connected');
|
log.warn('DB already connected');
|
||||||
return resolve();
|
return resolve();
|
||||||
}
|
}
|
||||||
mongoose.connect(dbPath,
|
mongoose.connect(dbPath,
|
||||||
(err) => {
|
(err) => {
|
||||||
if(err){
|
if(err){
|
||||||
log.info('Error : Could not connect to a Mongo Database.');
|
log.error('Error : Could not connect to a Mongo Database.');
|
||||||
log.info(' If you are running locally, make sure mongodb.exe is running.');
|
log.error(' If you are running locally, make sure mongodb.exe is running.');
|
||||||
return reject(err);
|
return reject(err);
|
||||||
}
|
}
|
||||||
log.info('DB connected.');
|
log.info('DB connected.');
|
||||||
|
|||||||
@@ -76,4 +76,7 @@
|
|||||||
font-size: 0.8em;
|
font-size: 0.8em;
|
||||||
line-height : 1.5em;
|
line-height : 1.5em;
|
||||||
}
|
}
|
||||||
|
.thumbnail.field{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user