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

restful-database

v1.0.2

Published

A library that makes adding routes to express app and interaction with database easier than ever

Downloads

4

Readme

Synopsis

A library that makes adding routes to express app and interaction with database easier than ever

Code Example

models/person.js

var mongoose = require('mongoose');

var person = {
    first_name: String,
    last_name: String,
    schoolId: mongoose.Schema.Types.ObjectId,
    address: {
        number: String,
        street: String,
        city: String,
        state: String,
        country: String,
        postalCode: String
    },
    birthdate: Date,
    age: Number
};

var PersonSchema = new mongoose.Schema(person);
var PersonModel = mongoose.model('Person', PersonSchema)

module.exports = function(app){
    var schema = [
        {
            'name': PersonModel,
            'requests': {
                'get': [
                    {
                        'url': '/first_name/:first_name'
                    },
                    {
                        'url': '/last_name/:last_name'
                    },
                    {
                        'url': '/full_name/:first_name/:last_name'
                    }
                ],
                'post': [
                    {
                        'url': '/new',
                        'schema': person // Optional
                    }
                ],
                'put': [
                    {
                        'url': '/update/first_name/:id',
                        'params': ['first_name'],
                        'id': true
                    },
                    {
                        'url': '/update/last_name/:id',
                        'params': ['last_name'],
                        'id': true
                    },
                    {
                        'url': '/update/full_name/:id',
                        'params': ['first_name', 'last_name'],
                        'id': true
                    },
                    {
                        'url': '/update/using/age/:age/last_name/:last_name',
                        'params': ['first_name'],
                        'id': false
                    }
                ],
                'remove':[
                    {
                        'url': '/remove/:id',
                        'id': true
                    }
                ]
            }
        }
    ];

app.js

var express = require('express');
var mongoose = require('mongoose');
var person = require('models/person.js')

var app = express();
/*
  Configure app
*/
person(app); // Adds routes
GET

Searches by the req param name (i.e :param). In the example above, it searches by "first_name" and "last_name". The field in the database should have the same name.

POST

No need to configure the backend to translate the request body to nested objects in the schema. All you have to do is "address.number" in the PUT request body. This supports as many nested objects in the schema. The schema parameter will allow error detection but is optional (not implemented yet)

PUT

Search the database by the parameters in the request URL and update the value of arguments in the params array. When the id is set to true, it seaches by id and update. Otherwise, the query may return multiple results, sort them, then update the first in the result set.

REMOVE

Similar to PUT but removes from the database. If id is false, the first of the sorted result set is removed.

Motivation

When configuring server's routes and database interactions, most of the code is repetitive and time consuming. It would be great to have a library ease the process. A library that takes in routes, parameters names, and a schema object, then configure it for you.

Installation

npm install database-restful

Contributors

If you have any ideas on how to improve the library, or if you find any bug, create an issue or contact me.

TODO list

  • [ ] Support for dates
  • [ ] Error check for POST based on provided schema
  • [ ] Aggregation
  • [ ] Support for other databases (current mongodb)