Skip to content

Instantly share code, notes, and snippets.

@alphacentory
Last active August 29, 2015 13:58
Show Gist options
  • Save alphacentory/9962822 to your computer and use it in GitHub Desktop.
Save alphacentory/9962822 to your computer and use it in GitHub Desktop.
orchestrate service layer for node.
/** Make sure that you have orchesrtate added to your package.json inside of sail.js **/
/**
*
* @This is a service script written for sailsjs.com framework for nodejs.
* @Author Tory Adams
**/
var db = require("orchestrate")("your token");
/**
* Graph creation.
*
* @param fromCol: is the from collection
* @param fromKey: is the key you are graphing from
* @param toCol: is the collection you graph to
* @param toKey: is the key you complete the graph to
* @param rel: is the relation of the graph between two records.
* @param callback: is callback function.
*
* @TODO add error handling.
*
**/
exports.graphBuilder = function (fromCol, fromKey, toCol, toKey, rel, callback) {
db.newGraphBuilder()
.create()
.from(fromCol, fromKey)
.related(rel)
.to(toCol, toKey)
.then(function (response){
if(typeof callback == "function"){
callback(response);
}
});
};
/**
*
* Record Store
*
* @param col: is the collection in which you are storing information.
* @param key: is the key which you will be able to pull your information.
* @param obj: is the "record" that you are storing in orchestrate.
* @param cb: is the callback.
*
* @TODO add error handling.
* @TODO move cb to callback.
*
**/
exports.recordPut = function (col, key, obj, cb) {
db.put(col, key, obj).then(function (results){
if(typeof cb == "function"){
cb('allgood');
}
});
};
/**
*
* Graph Reading
*
* @param col: is the collection
* @param key: is the key you are search for a graph relation from
* @param rel: is the relation of the graph between two records.
* @param cb: is callback function.
*
* @TODO add error handling.
*
**/
exports.graphReader = function (col, key, rel, cb){
db.newGraphReader()
.get()
.from(col, key)
.related(rel)
.then(function (results){
cb(results.body);
})
.fail(function (err){
console.log(err);
});
};
/**
*
* Basic Search
*
* @param col: is the collection you are searching.
* @param query: is the search query
* @param cb: is the callback
*
* @TODO add error handling.
*
**/
exports.graphSimpleSearch = function (col, query, cb) {
db.search(col, query)
.then(function (results){
cb(results.body);
});
};
/**
*
* Graph Removal
*
* @param fromCol: is the starting collection
* @param fromKey: is the starting key
* @param toCol: is the final collection
* @param toKey: is the final key
* @param rel: is the relation between the two records that will be removed.
* @param cb: callback
*
* @TODO add error handling.
*
**/
exports.graphRemoval = function (fromCol, fromKey, toCol, toKey, rel, cb) {
db.newGraphBuilder()
.remove()
.from(fromCol, fromKey)
.related(rel)
.to(toCol, toKey);
};
/**
*
* Record Get
*
* @param col: is the collection in which you are pulling information.
* @param key: is the key which you will be able to pull your information.
* @param obj: is the "record" that you are storing in orchestrate.
* @param callback: is the callback.
*
*
**/
exports.recordGet = function (col, key, callback) {
db.get(col, key)
.then(function (results){
callback(results.body);
}).fail(function (err){
callback(undefined);
});
};
/**
*
* Record Remove
*
* @param col: is the collection in which you are pulling information.
* @param key: is the key which you will be able to pull your information.
* @param callback: is the callback.
*
*
**/
exports.recordRemove = function (col, key, callback) {
db.remove(col, key)
.then(function (results){
if(typeof callback == "function"){
callback(results);
}
});
}
exports.recordSearch = function (col, query, offset, callback) {
db.newSearchBuilder()
.collection(col)
.limit(10)
.offset(offset)
.query(query)
.then(function (results){
console.log(results);
callback(results.body, offset);
})
.fail(function (err){
console.log(err);
});
};
exports.recordSearchHard = function (col, query, limit, offset, callback) {
db.newSearchBuilder()
.collection(col)
.limit(limit)
.offset(offset)
.query(query)
.then(function (results){
callback(results.body);
});
};
exports.eventBuilder = function (col, key, type, data, callback) {
db.newEventBuilder()
.from(col, key)
.type(type)
.data(data)
if(typeof callback == "function") {
callback();
}
}
@alphacentory
Copy link
Author

This service layer for sails.js is being constantly updated. more comments and function calls coming.

This built on top of the node.js module https://www.npmjs.org/package/orchestrate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment