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

rest-mongoose

v2.4.3

Published

Rest controllers and routes for mongodb over mongoose and express

Downloads

122

Readme

rest-mongoose

This package aims to be more easy the build of simple rest apis based on the combination MongoDB-Nodejs-Express.

Installation

npm install rest-mongoose

For potential developers

I'll be happy to get some extra help from developers who want to collaborate with this project. If you are one of them please write me to [email protected], or just start creating issues in the github repo. You should take a look to the issues already opened. Thank you.

Basic usage

Here is a simple server example:

Require necessary packages

const rest_mongoose = require('rest-mongoose');
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');

Build a basic express app

const app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

Conect to a mongoo database

mongoose.connect('mongodb://localhost:27017/testdb', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    useCreateIndex: true
})
.then( () => {
    console.log("Successfully connected to database");    
})
.catch( err => {
    console.log('Could not connect to the database. Exiting now...', err);
    process.exit();
});

Create a route for test app

app.get('/', (request, response) => {
    response.json({"message": "Welcome to test api."});
});

Now use the magic of rest-mongoose package to define as models as you want and create routes for them

Defining models, controllers, auths and routers.

// Defining a model 1
// This model has one field and we will create all valid routes.
// You can get a list of valid routes by using rest_mongoose.valid_actions
// All the created routes in this case will need authentication because the
// empty list passed in the second parameter in Auth constructor.

const valid_actions = rest_mongoose.valid_actions;

var model_1 = new rest_mongoose.MongoModel("name_1", {
    field_a: {
        type: String,
        unique: true,
        required: true
    }
}, true);

var controller_1 = new rest_mongoose.MongoController(model_1, valid_actions);
var auth_1 = new rest_mongoose.Auth(model_1, async function(token, body, action, instance_id){return true}, []);
var router_1 = new rest_mongoose.MongoRouter(app, controller_1, auth_1);

// Define a model 2
// This model has two fields. For this model we only alow
// CREATE and FINDALL routes specified in the controller constructor,
// and only for the CREATE action the route will need an authentication, because
// we provide a free_actions list ["FINDALL"]

var model_2 = new rest_mongoose.MongoModel("name_2", {
    field_b: {
        type: String,
        unique: true,
        required: true
    },
    field_c: {
        type: Number,
        unique: true,
        required: true
    }
}, true);

var controller_2 = new rest_mongoose.MongoController(model_2, ["CREATE", "FINDALL"]);
var auth_2 = new rest_mongoose.Auth(model_2, async function(token, body, action, instance_id){return true}, ["FINDALL"]);
var router_2 = new rest_mongoose.MongoRouter(app, controller_2, auth_2);

Finally create the routes and listen on port 8000

function routing_callback(action, data) {
    // This function will be called once the request had successed and just before
    // it gives the response.
    switch(action) {
        case "CREATE":
            console.log(`Provided data: ${data}`);
            break;
        case "FINDALL":
            console.log(`Instances: ${data}`);
            break;
        case "FINDONE":
            console.log(`Instance: ${data}`);
            break;
        case "UPDATE":
            console.log(`Updated instance: ${data}`);
            break;
        case "DELETE":
            console.log(`Deleted instances: ${data}`);
            break;
    }
}

router_1.route(routing_callback);
router_2.route(routing_callback);

app.listen(8000, () => {
    console.log("Server is listening on port " + String(8000));
});

Valid Actions

The valid actions are ["CREATE", "FINDALL", "FINDONE", "UPDATE", "DELETE"]

You can use all of them or only someones.

Result

The created endpoints are in based to model name. For this example there will be created the next ones: Notes the path endpoint use the name of the model followed by an 's' In this case, the GET endpoint for model 2 does not need an access-token header because it was specified as a free action in the Auth costructor. Note also that you are who provides the validation function for the resources based on the token and the action in requests.

| Method | url | payload | Headers | | ------------- | :-------------------| :--------------------------------------| :------------| | POST | /name_1s | {"field_a": string} | access-token | | GET | /name_1s | | access-token | | GET | /names_1s/:name_1id | | access-token | | PUT | /names_1s/:name_1id | {"field_a": string} | access-token | | DELETE | /names_1s/:name_1id | | access-token | | POST | /name_2s | {"field_b": string, "field_c": number} | access-token | | GET | /name_2s | | |