Skip to content

Instantly share code, notes, and snippets.

@yagop
Last active October 22, 2017 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yagop/9448103fbad9213c1b0799d5412de086 to your computer and use it in GitHub Desktop.
Save yagop/9448103fbad9213c1b0799d5412de086 to your computer and use it in GitHub Desktop.
Simple example of WebTask Storage

This example demostrate the usage of WebTask Storage with multiple models and how to save / filter / find them.

$ curl https://__host__.run.webtask.io/example\?action\=delete_all
{}

$ curl https://__host__.run.webtask.io/example\?action\=save_model \
  -H "Content-Type: application/json" \
  --data '{"modelName": "planets", "properties": {"name": "Earth"}}'
{}

$ curl https://__host__.run.webtask.io/example\?action\=save_model \
  -H "Content-Type: application/json" \
  --data '{"modelName": "planets", "properties": {"name": "Mars"}}' 
{}

$ curl https://__host__.run.webtask.io/example\?action\=filter_model \
  -H "Content-Type: application/json"\
  --data '{"modelName": "planets", "properties": {"name": "Mars"}}'
[{"name":"Mars"}]

$ curl https://__host__.run.webtask.io/example\?action\=find_model \
  -H "Content-Type: application/json" \
  --data '{"modelName": "planets", "properties": {"name": "Mars"}}'
{"name":"Mars"}
const Bluebird = require('bluebird@3.5.0')
const lodash = require('lodash@4.8.2')
/**
* storage is an Object with properties as Array models:
* storage: {
* modelA: [{id: 1, name: 'example'}],
* modelB: [{id: 1, color: 'red'}]
* }
*/
const getStorage = (ctx) => Bluebird.fromCallback(cb => {
ctx.storage.get(cb)
})
const getObjectStorage = (ctx) => getStorage(ctx)
.then(storage => (typeof storage === 'object') ? storage : {})
const setStorage = (ctx, data, force) => Bluebird.fromCallback(cb => {
ctx.storage.set(data, {force}, cb)
})
const getModels = (ctx, modelName) => Bluebird.try(() => {
if (!modelName) {
throw new Error('modelName not provided')
}
return getObjectStorage(ctx).then(storage => {
if (!Array.isArray(storage[modelName])) {
storage[modelName] = []
}
return storage[modelName]
})
})
const filterModel = (ctx, modelName, props) =>
getModels(ctx, modelName).then(models => lodash.filter(models, props))
const findModel = (ctx, modelName, props) =>
getModels(ctx, modelName).then(models => lodash.find(models, props))
const saveModel = (ctx, modelName, props) => Bluebird.try(() => {
if (!modelName) {
throw new Error('modelName not provided')
}
return getObjectStorage(ctx).then(storage => {
if (!storage[modelName]) {
storage[modelName] = []
}
storage[modelName].push(props)
return setStorage(ctx, storage)
})
})
const main = (ctx) => Bluebird.try(() => {
const action = ctx.query.action
console.log(`Action: ${action}`)
console.log('Body: %j', ctx.body)
switch (action) {
case 'get_storage': {
return getStorage(ctx)
}
case 'get_models': {
return getModels(ctx, ctx.body.modelName)
}
case 'save_model': {
return saveModel(ctx, ctx.body.modelName, ctx.body.properties)
}
case 'filter_model': {
return filterModel(ctx, ctx.body.modelName, ctx.body.properties)
}
case 'find_model': {
return findModel(ctx, ctx.body.modelName, ctx.body.properties)
}
case 'delete_all': {
return setStorage(ctx, {}, 1)
}
default: {
throw new Error('Action not allowed')
}
}
})
module.exports = (ctx, cb) => main(ctx)
.then(resp => {
console.log('Response will be:', resp)
cb(null, resp)
})
.catch(err => {
console.error('Response error')
console.error(err)
cb(err.message, null)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment