14

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?

wscourge
  • 10,657
  • 14
  • 59
  • 80
no7dw
  • 505
  • 5
  • 13

4 Answers4

11

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://git@github.com:example/example-repo.git#v1.0.0

// bitbucket url
git+ssh://git@bitbucket.org/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.

Josnidhin
  • 12,469
  • 9
  • 42
  • 61
7

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://git@models-repo-path.git" }

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.

Aditya Chowdhry
  • 349
  • 3
  • 9
3

You can definitely put them in a package and reuse it. And for having its own DB Ops and Network Ops, you can take the DB URL as an environment variable while starting the project. And use the same from process.env.$variable while connecting to the database.

Ashish Pandey
  • 483
  • 5
  • 18
2

There are many ways to solve this problem, rather elegantly. You should definitely avoid copy and pasting such content. Especially schema designs which are highly susceptible to changes of the application life cycle during development and of course maintenance procedures. The following listed below are simply options you and your team can take to implement your solutions for the aforementioned problem.

  1. Most easiest and reusable approach would be to use NPM to package your package and distribute to your team. The install command as an option for GIT url. Of course for privacy concerns , private hosting is mandated. Please see this link for the command line arguments.
  2. Instead of hosting the NPM on GitHub | Bitbucket, what can you do instead , still create the Node module, using standard Node.js modules. and locally share the file using git serve. This way any developer can connect and fetch the data, merge it their codebase. Since this temporary as well, it will be discarded when done. This way whenever a change does occur, you can use git to revise history. Do read up on Git Daemon.

Note: his is common choice for fast, unauthenticated access to your Git data. Remember that since it’s not an authenticated service, anything you serve over this protocol is public within its network.

Remario
  • 3,813
  • 2
  • 18
  • 25
  • Another option which is an overkill, you can use docker containers, or any container technology. I will use docker for this example, create the Node.js schema module, you Docker to create a container to the file, do this by using volumes. LInk the local copy to the one in the container, then share this container with teammates, – Remario Feb 05 '18 at 15:52