Mithink
npm install mithink --save
A plug-n-play module which aims for Meteor.js like capabilities without the overhead for transparently syncing a RethinkDB instance across clients using Socket.IO and Mithril.js on the client-side out of the box, but it's easy enough to roll your own adapter for Angular/Ember/Vue/whatever the hot framework of the week is.
Under The Hood
server
On the server mithink
uses the excellent thinky module for interaction with RethinkDB. It should always have the same API with a few additions to add functionality, rather than replicating it. We are using socket.io rooms to separate table events from the global event space.
Example usage:
rethinkdb = require"mithink"configdevio = require'socket.io' # set up the event bus rethinkdbbusio # this returns an extended version of the thinky Model Thing = rethinkdbcreateModel "Thing" name : rethinkdbtypestring id : rethinkdbtypenumber active : rethinkdbtypeboolean # add access control Thingguard : return true if socketsessionauthenticated return false : return true if socketsessionisAdmin return false : return true is socketsessionwhatever return false # Async access control transparently handles Promises : return Dosomestuffthen if passed return resolvetrue # if you want to override the default error message # for this check in your logs because it is special reject message: "you failed the Dosomestuff check" # add or override actions ThingregisterActions : # a special query @socketemit "reduction"fancyData # override default action : # context includes: # @model { the model of the current context } # @socket { the socket that emitted the event }
client
On the client mithink
caches the synced data in memory after connection and loading using socket.io in the background. You must include socket.io in your webapp, as mithink
makes no assumptions about the version you want to use or how you want to deliver it to the client.
example usage:
# connect to socket.io r = require'mithink' # for no conflict & sharing the same rendering engine # otherwise mithink cannot redraw diff changes from the server in the background. # you can set this to your own redrawing function if needed r.redraw = mredraw # what to do when an "http_error" event occurs in the background = mroute "/error" # a normal mithril controller/view module Things = : -> # ask mithink to lazy load the Things table @list = rtable"Things"load = rtable"Things"update @set'name'evttargetvalue return @ : return m 'h1''loading...' if ctrllistisLoading m 'ol.things'ctrllistmap m 'li' m 'input'onchange: ctrlupdatebindthingvalue: thingget'name' mmountdocumentgetElementById"things"Things
custom events & other random examples
rrequire'mithink'r.redraw = mredraw # register a global default event handler = # do your fancy stuff invoices = rtable'Invoices' invoiceson "pivot" rtable"Invoices:pivot"resetdatafinishLoading rredraw # create a virtual table on the client # it will be initalized in a loading state which you must manually reset pivot = rtable"Invoices:pivot" # emit the pivot event to the server rtable'Invoices'emit'pivot' # you can then use the r.table("Invoices:pivot") table in your views
Client-side Inbound Events Catalog
load
populdate a client-side table from RethinkDB. You can add params to it which are passed to a filter
sync
When an update is attempted, but failed due to any reason, the table on the client side resyncs the document that failed to update back to the originally value and emits an http_error
destroy
Whenever a document is deleted from RethinkDB, this event is received and updates the client-side table replicate if the document exists in the table replicate.
upsert
Whenever a document was updated or created on the server. It transparently adds it to the r.table(tableName)
in the background and triggers a redraw (if there are no diffs in the HTML this is a no-op).
http_error
Anytime something unexpected occurs, from a failed permission check to a failed validation check. The event data returned is structured like this:
code : code message : errmessage raw : err data : data table : "Things" action : "action"
Server-side Inbound Events Catalog
The defaults are as follows:
load
create
update
destroy
sync
http_error
A variety of situations can cause an http_error
event to be emitted from the server, a few examples are anytime thinky
catches an error, or a permission check is not passed.
Roadmap
v1.1 (TTL: < 3 Weeks)
- Documentation
- Test coverage
- Wercker CI integration
delete
event will be migrated to thedestroy
event
I mistakenly published the first version using the delete
keyword in several places because of how easy it is to use in Coffeescript, but I would like to make it as easy to use to ES6/Vanilla JS as possible, so in the next release we will be migrating from that.