Data Sources: Difference between revisions

m
no edit summary
mNo edit summary
mNo edit summary
Line 7: Line 7:
To add a custom data source for a library, edit the library, press the Autofill tab, and select '''Custom Source'''.
To add a custom data source for a library, edit the library, press the Autofill tab, and select '''Custom Source'''.


== Lifecycle of a custom data source ==
== Lifecycle of an Autofill from a custom data source ==
# A user types text in the field of an Entry Edit card.
# A user types text in a field in an Entry Edit card.
# Entered text is passed to the custom data source in the form of the global variable '''''query'''''.
# The entered text is passed to the custom data source script in the form of the global variable '''''query'''''.
# The data source script is executed.
# The data source script is executed.
# The user is presented with a list of objects found by the script.
# The user is presented with a list of objects found by the script.
# The user selects an object and arguments of the objects written into the entry field according to data load rules.
# The user selects an object its arguments written into the entry fields according to data load rules.
# If a function is returned as a result of the script, the function is executed and the result is loaded into the field.
# If a function is returned as a result of the script, the script function is executed and the result is loaded into fields according to data load rules.


== Step 1 — Adding a data source for autofill ==
== Adding a custom data source ==
=== Step 1 — Adding a data source for autofill ===
# Open the Library Edit screen. Click on tab Autofill, and click <big>'''+'''</big>.
# Open the Library Edit screen. Click on tab Autofill, and click <big>'''+'''</big>.
# The user selects the type of search &mdash; By Name or By Barcode.
# The user selects the type of search &mdash; By Name or By Barcode.
Line 22: Line 23:
# The user is presented with an Autofill Rules edit card. The user selects the field to use as the search key for the custom data source.
# The user is presented with an Autofill Rules edit card. The user selects the field to use as the search key for the custom data source.


== Step 2 &mdash; Script writing ==
=== Step 2 &mdash; Script writing ===
The script is executed in the context of the library or entry, so it can use the same functions and objects as a trigger.<br/>A script has the same security parameters as a trigger.<br/>The result of the script should be returned as an array object.
The script is executed in the context of the library or entry, so it can use the same functions and objects as a trigger.<br/>A script has the same security parameters as a trigger.<br/>The result of the script should be returned as an array object.


Line 37: Line 38:
:::; id: Object identifier. Used if the result is a function.
:::; id: Object identifier. Used if the result is a function.


=== Example &mdash; Filling data from another library ===
==== Example &mdash; Filling data from another library ====
[[insert script here]]
[[insert script here]]


== Additional data load ==
==== Additional data load ====
Many services (APIs) return general information without details as a result of a search. To extract complete detailed information, an additional API request is needed. In this case, a function for additional data load should be used. The argument for this function is the identifier of the object selected by the user. The function returns an object whose parameters will be loaded into the fields of the entry according to autofill rules. As an example, let's use an API with two methods:
Many services (APIs) return general information without details as a result of a search. To extract complete detailed information, an additional API request is needed. In this case, a function for additional data load should be used. The argument for this function is the identifier of the object selected by the user. The function returns an object whose parameters will be loaded into the fields of the entry according to autofill rules. As an example, let's use an API with two methods:
# http://api.example.com/search?q='search query' &mdash; This API does a search for an object and returns only identifiers and names of the objects. The result is a JSON array of objects. Each object has a ''title'' and an ''id'' properties.
# http://api.example.com/search?q='search query' &mdash; This API does a search for an object and returns only identifiers and names of the objects. The result is a JSON array of objects. Each object has a ''title'' and an ''id'' properties.
Line 48: Line 49:
: [[insert code]]
: [[insert code]]


== Step 3 &mdash; Settings for autofill rules ==
=== Step 3 &mdash; Settings for autofill rules ===
After the script is written, it is necessary to map the arguments of the returned object to the fields of the library:
After the script is written, it is necessary to map the arguments of the returned object to the fields of the library:
# Close the script edit card and click <big>'''+'''</big>.
# Close the script edit card and click <big>'''+'''</big>.
Line 56: Line 57:
# Create rules for the remaining object properties.
# Create rules for the remaining object properties.


=== Example &mdash; Data source ===
==== Example &mdash; Data source ====
Let's consider service http://www.discogs.com/ &mdash; Provides information about music albums. This service has an API documented as http://www.discogs.com/developers.
Let's consider service http://www.discogs.com/ &mdash; Provides information about music albums. This service has an API documented as http://www.discogs.com/developers.