Skip to content

REST API generator using knex for node's express.js, generating routes with knex models.

License

Notifications You must be signed in to change notification settings

petrutoader/bald-knex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bald-knex

REST API generator using knex for express.js.

NPM

Build Status Dependency Status

Installing via NPM

npm install bald-knex

Getting started

Using bald is really easy, let's take the following example:

We have a table called Users we'd like to expose to the API:

bald = new Bald({app: app, knex: knex})

bald.resource({
  model: 'Users'
});

That's it! You can now create, read, update and delete using the newly created API. You can check them at /api/Users/ and /api/Users/:pk where :pk is the primary key of the table.

Primary keys

Sometimes you may want to set a different primary key than id, for that you can set the primaryKey parameter in the resource initialization as follows:

bald.resource({
  model: 'Users'
  primaryKey: 'id'
})

Middleware support

Bald also optionally includes middleware support for each route that is declared:

isLoggedIn = function(req, res, next) {
  if (req.user) {
    return next();
  } else {
    return res.end('permission-denied')
  }
};

bald.resource({
  model: model
  middleware: {
    list: [isAdmin, isLoggedIn],
    create: [isAdmin, isLoggedIn],
    read: [isLoggedIn],
    update: [isAdmin, isLoggedIn],
    delete: [isAdmin, isLoggedIn]
  }
})

You do not need to declare all the routes middleware if none is needed.

HTTP

Following the same method as in the manager, you PUT or POST the data to the endpoint with x-www-form-urlencoded.

REST API

Available routes are listed below:

Method URL Description
GET /api/Users Displays all users
GET /api/Users/1 Displays a user, searched by id
PUT /api/Users Edits multiple users (JSON format)
PUT /api/Users/1 Edits a user
POST /api/Users Adds a user
DELETE /api/Users/1  Deletes a user

Custom endpoints

You can declare your own endpoints instead of letting bald pluralize the model's name. In order to do so you'll have to declare the bald resource in the following way:

bald.resource({
  model: 'User',
  endpoints: {
    plural: '/api/CoolUsers'
    singular: '/api/CoolUser/:id'
  }
});

Please note that the singular must include the id query parameter in the string. If you are specifiyign custom endpoints, both singular and plural endpoints are mandatory.

Mentions

Developed at Phyramid

LICENSE

The MIT License (MIT)

Copyright (c) 2015 Petru-Sebastian Toader

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

REST API generator using knex for node's express.js, generating routes with knex models.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •