DEV Community

Cover image for Multidatabase with mongoose
Sibelius Seraphini for Woovi

Posted on

2

Multidatabase with mongoose

Introduction

At Woovi we always focus on the simplest software architecture that can support our product requirements.
Although we started using one database to support all our features, we decided to move some collections of logs to another database to reduce the load from the core database.

Adding support to multi databases

We had only a MONGO_URI environment variable.
We decided to create one environment variable for each other database we are going to add. We are following the pattern MONGO_URI_<>.

After adding the new env, we need to create a connection to this database:

// default connection
await mongoose.connect(config.MONGO_URI, options);

// other connections
await mongoose.createConnection(config.MONGO_URI_LOGS)
.asPromise();
Enter fullscreen mode Exit fullscreen mode

Mongoose has a concept of connections, and it has a default connection. The default connection will be used for all models and schemas that you define.
The other connections need to be explicitly used.

Working with different databases

First, we need to define our collection schema:

export const LogSchema = new mongoose.Schema(
  {
    metadata: {
      type: mongoose.SchemaTypes.Mixed,
    },
  },
  {
    collection: 'Log',
    timestamps: true,
  },
);

LogSchema.name = 'Log';
LogSchema.databaseName = 'woovi-log';

const LogModel: Model<ILog> = mongoose.model('Log', LogSchema);

export default LogModel;
Enter fullscreen mode Exit fullscreen mode

In the code above, we define the LogSchema, and the LogModel.
The LogModel is the LogSchema in the context of the default connection
To query Log collection from the default connection, we would do:

const logs = await LogModel.find();
Enter fullscreen mode Exit fullscreen mode

To query logs from the woovi-logs database, we need a bit more work

const Log = getModelBySchema(LogSchema);

const logs = await Log.find();
Enter fullscreen mode Exit fullscreen mode

We first need to create a model from the schema.
Let's see how getModelBySchema is implemented:

export const getConnection = (connectionName: string) => {
  const connection = mongoose.connections.find(
    (c) => c.name === connectionName,
  );

  if (!connection) {
    return mongoose;
  }

  return connection;
};

export const getModelBySchema = (schema) => {
  const connection = getConnection(schema.databaseName);

  return connection.model(schema.name, schema);
};
Enter fullscreen mode Exit fullscreen mode

We first get the connection by name from mongoose.connections array, then we call connection.model to create a model from the schema.

To Conclude

As you scale you are going to need to break your database into more databases.
Creating the right abstraction can make this simple.

How many databases do you have in your product?


Woovi
Woovi is a Startup that enables shoppers to pay as they like. Woovi provides instant payment solutions for merchants to accept orders to make this possible.

If you want to work with us, we are hiring!


Image By rorozoa

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay