https://github.com/kth/kth-node-mongo
Database connection module for Node.js applications using Mongoose.
https://github.com/kth/kth-node-mongo
Last synced: 10 months ago
JSON representation
Database connection module for Node.js applications using Mongoose.
- Host: GitHub
- URL: https://github.com/kth/kth-node-mongo
- Owner: KTH
- License: mit
- Created: 2016-08-31T15:06:44.000Z (almost 10 years ago)
- Default Branch: main
- Last Pushed: 2024-12-01T04:47:22.000Z (over 1 year ago)
- Last Synced: 2025-01-01T17:34:34.208Z (over 1 year ago)
- Language: JavaScript
- Size: 225 KB
- Stars: 0
- Watchers: 21
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @kth/mongo
Database connection wrapping Mongoose (for MongoDB)
This module connects to mongoDB using Mongoose default connection.
To use this module
## Connect to database
1. Import module
const nodeMongo =require(@kth/mongo')
1. Connect to mongoDB :
nodeMongo.connect(options)
1. Use Mongoose schema and model to interact with mongoDB
Function connect() returns a promise to be resolved upon completed connection or rejected on error.
### Options
- **dbUsername** (required)
Credentials, the database user
- **dbPassword** (required)
Credentials, the password for the database user
- **dbUri** (required)
The URI for the mongoDb to connect to
- **logger** (optional)
A logger to use, defaults to stdout(console.log)
### SSL(TLS) Options
- **ssl** (optional)
Boolean flag if database connection shoold be encrypted or not
Example without secure database connction
```js
nodeMongo
.connect({
dbUsername: 'user',
dbPassword: 'himligt',
dbUri: 'mongodb://localhost/le_database?authSource=authDB',
logger: log,
})
.then(() => log.debug('Connected to Mongo'))
.catch(err => log.error(err))
```
Example with secure database connction
```js
nodeMongo
.connect({
dbUsername: 'user',
dbPassword: 'himligt',
dbUri: 'mongodb://localhost/le_database',
logger: log,
ssl: true,
})
.then(() => log.debug('Connected to Mongo'))
.catch(err => log.error(err))
```
## Check status of connection
1. Import module
const nodeMongo =require('@kth/mongo')
1. Check connection:
```js
if (nodeMongo.isOk()) {
// OK
} else {
// ERROR
}
```