@oada/oada-cache
node library for interacting with and locally caching data served on an oada-compliant server
Last updated 3 months ago by cyrusbowman .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @oada/oada-cache 
SYNC missed versions from official npm registry.

oada-cache

A client tool for interacting with an OADA server instance. It can be used for fully cached web applications, data import scripts, IoT devices streaming data, and back-end microservices.

Installation

npm install @oada/oada-cache

var oada = require("@oada/oada-cache")

connect

var connection = await oada.connect({
  domain: "https://api.oada.com",
  options: {
    redirect: "http://localhost:8000/oauth2/redirect.html",
    metadata: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    scope: "oada.yield:all"
  }
})

This will initiate OADA OAUTH-style authentication.

get

var response = await connection.get({path: '/bookmarks/test'})

put

var response = await connection.put({
  path: "/bookmarks/test",
  data: { "power_level": 9001},
  headers: {"Content-Type": "application/json"}
})

post

var response = await connection.post({
  path: "/bookmarks/test",
  data: { "power_level": 9001},
  headers: {"Content-Type": "application/json"}
})
var  = response.headers["content-location"]; // A path including the uuid created

delete

var response = await connection.delete({
  path: "/bookmarks/test",
  headers: {"Content-Type": "application/json"}
})

"Advanced" API

tree

A tree option can be used in many of the oada-cache API calls as a means to specify the data structure you intend to create or pull from the OADA server. It is used to represent a particular subgraph on the sever. The use of a _type key in the tree indicates that a resource break exists at this particular level of the tree and a link should be used to connect the parent resource to the resource(s) at this location. A _rev key in the tree indicates that versioned links should be created at this location; versioned links indicate that the parent will be updated with new revision numbers (_rev) each time the linked child resource is altered. A * key in the tree represents a placeholder when a particular key cannot be specified.

var todoTree = {
  "bookmarks": { //Represents a user's "home" directory
    "_type": "application/vnd.oada.bookmarks.1+json",
    "_rev": 0,
    "todoList": {
      "_type": "application/vnd.oada.todoList.1+json",
      "_rev": 0,
      "todo-index": {
        "*": {
          "_type": "application/vnd.oada.todoItem.1+json",
      	  "_rev": 0,
        }
      }
    }
  }
};

get

Using the tree option for recursive retrieval of en entire OADA subgraph:

var response = await connection.get({
  path: "/bookmarks/todoList",
  tree: todoTree
})

GETs that include this tree option will recursively retreive and merge the data into the object returned. In this example, the tree option tells it to recursively GET all of the data below the object at the given path. The * key matches all keys in the todo-index object; since each of these are a link to a resource, GETs will be performed to retreive each of these resources.

put

Using the tree option to execute a "smart PUT", ensuring that all parent resources will be created on the server.

var response = await connection.put({
  path: "/bookmarks/todoList/todo-index/fj029j52-mc20m23c23",
  data: {"order": 1, text: "get groceries"},
  headers: {"Content-Type": "application/vnd.oada.todoItem"},
  tree: todoTree
})

A PUT of this variety may actually result in several PUTs. For example, when /bookmarks is an empty object, the above PUT request will resources for the objects at /bookmarks/todoList and /bookmarks/todoList/todo-index/fj029j52-mc20m23c23.

post

POSTs including the tree option are identical to PUT requests except a uuid is appended to the given request path.

delete

Using the tree option on a DELETE request will recursively DELETE all resources and links below the request path.

var response = await connection.delete({
  path: "/bookmarks/todoList",
  headers: {"Content-Type": "application/vnd.oada.todoList.1+json"},
  tree: todoTree
})

Watch

Resource watching provides a change feed of updates on demand from the resource rooted at the given path. Watches are implemented as an extension of a GET request when the 'watch' key is supplied. When a watch is established, it will automatically keep the cache synced with the incoming change feed. When a watch is initiated, the current _rev of the resources is automatically passed along, and any remote changes will be pushed down to bring that resource up to date. The optional callback key of the watch object is used to supply a callback function as changes are received; this callback receives a payload argument containing the change. The payload key of the watch object is used to supply additional data to the callback payload.

const watchHandler = function(payload) {
  console.log(payload); // {foo: 'bar', response: {...}, request: {...}}
  console.log(payload.response.change.type); // Either 'merge' or 'delete' given the particular change that occurred.
  console.log(payload.response.change.body); //JSON object rooted at the watch path '/bookmarks/todoList'; For deletes, this sparse tree terminates at a key with the value `null` to indicate the deleted key.
}

var response = await connection.get({
  path: "/bookmarks/todoList",
  watch: {
    payload: { foo: 'bar' }
    callback: watchHandler
  }
});

Developing against an application

To further develop this library against e.g., a runnning application, utilize the npm run build-watch command to continuously build the source code. Utilize the npm run dev command to watch for changes to the built code and copy it into another project directory (e.g., node_modules of an application being developed); you must first set the APP_DIR environment variable to the root path of a directory containing a node_modules directory.

Current Tags

  • 4.0.0                                ...           latest (a month ago)

50 Versions

  • 4.0.0                                ...           a month ago
  • 3.1.8                                ...           2 months ago
  • 3.1.7                                ...           2 months ago
  • 3.1.6                                ...           3 months ago
  • 3.1.5                                ...           3 months ago
  • 3.1.4                                ...           3 months ago
  • 3.1.3                                ...           3 months ago
  • 3.1.2                                ...           4 months ago
  • 3.1.1                                ...           4 months ago
  • 3.1.0                                ...           4 months ago
  • 3.0.1                                ...           7 months ago
  • 3.0.0                                ...           7 months ago
  • 2.1.0                                ...           a year ago
  • 2.0.10                                ...           a year ago
  • 2.0.9                                ...           a year ago
  • 2.0.8                                ...           2 years ago
  • 2.0.7                                ...           2 years ago
  • 2.0.6                                ...           2 years ago
  • 2.0.5                                ...           2 years ago
  • 2.0.4                                ...           2 years ago
  • 2.0.3                                ...           2 years ago
  • 2.0.2                                ...           2 years ago
  • 2.0.1                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 1.3.1                                ...           2 years ago
  • 1.3.0                                ...           2 years ago
  • 1.2.3                                ...           2 years ago
  • 1.2.2                                ...           2 years ago
  • 1.2.1                                ...           2 years ago
  • 1.2.0                                ...           2 years ago
  • 1.1.9                                ...           2 years ago
  • 1.1.8                                ...           2 years ago
  • 1.1.7                                ...           2 years ago
  • 1.1.5                                ...           2 years ago
  • 1.1.4                                ...           2 years ago
  • 1.1.3                                ...           2 years ago
  • 1.1.2                                ...           2 years ago
  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.10                                ...           2 years ago
  • 1.0.9                                ...           2 years ago
  • 1.0.8                                ...           2 years ago
  • 1.0.7                                ...           2 years ago
  • 1.0.6                                ...           2 years ago
  • 1.0.5                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 1
Last Month 2
Dependencies (17)
Dev Dependencies (2)

Copyright 2014 - 2017 © taobao.org |