Child pages
  • Persist your content
Skip to end of metadata
Go to start of metadata

No SQL persistence

Apstrata relieves you from having to create a relational database, defining tables or manipulating DDL or SQL. You are provided with a ready to use store, within which you create documents. Documents have a key/value pairs structure, where the key is a field name and the value can have one of the following types: string, numeric, text, date, file or geospatial.

To create or update a document, just invoke the SaveDocument API and pass it the key/value pairs that you need to create or update. When you create a document, Apstrata generates a document key for you if you did not specify one and returns it in the response of the call to SaveDocument. The document key is the unique identifier of the document in your store: use it to retrieve the document or to update it.

Back to the map       Next station: query your content

Example 1: save my game

Let us say that you are implementing a cool game that runs on mobile devices, such as smartphones or tablets. Since a user could run this game on any of his devices, it is better to persist the data of the current game, such as the score, the count of remaining lives, the current level and the player's name, in a central and shared location. So let us create an Apstrata document in your store for that.

 

curl -X POST "https://varick.apstrata.com/apsdb/rest/O763A7F690/SaveDocument?apsws.time=1418992125166" 
-F apsws.responseType=json -F apsws.authMode=simple -F apsdb.store=DefaultStore 
-F score=0 -F score.apsdb.fieldType=numeric -F player=gangsta -F level=1
-F level.apsdb.fieldType=numeric -F lives=3 -F lives.apsdb.fieldType=numeric 
-F apsws.authSig=1933c4b633b2ad05c40c2bd7c7ad0aa8
// Prepare the parameters for the API
var params = {
	"apsdb.store": "DefaultStore", // "DefaultStore" is the name by default. If you did not rename your store, this line is optional
	"score": "0",
	"score.apsdb.fieldType": "numeric" // we specify that the "score" field is of type "numeric"
	"player": "gangsta", // fields are of type "string" by default, so no need to specify it for "player"
	"level": "1",
	"level.apsdb.fieldType": "numeric",
	"lives": "3",
	"lives.apsdb.fieldType": "numeric"
}
 
// Invoke the API using the native "apsdb" object
// Since we're not uploading files, we send "null" in place of the "files" parameter
var response = apsdb.callApi("SaveDocument", params, null); // the response JSON object contains the "key" field, document key of the created document
String response = "no response yet";
try {
		
	String authKey = "O763A7F690"; // Replace with your Application key
	String baseURL = "https://varick.apstrata.com/apsdb/rest";
				 
	// 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("apsdb.store","DefaultStore")); // "DefaultStore" is the name by default. If you did not rename your store, this line is optional
	parameters.add(new BasicNameValuePair("score","0")); // we add the "score" field to our document, with initial value set to '0'
	parameters.add(new BasicNameValuePair("score.apsdb.fieldType","numeric")); // we specify that the "score" field is of type "numeric"
	parameters.add(new BasicNameValuePair("player","gangsta")); // fields are of type "string" by default, so no need to specify it for "player"
	parameters.add(new BasicNameValuePair("level","1"));
	parameters.add(new BasicNameValuePair("lives.apsdb.fieldType","numeric"));
	parameters.add(new BasicNameValuePair("lives","3"));
	parameters.add(new BasicNameValuePair("level.apsdb.fieldType","numeric"));		
			
	// Invoke the API using the above Client instance 
	// Since we're not uploading files, we send "null" in place of the "files" parameter
	response = client.callAPIJson("SaveDocument", parameters, null, mode); // the response JSON object contains the "key" field, document key of the created document
} catch (Exception e) {
	response = "Error: " + e.getMessage();
	e.printStackTrace();
}
Try it!

Example 2: update my game

Your app user decides to take a short break and stop playing. We thus need to persist the current status of his game and hence update the Apstrata document created in example 1, by passing a new value for the "score", the "level" and "lives" fields (we assume that the document key obtained upon creation of the document was "C4E11A5C2A96B94C12F5C4F5AA44C4CF").

 

curl -X POST "https://varick.apstrata.com/apsdb/rest/O763A7F690/SaveDocument?apsws.time=1418992125166" 
-F apsws.responseType=json -F apsws.authMode=simple -F apsdb.store=DefaultStore 
-F apsws.authSig=1933c4b633b2ad05c40c2bd7c7ad0aa8
-F score=5 -F score.apsdb.fieldType=numeric -F player=gangsta -F level=2
-F level.apsdb.fieldType=numeric -F lives=3 -F lives.apsdb.fieldType=numeric 
-F apsdb.documentKey=C4E11A5C2A96B94C12F5C4F5AA44C4CF -F apsdb.update=true
// Prepare the parameters for the API
var params = {
	"apsdb.store": "DefaultStore", // If you did not rename your store, this line is optional
	"score": "5",
	"score.apsdb.fieldType": "numeric",
	"level": "2",
	"level.apsdb.fieldType": "numeric",
	"lives": "3",
	"lives.apsdb.fieldType": "numeric"
	"apsdb.documentKey": "C4E11A5C2A96B94C12F5C4F5AA44C4CF" // since we are updating, we pass the key of the document to update
	"apsdb.update": "true" // notice that we need to pass the "apsdb.update" field set to "true"
}
 
var response = apsdb.callApi("SaveDocument", params, null); 
String response = "no response yet";
try {
	
	String authKey = "O763A7F690"; // Replace with your Application key
	String baseURL = "https://sandbox.apstrata.com/apsdb/rest";
			 
	// 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									
	parameters.add(new BasicNameValuePair("apsdb.store","DefaultStore")); 
	parameters.add(new BasicNameValuePair("apsdb.documentKey","C4E11A5C2A96B94C12F5C4F5AA44C4CF")); // since we are updating, we pass the key of the document to update
	parameters.add(new BasicNameValuePair("apsdb.update","true")); // notice that we need to pass the "apsdb.update" field set to "true"
	parameters.add(new BasicNameValuePair("score","5"));
	parameters.add(new BasicNameValuePair("score.apsdb.fieldType","numeric"));
	parameters.add(new BasicNameValuePair("player","gangsta"));
	parameters.add(new BasicNameValuePair("level","2"));
	parameters.add(new BasicNameValuePair("lives.apsdb.fieldType","numeric"));
	parameters.add(new BasicNameValuePair("lives","3"));
	parameters.add(new BasicNameValuePair("level.apsdb.fieldType","numeric"));
		
	response = client.callAPIJson("SaveDocument", parameters, null, mode);
} catch (Exception e) {
	response = "Error: " + e.getMessage();
	e.printStackTrace();
}
Try it!

Dig deeper

Related tutorials