The purpose of this module is to provide a wrapper for using mongodb functions easier and provide a straightforward way of supporting schema validation.
First install Node.js. Then:
npm i @gkampitakis/mongo-client --save
// Using Node.js `require()`
const { MongoClient } = require('@gkampitakis/mongo-client');
//Using ES6 import
import { MongoClient } from '@gkampitakis/mongo-client';
Connecting to MongoDB
await MongoClient.connect('mongodb://localhost:27017', 'databaseName', {
useNewUrlParser: true,
useUnifiedTopology: true
});
Note: You only have to create the connection once and then have access from anywhere in your application.
Models are referring to mongo collections
Defining a Model
const user = Model('user');
// or
const user = Model('user', new Schema());
A model gives access to collection manipulation. You can provide a Schema on Model initialisation that provides extra functionality as schema validation and hooks.
- instance
javascript await model.instance(data);
Returns a Document. - create
javascript await model.create(data,options);
Creates an entry to database and returns a Document or a plain object depending onoptions:{lean}
boolean flag. - deleteOne
javascript await model.deleteOne(filter,options);
Deletes the first entry from database that matches the given filter. - findOne
javascript await model.findOne(filter,options);
Returns the first entry from database that matches the filter. The return value is either a Document or a plain object depending onoptions:{lean}
boolean flag. - updateOne
javascript await model.updateOne(filter,data,options);
Updates an entry to database and returns a Document or a plain object depending onoptions:{lean}
boolean flag. - findByIdAndDelete
javascript await model.findByIdAndDelete(id,options);
Deletes an entry from database that has the id given as parameter. - findById
javascript await model.findById(id,options);
Returns an entry from database that matches the id given. The return value is either a Document or a plain object depending onoptions:{lean}
boolean flag. - findByIdAndUpdate
javascript await model.findByIdAndUpdate(id,updateObject,options);
Finds an entry based on the id provided, updates it and return a Document or a plain object depending onoptions:{lean}
boolean flag. - deleteMany
javascript await model.deleteMany(filter);
Deletes multiples entries from database based on the filter provided.
The document is the wrapper object for the data. It has the structure:
{
"collectionName": "users",
"data": {...},
"schema": {...},
"lean": () => object,
"save": () => Document,
"remove": () => Document
}
- data the actual data saved in the database.
- collectionName the name of the collection where the data are saved.
- schema contains the definition for the schema.
- lean returns the plain object of data.
- save saves the data or updates it.
- remove deletes the entry from the database.
The schema has a description about how the data object should look like. It can contain more restrictions about the type of data.
const userSchema = new Schema({
type: 'object',
properties: {
username: {
type: 'string'
},
password: {
type: 'string'
},
age: {
type: 'number'
}
},
required: ['username', 'password']
});
For the validation of the object Ajv library is used. The same syntax is used.
When you try to create a new entry with a model that has been initialised with schema, the data are being validated against this schema.
For the hooks the kareem library is being used. There are two hooks pre
and post
and currently 6 supported types ( save, delete, update, remove, create, delete ) and registered in the schema like:
const schema = new Schema();
// it is important to be a function and not an arrow function
// cause inside the callback you have access on the context of `hooked` object
schema.pre('save', function () {
console.log(this.data);
// or
console.log(this);
//depending on **lean** boolean flag
//if it returns a document or plain object
});
schema.post('save', function () {});
-
npm run playground
An example of simple test cases on how to use this module. -
npm run playground:mongo
Run the examples on your local mongo instance. -
npm run test
Run Jest testing suite. -
npm run benchmarks
Run benchmark suite for Mongo Client, Mongoose and Native MongoDb for Nodejs.
For any issues.