用于數據同步的統一通信接口:Mesh

jopen 9年前發布 | 11K 次閱讀 Mesh JavaScript開發

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.

項目主頁:http://www.baiduhome.net/lib/view/home/1428458205119

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!