Child pages
  • Create Query components
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

No SQL query components

Apstrata developers can persist their data inside NoSQL key/value pair structures called documents, which are saved into their application store. To retrieve the data that you have persisted in your documents, you can define sophisticated queries to search for document matching your criteria, using the Query API.
While you can invoke the Query API from your client side and pass it all the required parameters to execute your query, you will most of the time need to reuse this same logic across clients or event across server-side components. Since Apstrata always has modularity and reusability in mind, it allows you to encapsulate your query logic into components, called "Saved Queries" using the SaveQuery API

Example 1: load my saved game

You are developing this cool game that runs on mobile devices. Although your game is addictive, your players will stop playing from time to time so you decided to persist the games as Apstrata documents. When your players get back to the game, you need to load its data back to the device, i.e. get the corresponding document using its document key (in our example, we assume our document has the following key F0BBBBEC7EE9EF59EAC220599BADFD08). 

The simplest way for you to create a "Saved Query" is to log in to the Apstrata workbench, click on "Manage App > Saved Queries > New". This opens a query editor within which a predefined template is already loaded. 

	<executeACL>creator</executeACL> <!-- This query only authorizes the retrieval of a document created by the caller of the query -->
	<store>DefaultStore</store> <!-- Put the name of your store here. "DefaultStore" is the default value -->
	<condition><![CDATA[ apsdb.documentKey<string> = {docKey}]]></condition> <!-- {docKey} is a mandatory parameter to pass to the query -->
	<returnedFields> <!-- The list of document fields we want to return -->

Try it!

From the "Saved Queries" editor, click "Save" to save your query then "Run". The workbench opens a form where you have to enter the "docKey" field with a value that matches one of your game document keys. Once done, click "Go" to execute the saved query.

Invoke the saved query

curl "
		    // We retrieve the value of the docKey parameter expected by the saved query, from the request
		    var docKey = request.parameters["docKey"];
		    var params = {
		        "apsdb.queryName": "loadGame", // we pass the name of the saved query
		        "docKey": docKey
		    return apsdb.callApi("Query", params, null);
try {
	String authKey = "O763A7F690"; // Replace with your Application key
    String baseURL = "";
    // Sign your call with a signature based on your application key and secret  to authenticate against Apstrata
    // Simple signature is used in this example
    String secret = "S4A0120A6E64952FE75285BD0XD243S2"; // Replace with your application secret
    Connection ownerConnection = new OwnerConnection(baseUrl, authKey, secret);
    Client client = new Client(baseURL, authKey, ownerConnection);
    Client.AuthMode mode = Client.AuthMode.SIMPLE;
    // Prepare the parameters to send to the SaveDocument API                                 
    List<NameValuePair> parameters = new ArrayList<NameValuePair>();               
    parameters.add(new BasicNameValuePair("","DefaultStore")); // "DefaultStore" is the name by default. If you did not rename your store, this line is optional
	// Prepare the parameters to send according to the requested Apstrata API									
	List<NameValuePair> parameters = new ArrayList<NameValuePair>();
	parameters.add(new BasicNameValuePair("apsdb.queryName","loadGame"));
	parameters.add(new BasicNameValuePair("docKey","F0BBBBEC7EE9EF59EAC220599BADFD08"));												
	// Invoke the API using the Client instance and signature mode 
	// note that since we're not passing files, we send "null" in place of the "files" parameter
	response = client.callAPIJson(Query, parameters, null, mode);
} catch (Exception e) {
	response = "Error: " + e.getMessage();
  • No labels