0

This is my user schema :

import mongoose from "mongoose";
const { Schema } = mongoose;

//Address Schema
const addressSchema: mongoose.Schema = new Schema({});

//Talk about token saving planning
const userSchema: mongoose.Schema = new Schema(
  {
    name: {
      type: String,
      trim: true,
      required: true,
    },
    username: {
      type: String,
      trim: true,
    },
    email: {
      type: String,
      trim: true,
      required: true,
      unique: true,
    },
    mobile_number: {
      type: Number,
      trim: true,
      required: true,
      unique: true,
    },
    password: {
      type: String,
      required: true,
      min: 9,
      max: 30,
    },
    picture: {
      //AWS return object as response after upload
      type: {},
      //we will not provide any default
      //default will be set in frontend
    },
    
    
    
  },

  { timestamps: true }
);

export default mongoose.model("User", userSchema);


Is there any way or anyone know how to create new database when newuser register in Nodejs & Express mongoose.

I am making a crm , I want to create separate database for every user who register.

  • Please see this first (comments as well): https://stackoverflow.com/questions/47066110/separate-database-for-each-user-using-mongodb-and-mongoose – Elvis Pimentel May 16 '22 at 13:33
  • 4
    As a first impression, this doesn't seem good design for your project, can you explain why you need a db for each user? – Elvis Pimentel May 16 '22 at 13:34
  • @ElvisPimentel I was thinking for performance, like in nodejs we generally work with single db , and then inside their collection , If any way to create single db for single user and work with that , or how can I improve that performance any advice . Or any better solution – Debanjan Tewary May 16 '22 at 13:41

2 Answers2

0
 var MongoClient = require('mongodb').MongoClient;
    //Create a database named "userName":
    userCollection.foreach(name => {
    var url = "mongodb://localhost:27017/"+ name;
    MongoClient.connect(url, function(err, db) {
      if (err) throw err;
      console.log("Database created!");
      db.close();
    });
    }
-1

Yes, Its possible in mySql !! and You can try it mongoose sequelize node js

const sequelizedb = new Sequelize("", MysqlInfo.username, MysqlInfo.password, {
    host: MysqlInfo.dbhost,
    dialect: "mysql"
  });

  let dbcreation = "CREATE DATABASE `" + username + "`;";
  sequelizedb.query(dbcreation).then(res => {
    const newDBconnection = new Sequelize(
      "mysql://" +
      MysqlInfo.username +
      ":" +
      MysqlInfo.password +
      "@" + MysqlInfo.dbhost + ":3306/" +
      schooldbname
    );

    newDBconnection
      .authenticate()
      .then(() => {
})