# restleaf

> a nodejs rest api toolkit

Latest version **0.0.3** (published 2017-03-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install restleaf
pnpm add restleaf
yarn add restleaf
bun add restleaf
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2017-03-02 |
| First published | 2017-03-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Alloys Mila |
| Maintainers | mofax |

## Links

- npm: https://www.npmjs.com/package/restleaf
- Repository: https://github.com/mofax/restleaf
- Homepage: https://github.com/mofax/restleaf#readme
- Issues: https://github.com/mofax/restleaf/issues
- npm.io page: https://npm.io/package/restleaf

## Recent versions

- 0.0.3 (latest) — 2017-03-02
- 0.0.2 — 2017-03-02
- 0.0.1 — 2017-03-01

## README

# 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
```bash
$ npm install leafless
# or with yarn
$ yarn add leafless
```

Then install restleaf
```bash
$ 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
```js
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...

```js
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();
    }
  }
})
```

---
_Source: https://npm.io/package/restleaf · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
