@condor-labs/mongodb
Advanced tools
Comparing version 1.1.2 to 1.1.3
{ | ||
"name": "@condor-labs/mongodb", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "This module provide and usefull helper to use mongoose library.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
184
README.md
@@ -72,5 +72,187 @@ This module provide and usefull helper to use mongoose library. | ||
There are two ways to connect the MongoDB with @condor-labs/mongodb, one is a single connection and the other is multi-connection | ||
## Example Code | ||
## 1. Connect using a single connection and Replica Set | ||
This session provides a help to connect to mongodb using a single connection and Replica Sets. | ||
### How to use it | ||
To use this connection you need the following | ||
A. Define the configuration variables in an object in the `constants.js` file for the connection, which are the following | ||
```js | ||
module.exports.mongoDbSettings = { | ||
Settings: { | ||
host: "exapmle-shard-00-00-8burs.mongodb.net,exapmle-shard-00-01-8burs.mongodb.net,exapmle-shard-00-02-8burs.mongodb.net", | ||
port: 27017, | ||
database: "dev", | ||
user: "my-user", | ||
password: "superSecretPass", | ||
replicaSet: "compliance-support-shard-0", | ||
ssl: true, | ||
authSource: "admin", | ||
}, | ||
}; | ||
``` | ||
B. Import the @condor-labs/mongodb library and the configuration variables in the `connectionMongoDb.js` file as follows | ||
```js | ||
const mongo = require("./constants"); | ||
const mongodb = require("@condor-labs/mongodb")(mongo.settings); | ||
``` | ||
C. To establish the connection with MongoDB the instance is created in the `connectionMongoDb.js` file | ||
```js | ||
const mongodb = require("@condor-labs/mongodb")(mongo.settings); | ||
async function connect()=> { | ||
// connect to Mongo | ||
await mongodb.getClient(); | ||
console.log(`isConnected(after):${mongodb._isConnected()}`); | ||
}; | ||
module.exports = connect; | ||
``` | ||
D. Import the connect function in the main `index.js` of the application | ||
```js | ||
const connect = require("./connectionMongoDb"); | ||
(async () => { | ||
await connect(); | ||
}) | ||
``` | ||
## 2. How to create models using a single connection and Replica Sets | ||
To create models using a single connection, in a `models.js` file you create the database schemas and import the @condor-labs/mongodb library. | ||
```js | ||
const mongodb = require("@condor-labs/mongodb")(); | ||
const schemaOne = mongodb.mongoose.Schema({ | ||
attributeOne: { | ||
type: String, | ||
required: true, | ||
}, | ||
attributeTwo: { | ||
type: String, | ||
required: true, | ||
}, | ||
}); | ||
const modelOne = mongodb.mongoose.model('name-model', schemaOne); | ||
module.exports = modelOne; | ||
``` | ||
## 3. How to connect using a single connection with locally installed mongodb | ||
To use locally installed mongodb using a single connection, change the `constants.js` file with the following configuration variables. | ||
```js | ||
module.exports.mongoDbSettings = { | ||
Settings: { | ||
host: "localhost", | ||
port: 27017, | ||
database: "development", | ||
user: "local-user", | ||
password: "superSecretPass", | ||
ssl: false, | ||
} | ||
} | ||
``` | ||
## 4. How to connect using a single connection and mongodb-srv (connection string) | ||
Nowaday, the library does not support SRV connections. You must split your connection string into the necessary configs required. | ||
## 5. How to connect using a single connection when running in Docker | ||
To create this connection it is necessary to have the files `docker-compose.yml` and `dockerfile`. | ||
In the `dockerfile` the environment is built and the application is installed in the following way | ||
```js | ||
FROM node:version node | ||
WORKDIR /usr/app | ||
COPY package*.json ./ | ||
RUN npm install pm2 -g | ||
RUN npm install --production | ||
COPY . . | ||
EXPOSE 3000 | ||
CMD ["pm2-runtime","pm2.proccess.json"] | ||
``` | ||
In the `docker-compose.yml` the images that will be run in the docker container are created and it is built as follows | ||
```js | ||
version: '3.1' | ||
services: | ||
nameApp: | ||
container_name: "name-container" | ||
depends_on: | ||
- mongo | ||
restart: always | ||
build: . | ||
ports: | ||
- "3000:3000" | ||
networks: | ||
- app | ||
volumes: | ||
- .:/usr/app | ||
mongo: | ||
image: mongo | ||
container_name: mongodb | ||
restart: always | ||
environment: | ||
MONGO_INITDB_ROOT_USERNAME: root | ||
MONGO_INITDB_ROOT_PASSWORD: "pass-root" | ||
MONGO_INITDB_DATABASE: "name-database" | ||
ports: | ||
- 27017:27017 | ||
networks: | ||
- app | ||
volumes: | ||
- ~/mongo/data:/data/db | ||
networks: | ||
app: | ||
driver: "bridge" | ||
``` | ||
The previous files describe the construction of the application images and the database, the network connection between the two images is created. | ||
To perform the connection in the `constants.js` file, the configuration variables are created to the database | ||
```js | ||
module.exports.mongo = { | ||
settings: { | ||
host: 'mongo', //database image name | ||
port: 27017, | ||
database: "name-databases", | ||
user: "root", | ||
password: "pass-root", | ||
ssl: false, | ||
authSource: "admin" | ||
} | ||
} | ||
``` | ||
The commands to build and start the images in docker are the following | ||
```js | ||
docker-compose up -d --build | ||
``` | ||
This command builds and initializes the images exposed in the docker-compose file. | ||
```js | ||
docker logs -f name-app | ||
``` | ||
With this command you can see the logs of the image where the application is running. | ||
## Connect using a multi-connection | ||
this is an example for multiple connections: | ||
@@ -77,0 +259,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
40881
378
1