plugdo-mongo
v1.0.19
Published
plugdo mongo provides little features of orm with schema validation
Downloads
11
Maintainers
Readme
Plugdo Mongo
This module is an adapter of the mongdb client for NodeJS. It provides us the following features:
- Schema validation
- Working with table object with CRUD methods
- Dynamic creation of database
- Transform a table to be used as cache storage applying expiration rules to it in mongodb
- Async Await to handle the promise for clean coding
npm install plugdo-mongo --save
Table definition
Create a js file to define the table, and It will be exported as taskTable.
const { mongodb } = require("plugdo-mongo");
function taskTable () {
var schema = {
"_id": {
required: false,
unique: true
},
title: {
required: true,
typeof: "string",
min: 4,
code: 550
},
description: {
required: true,
typeof: "string",
min: 4,
code: 560
},
priority: {
required: true,
typeof: "string",
code: 570
}
};
var table = mongodb.server("mongodb+srv://username:[email protected]/taskdb?retryWrites=true&w=majority")
.db("taskdb")
.table(schema, "tasks");
return table;
}
exports.taskTable = taskTable();
Let's explain what is happening:
- Export the plugdo-mongo module
- Define a function to return the table
- Define the schema to be used for validation of the table identity
- Create the table passing the connection string, database name, schema defined and table name.
- Exports the table for external use
Cache table definition
Create a js file to define the table, and It will be exported as sessionTable.
const { mongodb } = require("plugdo-mongo");
function sessionTable() {
var schema = {
token: {
required: true,
typeof: "string",
code: 554
},
userID: {
required: true,
unique: true,
typeof: "string",
code: 555
},
ip: {
required: true,
typeof: "string",
code: 556
},
createdOn: {
required: true,
code: 557
},
expireOn: {
required: true,
code: 558
}
};
var table = mongodb.server("mongodb+srv://username:[email protected]/taskdb?retryWrites=true&w=majority")
.db("sessiondb", {
cache: {
collection: "sessions",
field: "expireOn",
seconds: 3600
}
}).table(schema, "sessions");
return table;
}
exports.sessionTable = sessionTable();
We will explain a bit better the schema definition in the next section, but so far let's explain what is happening:
- Export the plugdo-mongo module
- Define a function to return the table
- Define the schema to be used for validation of the table identity
- Define the cache rule to the database passing the options
- Create the table passing the connection string, database name, schema defined and table name.
- Exports the table for external use
Table Methods
table.has
The "has" method use the property defined as "unique:true" in the schema. You just need to pass the model that include that parameter.
// Passing just the unique property
var response = await userTable.has({ email: "[email protected]" });
// Passing a model with multiple properties including the unique one
var response = await userTable.has(model);
table.getAll
The "getAll" method does not require a model. it will return all the documents saved in the collection.
var response = await userTable.getAll();
// Excluding properties
var response = await userTable.getAll({ password: 0 });
table.get
The "get" method will return the documents that match the model or query.
var response = await userTable.get({ email: "[email protected]" });
// Excluding properties
var response = await userTable.get({ email: "[email protected]" }, { password: 0 });
table.add
The "add" method will create a document in the collection.
var response = await userTable.add({
name: "Full Name",
email: "[email protected]",
password: "password"
});
table.change
The "change" method will modify a document in the collection. You pass the query and the model.
// First parameter is the query, and the second is the model with the value to be changed
var response = await userTable.change(
{ email: "[email protected]" },
{ password: "new-password" });
table.remove
The "remove" method will delete a document from the collection, and It use the property defined as "unique:true" in the schema. You just need to pass the model that include that parameter.
var response = await userTable.remove({ email: "[email protected]" });
Response Object
All the method return a response model that has the following information:
- success : It is false if an error has been found, or true if everything ran without issues
- error : This is a message with the error found
- errorCode : This is the error code
- data : Here you will get the result of the method
// Passing just the unique property
var response = await userTable.has({ email: "[email protected]" });
if(response.success) {
console.log(response.data);
}
else {
console.log(response.error);
console.log(response.errorCode);
}
We define a response object to make simple the interaction of Web API with our Mongo module, we can send back the response object in the return of the Web API because the mongo module has validation defined in the schema.