0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-02 12:52:38 +00:00

Merge pull request #4436 from G-Ambatte/addMongoIndexes

Add indexes to Mongo schema
This commit is contained in:
Trevor Buckner
2025-10-02 20:48:32 -04:00
committed by GitHub
3 changed files with 33 additions and 17 deletions

View File

@@ -75,8 +75,9 @@ it using the two commands:
1. `npm install` 1. `npm install`
1. `npm start` 1. `npm start`
You should now be able to go to [http://localhost:8000](http://localhost:8000) 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.
in your browser and use The Homebrewery offline.
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: 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 - [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 [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 [gitter-url]: https://gitter.im/naturalcrit/Lobby

View File

@@ -27,7 +27,10 @@ const disconnect = async ()=>{
}; };
const connect = async (config)=>{ const connect = async (config)=>{
return await Mongoose.connect(getMongoDBURL(config), { retryWrites: false }) return await Mongoose.connect(getMongoDBURL(config), {
retryWrites : false,
autoIndex : (config.get('local_environments').includes(config.get('node_env')))
})
.catch((error)=>handleConnectionError(error)); .catch((error)=>handleConnectionError(error));
}; };

View File

@@ -7,29 +7,29 @@ import zlib from 'zlib';
const HomebrewSchema = mongoose.Schema({ const HomebrewSchema = mongoose.Schema({
shareId : { type: String, default: ()=>{return nanoid(12);}, index: { unique: true } }, shareId : { type: String, default: ()=>{return nanoid(12);}, index: { unique: true } },
editId : { type: String, default: ()=>{return nanoid(12);}, index: { unique: true } }, editId : { type: String, default: ()=>{return nanoid(12);}, index: { unique: true } },
googleId : { type: String }, googleId : { type: String, index: true },
title : { type: String, default: '' }, title : { type: String, default: '', index: true },
text : { type: String, default: '' }, text : { type: String, default: '' },
textBin : { type: Buffer }, textBin : { type: Buffer },
pageCount : { type: Number, default: 1 }, pageCount : { type: Number, default: 1, index: true },
description : { type: String, default: '' }, description : { type: String, default: '' },
tags : [String], tags : { type: [String], index: true },
systems : [String], systems : [String],
lang : { type: String, default: 'en' }, lang : { type: String, default: 'en', index: true },
renderer : { type: String, default: '' }, renderer : { type: String, default: '', index: true },
authors : [String], authors : { type: [String], index: true },
invitedAuthors : [String], invitedAuthors : [String],
published : { type: Boolean, default: false }, published : { type: Boolean, default: false, index: true },
thumbnail : { type: String, default: '' }, thumbnail : { type: String, default: '', index: true },
createdAt : { type: Date, default: Date.now }, createdAt : { type: Date, default: Date.now, index: true },
updatedAt : { type: Date, default: Date.now }, updatedAt : { type: Date, default: Date.now, index: true },
lastViewed : { type: Date, default: Date.now }, lastViewed : { type: Date, default: Date.now, index: true },
views : { type: Number, default: 0 }, 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 }); }, { versionKey: false });
HomebrewSchema.statics.increaseView = async function(query) { HomebrewSchema.statics.increaseView = async function(query) {
@@ -43,6 +43,8 @@ HomebrewSchema.statics.increaseView = async function(query) {
return brew; return brew;
}; };
// STATIC FUNCTIONS
HomebrewSchema.statics.get = async function(query, fields=null){ HomebrewSchema.statics.get = async function(query, fields=null){
const brew = await Homebrew.findOne(query, fields).orFail() const brew = await Homebrew.findOne(query, fields).orFail()
.catch((error)=>{throw 'Can not find brew';}); .catch((error)=>{throw 'Can not find brew';});
@@ -63,6 +65,15 @@ HomebrewSchema.statics.getByUser = async function(username, allowAccess=false, f
return brews; 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); const Homebrew = mongoose.model('Homebrew', HomebrewSchema);
export { export {