Skip to content

Instantly share code, notes, and snippets.

@jeremydmiller
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremydmiller/96f676cc2ed653ccc364 to your computer and use it in GitHub Desktop.
Save jeremydmiller/96f676cc2ed653ccc364 to your computer and use it in GitHub Desktop.
Javascript Dialect for Projections in Postgresql
/*
ASSUMPTIONS:
- We want the definition of the projection to be "mobile", meaning that it could be executed
either in a PLV8 function in Postgresql, a Node.js web or console application, or anything that can host JS.
Even the browser I suppose if we ever wanted to do historical "replay" like Josh & I did on our Firefly
project
- I'm assuming that we can make a super lightweight analogue for Browserify inside of the Postgresql database so
we can write all Javascript as Node.js modules
- We'll do something to load the projection definitions into the database, but they'll primarily live as *.js
files
*/
// Projection across a stream of events
require('postgres-projections')
.projectStream('stream type1')
.named('name of projection')
.async() // this will be optional
.by({
$init: function(){
return {count:0, members:[]};
},
[Event Name]: function(state, evt){
// modify the existing state here
}
});
// Projection for a single type of event
// Could map additional event types to this
// projection name
require('postgres-projections')
.projectEvent('event name')
.named('name of projection')
.async() // again, optional
.by(function(evt){
// return a transformed version of the evt
});
// aggregate across all streams
// this is going to have to run serially in most cases
// Not sure this is completely a good idea
require('postgres-projections')
.aggregate('name of aggregate')
.async()
.by({
$init: function(){
// create an initial doc
},
[Event Name]: function(state, evt){
// transform the document
}
});
// aggregate across a category like client
// woulda loved to have had this on Portal
// EventStore has something like this
require('postgres-projections')
.aggregateByCategory('name')
.async()
.by({
$init = function(){},
[Event Name]: {
category: function(evt){
// what's the category? client id?
},
transform: function(state, evt){
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment