Created
December 7, 2016 14:39
-
-
Save trustmaster/bf52bcdcfb4ccfa250697af758bf1649 to your computer and use it in GitHub Desktop.
Example assembly component
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
assembly = require '../lib/assembly' | |
class DbInsert extends assembly.Component | |
description: 'Inserts a database record' | |
# Validation of the input message is done automatically | |
# before relay() method is entered | |
validates: | |
'db': 'func' # expect a constructor function | |
'query.table': 'ok' # expect something truthy | |
'query.data': 'obj' # expect an object | |
relay: (msg, output) -> | |
# Here we have a valid assembly message | |
returning = msg.query.returning ? undefined | |
# Using the data right away | |
msg.db msg.query.table | |
.insert msg.query.data, returning | |
.then (rows) -> | |
# Embedding result and passing along | |
msg.rowset = rows | |
msg.query = undefined | |
output.sendDone msg | |
.catch (err) -> | |
# Oops, adding more info and passing a failed request | |
err.message += ' [in DbInsert]' | |
assembly.fail output, msg, err | |
# Named classes are encouraged for meaningful error stack traces | |
exports.getComponent = -> new DbInsert |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment