npm-dependency-for-microservises

Notice that we have duplicated our models from the other services - this is bad, we have to extract the common code in to a npm module that we can then import in to our project just like any other package.

Usage no npm install needed!

<script type="module">
  import npmDependencyForMicroservises from 'https://cdn.skypack.dev/npm-dependency-for-microservises';
</script>

README

Description

Notice that we have duplicated our models from the other services - this is bad, we have to extract the common code in to a npm module that we can then import in to our project just like any other package.

The thing is that microservice search does not require its own database, it does not require its own model in like a very basic example - it does not need it. But what we do need - is to know how to basically get data from the database, we need to be able to need what the other services that we want to be able to search for. We need to have some type of connection. The idea is - have common code. If everythimg running on Node.js and we have things that need to be shared between different modules or different microservices. So that I put it in a npm package. That makes it very easy for us to move common code amongst our projects, we do not have to copy and paste duplicate. Because this is very dangerous - for example, I changed the book's module in my book's microservice, then I have to make sure that I'm changing it here /search/src/app.js as well. That's not so nice.

Source Code

'use strict'

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const bookSchema = new Schema({
 name: String,
 type: { type: String, default: 'book' },
 createdAt: { type: Date, default: Date.now }
})

const videoSchema = new Schema({
 name: String,
 type: { type: String, default: 'video' },
 createdAt: { type: Date, default: Date.now }
});

module.exports = {
 Video: mongoose.model('Video', videoSchema),
 Book: mongoose.model('Book', bookSchema)
}