sequelize-restful

A connect module that adds a restful API for all defined models to your application.

npm install sequelize-restful
1 downloads in the last day
8 downloads in the last week
62 downloads in the last month

sequelize-restful

A connect module that adds a restful API for all defined models to your application.

Usage

var express   = require('express')
  , Sequelize = require('sequelize')
  , http      = require('http')
  , restful   = require('sequelize-restful')
  , sequelize = new Sequelize('database', 'username', 'password')
  , app       = express()

// define all your models before the configure block

app.configure(function() {
  app.use(restful(sequelize, { /* options */ }))
})

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'))
})

Options

{
  // Parameter:   endpoint
  // Description: Define the path to the restful API.
  // Default:     '/api'

  endpoint: '/restful'
}

The API

GET /api

Returns a list of all declared models

$ curl http://localhost:3000/api
{
  "status": "success",
  "data": [
    {
      "name": "Tag",
      "tableName": "Tags"
    },
    {
      "name": "Image",
      "tableName": "Images"
    },
    {
      "name": "Project",
      "tableName": "Projects"
    }
  ]
}

HEAD /api/Tags

Returns a description of a declared model

$ curl -i -X HEAD http://localhost:3000/api/Tags

The result of the request is part of the response headers! The header's name is Sequelize-Admin.

{
  "status": "success",
  "data": {
    "name": "Tag",
    "tableName": "Tags",
    "attributes": {
      "title": "VARCHAR(255)",
      "id": {
        "type": "INTEGER",
        "allowNull": false,
        "primaryKey": true,
        "autoIncrement": true
      },
      "createdAt": {
        "type": "DATETIME",
        "allowNull": false
      },
      "updatedAt": {
        "type": "DATETIME",
        "allowNull": false
      },
      "ProjectId": {
        "type": "INTEGER"
      }
    }
  }
}

GET /api/Tags/1

Returns the data of the Tag with the id 1.

$ curl http://localhost:3000/api/Tags/1
{
  "status": "success",
  "data": {
    "title": "foo",
    "id": 1,
    "createdAt": "2013-02-09T09:48:14.000Z",
    "updatedAt": "2013-02-09T09:48:14.000Z",
    "ProjectId": 1
  }
}

POST /api/Tags

Creating a new instance of a model

curl -d "title=hallo%20world" http://localhost:3000/api/Tags
{
  "status": "success",
  "data": {
    "title": "hallo world",
    "id": 1,
    "createdAt": "2013-02-09T09:48:14.000Z",
    "updatedAt": "2013-02-09T09:48:14.000Z"
  }
}

UPDATE /api/Tags/1

Updating an already existing instance of a model

curl -d "title=fnord" -X PUT http://localhost:3000/api/Tags/1

It returns the updated record

{
  "status": "success",
  "data": {
    "title": "fnord",
    "id": 1,
    "createdAt": "2013-02-14T19:52:04.000Z",
    "updatedAt": "2013-02-14T19:53:30.066Z",
    "ProjectId": 1
  }
}

DELETE /api/Tags/1

Deleting an existing instance of a model

curl -i -X DELETE http://localhost:3000/admin/api/Tags/3
{
  "status": "success",
  "data": {}
}
npm loves you