base-domain
Version:
simple module to help build Domain-Driven Design
88 lines (64 loc) • 2.41 kB
text/coffeescript
'use strict'
BaseSyncRepository = require './base-sync-repository'
###*
Master repository: handling static data
Master data are loaded from master-data directory (by default, it's facade.dirname + /master-data)
These data should be formatted in Fixture.
Master data are read-only, so 'save', 'update' and 'delete' methods are not available.
(And currently, 'query' and 'singleQuery' are also unavailable as MemoryResource does not support them yet...)
MasterRepository
BaseSyncRepository
base-domain
###
class MasterRepository extends BaseSyncRepository
###*
Name of the data in master data.
is used if not set.
{String} dataName
###
: null
constructor: ->
super
{ master } =
if not master?
throw ('masterNotFound', """
MasterRepository is disabled by default.
To enable it, set the option to Facade.createInstance() like
Facade.createInstance(master: true)
""")
dataName = .dataName ? .modelName
= master.getMemoryResource(dataName)
###*
Update or insert a model instance
Save data with "fixtureInsertion" option. Otherwise throw an error.
save
###
save: (data, options = {}) ->
if options.fixtureInsertion
super(data, options)
else
throw ('cannotSaveWithMasterRepository', 'base-domain:cannot save with MasterRepository')
###*
Destroy the given entity (which must have "id" value)
delete
{Entity} entity
{ResourceClientInterface} [client=]
{Boolean} isDeleted
###
delete: ->
throw ('cannotDeleteWithMasterRepository', 'base-domain:cannot delete with MasterRepository')
###*
Update set of attributes.
update
{String|Number} id of the entity to update
{Object} data key-value pair to update (notice: this must not be instance of Entity)
{ResourceClientInterface} [client=]
{Entity} updated entity
###
update: ->
throw ('cannotUpdateWithMasterRepository', 'base-domain:cannot update with MasterRepository')
module.exports = MasterRepository