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.
CodePudding user response:
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();
});
}
CodePudding user response:
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(() => {
})
