Skip to content

Instantly share code, notes, and snippets.

@iantocristian
Last active March 10, 2016 15:18
Show Gist options
  • Save iantocristian/e698959be31d53f5bba1 to your computer and use it in GitHub Desktop.
Save iantocristian/e698959be31d53f5bba1 to your computer and use it in GitHub Desktop.
Seneca snippets

Extract actual arguments from args:

seneca.add({role:'api', cmd:'ping'}, function(args, cb) { 
  var actualargs = seneca.util.argprops({}, args, {}, 'role, cmd')
})

argprops signature: argprops(defaults, args, fixed, omit) defaults, args, and fixed are deepextended together in that order

Clean object of props$

seneca.clean(obj)

Contextual seneca

seneca.add({role:'api', cmd:'ping'}, function(args, cb) { 
  var seneca = this;
})

http endpoints, respond with different status code

seneca.add({role:’api’, end: 'end'}, function(args, done) { 
  done(null, {httpstatus$:400, ...})  
})

Extract data from entity object:

entobj.data$()

or

_.omit(entobj.data$(), ['entity$']

to also remove the entity$ field

Refs as entity objects

entobj1.parent = entobj2 

entobj1.parent will be persisted (automagically) as entobj2.id on save$

Deep extend

seneca.util.deepextend(obj, [sources])

Useful for extending options.

Access the full options object

seneca.options() 

Overriding action handlers and prior

If action handlers are registered for the same pattern multiple times, it’s only the last one that will get invoked. The previously registered action handler can be called via this.prior from the overriding handler:

seneca.add({cmd: ‘load’}, function(args, done) { 
  ...  // first handler
})
seneca.add({cmd: ‘load’}, function(args, done) { 
  this.prior(args, done) // second handler calling first handler
}) 

Actions time out

Avoid writing actions that take a long time to execute. Or otherwise you need to override the default seneca timeout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment