@tailored-apps/rethinkable
v1.1.1
Published
Util collection for APIs using RethinkDB (Model, CRUD Router, CRUD Pluralized Router).
Downloads
6
Readme
rethinkable
rethinkable is a model and CRUD helper library for rethinkdb and Koa, using Koa-Router].
Installation and Updating
npm install @tailored-apps/rethinkable
Usage
Configuration
Once you should set the connection and if you want your custom logger instance to the library.
Therefor the library provides you 2 functions setConnection(__connectioon__)
and setLogger(__logger__)
.
Without the connection the library will not be able to conenct to your database.
Model
I would prefer you to create your own abstract model which extends from the Model
provided by the library.
At this place you could setup your configuration and some more optional custom changes.
import { Model, setLogger, setConnection } from 'rethinkable'
import { getLogger } from './logger'
import { getConnection ] from './database'
setLogger(getLogger())
setConnection(getConnection())
export default class Abstract extends Model {
// general custom changes could be implemented hiere
}
Per default your models should be named (uppercase first character) like your tables.
The id
property will be handled from the library's model class.
import Abstract from './Abstract'
export default class TableName extends Abstract {
constructor ({ name1, name2, name3, ...other }) {
super(other)
this.name1 = name1
this.name2 = name2
this.name3 = name3
}
}
Router and RouterPluralized
In general each router is an instance of the koa-router. Like for models I would prefer you to create your own abstract router, at least to set the require path for your models.
import Router from 'rethinkable/RouterPluralized'
export default class Abstract extends Router {
get modelsRequirePath () {
return '../models'
}
}
Like models, the router should be named (uppercase first character) like your tables, to work out of the box with your previously generated models.
import Abstract from './Abstract'
esport default class TableName extends Abstract {}
After all classes are created, you have to enable router's route to your koa-router.
import Router from 'koa-router'
import TableNameRouter from './routers/TableName'
const router = new TableNameRouter({ prefix: '/tableName', listFields: 'id,name1' }).enableRoutes()
export default new Router()
.use(router.routes(), router.allowedMethods())
Router Configuration
The router class could initialized with some configuration options:
| Option | Description | | ------------- | ---------------------------------------------------------------------------- | | middleware | a list of middleware which should be executed before the router calls | | routes | a list of all routes which should be enables | | modifiers | a modifier function, which prepare each returned entry | | offset | the default offset value for the list call (default: 0) | | limit | the default limit value for the list call (default: 10) | | listFields | a comma separated list of values, which should be returned on the list call |
Routes
| Route | HTTP Method | Path |
| ------- | ----------- | ------ |
| load | GET | /
|
| loadAll | GET | /:id
|
| create | POST | /
|
| replace | PUT | /
|
| update | PATCH | /:id
|
| remove | DELETE | /:id
|