auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps) => {
}
/*
DESCRIPTION:
Persist HTTP request data to a real-time data store with
an API to query event counts, event data, and reset to
delete data in the store.
GETTING STARTED:
Copy this workflow into your account to run it for free.
HOW IT WORKS:
- Event details for any HTTP requests to the endpoint URL
for this workflow will be saved to $checkpoint (e.g.,
(headers, body, query data, etc).
- To query the number of events that were made/saved in
$checkpoint, make a request to the workflow endpoint
with the path `/api/events/count`
- To retrieve the contents for any requests made to this
workflow, make a request to the worfklow endpoint URL
with the path `/api/events/`
- To reset $checkpoint, make a request to the workflow
endpoint URL with the path `/api/events/reset`
- To customize this works workflow, just edit the Node.js
code below in `steps.process_event_data`
SAVING DATA TO PIPEDREAM'S SQL SERVICE:
- Enable `steps.sql` at the end of this workflow.
- Add a table name -- Pipedream will automagically create
a table and schema to match your data.
- Navigate to the SQL tab above to query your data. Data will
automatically be deleted after 30 days.
You can delete this step at any time (or just move it down
using the arrovs at the top right of the step).
*/
auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps) => {
}
// customize the max number of events to save in $checkpoint
const event_cache_size = 1000
if (!Array.isArray($checkpoint)) {
// if $checkpoint is not an array, instantiate it
$checkpoint = []
}
const eventCount = $checkpoint.length
if (event.url.indexOf('/api/events/count') !== -1) {
// if the request URL contains /api/events/count,
// return the count of events in $checkpoint
$respond({
status: 200,
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({ eventCount } )
})
console.log(`Returned count of events: ${eventCount}`)
} else if (event.url.indexOf('/api/events/reset') !== -1) {
// if the request URL contains /api/events/reset, set
// $checkpoint to an empty array.
$checkpoint = []
$respond({
status: 200,
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({ 'reset': true })
})
console.log('Reset $checkpoint')
} else if (event.url.indexOf('/api/events') !== -1) {
// if the request URL contains /api/events, return the
// event contents.
$respond({
status: 200,
headers: {
'content-type': 'application/json'
},
body: JSON.stringify($checkpoint)
})
console.log(`Returned ${eventCount} events.`)
} else {
// in all other cases, save the event data to $checkpoint.
// if the number of events is >= event_cache_size, remove
// older items first.
const overage = eventCount + 1 - event_cache_size
let eventsRemoved = 0
if (overage > 0) {
for (let i = 0; i < overage; i++) {
$checkpoint.shift(overage)
}
eventsRemoved = overage
}
// then add the event to $checkpoint and respond to the client.
$checkpoint.push(event)
$respond({
status: 200,
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
'success': true,
'eventCount': $checkpoint.length,
'oldEventsRemoved': eventsRemoved
})
})
console.log(`Added event to $checkpoint.`)
}
Enter the name of the table (e.g., my_table_name) to load the payload data into. Pipedream's SQL service automatically creates the table and adapts the schema to your data.
Enter a reference to the data (for example, event.body or steps.step_name.return_value) you'd like to insert into the table. Pipedream’s SQL service automatically converts the data to JSON and maps the table schema to its keys.
async
params => {
}
$send.sql({
table: params.table,
payload: params.payload,
})