Closed noghartt closed 11 months ago
can we be able to define the collection name and the database name in the SchemaModel ?
Schema.collectionName = 'ok': Schema.databaseName = 'another-database';
I typically end up with a function that I use to create a connection and attach models. That's usually the way to go when you have multiple connections. Something like the following:
const connectionString = process.env.MONGODB_CONNECTION_STRING;
console.log('Connecting to', connectionString);
module.exports = function models(connection) {
connection = connection ?? mongoose.createConnection(connectionString, {
serverSelectionTimeoutMS: 5000
});
initTask(null, connection);
for (const [schemaName, schema] of Object.entries(schemas)) {
// "accessTokenSchema" -> "AccessToken"
const modelName = schemaName.charAt(0).toUpperCase() +
schemaName.replace(/Schema$/, '').slice(1);
connection.model(modelName, schema, modelName);
}
return connection;
};
Does that help?
if have something like this
export const getConnection = (connectionName: string) => {
const connection = mongoose.connections.find(
(c) => c.name === connectionName,
);
if (!connection) {
return mongoose;
}
return connection;
};
export const getModelBySchema = (schema: SchemaMultiDB) => {
const connection = getConnection(schema.databaseName);
return connection.model(schema.name, schema);
};
the usage is
const Event = getModelBySchema(EventSchema);
We would like to keep using just EventModel
instead of having a lazy function to get the model from a custom connection
@sibelius that patterns works as well. It's not my favorite pattern, but it's the only real option if you have multiple connections and want to be able to import models via require()
/ import
.
Prerequisites
Mongoose version
7.6.3
Node.js version
18.16.0
MongoDB version
5.8.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
14.1.0
Issue
I have a monorepo where I handle 2+ connections accordingly to the entrypoints.
In case, I have a specific flow where a model registered via
mongoose.model
runs before the connection to the DB.If a try to do something like:
connection.model('Model', ModelSchema)
, it throws an error becauseconnection
is undefined until connnect.I would like to know if there's a way/strategy to handle this kind of "model registry" in a multi DB architecture allowing this lazy code evaluation.