eventric-store-lowdb
Version:
eventric LowDB Store Adapter
83 lines (58 loc) • 2.59 kB
text/coffeescript
lowdb = require 'lowdb'
fs = require 'fs'
class LowDBStore
_optionDefaults:
path: 'db.json'
initialize: (, [options]..., callback=->) ->
(options ?= {}),
= "#{@_contextName}.DomainEvents"
= "#{@_contextName}.Projection"
= lowdb
.path = options.path
if fs.existsSync options.path
.load options.path
callback null
_defaults: (options, optionDefaults) ->
allKeys = [].concat (Object.keys options), (Object.keys optionDefaults)
for key in allKeys when !options[key] and optionDefaults[key]
options[key] = optionDefaults[key]
saveDomainEvent: (domainEvent, callback) ->
callback null, .insert(domainEvent).value()
findAllDomainEvents: (callback) ->
callback null, .where().value()
findDomainEventsByName: (names, callback) ->
names = [names] if names not instanceof Array
results = []
for name in names
results = [].concat .where(name: name).value()
callback null, results
findDomainEventsByAggregateId: (aggregateIds, callback) ->
aggregateIds = [aggregateIds] if aggregateIds not instanceof Array
results = []
for aggregateId in aggregateIds
results = [].concat .where(aggregate: id: aggregateId).value()
callback null, results
findDomainEventsByAggregateName: (aggregateNames, callback) ->
aggregateNames = [aggregateNames] if aggregateNames not instanceof Array
results = []
for aggregateName in aggregateNames
results = [].concat .where(aggregate: name: aggregateName).value()
callback null, results
findDomainEventsByNameAndAggregateId: (names, aggregateIds, callback) ->
names = [names] if names not instanceof Array
aggregateIds = [aggregateIds] if aggregateIds not instanceof Array
results = []
for aggregateId in aggregateIds
for name in names
results = [].concat .where(
name: name
aggregate: id: aggregateId
).value()
callback null, results
getProjectionStore: (projectionName, callback) ->
callback null, "#{@_projectionCollectionName}.#{projectionName}"
clearProjectionStore: (projectionName, callback) ->
delete .db["#{@_projectionCollectionName}.#{projectionName}"]
.save()
callback()
module.exports = LowDBStore