From 878ea1449db719d7abe413b3c3b062cd4b2bba51 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sun, 20 Jul 2025 11:42:24 +1200 Subject: [PATCH 1/4] Add indexes to HomebrewSchema --- server/homebrew.model.js | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/server/homebrew.model.js b/server/homebrew.model.js index 2e74b1de2..ff371ee42 100644 --- a/server/homebrew.model.js +++ b/server/homebrew.model.js @@ -7,29 +7,29 @@ import zlib from 'zlib'; const HomebrewSchema = mongoose.Schema({ shareId : { type: String, default: ()=>{return nanoid(12);}, index: { unique: true } }, editId : { type: String, default: ()=>{return nanoid(12);}, index: { unique: true } }, - googleId : { type: String }, - title : { type: String, default: '' }, + googleId : { type: String, index: true }, + title : { type: String, default: '', index: true }, text : { type: String, default: '' }, textBin : { type: Buffer }, - pageCount : { type: Number, default: 1 }, + pageCount : { type: Number, default: 1, index: true }, description : { type: String, default: '' }, - tags : [String], + tags : { type: [String], index: true }, systems : [String], - lang : { type: String, default: 'en' }, - renderer : { type: String, default: '' }, - authors : [String], + lang : { type: String, default: 'en', index: true }, + renderer : { type: String, default: '', index: true }, + authors : { type: [String], index: true }, invitedAuthors : [String], - published : { type: Boolean, default: false }, - thumbnail : { type: String, default: '' }, + published : { type: Boolean, default: false, index: true }, + thumbnail : { type: String, default: '', index: true }, - createdAt : { type: Date, default: Date.now }, - updatedAt : { type: Date, default: Date.now }, - lastViewed : { type: Date, default: Date.now }, + createdAt : { type: Date, default: Date.now, index: true }, + updatedAt : { type: Date, default: Date.now, index: true }, + lastViewed : { type: Date, default: Date.now, index: true }, views : { type: Number, default: 0 }, - version : { type: Number, default: 1 }, + version : { type: Number, default: 1, index: true }, - lock : { type: Object } + lock : { type: Object, index: true } }, { versionKey: false }); HomebrewSchema.statics.increaseView = async function(query) { @@ -43,6 +43,8 @@ HomebrewSchema.statics.increaseView = async function(query) { return brew; }; +// STATIC FUNCTIONS + HomebrewSchema.statics.get = async function(query, fields=null){ const brew = await Homebrew.findOne(query, fields).orFail() .catch((error)=>{throw 'Can not find brew';}); @@ -63,6 +65,15 @@ HomebrewSchema.statics.getByUser = async function(username, allowAccess=false, f return brews; }; +// INDEXES + +HomebrewSchema.index({ updatedAt: -1, lastViewed: -1 }); +HomebrewSchema.index({ published: 1, title: 'text' }); + +HomebrewSchema.index({ lock: 1, sparse: true }); +HomebrewSchema.path('lock.reviewRequested').index({ sparse: true }); + + const Homebrew = mongoose.model('Homebrew', HomebrewSchema); export { From d9cd270f3b993e02cb4a76bfd867d70247991989 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sun, 20 Jul 2025 11:43:14 +1200 Subject: [PATCH 2/4] Add autobuild to Mongoose connection (local/docker ONLY) --- server/db.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/db.js b/server/db.js index 97da56a08..f4e7fd851 100644 --- a/server/db.js +++ b/server/db.js @@ -27,7 +27,10 @@ const disconnect = async ()=>{ }; const connect = async (config)=>{ - return await Mongoose.connect(getMongoDBURL(config), { retryWrites: false }) + return await Mongoose.connect(getMongoDBURL(config), { + retryWrites : false, + autoIndex : (config.get('node_env') == 'local' || config.get('node_env') == 'docker') + }) .catch((error)=>handleConnectionError(error)); }; From 4856c803ed6102d93397be2ba544da0d30eb6694 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sun, 20 Jul 2025 11:48:22 +1200 Subject: [PATCH 3/4] Use local_environments from config --- server/db.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/db.js b/server/db.js index f4e7fd851..196eba650 100644 --- a/server/db.js +++ b/server/db.js @@ -29,7 +29,7 @@ const disconnect = async ()=>{ const connect = async (config)=>{ return await Mongoose.connect(getMongoDBURL(config), { retryWrites : false, - autoIndex : (config.get('node_env') == 'local' || config.get('node_env') == 'docker') + autoIndex : (config.get('local_environments').includes(config.get('node_env'))) }) .catch((error)=>handleConnectionError(error)); }; From f22f7196caf2448561a2eabda84d9debb70a8405 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Fri, 3 Oct 2025 13:41:53 +1300 Subject: [PATCH 4/4] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5206f4cbf..a33f2f073 100644 --- a/README.md +++ b/README.md @@ -75,8 +75,9 @@ it using the two commands: 1. `npm install` 1. `npm start` -You should now be able to go to [http://localhost:8000](http://localhost:8000) -in your browser and use The Homebrewery offline. +When the Homebrewery server is started for the first time, it will modify the database to create the indexes required for better Homebrewery performance. This may take a few moments to complete for each index, dependent on how much content is in your local database - a brand new, empty database should be done in seconds. + +On completion, you should be able to go to [http://localhost:8000](http://localhost:8000) in your browser and use The Homebrewery offline. If you had any issue at all, here are some links that may be useful: - [Course](https://learn.mongodb.com/courses/m103-basic-cluster-administration) on cluster administration, useful for beginners @@ -145,3 +146,4 @@ your contribution to the project, please join our [gitter chat][gitter-url]. [github-pr-docs-url]: https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request [gitter-url]: https://gitter.im/naturalcrit/Lobby +