Let's say I have 3 node.js projects(1 app backend, 1 app-admin backend, 1 analysis api). In each projects, I have a model schema call loan.
{
attributes: {
userId: { type: String, required: true, index: true, ref: 'users', comment: '用户id' },
amount: { type: Number, required: true, min: 0},
totalAmount: { type: Number, required: true, min: 0},
penaltyInterest: { type: Number, min: 0, required: true, default:
0 }
}
methods: {
getFee () {//some calculation ops
}
save() {//some db ops
}
sendTo3rdComponent() {//some network ops
}
}
This model has: some methods, it's schema design, api implement. How can I reuse it in other two projects.
It's very import to reuse the design and api for multiple projects.
Usually we reuse the component via public it as npm package. However this component has it's own db ops, and network ops. Is it possible and proper to make it as a npm package?
Another option is like eggjs
So what's the elegant solution beside copy-paste?
Mongoose save with an existing document will not override the same object reference. Bookmark this question. Show activity on this post.
Mongoose Schema vs. Model. A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.
The mongoose. model() function of the mongoose module is used to create a collection of a particular database of MongoDB.
I wont advice you to make a published npm package reason being, as A Good NodeJS developer you shouldn't pollute npm with packages that doesnt help anyone else. Unless you are a paid npm user with access to the private packages option.
Did you know package.json
supports git urls, you can read about that @ Git URLs as Dependencies
A few examples of git urls in package.json
// github url
git+ssh://[email protected]:example/example-repo.git#v1.0.0
// bitbucket url
git+ssh://[email protected]/example/example-repo.git#v1.0.0
My suggestion is create a separate package with an API to set config, which in a scenario like yours would be DB connection related stuff. Upload it to a private git repo and use the private git repo url in all the application. Then during the application initializing phase configure the package and use its API.
Now the applications can be build on any system which has access to the private repo and can reuse the code.
You can also put your package on a public repo in case you dont have access to a private repo, which is still better than publishing npm packages in order to shared it across your applications.
You can just create another package containing common models and push it to a private git repository or maybe a public one if you are fine with it. Then use the git repository url in package.json rather than publishing it to NPM. Let's say you named it models-repo.
It can be a simple package consisting of:
├── README.md
├── index.js
├── models
│ ├── carLoan.js
└── package.json
You can include it in your package.json file of the application using git URLs:
{ "models-repo" : "git+ssh://[email protected]" }
Now you can require it in any file and start using it:
const models = require('models-repo');
const carLoanModel = models.car_loan;
//Do something
carLoanModel.find({})
You need to be careful with the permissions when using it in production.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With