Last active
August 29, 2015 14:02
-
-
Save jeremydmiller/96f676cc2ed653ccc364 to your computer and use it in GitHub Desktop.
Javascript Dialect for Projections in Postgresql
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
/* | |
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