The Substance Store is being used to persist Substance Documents. It is used for the Substance Composer to store documents locally, as well as on the serverside for the shared document store.
There are two implementations so far, one that uses a native extension for Redis (Substance.RedisStore
) and one that talks to HTTP (Substance.RemoteStore
).
Prerequisites
- Install Node.js 0.8.x
- PCRE (sudo port install pcre)
- Automake (sudo port install automake)
- Autoconf (sudo port install autoconf)
Install (Mac OSX)
$ npm install https://github.com/substance/store.git
Use with Node:
var store = require('substance-store');
Usage
Create a new Store
We'll be using the RemoteStore
implementation, since it runs in any browser.
var store = new Substance.RemoteStore({
client: new Substance.Client({
hub_api: "https://substance.io/api/v1",
token: "YOUR_SESSION_TOKEN"
});
});
Clear store
store.clear(function(err) {
console.log('store is now empty');
});
Create a new document
store.create("hello-world", function(err, doc) {
console.log('new doc', doc);
});
Update document (store new commits)
var commits = [{
"op": [
"insert",
{
"id": "heading:42c72d87e40f529dba27a9970c0a6ef3",
"type": "heading",
"data": {
"content": "Hello World?"
}
}
],
"sha": "b0a4df43adba704eaef6809ada25bc4a",
"parent": "46d783ce5341c6318199e1390a3c51ba"
}];
var meta = {
"updated_at": new Date()
};
var refs = {
"master": {
"head": "b0a4df43adba704eaef6809ada25bc4a",
"last": "b0a4df43adba704eaef6809ada25bc4a"
}
}
store.update("hello-world", commits, meta, refs function(err) {
});
Get an existing document, complete with commits and references
store.get("hello-world", function(err, doc) {
console.log('your doc', doc);
});
Set reference
You can easily store custom references that point to a certain commit.
store.setRef("edition-2", "b0a4df43adba704eaef6809ada25bc4a", function(err) {
});
Get reference
store.getRef("edition-2", function(err, sha) {
});
Update Metadata
You can easily update metadata on your document like so:
store.updateMeta("hello-world", {"author": "Michael Aufreiter"}, function(err) {
});
Delete Document
store.delete("hello-world", function(err) {
});
Document gets deleted but is remembered in a deleted-documents
list. This is needed for replication, so your deletion gets propagated to other stores. You can access that list by calling store.deletedDocuments(cb)
or confirm your deletion by calling store.confirmDeletion(docId, cb)
.
List commit range
store.commits("hello-world", "b0a4df43adba704eaef6809ada25bc4a", "46d783ce5341c6318199e1390a3c51ba", function(err, commits) {
console.log('commits', commits);
});
Create blob
store.createBlob("my-blob", "BASE64_BLOBDATA", function(err) {
console.log('Stored blob');
});
Get blob
store.getBlob("my-blob", function(err, blob) {
console.log('blob', blob);
});
List blobs
store.listBlobs(function(err, blobs) {
console.log('available blobs', blobs);
});
Delete blob
store.deleteBlob("my-blob", function(err) {
console.log('Deleted blob');
});