DailyJS

Mesh.js

Alex R. Young

Subscribe

@dailyjs

Facebook

Google+

database libraries modules

Mesh.js

Posted by Alex R. Young on .
Featured

database libraries modules

Mesh.js

Posted by Alex R. Young on .

Mesh

Mesh (GitHub: mojo-js/mesh.js, License: MIT, npm: mesh) by Craig Condon is a data synchronisation library that caters for server-side and client-side requirements. The author has pitched it as the "underscore of data", meaning it's a highly focused library that you can drop in to existing applications.

It covers both storage and transit. For example, in the browser you could use localStorage, and Socket.IO to broadcast data to other clients. The supported databases are in-memory, localStorage, and MongoDB. If you want to use Mesh with a database that isn't supported, then you can write a new database adapter based on a CRUD API.

The API is based on CRUD operations and Node-like streams. Let's say you want to store data to local storage:

var mesh = require('mesh');  
var storage = require('mesh-local-storage');  
var bus = storage();  

You can now insert data (persisted to local storage) like this:

bus({  
  name: 'insert',
  collection: 'messages',
  data: { text: 'hello world' }
});

But what if you wanted to also send data over Socket.IO to a server? All you need is a suitable adapter, and to wire it up with one of Mesh's stream merging methods:

var realtime = require('mesh-socket.io');  
var mergedBus = mesh.parallel(  
  bus,
  realtime({ channel: 'operations' }, bus)
);

mergedBus({  
  name: 'insert',
  collection: 'messages',
  data: { text: 'hello everyone' }
});

The main motivation behind this library is to make data sources interoperable with one another, so I'd like to see what this looks like with a React application in the client, and a MongoDB-based REST API on the server.