architect / Functions
Programming Languages
@architect/functions
Runtime helper library for serverless apps built with Architect
Check out the full docs: arc.codes
API
Given:
let arc = require('@architect/functions')
The following APIs exists:
arc.events.subscribe(fn)
Used to define a lambda function that will act as an event handler. Event handlers are defined in your application's Architect project manifest file under the @events
pragma. The function code for the accompanying handler to each event should use arc.events.subscribe
to wrap the handler. For example, given the following project manifest snippet:
@events
concerts
... the following file will be initialized representing the event handler for the concerts
event, wherein you need to use arc.events.subscribe
:
// file: src/events/concerts/index.js
let arc = require('@architect/functions')
module.exports = arc.events.subscribe(function(payload, callback) {
console.log(payload)
callback()
})
arc.events.publish(params, callback)
Publishes params.payload
to the SNS Topic (event) with name params.name
. The params.name
parameter should match the event defined under @events
. Building on the example we described above, to trigger the concerts
event handler, we would set params.name
to be concerts
.
This allows you to publish events from any function within your application (@app
.arc
file namespace) to be handled by the event handler.
When running in local/testing mode, will publish the event to the sandbox.
arc.queues.subscribe(params, callback)
Used to define a lambda function that will act as a queue handler. Queue handlers are defined in your application's .arc
file under the @queues
pragma. The function code for the accompanying handler to each queued item should use arc.queues.subscribe
to wrap the handler. For example, given the following .arc
file snippet:
@queues
concert-tickets
... the following file will be initialized representing the event handler for the concert-tickets
queue, wherein you need to use arc.queues.subscribe
:
// file: src/queues/concert-tickets/index.js
let arc = require('@architect/functions')
module.exports = arc.queues.subscribe(function(payload, callback) {
console.log(payload)
callback()
})
arc.queues.publish(params, callback)
Publishes params.payload
to the SQS Queue (queue) with name params.name
. The params.name
parameter should match the queue defined under @queues
. Building on the example we described above, to trigger the concert-tickets
queue handler, we would set params.name
to be concert-tickets
.
This allows you to publish to queues from any function within your application (@app
.arc
file namespace) to be handled by the queue handler.
When running in local/testing mode, will publish the event to the sandbox.
arc.static(assetPath, options)
Returns the fully-qualified URI of a static asset for the project-relative assetPath
parameter. Takes into account:
- What environment (testing, staging, production) we are running in.
- Whether fingerprinting is enabled.
- Whether the override environment variable
ARC_STATIC_BUCKET
is present.
options
is an object with the following currently-supported properties:
-
stagePath
: boolean, prepends/staging
or/production
to the asset path; useful if the current app is being run on an naked (non-domain-mapped) API Gateway
arc.tables(callback)
Returns an object that can be used to access data in database tables as defined under @tables
in your .arc
file. For example, given the following .arc
file snippet:
@tables
accounts
accountID *String
messages
msgID *String
Running the following code:
let data = await arc.tables()
Would yield the following objects:
-
data.accounts
: reference to theaccounts
table -
data.messages
: reference to themessages
table
.. which contain the following methods:
-
delete(key, callback)
: deletes the record from the table with keykey
and invokescallback
with the result -
get(key, callback)
: retrieves the record from the table with keykey
and invokescallback
when complete -
put(item, callback)
: addsitem
to the table and invokescallback
with the item when complete -
query(params, callback)
: queries the table usingparams
and invokescallback
with the result -
scan(params, callback)
: scans the table usingparams
and invokescallback
with the result -
update(params, callback)
: updates an item in the table usingparams
and invokescallback
when complete
arc.ws.send({ id, payload })
Sends the object present on payload
to the connection ID on id
. Payload is passed to JSON.stringify()
on your behalf. Uses postToConnection
from the ApiGatewayManagementApi
. Returns a promise with no data on success.