restleaf

0.0.3 • Public • Published

restleaf

restleaf is a nodejs rest api toolkit, that allows you to easily get started with creating a restfull api

# instalation restleaf depends on leafless, so you have to install leafless first

$ npm install leafless
# or with yarn 
$ yarn add leafless

Then install restleaf

$ npm install restleaf
# or with yarn 
$ yarn add restleaf

initialization

set up restleaf by initializing leafless, and then passing it into the expose restleaf function

let LL = require('leafless');
let restleaf = require('restleaf');
let app = new LL({});
 
let options = {
  version:1, // the api version
}
let rest = restleaf(app, options);
 
app.listen(process.env.PORT || 4000);

restleaf options

when you initialize restleaf you provide an instance of Leafless and a set of options the options include

version

options.version is the api version and is prependended infront of all your endpoints
so that if options.version === 1 then you endpoints will look like this http://domain/v1/resource/param
and if options.version === 12 then you endpoints will look like this http://domain/v12/resource/param

adding your models

a restleaf model is just a javascript object that exposes functions for performing CRUD operations on a certain resource
you add a model by calling the rest.addModel passing in the model name, and the object the functions expected on the object are

fetchAll() called when we want a Array of everything
fetchById(id) called when we want a resource identified by a specific id
insert(data) called when we are creating a new entry
update(id, data) called when we are updating an existing value
delete(id) called when we want to delete an item of the specied id

the functions are implemented as an object with a method called exec that is the javascript function that is actually called during execution

exec MUST always return a PROMISE

That is...

let rest = restleaf(app, options);
 
rest.addModel('users', {
  // return all users
  fetchAll: {
    exec() {
      return db.users.fetch();
    }
  },
 
  // get a user by id
  fetchById: {
    exec(id) {
      return db.users.get(id);
    }
  },
 
  // insert a new user
  insert: {
    exec(data) {
      return db.users.insert(data);
    }
  },
 
  // update and existing user
  update: {
    exec(id, data) {
      return db.users.get(id).update(data);
    }
  },
 
  // delete and exisitig user
  delete: {
    exec(data) {
      return db.users.get(id).delete();
    }
  }
})

Readme

Keywords

none

Package Sidebar

Install

npm i restleaf

Weekly Downloads

0

Version

0.0.3

License

MIT

Last publish

Collaborators

  • mofax