CollectionsCollections Database

Javascript API

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

The Javascript API is limited to Sources, 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.
xml() XML Document
Parse the response as XML. It returns undefined if the response can't be parsed. Please visit XML Parser for the XML parser documentation.
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, mergeValues bool)
Set list items (as an array of suggestions) for the specified field. mergeValues is optional. If mergeValues is true, existing values will be merged with the new values.
document.setDocument(value document, fieldAlias string)
Set a document (as builder) for the specified field.
document.setDocuments(value documents, fieldAlias string, mergeValues bool)
Set documents (as an array of builders) for the specified field. mergeValues is optional. If mergeValues is true, existing values will be merged with the new values.
document.setManagedDocuments(value documents, fieldAlias string, mergeValues bool)
Set managed documents (as an array of builders) for the specified field. mergeValues is optional. If mergeValues is true, existing values will be merged with the new values.
document.setLocation(value location, fieldAlias string)
Set a location (as a auggestion) for the specified field.
document.setGroup(value group, fieldAlias string, mergeValues bool)
Set a group (as a group builder) for the specified field. mergeValues is optional. If mergeValues is true, existing values will be merged with the new values.
document.setEntity(name string)
Set the entity for the document by passing the entity name.
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.
  • For Color you can use setString specifying the hex value (e.g. #00FF00).

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.

Suggest a location

app.location.suggestWithCoordinates(latitude number, longitude number) location suggestion
Create a new suggestion for the location with the latitude and longitude.
app.location.suggestWithAddress(address string) location suggestion
Create a new suggestion for the location with the address.

You can suggest a location with the coordinates or the address.

let location = app.location.suggestWithCoordinates(40.730610, -73.935242);

let document = app.document.builder();
document.setLocation(location, "location");

Group builder

You can use Group builder to add values to a group.

app.group.builder() group builder
Create a new group builder.
group.add.string(value string)
Add a new value.
group.add.integer(value number)
Add a new value.
group.add.decimal(value number)
Add a new value.
group.add.boolean(value bool)
Add a new value.
group.add.date(value date)
Add a new value.
group.add.image(value image)
Add a new value.
group.add.listItem(value list item)
Add a new value.
group.add.listItems(value list items)
Add a new value.
group.add.document(value document)
Add a new value.
group.add.documents(value documents)
Add a new value.
group.add.location(value location)
Add a new value.
let document = app.document.builder();

let group = app.group.builder();

group.add.string("String 1");
group.add.string("String 2");
group.add.string("String 3");

document.setGroup(group, "my-group");

Document

An object that manages an existing document.

app.currentDocument document
When updating a document, this property contains the current document.
app.document.findById(shortID string) document
Find a document by its short ID.
document
getValue(fieldAlias string) Javascript object
Get a document value as a Javascript object.
getValueContainer(fieldAlias string) Value container
Get a document value as a value container.
valueContainer
A value container is an abstract wrapper that provides utilities such as testing equality, comparison or formatting to string.
getValue() Javascript object
Get the underlying Javascript object.
equals(anotherContainer Value container) bool
Returns true if the value is equal to another value.
compare(anotherContainer Value container) integer
Returns 0 if the values are equal. -1 if the first value is less than the second one. 1 if the first value is greater than the second one.
toString() string
Returns the value as a formatted string.
// During a document update
let document = app.currentDocument;
let title = document.getValue("title");

// Find a document by short ID
let document = app.document.findById("ABC123");
let title = document.getValue("title");

Updating a document

A document can be updated from a source. To begin with, enable the feature from → Edit → Sources → SourceDocument update enabled.

Then open a document, and from the detail view, tap on → Update from... → Source.

app.currentDocument document
When updating a document, this property contains the current document.
app.action action
The action performed by the script (create or update). It's an enum-like value, valid values are listed below.
app.actions.CREATE
app.actions.UPDATE

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.