Last active
August 21, 2017 19:36
-
-
Save joshuaebowling/185afffb25cf867644aa112eb09bbc80 to your computer and use it in GitHub Desktop.
A prototype for building a JSON object what will act as a message bus's (postal.js) phonebook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require( 'lodash' ), | |
async = require( 'async' ), | |
fs = require('fs') | |
; | |
var actions, base, result, states, subActions, template, toObject, types; | |
result = {}; | |
template = `module.exports = ${JSON.stringify(_.omit(result, 'any'), '', 4)}`; | |
base = [ '*' ]; | |
toObject = function( arr ) { | |
var result = {}; | |
_.each( _.union( base, arr ), (t) => { | |
result[t.replace('*', 'any')] = t; | |
}); | |
return result; | |
}; | |
types = toObject( [ 'user', 'item', 'account', 'order', 'driver' ] ); | |
actions = toObject( [ 'save', 'delete', 'get' ] ); | |
subActions = toObject( [ 'one','many' ] ); | |
states = toObject( [ 'request', 'response', 'error', 'timeout', 'report'] ); | |
_.each(types, (type) => { | |
var typeKey = type.replace('*', 'any'); | |
result[typeKey] = {}; | |
result[typeKey].busKey = type; | |
_.each(actions, (action) => { | |
var actionKey = action.replace('*', 'any'); | |
result[typeKey][actionKey] = {}; | |
_.each(subActions, (subA) => { | |
var subAKey = subA.replace('*', 'any'); | |
result[typeKey][actionKey][subAKey] = {}; | |
_.each(states, (state) => { | |
var stateKey = state.replace('*', 'any'); | |
result[typeKey][actionKey][subAKey][stateKey] = `${type}.${action}.${subA}.${state}`; | |
}); | |
}); | |
}); | |
}); | |
fs.writeFileSync('bus-book.js',template , 'utf8'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
** PLEASE DONT TAKE ANYTHING IN THIS CODE FOR GRANTED. MOST OF THE COMMENTS ARE MISLEADING AT PRESENT. | |
**/ | |
const | |
postal = require('postal'), | |
bus = require('./bus') | |
; | |
// pickup ALL requests on user Channel | |
postal.channel(bus.user.busKey).subscribe(bus.any.any.any.request, ( payload, env) => { | |
console.log(env); | |
console.log('any request > ', payload); | |
}); | |
// pickup all requests for bus.order | |
postal.channel(bus.order.busKey).subscribe(bus.order.any.any.request, ( payload, env) => { | |
console.log(env); | |
console.log('any request > ', payload); | |
}); | |
// pickup ALL responses on ALL channels | |
postal.channel(bus.order.busKey).subscribe(bus.any.any.any.response, ( payload, env) => { | |
console.log(env); | |
console.log('any request > ', payload); | |
}); | |
// publish a user.save.one.request | |
// publish a driver.save.one.request | |
postal.channel(bus.driver.busKey).publish(bus.driver.save.one.request, {test: bus.driver.save.one.request}); | |
var publish = function(channel, topic, payload) { | |
if(topic.indexOf('*') < 0) throw new Error("You may not publish with * in the topic"); | |
postal.chanel(channel).publish(topic, payload); | |
}; | |
var itemPublish = function(topic, payload) { | |
publish(bus.item.busKey).topic(topic, payload); | |
}; | |
var objectActionPublish = function(channel, topic) { | |
return function(payload) { | |
publish(channel, topic, payload); | |
}; | |
}; | |
var save = function(busType, busTopic, payload) { | |
postal.channel(busType).publish(busTopic, {test: bus.user.save.one.request}); | |
}; | |
var saveUser = function(user) { | |
save(bus.user.busKey, bus.user.save.one.request, user); | |
}; | |
var saveDriver = function(driver) { | |
save(bus.driver.busKey, bus.driver.save.one.request, driver); | |
}; | |
// will now be a function that takes a user payload | |
var saveItem = objectActionPublish(bus.item.busKey, bus.item.save.one.request); | |
var saveItems = function(items) { | |
save(bus.item.busKey, bus.item.save.many.request, items); | |
}; | |
var onSave = function(busType, busTopic) { | |
postal.channel(busType).subscribe(busTopic, (payload, env) => { | |
}); | |
}; | |
var onSaveItem = function() { | |
// set all the subscriptions just for saving an item | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
"user": { | |
"busKey": "user", | |
"any": { | |
"any": { | |
"any": "user.*.*.*", | |
"request": "user.*.*.request", | |
"response": "user.*.*.response", | |
"error": "user.*.*.error", | |
"timeout": "user.*.*.timeout", | |
"report": "user.*.*.report" | |
}, | |
"one": { | |
"any": "user.*.one.*", | |
"request": "user.*.one.request", | |
"response": "user.*.one.response", | |
"error": "user.*.one.error", | |
"timeout": "user.*.one.timeout", | |
"report": "user.*.one.report" | |
}, | |
"many": { | |
"any": "user.*.many.*", | |
"request": "user.*.many.request", | |
"response": "user.*.many.response", | |
"error": "user.*.many.error", | |
"timeout": "user.*.many.timeout", | |
"report": "user.*.many.report" | |
} | |
}, | |
"save": { | |
"any": { | |
"any": "user.save.*.*", | |
"request": "user.save.*.request", | |
"response": "user.save.*.response", | |
"error": "user.save.*.error", | |
"timeout": "user.save.*.timeout", | |
"report": "user.save.*.report" | |
}, | |
"one": { | |
"any": "user.save.one.*", | |
"request": "user.save.one.request", | |
"response": "user.save.one.response", | |
"error": "user.save.one.error", | |
"timeout": "user.save.one.timeout", | |
"report": "user.save.one.report" | |
}, | |
"many": { | |
"any": "user.save.many.*", | |
"request": "user.save.many.request", | |
"response": "user.save.many.response", | |
"error": "user.save.many.error", | |
"timeout": "user.save.many.timeout", | |
"report": "user.save.many.report" | |
} | |
}, | |
"delete": { | |
"any": { | |
"any": "user.delete.*.*", | |
"request": "user.delete.*.request", | |
"response": "user.delete.*.response", | |
"error": "user.delete.*.error", | |
"timeout": "user.delete.*.timeout", | |
"report": "user.delete.*.report" | |
}, | |
"one": { | |
"any": "user.delete.one.*", | |
"request": "user.delete.one.request", | |
"response": "user.delete.one.response", | |
"error": "user.delete.one.error", | |
"timeout": "user.delete.one.timeout", | |
"report": "user.delete.one.report" | |
}, | |
"many": { | |
"any": "user.delete.many.*", | |
"request": "user.delete.many.request", | |
"response": "user.delete.many.response", | |
"error": "user.delete.many.error", | |
"timeout": "user.delete.many.timeout", | |
"report": "user.delete.many.report" | |
} | |
}, | |
"get": { | |
"any": { | |
"any": "user.get.*.*", | |
"request": "user.get.*.request", | |
"response": "user.get.*.response", | |
"error": "user.get.*.error", | |
"timeout": "user.get.*.timeout", | |
"report": "user.get.*.report" | |
}, | |
"one": { | |
"any": "user.get.one.*", | |
"request": "user.get.one.request", | |
"response": "user.get.one.response", | |
"error": "user.get.one.error", | |
"timeout": "user.get.one.timeout", | |
"report": "user.get.one.report" | |
}, | |
"many": { | |
"any": "user.get.many.*", | |
"request": "user.get.many.request", | |
"response": "user.get.many.response", | |
"error": "user.get.many.error", | |
"timeout": "user.get.many.timeout", | |
"report": "user.get.many.report" | |
} | |
} | |
}, | |
"item": { | |
"busKey": "item", | |
"any": { | |
"any": { | |
"any": "item.*.*.*", | |
"request": "item.*.*.request", | |
"response": "item.*.*.response", | |
"error": "item.*.*.error", | |
"timeout": "item.*.*.timeout", | |
"report": "item.*.*.report" | |
}, | |
"one": { | |
"any": "item.*.one.*", | |
"request": "item.*.one.request", | |
"response": "item.*.one.response", | |
"error": "item.*.one.error", | |
"timeout": "item.*.one.timeout", | |
"report": "item.*.one.report" | |
}, | |
"many": { | |
"any": "item.*.many.*", | |
"request": "item.*.many.request", | |
"response": "item.*.many.response", | |
"error": "item.*.many.error", | |
"timeout": "item.*.many.timeout", | |
"report": "item.*.many.report" | |
} | |
}, | |
"save": { | |
"any": { | |
"any": "item.save.*.*", | |
"request": "item.save.*.request", | |
"response": "item.save.*.response", | |
"error": "item.save.*.error", | |
"timeout": "item.save.*.timeout", | |
"report": "item.save.*.report" | |
}, | |
"one": { | |
"any": "item.save.one.*", | |
"request": "item.save.one.request", | |
"response": "item.save.one.response", | |
"error": "item.save.one.error", | |
"timeout": "item.save.one.timeout", | |
"report": "item.save.one.report" | |
}, | |
"many": { | |
"any": "item.save.many.*", | |
"request": "item.save.many.request", | |
"response": "item.save.many.response", | |
"error": "item.save.many.error", | |
"timeout": "item.save.many.timeout", | |
"report": "item.save.many.report" | |
} | |
}, | |
"delete": { | |
"any": { | |
"any": "item.delete.*.*", | |
"request": "item.delete.*.request", | |
"response": "item.delete.*.response", | |
"error": "item.delete.*.error", | |
"timeout": "item.delete.*.timeout", | |
"report": "item.delete.*.report" | |
}, | |
"one": { | |
"any": "item.delete.one.*", | |
"request": "item.delete.one.request", | |
"response": "item.delete.one.response", | |
"error": "item.delete.one.error", | |
"timeout": "item.delete.one.timeout", | |
"report": "item.delete.one.report" | |
}, | |
"many": { | |
"any": "item.delete.many.*", | |
"request": "item.delete.many.request", | |
"response": "item.delete.many.response", | |
"error": "item.delete.many.error", | |
"timeout": "item.delete.many.timeout", | |
"report": "item.delete.many.report" | |
} | |
}, | |
"get": { | |
"any": { | |
"any": "item.get.*.*", | |
"request": "item.get.*.request", | |
"response": "item.get.*.response", | |
"error": "item.get.*.error", | |
"timeout": "item.get.*.timeout", | |
"report": "item.get.*.report" | |
}, | |
"one": { | |
"any": "item.get.one.*", | |
"request": "item.get.one.request", | |
"response": "item.get.one.response", | |
"error": "item.get.one.error", | |
"timeout": "item.get.one.timeout", | |
"report": "item.get.one.report" | |
}, | |
"many": { | |
"any": "item.get.many.*", | |
"request": "item.get.many.request", | |
"response": "item.get.many.response", | |
"error": "item.get.many.error", | |
"timeout": "item.get.many.timeout", | |
"report": "item.get.many.report" | |
} | |
} | |
}, | |
"account": { | |
"busKey": "account", | |
"any": { | |
"any": { | |
"any": "account.*.*.*", | |
"request": "account.*.*.request", | |
"response": "account.*.*.response", | |
"error": "account.*.*.error", | |
"timeout": "account.*.*.timeout", | |
"report": "account.*.*.report" | |
}, | |
"one": { | |
"any": "account.*.one.*", | |
"request": "account.*.one.request", | |
"response": "account.*.one.response", | |
"error": "account.*.one.error", | |
"timeout": "account.*.one.timeout", | |
"report": "account.*.one.report" | |
}, | |
"many": { | |
"any": "account.*.many.*", | |
"request": "account.*.many.request", | |
"response": "account.*.many.response", | |
"error": "account.*.many.error", | |
"timeout": "account.*.many.timeout", | |
"report": "account.*.many.report" | |
} | |
}, | |
"save": { | |
"any": { | |
"any": "account.save.*.*", | |
"request": "account.save.*.request", | |
"response": "account.save.*.response", | |
"error": "account.save.*.error", | |
"timeout": "account.save.*.timeout", | |
"report": "account.save.*.report" | |
}, | |
"one": { | |
"any": "account.save.one.*", | |
"request": "account.save.one.request", | |
"response": "account.save.one.response", | |
"error": "account.save.one.error", | |
"timeout": "account.save.one.timeout", | |
"report": "account.save.one.report" | |
}, | |
"many": { | |
"any": "account.save.many.*", | |
"request": "account.save.many.request", | |
"response": "account.save.many.response", | |
"error": "account.save.many.error", | |
"timeout": "account.save.many.timeout", | |
"report": "account.save.many.report" | |
} | |
}, | |
"delete": { | |
"any": { | |
"any": "account.delete.*.*", | |
"request": "account.delete.*.request", | |
"response": "account.delete.*.response", | |
"error": "account.delete.*.error", | |
"timeout": "account.delete.*.timeout", | |
"report": "account.delete.*.report" | |
}, | |
"one": { | |
"any": "account.delete.one.*", | |
"request": "account.delete.one.request", | |
"response": "account.delete.one.response", | |
"error": "account.delete.one.error", | |
"timeout": "account.delete.one.timeout", | |
"report": "account.delete.one.report" | |
}, | |
"many": { | |
"any": "account.delete.many.*", | |
"request": "account.delete.many.request", | |
"response": "account.delete.many.response", | |
"error": "account.delete.many.error", | |
"timeout": "account.delete.many.timeout", | |
"report": "account.delete.many.report" | |
} | |
}, | |
"get": { | |
"any": { | |
"any": "account.get.*.*", | |
"request": "account.get.*.request", | |
"response": "account.get.*.response", | |
"error": "account.get.*.error", | |
"timeout": "account.get.*.timeout", | |
"report": "account.get.*.report" | |
}, | |
"one": { | |
"any": "account.get.one.*", | |
"request": "account.get.one.request", | |
"response": "account.get.one.response", | |
"error": "account.get.one.error", | |
"timeout": "account.get.one.timeout", | |
"report": "account.get.one.report" | |
}, | |
"many": { | |
"any": "account.get.many.*", | |
"request": "account.get.many.request", | |
"response": "account.get.many.response", | |
"error": "account.get.many.error", | |
"timeout": "account.get.many.timeout", | |
"report": "account.get.many.report" | |
} | |
} | |
}, | |
"order": { | |
"busKey": "order", | |
"any": { | |
"any": { | |
"any": "order.*.*.*", | |
"request": "order.*.*.request", | |
"response": "order.*.*.response", | |
"error": "order.*.*.error", | |
"timeout": "order.*.*.timeout", | |
"report": "order.*.*.report" | |
}, | |
"one": { | |
"any": "order.*.one.*", | |
"request": "order.*.one.request", | |
"response": "order.*.one.response", | |
"error": "order.*.one.error", | |
"timeout": "order.*.one.timeout", | |
"report": "order.*.one.report" | |
}, | |
"many": { | |
"any": "order.*.many.*", | |
"request": "order.*.many.request", | |
"response": "order.*.many.response", | |
"error": "order.*.many.error", | |
"timeout": "order.*.many.timeout", | |
"report": "order.*.many.report" | |
} | |
}, | |
"save": { | |
"any": { | |
"any": "order.save.*.*", | |
"request": "order.save.*.request", | |
"response": "order.save.*.response", | |
"error": "order.save.*.error", | |
"timeout": "order.save.*.timeout", | |
"report": "order.save.*.report" | |
}, | |
"one": { | |
"any": "order.save.one.*", | |
"request": "order.save.one.request", | |
"response": "order.save.one.response", | |
"error": "order.save.one.error", | |
"timeout": "order.save.one.timeout", | |
"report": "order.save.one.report" | |
}, | |
"many": { | |
"any": "order.save.many.*", | |
"request": "order.save.many.request", | |
"response": "order.save.many.response", | |
"error": "order.save.many.error", | |
"timeout": "order.save.many.timeout", | |
"report": "order.save.many.report" | |
} | |
}, | |
"delete": { | |
"any": { | |
"any": "order.delete.*.*", | |
"request": "order.delete.*.request", | |
"response": "order.delete.*.response", | |
"error": "order.delete.*.error", | |
"timeout": "order.delete.*.timeout", | |
"report": "order.delete.*.report" | |
}, | |
"one": { | |
"any": "order.delete.one.*", | |
"request": "order.delete.one.request", | |
"response": "order.delete.one.response", | |
"error": "order.delete.one.error", | |
"timeout": "order.delete.one.timeout", | |
"report": "order.delete.one.report" | |
}, | |
"many": { | |
"any": "order.delete.many.*", | |
"request": "order.delete.many.request", | |
"response": "order.delete.many.response", | |
"error": "order.delete.many.error", | |
"timeout": "order.delete.many.timeout", | |
"report": "order.delete.many.report" | |
} | |
}, | |
"get": { | |
"any": { | |
"any": "order.get.*.*", | |
"request": "order.get.*.request", | |
"response": "order.get.*.response", | |
"error": "order.get.*.error", | |
"timeout": "order.get.*.timeout", | |
"report": "order.get.*.report" | |
}, | |
"one": { | |
"any": "order.get.one.*", | |
"request": "order.get.one.request", | |
"response": "order.get.one.response", | |
"error": "order.get.one.error", | |
"timeout": "order.get.one.timeout", | |
"report": "order.get.one.report" | |
}, | |
"many": { | |
"any": "order.get.many.*", | |
"request": "order.get.many.request", | |
"response": "order.get.many.response", | |
"error": "order.get.many.error", | |
"timeout": "order.get.many.timeout", | |
"report": "order.get.many.report" | |
} | |
} | |
}, | |
"driver": { | |
"busKey": "driver", | |
"any": { | |
"any": { | |
"any": "driver.*.*.*", | |
"request": "driver.*.*.request", | |
"response": "driver.*.*.response", | |
"error": "driver.*.*.error", | |
"timeout": "driver.*.*.timeout", | |
"report": "driver.*.*.report" | |
}, | |
"one": { | |
"any": "driver.*.one.*", | |
"request": "driver.*.one.request", | |
"response": "driver.*.one.response", | |
"error": "driver.*.one.error", | |
"timeout": "driver.*.one.timeout", | |
"report": "driver.*.one.report" | |
}, | |
"many": { | |
"any": "driver.*.many.*", | |
"request": "driver.*.many.request", | |
"response": "driver.*.many.response", | |
"error": "driver.*.many.error", | |
"timeout": "driver.*.many.timeout", | |
"report": "driver.*.many.report" | |
} | |
}, | |
"save": { | |
"any": { | |
"any": "driver.save.*.*", | |
"request": "driver.save.*.request", | |
"response": "driver.save.*.response", | |
"error": "driver.save.*.error", | |
"timeout": "driver.save.*.timeout", | |
"report": "driver.save.*.report" | |
}, | |
"one": { | |
"any": "driver.save.one.*", | |
"request": "driver.save.one.request", | |
"response": "driver.save.one.response", | |
"error": "driver.save.one.error", | |
"timeout": "driver.save.one.timeout", | |
"report": "driver.save.one.report" | |
}, | |
"many": { | |
"any": "driver.save.many.*", | |
"request": "driver.save.many.request", | |
"response": "driver.save.many.response", | |
"error": "driver.save.many.error", | |
"timeout": "driver.save.many.timeout", | |
"report": "driver.save.many.report" | |
} | |
}, | |
"delete": { | |
"any": { | |
"any": "driver.delete.*.*", | |
"request": "driver.delete.*.request", | |
"response": "driver.delete.*.response", | |
"error": "driver.delete.*.error", | |
"timeout": "driver.delete.*.timeout", | |
"report": "driver.delete.*.report" | |
}, | |
"one": { | |
"any": "driver.delete.one.*", | |
"request": "driver.delete.one.request", | |
"response": "driver.delete.one.response", | |
"error": "driver.delete.one.error", | |
"timeout": "driver.delete.one.timeout", | |
"report": "driver.delete.one.report" | |
}, | |
"many": { | |
"any": "driver.delete.many.*", | |
"request": "driver.delete.many.request", | |
"response": "driver.delete.many.response", | |
"error": "driver.delete.many.error", | |
"timeout": "driver.delete.many.timeout", | |
"report": "driver.delete.many.report" | |
} | |
}, | |
"get": { | |
"any": { | |
"any": "driver.get.*.*", | |
"request": "driver.get.*.request", | |
"response": "driver.get.*.response", | |
"error": "driver.get.*.error", | |
"timeout": "driver.get.*.timeout", | |
"report": "driver.get.*.report" | |
}, | |
"one": { | |
"any": "driver.get.one.*", | |
"request": "driver.get.one.request", | |
"response": "driver.get.one.response", | |
"error": "driver.get.one.error", | |
"timeout": "driver.get.one.timeout", | |
"report": "driver.get.one.report" | |
}, | |
"many": { | |
"any": "driver.get.many.*", | |
"request": "driver.get.many.request", | |
"response": "driver.get.many.response", | |
"error": "driver.get.many.error", | |
"timeout": "driver.get.many.timeout", | |
"report": "driver.get.many.report" | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const | |
bus = require('./bus'), | |
_ = require('lodash'), | |
postal = require('postal'), | |
Promise = require('bluebird') | |
; | |
var _data = [ | |
{name: 'test-user-1'}, | |
{name: 'test-user-2'}, | |
{name: 'test-user-3'} | |
]; | |
var notImplemented = function(address) { | |
throw new Error(`Not Implemented: ${address}`); | |
}; | |
var implemented = { | |
[bus.user.save.one.request] : function (user) { | |
user.index = _data.length | |
_data.push(user); | |
postal.channel(bus.user.busKey).publish(bus.user.save.one.response, user); | |
}, | |
[bus.user.get.one.request]: function (crit) { | |
var result = _.find(_data, crit); | |
postal.channel(bus.user.busKey).publish(bus.user.get.one.response, result); | |
}, | |
[bus.user.get.many.request]: function (crit) { | |
var result = _.filter(_data, crit); | |
postal.channel(bus.user.busKey).publish(bus.user.get.one.response, result); | |
}, | |
[bus.user.delete.one.request]: function (crit) { | |
var dataLength = _data.length; | |
_.remove(_data, crit); | |
postal.channel(bus.user.busKey).publish(bus.user.delete.one.response, {affected: dataLength - _data.length}); | |
} | |
}; | |
var publish = function(address, payload) { | |
if(implemented[address]) { | |
implemented[address](payload); | |
} | |
else { | |
notImplemented(address); | |
} | |
}; | |
var subscribe = function(address) { | |
// if(!bus.glossary[address]) throw new Error(`Address Not Implemented: ${address}`) | |
return new Promise((resolve, reject) => { | |
postal.channel(bus.user.busKey).subscribe(address, (data, env) => { | |
resolve({data: data, env: env}); | |
}); | |
}); | |
}; | |
module.exports = { | |
publish: publish, | |
subscribe: subscribe, | |
channel: postal.channel(bus.user.busKey) | |
}; |
add blacklist feature to generator to specify any strings, and thus any entries in the map for given operations.
for example, if only one user should be saved at a time (bus.user.save.one.request) then adding 'bus.user.save.many.request' to the blacklist remove explicitly and well as implicit (if no bus.user.save.many.request then no bus.user.save.many.response)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bus-book-generator.js yields bus-book.js