How to use the existing schema of mongoose of one project in another project?

Viewed 240

Suppose I have an existing project on student records in which I have a collection named student.

const mongoose = require('mongoose');
const studentSchema = mongoose.Schema({
name,
email,
});

mongoose.model('student', studentSchema);

Now I am working on new on new project of teacher details in which I want all the details of students too. so for this what I want is that to use the same database url which I used in the student records project for the connection and use the existing schema of student by the same name without creating its copy. But I don't know how to do this. So, can you please help me. I will be very much thankful for you.

note:- both the project are located on different locations and will be host on different servers.

1 Answers

This is not specifically a mongodb/mongoose question as you're looking for a way to share a datamodel between 2 projects.

If you're using git you can use git submodules, simply move the shared models to a new repository. Then include this repo in both existing projects with git submodule add [repo]

Another option would be to create an npm-package.

Related