1.1.1 • Published 6 years ago

@tailored-apps/rethinkable v1.1.1

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
6 years ago

rethinkable

js-standard-style

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:

OptionDescription
middlewarea list of middleware which should be executed before the router calls
routesa list of all routes which should be enables
modifiersa modifier function, which prepare each returned entry
offsetthe default offset value for the list call (default: 0)
limitthe default limit value for the list call (default: 10)
listFieldsa comma separated list of values, which should be returned on the list call

Routes

RouteHTTP MethodPath
loadGET/
loadAllGET/:id
createPOST/
replacePUT/
updatePATCH/:id
removeDELETE/:id