CollectionsCollections Database

Javascript API

Collections provides a Javascript API and an in-app code editor.

The Javascript API is limited to Sources, so you can use Javascript to search and create documents querying external services (E.g. Google Books). To create a Source, open a collection → tap on button → Edit → Sources.

The API is accessible through the app object.

app.query
This object contains informations about the current query. The query type is determined by the input method (text or barcode).
app.query.isText() bool
Returns true for a text query.
app.query.isBarcode() bool
Returns true for a barcode query.
app.query.value object
The query value. For text and barcode queries, the value is a string.
app.params
In a Source, this object will store parameters passed from the search script to the document script.
app.log(object)
Write a message to the console. Equivalent to console.log()

Result

app.result()
This function must be called at the end of the script to return a result. This function has a different declaration depending on the context.
app.result(results array of search results)
In a search script, you should call it by passing an array of search results.
app.result(builder document builder)
In a document script, you should call it by passing a document builder.
app.fail()
Call this function to terminate the script execution. You can call it at any point in the code.

Networking

app.request(url string) request
Create a new HTTPS request with the specified URL.
Note: the app supports only secure HTTPS requests, any http URL is converted to https.
request.method string
Set or get the method for the request. "GET" or "POST"
request.body string
Set or get the body used by the POST request.
request.addHeaderValue(value string, key string)
Add a header value specifying a value and a key.
request.send() response
Sends the request and returns a response object.
response
The response returned by request.send()
json() JSON object
Parse the response as JSON. It returns undefined if the object can't be parsed.
image() image
Parse the response as image. It returns undefined if the image is not valid.
statusCode integer
Get the response status code. If the request fails, the status code is -1.
success bool
True if the server sends any response. False otherwise (no connection).
app.image.fromURL(url string) image
A shorthand to request an image from web, it returns an image object, or undefined if the request fails.
let request = app.request("https://www.google.com");
let response = request.send();
let data = response.json();

Storage

app.storage
You can use app.storage as a key-value persistence storage. app.storage must be a dictionary and can store any serializable object like string, number, date, array. The dictionary is initialized before evaluating a script.
app.storage.firstName = "John";
app.storage.lastName = "Doe";
app.storage["full-name"] = "John Doe";

Search results

app.searchResult.new() search result
Create a new search result.
app.searchResult
searchResult.title string
Set or get the title for the result.
searchResult.subtitle string
Set or get the subtitle for the result.
searchResult.imageURL string
Set or get the image URL for the result.
searchResult.params dictionary
Set a custom dictionary that will be passed to the document script in app.params
let result = app.searchResult.new();
result.title = "Harry Potter";
result.subtitle = "J. K. Rowling";
result.params = {id: 12345};

Document builder

You can use the Document builder to create a new document or use an existing one.

Before using the document builder, be sure to set an alias for each field (Edit a field → Alias).

app.document.builder() document builder
Create a new document builder. Right below, we simply refer to it as document.
document.setString(value string, fieldAlias string)
Set a string value for the specified field.
document.setInteger(value number, fieldAlias string)
Set an integer value for the specified field.
document.setDecimal(value number, fieldAlias string)
Set a decimal value for the specified field.
document.setBoolean(value bool, fieldAlias string)
Set a boolean value for the specified field.
document.setDate(value date, fieldAlias string)
Set a date value for the specified field.
document.setImage(value image, fieldAlias string)
Set an image value for the specified field. You can create the image with app.image.fromURL(url)
document.setListItem(value list item, fieldAlias string)
Set a list item (as a suggestion) for the specified field.
document.setListItems(value list items, fieldAlias string)
Set list items (as an array of suggestions) for the specified field.
document.setDocument(value document, fieldAlias string)
Set a document (as builder) for the specified field.
document.setDocuments(value documents, fieldAlias string)
Set documents (as an array of builders) for the specified field.
document.setManagedDocuments(value documents, fieldAlias string)
Set managed documents (as an array of builders) for the specified field.
document.setIdentifier(fieldAlias string)
Set a field as the identifier to find an existing document. Optional.
document.setTitle(fieldAlias string)
Set a field as the title of the suggestion. By default, the first field added to the builder is used as the title.

For URL, Email, Phone, Barcode you can simply use setString. For Time interval you can use setDecimal specifying the value in seconds. For "Date and time" you can use setDate.

let document = app.document.builder();
document.setString("Harry Potter", "name");
document.setString("Pottermore Publishing", "publisher");
document.setInteger(223, "page-count");

Suggestions

When you create a document, you usually want to set existing list items or documents to it. To solve this problem, the app uses the concept of suggestion.

A suggestion contains an identifier and all the data needed to create the item. The app will find the item matching the specified identifier, and if that item doesn't exist, it will create a new one.

Suggest a list item

app.listItem.suggest(alias string, name string) list item suggestion
Create a new suggestion for a list item specifying an alias and a name. By default, the alias is used as the identifier.
suggestion.aliasAsIdentifier()
Use the alias as the identifier.
suggestion.nameAsIdentifier()
Use the name as the identifier.
let listItem = app.listItem.suggest("fantasy", "Fantasy");
document.setListItem(listItem, "genre");

Suggest a document

To suggest a document, create a new document builder and call builder.setIdentifier(fieldAlias) to set a field as the identifier. If you are creating a new document, you don't need to call this method.

let author = app.document.builder();
author.setIdentifier("name");
author.setString("J. K. Rowling", "name");

let book = app.document.builder();
book.setString("Harry Potter", "name");
book.setDocument(author, "author");

The example creates an author with the name field as the identifier. The author is then saved in a book, the app will find an author with "J. K. Rowling" as name or it will create a new one if that doesn't exist.

Utility

app.date.isValid(date date) bool
Returns true if the date object is valid.
app.date.fromUnixTimestamp(timestamp int) date
Create a date from an Unix timestamp. It returns a date or undefined if the date is not valid.