so I recently wanted to be able to apply changes to my scripts without needing to restart it, I've mostly got it all working but one issue I'm having is getting my MongoDB models to refresh too, I tried many ways but none worked. The current way I am refreshing my code is by fetching one of my API points to trigger the refresh Models script, it says the models have been refreshed but when i try to check it, it wouldn't show me the things I added
My current code to refresh the models:
const mongoose = require('mongoose');
const fs = require('fs');
const path = require('path');
function refreshModels() {
try {
mongoose.models = {};
mongoose.modelSchemas = {};
const modelsPath = path.join(__dirname, '../models');
const modelFiles = fs.readdirSync(modelsPath).filter(file => file.endsWith('.js'));
for (const file of modelFiles) {
const filePath = path.join(modelsPath, file);
const model = require(filePath);
const modelName = path.basename(file, '.js');
mongoose.model(modelName.charAt(0).toUpperCase() + modelName.slice(1), model.schema);
}
console.log('Models have been refreshed');
} catch (error) {
console.log('Error refreshing models:', error);
}
}
module.exports = refreshModels;
so I recently wanted to be able to apply changes to my scripts without needing to restart it, I've mostly got it all working but one issue I'm having is getting my MongoDB models to refresh too, I tried many ways but none worked. The current way I am refreshing my code is by fetching one of my API points to trigger the refresh Models script, it says the models have been refreshed but when i try to check it, it wouldn't show me the things I added
My current code to refresh the models:
const mongoose = require('mongoose');
const fs = require('fs');
const path = require('path');
function refreshModels() {
try {
mongoose.models = {};
mongoose.modelSchemas = {};
const modelsPath = path.join(__dirname, '../models');
const modelFiles = fs.readdirSync(modelsPath).filter(file => file.endsWith('.js'));
for (const file of modelFiles) {
const filePath = path.join(modelsPath, file);
const model = require(filePath);
const modelName = path.basename(file, '.js');
mongoose.model(modelName.charAt(0).toUpperCase() + modelName.slice(1), model.schema);
}
console.log('Models have been refreshed');
} catch (error) {
console.log('Error refreshing models:', error);
}
}
module.exports = refreshModels;
And here is my model i am testing, the test part is added before refreshing
const { Schema, model } = require('mongoose');
const Alerts = new Schema({
guildId: {
type: String,
required: true,
},
channelId: {
type: String,
required: true,
},
test: {
type: String,
required: true,
},
});
module.exports = model('Alerts', Alerts);
does anyone know how i could possibly get this to work? or should i use something like MySQL but I would rather not transfer all the data over.
Share Improve this question asked Mar 3 at 17:42 2milliongunguy2milliongunguy 212 bronze badges1 Answer
Reset to default 1From what I saw, so your problem is that you've modified your Mongoose models and want to apply these changes in a running Node.js application without restarting the server. Simply clearing mongoose.models
and mongoose.modelSchemas
doesn't seem to work.
The problem comes when Mongoose
internally caches compiled models. After a model is used, it might continue using the cached version, even after clearing the model registry. And also Node.js stores the result in its module cache. Subsequent require
calls return the cached version, not a fresh copy. This means your require(filePath)
might not be loading the updated model file.
The solution can be you need to clean the Node.js module cache for your model files before re-requiring them. This will help you to load the latest version of the model. And for the moongose model handling you need to ensure that when a model is used, that it is used from the mongoose.models
object, and not from a locally stored variable.
You can try this, clear both the Mongoose model cache and the Node.js module cache for your model files.
const mongoose = require('mongoose');
const fs = require('fs');
const path = require('path');
function refreshModels() {
try {
const modelsPath = path.join(__dirname, '../models');
const modelFiles = fs.readdirSync(modelsPath).filter(file => file.endsWith('.js'));
// Clear Mongoose model cache
for (const modelName in mongoose.models) {
delete mongoose.models[modelName];
}
mongoose.modelSchemas = {};
// Clear Node.js module cache
modelFiles.forEach(file => {
const filePath = path.join(modelsPath, file);
const resolvedPath = require.resolve(filePath);
delete require.cache[resolvedPath];
});
// Re-register models
modelFiles.forEach(file => {
const filePath = path.join(modelsPath, file);
const model = require(filePath);
const modelName = path.basename(file, '.js');
mongoose.model(modelName.charAt(0).toUpperCase() + modelName.slice(1), model.schema);
});
console.log('Models have been refreshed');
} catch (error) {
console.error('Error refreshing models:', error);
}
}
module.exports = refreshModels;