Run

Run from the CLI

A Deta Micro can be run directly from the deta cli using the command deta run with an input.

In order to run a micro from the cli directly, the micro's code needs to define functions that will be run from the cli with the help of our library deta.

The deta library is pre-installed on a micro and can just be imported directly.

const { app } = require('deta');
// define a function to run from the cli
// the function must take an event as an argument
app.lib.run(event => "Welcome to Deta!");
module.exports = app;

With this code deployed on a micro, you can simply run

$ deta run

And see the following output:

Response:
"Welcome to Deta!"

Events

A function that is triggered from the cli must take an event as the only argument.

You can provide an input from the cli to the function which will be passed on as an event. It has four attributes:

  • event.json: object provides the JSON payload
  • event.body: string provides the raw JSON payload
  • event.type: string type of an event, run when running from the cli
  • event.action: string the action provided from the cli, defaults to an empty string
const { app } = require('deta');
app.lib.run(event => {
return {
// access input to your function with event.json
message: `hello ${event.json.name}!`}
};
});
module.exports = app;

With this code deployed on a micro, you can run

$ deta run -- --name deta

And should see the following output.

Response:
{
"message": "hello deta!"
}

Input

The input to your function on a micro can be provided through the deta cli and accessed in the code from the event object. The input is a JSON object created from the arguments provided to the cli.

An important consideration is that the values in key-value pairs in the input are always either strings, list of strings or booleans.

Boolean flags are provided with a single dash, string arguments with double dash and if multiple values are provided for the same key, a list of strings will be provided.

For instance:

$ deta run -- --name jimmy --age 33 --emails jimmy@deta.sh --emails jim@deta.sh -active

will provide the micro with the following input:

{
"name": "jimmy",
"age": "33", // notice '33' here is a string not an int
"emails": ["jimmy@deta.sh", "jim@deta.sh"],
"active": true
}

You need to explicitly convert the string values to other types in your code if needed.

Actions

Actions help you run different functions based on an action that you define for the function.

The action defaults to an empty string if not provided.

const { app } = require('deta');
app.lib.run(event => {
return {
message: `hello ${event.json.name}!`
};
// action 'hello'
}, "hello");
app.lib.run(event => {
return {
message: `good morning ${event.json.name}!`
};
// action 'greet'
}, "greet");
module.exports = app;

With this code deployed on a deta micro, if you run

$ deta run hello -- --name deta

where you tell the cli to run action hello with "name": "deta" as input. You should see the following output:

Response:
{
"message": "hello deta!"
}

And if you do deta run with action greet

$ deta run greet -- --name deta

you should see the following output:

Response:
{
"message": "good morning deta!"
}

Run and HTTP

You can combine both run and HTTP triggers in the same deta micro. For this you need to instantiate your app using the deta library that is pre-installed on a micro.

const { App } = require('deta');
const express = require('express');
const app = App(express());
// triggered with an HTTP request
app.get('/', async(req, res) => {
res.send('Hello deta, i am running with HTTP');
});
// triggered from the cli
app.lib.run(event => {
return 'Hello deta, i am running from the cli';
});
module.exports = app;

Run and Cron

You can use both run and cron triggers in the same deta micro. You can also stack run and cron triggers for the same function.

const { app } = require('deta');
const sayHello = event => 'hello deta';
const printTime = event => `it is ${(new Data).toTimeString()}`;
app.lib.run(sayHello);
// stacking run and cron
app.lib.run(printTime, 'time'); // action 'time'
app.lib.cron(printTime);
module.exports = app;