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

sequelize-on-rails

v1.0.14

Published

Speed up sequelize development by using these easy to use predefined methods

Downloads

8

Readme

Create a REST API in under 5 minutes!

This package includes commonly used methods that will help you build out common funtionality quicker than previously thought possible. Inspired by ruby on rails. Perfect for rapid prototyping and production use.

// Initialize at models/index.js
Sequelize = require("sequelize-on-rails").init(Sequelize);

Create a REST object

When creating an object in the database, most of the time the only concern is the fields the user is allowed to create, if this is the case, use the 'createWithWhitelisted' method!

models.Book.createWithWhitelisted(["book_name", "book_barcode","AuthorId"], req, res, next)
Reponse

Boom! The properties will be taken from the req.body and provided to the model.create() if they are provided!

{
    "data": {
        "id": 2,
        "book_name": "Game of Thrones",
        "book_barcode": "ABC123456789",
        "book_image_url": "http://img-src",
        "updatedAt": "2019-06-14T17:52:28.261Z",
        "createdAt": "2019-06-14T17:52:28.261Z"
    }
}

List a REST object

When listing objects already in the database, if your concerns are pagination including page sizing, then use the 'findAndPageAll' method! You can also specifiy ordering and the direction of the ordering!

router.get("/books", (req,res,next)=>{
    Book.findAndPageAll({
        // You can override with your own filtering, includes etc.
        where : {},
        include: [Author]
    }, req, res, next)
})

So now if we request https://api.sequelizeproductivity.org/books?limit=20&page=1?orderBy=createdAt,book_barcode&orderDirection=ASC,DESC

Reponse

It will give you a nicely paginated response !

    "data": [...],
    "count": 20,
    "limit" : 10,
    "page": 1,
    "totalPages" : 2
    "nextPage": true,
    "prevPage": false

View a REST object

Okay, this one probably isn't that useful but hey-ho

router.get("/books/:id", (req,res,next)=>{
    models.Book.findById({},req,res,next);
})

So now if we request https://api.sequelizeproductivity.org/books/1

Reponse

It will give you a nicely formatted response !

{
    "data": {
        // etc
    }
}

Update a REST object

Whitelist the fields you want to allow the user to update. Simple as that.

router.patch("/books/:id", (req,res,next)=>{
    models.Book.updateWhitelisted(["book_name"],req,res,next);
})

So now if we patch the object via http

Reponse

It will give you a nicely formatted response including the updated object!

{
    "updated": {
        "id": 1,
        "book_name": "Game of Thrones2",
        "book_barcode": "ABC123456789",
        "book_image_url": "http://img-src",
        "createdAt": "2019-06-14T18:04:43.000Z",
        "updatedAt": "2019-06-14T18:05:57.000Z",
        "AuthorId": null,
        "BookId": null
    }
}

Delete a REST object

Destroys by the ID in params.id - 404 if not found

router.delete("/books/:id", (req,res,next)=>{
    models.Book.destroyIfFound(req,res,next);
})

So now if we delete the object via http

Reponse

It will give you a nicely formatted response including the updated object!

204 NO CONTENT