用于数据同步的统一通信接口:Mesh
jopen
10年前
Mesh是一个统一接口用于数据源之间的通信,不管它是你的API,mongodb, pubnub, webrtc, socket.io, redis, 或本地存储。轻松构建高级的功能,例如离线模式,实时数据,回滚等。
Mesh是完全可定制的,并且不关心数据源的内部是如何工作的。您可以轻松地构建自己的API适配器来实现与其它数据源进行交互。
Here's a basic example of how you might implement an API that caches temporarily to local storage:
var mesh = require("mesh"); var http = require("mesh-http"); var localStorage = require("mesh-local-storage"); // local storage cache - keep stuff for one minute max var cache = localStorage({ ttl: 1000 * 60 }); var api = http({ prefix: "/api" }); // pipe all persistence operations to the cache api(mesh.op("tail")).pipe(mesh.open(cache)); // the DB we'll use, return the first result returned, and // only pass 'load' operations to the cache var db = mesh.first(mesh.accept("load", cache), api); db(mesh.op("insert", { collection: "people" // path is automatically resolved from the collection param, // but you can easily override it. path: "/people", // POST is resolved from the operation name, but it's // also overridable method: "POST", data: { name: "john" } })).on("data", function(personData) { // load the person saved. This should result in a cache // hit for local storage. Also note that the HTTP path & method // will automatically get resolved. db(mesh.op("load", { collection: "people", query: { name: person.name } })). on("data", function(personData) { // do stuff with data }); });
Highlights
- Streamable interface.
- Works with any library, or framework.
- Works on any platform.
- Tiny (11kb).
- Works nicely with other stream-based libraries such as highland.
- Isomorphic. Easily use different databases for different platforms.
- Easily testable. Stub out any database for a fake one.
- Simple design. Use it for many other things such as an event bus, message-queue service, etc.