1

I'm trying to develop an application that would allow organizations to track their inventory and margins for the products/services they sell. I've started writing the back-end using NodeJS/Express/Mongoose.

However, I'm struggling with the idea of how to set up the data model. This would be a subscription-based model so each organization subscribing to my application would need to be a separate database in MongoDB so all data is strictly separated from other customers of the application.

The way I currently started to design the application is:

  1. Allow a user to signup and generate a unique identifier for that user's organization which is stored in a separate 'Master' database in MongoDB that exists for the sole purpose of storing that identifier.
  2. Create a new connection string to the MongoDB database using the unique id from step 1 as the database's name
  3. Once the connection is made with the new database, generate all other records such as user details, etc...

The approach works but it feels kind of amateur and I'm sure there are better ways of doing this. Does anyone have a piece of good advice on how to tackle this problem?

Many thanks in advance!

Regards, Stefano

sgandolfo
  • 15
  • 4
  • you should not use a seperate database for each organization. You can maybe store the organization id in each document to differentiate – Tobias S. Aug 19 '21 at 12:56
  • Hi Tobias, thanks for the feedback. It crossed my mind to apply that strategy but don't you risk blowing up the size of your database, and hence the performance of the application if you have a lot of organizations creating thousands and thousands of records? – sgandolfo Aug 20 '21 at 13:16
  • well it depends on your data size, but with mongoDB having a 100 million documents or more should be no problem – Tobias S. Aug 20 '21 at 13:18
  • Ok, that seems quite ok. I don't think the size will ever amount to more than 100 million docs – sgandolfo Aug 20 '21 at 14:00

1 Answers1

0

//you need to connect through mongodb shell
// example 
//mongodb+srv://<username>:<password>@mongoose.5f5dsdc.mongodb.net/<database>

const express = require("express");

const app=express();
app.use(express.json())

const mongoose= require('mongoose');

//user schema created

const userSchema=new mongoose.Schema({
    email: {type:String,required:true,unique:true},
    password:{type:String,required:true}
}, {
    version_key:false,
    timestamps:true
})

// here dat is the collection name

mongoose.model("dat",userSchema);

//connected to a new database called mainthing
const connect = ()=>{
    return mongoose.connect("mongodb+srv://<username>:<password>@mongoose.5f5dsdc.mongodb.net/mainthing");
}

app.listen(8080,async ()=>{
   try {
    await connect();  
    console.log("connect");  
   } catch (error) {
        console.log({"message": error.message})
   }
})