npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

plugdo-mongo

v1.0.19

Published

plugdo mongo provides little features of orm with schema validation

Downloads

10

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.