Skip to main content

Derivative

Use derivatives field type to build your product automations on data CRUD. Use any NPM or APIs to code in JS. Derivative fields derive their value from other fields in the row. They are automatically evaluated whenever the dependent fields are updated.

In Column Settings, write a function body that returns the value to be displayed in the field. This code is run in a Node.js cloud function.

To use Derivative fields, you must:

  • be comfortable writing simple JavaScript code and
  • have Rowy Cloud Functions set up via the one click deploy of Rowy Run.

Video demo

API

Your code has access to the following parameters and can use the await keyword.

You can also use npm packages using CommonJS require imports.

NameTypeDescription
rowRecord<string, any>All data in the current row.
refDocumentReferenceReference to the corresponding Firestore document of the current row.
dbFirestoreAccess to the full Cloud Firestore instance to access any collection or document.
authAuthAccess to Firebase Auth via Admin SDK
storageStorageAccess to Firebase Storage via Admin SDK

Basic example

Add an additional field to simplify client-side queries.

Problem: On the client-side we want to see which requests are ready for approval. We need to know which rows have the amount and claim fieldset. We don’t want to create a composite index, which would be costly.

Solution: Create a new field that contains a boolean value based on the values of those fields. The client-side can filter by this new field.

const derivative: Derivative = async ({ row }) => {
// Mark document as ready for approval
if (row.amount && row.claim) return true;
else return false;
};

Intermediate example

Use data from other parts of the database in this row.

Problem: We want to attach the user’s email to a request. We want to automate this and not have to expose all the user’s sensitive data on the client-side.

Solution: Create a new field that gets the email. It will run on a secure backend environment and does not expose sensitive data.

const derivative: Derivative = async ({ row, db }) => {
// Get user doc
const userDoc = db.collection("users").doc(row.uid);
return userDoc.get("email");
};

Advanced examples

Use 3rd-party npm packages for more advanced functionality.

REST API request

To connect with any REST API using fetch

Problem: We want to get a list of all airport codes from natural language text.

Solution: Use OpenAI’s GPT-3 API to extract the airport codes as an array.

const derivative: Derivative = async ({ row }) => {
const openaiSecret = await rowy.secrets.get("openai");
const prompt = `Airport code extractor:

Text: \"I want to fly form Los Angeles to Miami.\"
Airport codes: LAX, MIA

Text: \"${row.text}\"
Airport codes:`;

const response = await fetch(
"https://api.openai.com/v1/engines/davinci/completions",
{
headers: {
Authorization: `Bearer ${openaiSecret}`,
contentType: "application/json",
},
body: JSON.stringify({
prompt,
max_tokens: 60,
temperature: 0.3,
frequency_penalty: 0,
top_p: 1,
stop: ["\n"],
}),
}
);
return response.choices[0].text.split(",'").map((x) => x.trim());
};

Google Cloud API

To use any Google Cloud API, first enable it in the Google Cloud Console. Enable Cloud Translation API ↗
We automatically set the API credentials as the project’s default service account.

Problem: We want to translate some text into another language.

Solution: Use the Google Cloud Translation API.

const derivative: Derivative = async ({ row }) => {
const { Translate } = require("@google-cloud/translate").v2;
// Instantiates a client
const translate = new Translate();
// Translates text field into Spanish
const [translation] = await translate.translate(row.text, "es");
return translation;
};

Cloud Storage

Problem: Users provide images using external URLs, but the images could get removed from the host provider, leading to broken images.

Solution: using rowy storage utility, store a copy of the image in Firebase Storage.


const derivative:Derivative = async ({row,ref})=>{
const url = row.externalImage;
const file = await rowy.storage.upload.url(url,{
folderPath: ref.path // Optional: stores the file in the storage same folder structure as the document on firestore
})
// Return an array of image objects that will be displayed by the Rowy UI
return [file];