Data Source
Table of contents
Memento Database offers several ways to automatically fill entry fields (Autofill feature). One of these ways is using Custom Data Sources - a powerful scripting mechanism that allows you to fetch data from virtually any source
Custom Data Sources give you the most flexibility by allowing you to:
- Connect to any web API or service
- Query other Memento libraries with complex logic
- Process and transform data before filling fields
- Implement custom search and data retrieval logic
- Combine multiple data sources
Query
When a user enters a search term, Memento automatically makes it available in your script through the global variable query
. This variable contains:
- The text entered by the user (for text searches)
- The scanned barcode (for barcode searches)
How Custom Data Sources Work
Basic Flow
- User enters a search term in an entry field
- The custom data source script runs using this search term (automatically available as the global variable
query
) - Script processes the search and must return results using the global
result()
method - The
result()
method expects an array of objects with this structure:{ title: "Display Name", // Required - shown as main text desc: "Description", // Optional - shown as subtitle thumb: "image_url", // Optional - shown as thumbnail id: "identifier", // Required for additional data fetch // ... any other properties for field mapping }
- Memento displays these objects to the user in a selection list
- User selects the desired result
- Selected object’s properties are mapped to entry fields according to defined rules
Advanced Flow (with Additional Data)
Some data sources can fetch additional details after the initial search. In this case:
- Initial search returns basic information (title, description, etc.)
- User selects an item
- Script fetches detailed information for the selected item
- Memento fills entry fields with the complete data
Setting Up a Custom Data Source
1. Create the Data Source
- Open the Library Edit screen
- Navigate to the Autofill tab
- Click the “+” button
- Choose the search type:
- By Name (text search)
- By Barcode (barcode scanner)
- Select “Custom source”
- Choose the field to use for searching
2. Write the Script
Scripts must return data in a specific format. Here’s a basic template:
// Basic search script
var results = [
{
title: "Item Name", // Required
desc: "Description", // Optional
thumb: "image_url", // Optional
id: "unique_identifier" // Required for additional data fetch
}
];
result(results);
// With additional data fetch
result(results, function(id) {
// Fetch and return additional data
return additionalData;
});
3. Map Data to Fields
After creating the script:
- Close the script editor
- Click “+” to add field mappings
- Enter the property name from your result object
- Select the corresponding library field
- Repeat for each field you want to populate
Script Requirements
Result Object Properties
title
(required): Display name of the itemdesc
(optional): Additional descriptionthumb
(optional): URL or reference to an imageid
(required for additional data): Unique identifier
The result()
Function
result(sourceObjects, getMoreFunction)
sourceObjects
: Array of objects matching the searchgetMoreFunction
: Optional function to fetch additional data
Scripts run with the same security context as triggers
Examples
Fetch Data from Another Library
var anotherLib = libByName("AnotherLib");
var entries = anotherLib.find(query);
var resultArray = [];
for(var i in entries ) {
var object = {};
object["title"] = entries[i].title;
object["desc"] = entries[i].description;
object["number"] = entries[i].field("Number");
resultArray.push(object);
}
result(resultArray);
Web API Integration
// Example using a REST API
var response = http().get("https://api.example.com/search?q=" +
encodeURIComponent(query));
result(
JSON.parse(response),
function(id) {
return http().get("https://api.example.com/items/" + id);
}
);