1.0.14 • Published 4 years ago

sequelize-on-rails v1.0.14

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

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
1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago