Requirements
-
A VPS Server, minimum 2GB RAM, 1 vCPUs, 20GB Disk Storage, Ubuntu 18.04 - Amazon AWS Lightsail or AlibabaCloud are the best choices in terms of speed, reliability and price. This API has been fully tested on AWS Lightsail and AlibabaCloud.
-
Sublime Text (recommended), or any other HTML/text editor of your choice.
-
An FTP account confgured in FileZilla, needed to upload/edit files in your server
-
Chrome Web Browser (recommended), and/or Safari or Firefox
Installation
Option 1:
With FileZilla (recommended), upload the api
folder into the root of your server - usually the /var/www/html/
directory on Ubuntu 18.04.
Option 2:
You may also protect your API by renaming the api
folder into some random characters - something like abd123DeF456
, or whatever you want - then upload that folder into the root of your server - as mentioned above
In any case, take note of the name of the API folder in order to propely configure the SDKs.
How to configure an Ubuntu instance on AWS Lightsail to make HelloDB work
Follow these steps to configure an Ubuntu 18.04 instance via SSH terminal and upload the API's files in your server.
NOTE: The Linux commands shown in this guide are the same you can use to configure your server on AlibabaCloud or any other VPS server with Ubuntu 18.04 as OS
-
In the Home page of your Lightsail account, click the Create instance button.

-
On the next page, select the instance location closer to your place, select the OS only button and choose Ubuntu 18.04 LTS.

-
Choose the instance plan you desire (the $10 one is recommended) and give a name to your instance.

-
Scroll down and click the Create instancebutton. You will be redirected to the Home page where you'll have to wait for a few seconds to get your new instance ready and running.

-
Once your instance is ready, click on its name to enter its details.

-
Click the Networking tab.

-
You have to create a Static IP, so click the + Create static IP button.

-
Give a name to your IP and click the Create button.

-
Take note of the new Static IP address that the system generated, and click the Home button.

-
Back to the Home page, click the little terminal icon to connect to your instance via SSH.

-
It's time to run some Linux commands to configure your server. Don't worry, it's super easy, just follow the steps below.
In the terminal window, type this command and hit Enter:
sudo apt update
Wait until you get the cursor ready again

-
Now this command and hit Enter:
sudo apt upgrade
You'll be asked to continue, type Y and hit Enter again, then wait for the upgrade to be done and the cursor to be visible again.

-
If you will get the following alerts, just always hit Enter:



-
Once the upgrade of your instance is done, install Apache2 by running this command:
sudo apt install apache2
You'll have to type 'Y' and hit Enter again to consitnue with the installation.
-
Now that Apache2 is installed, install PHP with this command:
sudo apt install php
-
Restart Apache2 with this command:
sudo systemctl restart apache2
-
Install cURL with this command:
sudo apt-get install php-curl
-
The last thing to install is Pear PHP mail. Run this first command:
sudo apt-get install yum
-
When the installation of Yum is done, run this other command:
sudo apt-get install php-pear
-
When Pear is installed, run this other command:
sudo pear install mail
-
Then, run this command:
sudo pear install Net_SMTP
-
Finally, run the last command to restart Apache2:
sudo systemctl restart apache2
-
You can close the SSH window now, and go back to your Lightsail page. Click the Account button and enter the SSH keys tab.

-
Click the Download button to download the SSH key to get connected to your instance via FileZilla. Save that file in a safe place, you'll need it soon.

-
Go back to Home, copy the Static IP of your instance, open a new tab in your browser and paste it, then hit Enter.

-
You should get a page like the one below, which means that your server is running smoothly and it's ready top host the HelloDB files.

-
Download Filezilla and install it on your computer.
Open it and click the icon on the top-left corner. Click the New site button and give a name to your FTP connection

-
In the Site Manager popup, select the SFTP - SSH protocol, paste your Static IP address in the Host field, Select the Key file as Logon Type, type ubuntu in the User field and upload the Key file you downloaded from your lightsail account a while ago.

-
Enter the Advanced tab and type:
/var/www/html/
Then cick the Connect button.

-
If you did everything correctly, you should see an index.html file in the list.
You have to enter the SSH terminal of your instance on Lightsail in order to run the command that sets the writing permissions to the html folder, so you'll be able to upload the HelloDB files.
Type this command in the terminal and hit Enter:
sudo chmod -R 777 /wav/www/html
-
You can finally delete the index.html file and upload the api folder in your server.
Simply drag and drop that folder and wait for Filezilla to upload all files

Configurations
On FileZilla you can right-click on a file and select the View/Edit option. That will open the selected file in your favorite text editor.
I recommend you to download
Sublime Text and configure it as custom editor in the FileZilla's Settings panel -> File editing option.
• Protect JSON data files from being viewed by Web browsers
Use FileZilla to edit the apache2.conf
file - it's into the /etc/apache2/
directory and you can access it with Filezilla - and add the following lines on the bottom that file:
<Directory /var/www/html/>
<Files "*.json">
Order allow,deny
Deny from all
</Files>
</Directory>
In this way, nobody will ever see any .json
file of your database from a browser, not even you, and your data are safe.
IMPORTANT: After pasting those lines of code and saving the apache2.conf
file, make sure to restart Apache with this command in the SSH Terminal window:
sudo systemctl restart apache2
Also, in case you won't be able to save the apache2.conf
file, run this command in the terminal:
sudo chmod -R 777 /etc/apache2
• HelloDB Database path
Open the _config.php
file and edit the Database Path:
$DATABASE_PATH = "http://1.2.3.4/api/";
You must change that string into the URL of your server, where you've uploaded the backend
folder into - even if you've renamed it.
Example: Let's pretend your server URL is http://1.60.156.165/
and you kept the api
folder's name. You should set the $DATABASE_PATH
variable as it follows:
$DATABASE_PATH = "http://1.60.156.165/api/";
Instead, if you've renamed the api
folder into something like abc123def456
:
$DATABASE_PATH = "http://1.60.156.165/abc123def456/";
• App name
You can set the name of your application in this variable:
$APP_NAME = "MyApp";
• Admin login credentials
In order to set the username
and password
credentials to access the dashboard (the database), you must set those strings here:
$ADMIN_USERNAME = "admin";
$ADMIN_PASSWORD = "admin";
• Admin email address
You may need to provide an email address to users to get in touch with you, so set it in this variable:
$ADMIN_EMAIL = "myemail@address.com";
• Google Sign In [Web OAuth Key]
In order to sing in with Google on a Login web page, you must provide your own OAuth key.
Create it on your own Google Cloud Platform and set it in this variable:
$GOOGLE_SIGNIN_KEY = '';
• Apple Sign In and iOS Push Notifications
In order to make your iOS app send and receive Push Notifications and Sign In with Apple, you must perform the following actions:
-
Register your app's Bundle Identifier (the App ID) on your Apple Developer Account -> Certificates, Identifiers & Profiles -> Identifiers section
-
Click the Keys option from the left menu, then the (+) blue button to add a new Key.
-
Type a key name, enable the
Apple Push Notifications service (APNs)
and Sign In with Apple
options in the Register a New Key section.
-
Click the Configure button next to the
Sign In with Apple
option and select your Bundle Identifier from the Primary App ID dropdown menu. Lastly, click the Save button.
-
Download the
AuthKey.p8
file on your computer - pay attention, you'll be able to download it only once, so save it in a safe place.
-
On the Configure key page, click the Continue and then the Save button.
-
Delete the existing
AuthKey_2NDC475ZK3.p8
file from the _Push
folder and upload your own one inside it.
When you're done, replace the following variables in the _config.php
file with your own data:
$AUTH_KEY_FILE = 'AuthKey_ABC123DEF4.p8'; // Your p8 Key file name
$APN_KEY_ID = 'ABC123DEF4'; // Your Apple Push Notification Key ID
$TEAM_ID = 'ZABC345QWE67'; // Your Team ID (see Apple Developer Portal -> Membership)
$BUNDLE_ID = 'com.myname.appname'; // Your iOS App's Bundle Identifier, the one you've set in Xcode
$APN_URL = 'https://api.development.push.apple.com'; // OR: http://api.push.apple.com [for Production environment]
You can leave the $APN_URL
variable as it is, since Push Notifications will still work even if you publish your app on the App Store without setting it into Production environment. It's just your choice.
• Android Push Notifications
If you don't have' a Firebase project on your Firebase Console, create one and enter the Project Overview -> Cloud Messaging section, copy the Server key
from the Project credentials box and replace the existing string of this variable with your copied one:
$FCM_SERVER_KEY = 'AAAALIIuEYQ:APA91bFzOf-yDRyp7Idb1AcweD6DQUVLr5zrbWVnB-uVTy0w6dt0UUXKDXthiHzqm...';
Remember to also download the google-services.json
file and replace the one in the app
folder of the Android Studio project's folder, that file will set the values of your project to send/receive Push Notifications in your Android app.
• Email Verification
If you want to send an email verification link to new users who sign up for the first time in your apps/website, just set the following variable into true
:
$IS_EMAIL_VERIFICATION = false;
NOTE: If you want to edit the message and other details of the Verification email, open the m-signup.php
file from the _Tables
folder, scroll down and check code inside the if ($IS_EMAIL_VERIFICATION) { ... }
statement.
Do not change or remove the $APP_NAME
and $DATABASE_PATH
variables, you may change the message strings and "From" email address.
Using PEAR Mail.php to send emails
If you open the following files:
m-signup.php
forgot-password-php
send-mail.php
you will notice 2 different mail functions.
The commented code is the default sendmail function, in case your server will support it.
The uncommented code is a PEAR Mail.php
function.
In order to use it, you need an email address hosted with a domain (ex: info@mydomain.com).
Add your eial's SMTP host name, address and password in the following lines:
host' => 'your_smtp_host', // <-- YOUR SMTP HOST
'username' => 'your@email.com', // <-- YOUR EMAIL ADDRESS
'password' => 'your_password' // <-- YOUR PASSWORD
You can get the SMTP host of your email address by checking your email settings of your hosting provider.
Alternatively, you may use to use a Gmail address. Edit the lines below as follows:
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'youremail@gmail.com',
'password' => 'your_gmail_password'
IMPORTANT: You must also turn the "Less Secure App" option ON on your Gmail settings
here.
Be aware that a Gmail SMTP server sends max 99 emails/day, and the From input gets ignored, so the emails you'll receive will show your gmail address in the
form
field, not a custom one.
• Utility Functions
The _config.php
file contains some utility functions and variables that make the entire system work.
DO NOT EDIT anything below the following comment:
// UTILITY FUNCTIONS AND GLOBAL VARIABLES
Otherwise the API will not work properly.
The Dashboard
The Dashboard is the admin panel where you can see your database data, add/delete columns, edit/delete/add rows and sort objects, as well as send Push Notifications to mobile devices.
• About Data Tables
All data get stored into dedicated JSON files, they are shown as Tables in the dashboard.
In the _Tables
folder you can find the Users.json
file, which is mandatory and cannot be deleted.
You can create new Tables
by clicking the Add Table button - in the Tools dropdopwn menu - but please follow these important suggestions:
-
You MUST NOT delete or rename the
Users.json
file.
-
Use the Add Table button on the Dashboard to create JSON files in the
_Tables
folder. Do not manually do that in your compurer and then upload them into your own server, it's not convenient and may cause errors!
-
When you create a Table, the next thing to do is to add the necessary columns, otherwise data cannot be properly saved.
-
Your Tables must at least have 1 row to keep column names stored, so if you use the Dashboard to delete rows, the API will create an empty default row that won't be visible in the Dashboard, but in the JSON file you'll see it. If you manually delete all data in a JSON file, you will lose all columns and will have to add them again thourhg the Dashboard.
-
Make a backup of your work frequently by downloading the JSON files form the
_Tables
folder with Filezilla.
• Add a Table
You can create a Table by clicking the [Add Table] button and typing the desired name.
• Import Tables
Use the [Import Tables] button and choose one or more JSON files (just make sure they are compatible with the API syntax for Tables).
Please note that Tables with the same name of the choosen one(s) will be overwitten, so be careful while importing data.
• Add a Column
Click the [Add Column] button and select the Type of data you want to set between the following ones:
String
Number
Array
File
Boolean
GPS
Pointer
Date
Type a name for your column and click the [Add column] button to create it.
You'll see that your new column gets created, along with 3 default other ones, which are:
ID_id
DT_createdAt
DT_updatedAt
• Column Types
There are 9 types of columns for your data, it's important to know their sytax and meaning, so you'll be able to wisely choose them while creating new columns for your database.
-
ID | composed by
ID_
prefix and id
suffix
This is the object Identifier of each row of the Table. It cannot be created manually, it gets automatically created by the API and its key is always ID_id
. It also cannot be edited, but it can be copied by a double-click on its cell.
-
String | composed by
ST_
prefix | the suffix will be the name you assigned to it
This is a String to host and display text
-
Number | composed by
NU_
prefix | the suffix will be the name you assigned to it
It supports integer and decimal numbers. PLEASE NOTE that decimal numbers must have the dot (.) as separator, not the comma (,).
Example: 123.45
-
Array | composed by
AR_
prefix | the suffix will be the name you assigned to it
This type is usually used to store arrays of Strings like object IDs or other text.
It's comma-separated, so be careful while storing an array, put commas where they are needed and do not use the array [""] brackets, just type values separated by commas in the array's input field.
Example: lorem,ipsum,dolor,sit
-
File | composed by
FL_
prefix | the suffix will be the name you assigned to it
This type hosts the URL of an uploaded file. Physical files get stored inside the uploads
folder
-
Boolean | composed by
BL_
prefix | the suffix will be the name you assigned to it
It's either true
or false
, as per standard JSON syntax.
Just keep in mind that if you use an input text field instead of a Checkbox or Switch button, the values you must pass are either "0" or "1" (True or False)
-
GPS | composed by
GPS_
prefix | the suffix will be the name you assigned to it
This type is an array of 2 decimal numbers, the latitude
and longitude
coordinates, usually used for location purposes.
Like the Number data type, the decimal separator of the coordinates must be a dot (.), and the separator of the 2 coordinates must be a comma (,)
Example: 12.236548,-45.845987
-
Pointer | composed by
PO_
prefix | the suffix will be the name you assigned to it
This type hosts the ID of an object taken from another Table. For instance, a user's ID from the Users table.
It's clickable in the Dashboard, it opens the selected object's row
-
Date | composed by
DT_
prefix | the suffix will be the name you assigned to it
The Date format is YYYY-MM-DDTHH:mm:ss
and it's a String
It's important to first plan what tables and columns you need to display and edit in your app or website, then create the proper JSON files into the _Tables
folder and add the necessary columns in your Dashboard.
• Rename a Table
From the Tools dropdown menu, click che [Rename a Table] button, select the Table you want to rename, type a new name for it and hit Rename Table.
Please note that you are not allowed to use special characters or spaces while renaming a Table.
• Delete a Table
Click che [Delete a Table] button, select the Table you want to delete and hit Delete Table.
• Add a row
Click the [Add Row] button form the Tools dropdown menu to add a row in a Table:
• Edit cells data
Double-click on a cell to view and edit its data in real time.
• Delete rows
Select one or multiple rows, then click the [Delete Row(s)] button to remove them.
• Copy a row
Select one row, then click the [Copy Row] button. You can then edit the copied row as you wish.
• Rename a column
Click the [Rename Column] button, select the column you want to rename, type a new name and hit Rename Column.
• Delete a column
Click the [Delete a Column] button, select the column you wish to remove, then hit [Delete column].
Column names will be shown with their the full names, which means you will see prefix and suffix together. Example: ST_text
• Search data
You can search for data in your Dashboard by clicking the [Search] button, select the column you wish to search data for, then choose the condition of your query and type the desired data.
Lastly, click the Search button:
• Sort data
You can order data in your table - ascending or descending order - by clicking the [Sort Data] button, select a column and the desired sorting condition. Click Order data to apply your query:
• Order Columns
You can reorder columns in your database by clicking the [ Order Columns] button.
Drag the desired columns up or down based on the order you need them to be shown, then click Apply:
• Send Push Notifications
You can send push notifications to all those users who have allowed their devices to receive Push Notifications in your app.
Click the [Send Push Notification] button in the left sidebar, select an audience, and type your message.
Hit the Send Push Notification button to send your push:
You can also type a string in the Push Type (optional)
field, in case you want to add a specific type for your push message.
• Background Jobs
The custom.js
file can host useful Javascript or jQuery functions that will be used as Background Jobs, here's an example:
$(function () {
//------------------------------------------------
// BACKGROUND JOB - DELETE ROWS WITH PASSED DATE
//------------------------------------------------
setInterval(function() {
// Search Date in Posts.json
$.getJSON("Posts.json", function(Posts){
for (var i=0; i < Posts.length; i++) {
var obj = Posts[i];
var aDate = new Date(obj['DT_aDate']);
var now = new Date();
if (aDate <= now) {
var rowID = obj['ID_id'];
var tableName = 'Posts';
// Delete row with passed Date
$.ajax({
url:"delete-row.php?tableName=Posts&rowID=" + rowID,
success:function(data) {
console.log('row deleted!');
}
});
}// ./ If
}// ./ For
});// ./ getJSON
}, 5 *1000); // execute every 5 seconds
});// ./ on page load
This is just an example to show you how to create a Background Job, a function that runs in a loop every X seconds and performs an action in the database.
In this case, the code checks for dates in the Posts
table -> DT_aDate
column, and if it finds some dates that are older than the current Date, it deletes the found rows.
You can create your own Jobs by using the jQuery setInterval(function() { ... }, SECONDS *1000);
function and perform actions in background (where "SECONDS" must be a number).
Please note that the Dashboard must be opened on your computer, like a server.
In case you must shut it down, a good trick may be to open the Dashboard on your smartphone's browser (Safari, Chrome, etc.) and leave it in background (do not kill the browser app), and of course, keep your smartphone on :)
[Web SDK]
The Web
folder contained in the package includes the HelloDBSDK.php
file that hosts the necessary functions to make the API work.
You must upload it in the same location of your website's file.
The SDK code is mainly written in Javascript, with the aid of jQuery, so you would need to use the $('#yourDivID').append()
to display data in your webpage.
Check the sample PHP files included in the Web
folder. I strongly recommend you to use those PHP files as a starter project template to build your own website, it'll be easier for you to edit code and design.
• Configurations
Open the HelloDBSDK.php
file and paste the path (URL) to your api folder (as mentioned in the beginning of this guide):
$DATABASE_PATH = 'https://1.2.3.4/api/';
It's always better to host a website in the root of your server - the public_html
folder - so the first options should be the right ones for you.
• Current User - HDBCurrentUser
You can get the Current logged in User by calling the HDBCurrentUser
function.
Here's an example (check the header.php
and index.php
files):
<script>
var currentUser = HDBCurrentUser();
if (currentUser != null) {
console.log(currentUser['ID_id']);
console.log(currentUser['ST_username']);
console.log(currentUser['ST_email']);
// currentuser IS NOT LOGGED IN...
} else {
console.log('currentUser is not logged in...');
// You may redirect the browser to the Login page:
window.location.replace("login.php");
}
</script>
• Sign In - HDBSignIn
To sign in as an existing user you have to call the HDBSignIn()
function and pass the usename
and password
parameters, usually via HTML inputs with id and name tags set as 'username'
and 'password'
.
Here's an example (check the user-login.php
file):
<!-- Sing in form -->
<input id="username" name="username" type="text" placeholder="Username">
<input id="password" name="password" type="password" placeholder="Password">
<!-- Sign In Button -->
<button id="loginButton" onclick="signIn()">Sign In</button>
<script>
function signIn() {
HDBSignIn();
});
</script>
• Sign Up - HDBSignUp
To sign up as a new user you have to call the HDBSignUp()
function and pass usename
, password
and email
parameters, usually via HTML inputs with id and name tags set as 'usename'
, 'password'
, 'email'
.
For additional parameters - for example, fullname
, file
, etc. - you must add them into the HDBSignUp()
function in the HelloDBSDK.php
file, like this example:
var p2 = { 'tableName': 'Users' };
p2['ID_id'] = cuArr[0];
// Additional data (optional)
p2['BL_bool'] = '0';
p2['ST_fullname'] = $('#fullName').val();
p2['FL_file'] = 'https://yourserver.com/assets/img/profilepic.png';
HDBObject(p2);
As you can see by the code above, the function's parameters needs to get the Users
table, the ID
of the signed up User and then the additional data (the extra columns you may have added to the Users table).
Here's an example of form
and Sign Up
execution (check the user-signup.php
file):
<!-- Inputs to pass data -->
<input id="username" name="username" type="text">
<input id="password" name="password" type="password">
<input id="email" name="email" type="email">
<input id="fullName" name="fullName" type="text">
<!-- Sign up button -->
<button id="signUpButton"><i class="fas fa-arrow-circle-right"></i></button>
<script>
// Sign Up
$(function () { $("#signUpButton").click(function () {
if ($('#username').val() == ''
|| $('#password').val() == ''
|| $('#email').val() == ''
|| $('#fullName').val() == ''
){ console.log('Please fill all the fields.');
} else {
var params = { 'signInWith': '' };
params['ST_username'] = $('#username').val();
params['ST_password'] = $('#password').val();
params['ST_email'] = $('#email').val();
params['ST_iosDeviceToken'] = '';
params['ST_androidDeviceToken'] = '';
HDBSignUp(params);
}// ./ If
}); });
</script>
• Reset Password - HDBForgotPassword
The user-login.php
page has a button called Forgot Password to reset the password stored after signing up for the first time - or even if it has already been changed.
The HDBForgotPassword()
function in the HelloDBSDK.php
file fires an alert with a text input where you have to type your valid email address and click the [Reset Password] button:
Here's how you call that function:
<a class="btn btn-warning" href="#" onclick="HDBForgotPassword()">Forgot password</a>
Check the HDBForgotPassword()
function in the HelloDBSDK.php
file to edit the alert messages as you wish.
• Query Objects - HDBQuery
You can query objects by calling the HDBQuery
function and passing it the tableName
, columnName
and orderBy
parameters. Then, inside a for loop based on the length of a JSON objects' array, set an IF statement to filter your query by the desired keys (columns) and values.
Here's an example - check the index.php
file:
<script>
function queryObjects() {
var objects = HDBQuery(
'tableName=' + 'Posts' // Set the Table name
+ '&columnName=' + 'DT_createdAt' // Get the createdAt Date column
+ '&orderBy=' + 'descending' // OR 'ascending'
);
// array of objects
var objectsArray = [];
for (var i = 0; i < objects.length; i++) {
// JSON Obj
var obj = objects[i];
// Query filters
if(
obj['ST_category'] == 'Fun'
|| obj['NU_likes'] == 1234
){ objectsArray.push(obj); }
// Finalize array of objects by removing duplicates (if any)
if (i == objects.length-1) {
objectsArray = HDBRemoveDuplicatesFromArray(objectsArray);
showData(objectsArray);
} //./ If
} // ./ For
}
// Show Data
function showData(objectsArray) {
for(var i = 0; i < objectsArray.length; i++){
// JSON object
var obj = objectsArray[i];
console.log(obj['ID_id']);
console.log(obj['ST_text']);
...
}
}
</script>
As you may notice at the end of the FOR
loop, we check when the loop ends and call a very important function called HDBRemoveDuplicatesFromArray
. That's because the IF statements may find the same object(s) more than once, so we must call that function to get the right data out of out query.
• Refresh Object Data - HDBRefreshObjectData
In order to get updated data of an object, call the HDBRefreshObjectData
function and pass the tableName
and the object ID
parameters.
Here's an example:
var refreshedObject = HDBRefreshObjectData("Users", "as34FiPrT");
console.log(refreshedObject['ST_username']);
...
• Get Pointer - HDBGetPointer
You can get the object's data of a Pointer object by calling the HDBGetPointer()
function and passing it the columnName
and tableName
parameters.
Here's an example - check the index.php
file:
var userPointer = HDBGetPointer(obj['PO_userPointer_Users'], 'Users');
Then, you can get the Pointer's data as a JSON object:
console.log(userPointer['ST_username']);
console.log(userPointer['ST_email']);
...
• Add/Edit an Object - HDBObject
In order to save/update an Object, you need to call the HDBObject()
function and pass it the necessary parameters, usually via HTML inputs with id and name tags set as the column names that need to be saved.
Here's an example - check the obj-details.php
and add-object.php
files:
<!-- Text -->
<textarea type="text" id="ST_text" name="ST_text" rows="2" ></textarea>
<!-- Likes -->
<input type="number" step="any" id="NU_likes" name="NU_likes" value="0">
<!-- GPS coordinates -->
<input type="number" step="any" id="latitude" name="latitude" >
<input type="number" step="any" id="longitude" name="longitude" >
...
<!-- Save button -->
<a href="#" id="saveButton" onclick="saveData()">Save Object
<script>
function saveData() {
// Prepare data
var params = {'tableName=':'Posts'}; // <- Mandatory!
params['ID_id'] = objectID; // USE THIS TO UPDATE AN OBJECT / REMOVE THIS LINE TO SAVE A NEW OBJECT
params['ST_text'] = $('#ST_text').val();
params['NU_likes'] = $('#NU_likes').val();
params['GPS_coords'] = $('#latitude').val() +','+ $('#longitude').val();
params['PO_userPointer_Users'] = currentUser['ID_id'];
// Save
var result = HDBObject(params);
if (result.includes('ID_id')) {
console.log('Object updated: ' + result);
}
});
</script>
• Delete an Object - HDBDelete
In order to delete an Object you need to call the HDBDelete()
function and pass the tableName and id parameters.
Here's an example - check the obj-details.php
file:
<!-- Delete Object button -->
<a href="#" id="deleteButton" class="btn btn-danger btn-block">Delete Object</a>
<script>
// Get object's ID
objectID = obj['ID_id'];
// Delete object [HDBDelete]
$("#deleteButton").click(function () {
var params =
'tableName=' + 'Posts' +
'&id=' + objectID;
var result = HDBDelete(params);
if (result == 'deleted') {
console.log('Object successfully deleted!');
}
});
</script>
• Utility functions
The HelloDBSDK.php
file contains a few useful functions like:
-
HDBRemoveDuplicatesFromArray()
-
uploadFile()
-
roundBigNumber()
-
Custom alerts
Do not edit those functions, otherwise the API won't work properly.
• Conclusion
Check the files included in the Web
folder to learn how to use HelloDB API, and also use those files as a starter project to build your own website.
[iOS SDK]
The iOS
folder contained in the package includes a demo XCode project written in Swift language, for you to get started with the HelloDB API.
That Xcode project contains the HelloDBSDK.swift
file that hosts all the necessary functions for the HelloDB backend, as well as the SwiftyJSON.swift
file to handle JSON data.
I recommend you to use the HelloDB project as your starter template to build your own application, you can easily rename the Xcode project and edit it as you wish, from the Swift code to the Storyboard design.
Just make sure to keep a copy of the original HelloDB.xcodeproj and its relative HelloDB folder somewhere in your computer.
• Configurations
Open the HelloDBSDK.swift
file in Xcode and start by setting your app name in this variable:
let APP_NAME = "HelloDB"
As you've done for the Dashboard, you must set the path to where you've uploaded the api
folder in your server:
let DATABASE_PATH = "http://1.2.3.4/api/"
So, replace that string above with your own server's URL.
Then you must replace the Google Sign In client key of the following variable with the one you created in your Google Cloud Platform:
let GOOGLE_SIGN_IN_CLIENT_ID = "191162618244-qmjf889os6jffa53d53el3dsk87..."
In order to obtain your own iOS OAuth Google Sign In key, please refer to the Official Google Documentation, you can find the code to execute in the Terminal
• Data format
The objects data of the iOS app are in JSON format and can be retrieved as follows:
Object Name["Column name"].Data Type
Let's make a practical example: you have launched a query to get data form the Posts
table and need to get the text of an object.
myPostObject["ST_text"].string!
The code above will print the text string of your JSON object -> "ST_text"
column.
The JSON data types you can get are the following:
String
Integer
Double
Array
Boolean
Here are some examples about how to get data values:
myObj["ST_string"].string!
myObj["NU_number"].int!
myObj["NU_number"].double!
myObj["AR_array"].array!
myObj["BL_bool"].bool!
• Current User - HDBCurrentUser
You can get the Current User data by calling the HDBCurrentUser()
function.
Here's an example (check the Home.swift
file):
HDBCurrentUser { (currentUser) in
if currentUser != nil {
// User is logged in
print(currentUser!["ST_username"].string!)
} else {
// User is logged out -> You may open a Login screen
}
• Sign In - HDBSignIn
The Sign In function can be called in a Login screen with a couple of TextFields: username
and password
.
Here's an example (check the Login.swift
file):
HDBSignIn(username: usernameTxt.text!, password: passwordTxt.text!) { (succ, e) in
if e == nil {
print("Signed in!")
// Go to the Home screen...
// Wrong username/password
} else { print(e!) }
}// ./ HDBSignIn
• Sign Up - HDBSignUp
The Sign Up function can be called in a SignUp screen with 4 TextFields: username
, password
, email
and fullname
(this last one is optional, but we'll keep it for this example).
Here's an example (check the SignUp.swift
file):
HDBSignUp(username: self.usernameTxt.text!, password: self.passwordTxt.text!, email: self.emailTxt.text!, signInWith: "") { (userID, e) in
if e == nil {
var params = ["tableName": "Users"]
params["ID_id"] = userID!
// Additional data (optional)
params["ST_fullname"] = self.fullnameTxt.text!
self.HDBObject(params) { (succ, e) in
if e == nil {
print("Signed up!")
// Do something...
// error
} else { print(e!)
}}// ./ HDBObject
// error
} else { print(e!)
}}// ./ HDBSignUp()
As you may notice from the code above, it calls 2 functions, the HDBSignUp
one to store the default User's data, such as username, password and email. Then it calls the HDBObject
function to update the just created User with additional data - in this case, it's the fullname
string, but you may need to add other data, as the demo Xcode Project actually does.
So, in case you have additional data to store in the Users
table while signing up, you must add them as parameteres inside the HDBSignUp
function, as shown above.
If you also open the Intro.swift
file, where you can sign in with Google and Apple, you'll see that the code calls the HDBObject
function with additional parameters in both the appleAuthLogin()
and
googleAuthLogin()
functions.
So, remember to add the same additional parameters you used in the Sign Up screen in those functions as well - unless, of course, you'll remove Google and Apple Sign In functions :)
• Reset Password - HDBResetPassword
Users have the ability to also request the app to reset their password, in case they forgot it (or just need to change it).
Here's an example (check the Login.swift
file):
HDBResetPassword(email: "myemail@address.com") { (result, e) in
if e == nil {
print(result!)
} else {
print(e!)
}
The result
string you'll get will be "Thanks, an email with a link to reset your password has been sent. Check your Inbox in a little while.".
This is because the forgot-password.php
file of the api
folder returns that string in case of success.
If you want to change such message, just edit it in the forgot-password.php
file.
• Logout - HDBLogout
Users can logout from the app by calling the HDBLogout
function.
Here's an example (check the Account.swift
file):
HDBLogout { (succ) in
if succ! {
print("Logged out!")
// Go to the Login screen...
} else { print("Something went wrong. Try again.")
}}// ./ HDBLogout
• Get Pointer Object - HDBGetPointer
In order to retrieve data of a Pointer object use the HDBGetPointer
function and pass the object[columnName].string!
and tableName
parameters.
Here's an example (check the Home.swift
file):
HDBGetPointer(pObj["PO_userPointer_Users"].string!, tableName: "Users") { (userPointer) in
print(userPointer!["ST_username"].string!)
print(userPointer!["NU_age"].int!)
print(userPointer!["AR_followers"].array!)
}
As you can see by the code above, you got a full JSON object called userPointer
form the Users
table. You can get its data like you did for any other object and use it as you wish.
• Refresh Object Data - HDBRefreshObjectData
In order to get updated data of an object, call the HDBRefreshObjectData
function and pass the tableName
and Object
parameters.
Here's an example:
HDBRefreshObjectData(tableName: "Posts", object: postObj) { (postObj, e) in
if e == nil {
// Use the 'postObj' as you wish...
// error
} else { self.simpleAlert(e!) }
} // HDBRefreshObjectData
• Delete an Object - HDBDelete
In order to delete an object, call the HDBDelete
function and pass the tableName
and object["ID_id"].string!
parameters.
Here's an example (check the ObjDetails.swift
file):
HDBDelete(tableName: "Posts", id: pObj["ID_id"].string!) { (succ, e) in
if e == nil {
print("Object successfully deleted!")
} else {
print(e!)
}
}
• Query Objects - HDBQuery
In order to query objects you have to call the HDBQuery
function and pass it the tableName
, columnName
and orderBy
parameters.
Then, inside a FOR
loop, based on the length of a JSON objects array, set an IF
statement to filter your query by the desired columns and values.
Here's an example (check the Home.swift
file):
HDBQuery(tableName: "Posts", columnName: "DT_createdAt", orderBy: "") { (objects, e) in
if e == nil {
// For
for i in 0..< objects!.count {
let pObj = objects![i]
// [Search for keywords] --------------------------
let keywords = self.searchText.components(separatedBy: " ")
if (self.searchText.count != 0) {
for j in 0..< keywords.count{
if pObj["ST_text"].string!.lowercased().contains(keywords[j].lowercased())
{ self.objectsArray.append(pObj) }
}// ./ For • Search
// [No keywords -> Query filters] ---------------------------
} else if
pObj["ST_category"].string! == "Fun"
|| pObj["NU_likes"].int! == 789789
{ self.objectsArray.append(pObj) }
// [Finalize array of objects]
if i == objects!.count-1 { self.objectsArray.HDBRemoveDuplicatesFromArray() }
}// ./ For
if self.objectsArray.count != 0 {
// Show data in a TableView or somewhere else...
} else {
print("No results!")
}// ./ If
// error on query
} else { print(e!) }
}// ./ HDBQuery
As you may notice at the end of the FOR
loop, the code checks when the loop ends and calls a very important function called HDBRemoveDuplicatesFromArray
. That's because the IF
statements may find the same object(s) more than once, so we must call that function to get the right data out of out query.
• Add/Edit an Object - HDBObject
In order to save an Object you need to call the HDBObject
function and pass it the necessary parameters.
Here's an example (check the ObjDetails.swift
and AddObject.swift
files):
var params = ["tableName": "Posts"] // <- Mandatory
params["ID_id"] = myObj["ID_id"].string! // USE THIS TO UPDATE AN OBJECT / REMOVE THIS LINE TO SAVE A NEW OBJECT
params["ST_text"] = textTxt!.text!
params["NU_number"] = numberTxt!.text!
params["FL_file"] = fileURL!
params["DT_date"] = HDBGetStringFromDate(self.selectedDate) // Used to save a Date()
params["AR_array"] = HDBGetStringFromArray(likedBy) // Used to save an Array[String]
params["GPS_coords"] = latitudeTxt.text! + "," + longitudeTxt.text!
params["PO_userPointer_Users"] = currentUser["ID_id"].string!)
HDBObject(params) { (e, obj) in
if e == nil {
print("Object Updated: " + obj!["ID_id"].string!)
// error
} else { print(e!)
}}// ./ HDBObject
IMPORTANT:
-
In order to UPDATE AN OBJECT, add its
ID
in the parameters array.
Example:
params["ID_id"] = myObj["ID_id"].string!
-
In order to save a File, make sure to get its URL with the
HDBUploadFile
function.
Example:
HDBUploadFile(fileData: imageData!, fileName: "image.jpg") { (fileURL, e) in
if e == nil {
print(fileURL!)
var params = ["tableName":"Posts"]
// Add parameter to save the file
params["FL_file"] = fileURL!
...
// error
} else { print(e!)
}}// ./ HDBUploadFile()
-
In order to save a GPS value (a CLLocation object with
latitude
and longitude
coordinates), you must convert your coordinates into a String with the HDBGetStringFromLocation
function.
Example:
params["GPS_coords"] = HDBGetStringFromLocation(myLocation)
-
In order to save a Pointer object, you must get its
ID
.
Example:
params["PO_userPointer_Users"] = currentUser["ID_id"].string!
-
In order to save an Array value, you must convert it into a String with the
HDBGetStringFromArray
function.
Example:
var myArray = ["asD4e5Fo9","oIU8fDe23"]
params["AR_array"] = HDBGetStringFromArray(myArray)
-
In order to save a Number, you must convert it into a String.
Example:
params["NU_number"] = "\(1234)"
// OR
paramas["NU_number"] = "\(45.678)"
-
In order to save a Boolean value, you must convert it into a String.
Example:
var myBool = false
var bool = ""
if myBool { bool = "1" } else { bool = "0" } // "0" is True, "1" is False
params["BL_boolean"] = bool
-
In order to save a Date value, you must convert it into a String with the
HDBGetStringFromDate
function.
Example:
var myDate = Date()
param(columnName: "DT_date", value: HDBGetStringFromDate(myDate))
-
In order to save a String value, just pass it as a String.
Example:
params["ST_text"] = "Lorem ipsum dolor sit amet quantum estit sum"
• Send a Push Notification
You can send Push Notifications through the app to iOS and Android devices by calling the HDBSendiOSPush
and HDBSendAndroidPush
functions and passing the message
, deviceToken
and pushType
parameters.
The pushType
value can also be empty, like ""
, that's needed just in case you need to pass an extra parameter to your Push Notification, the Push alert will not show that parameter in its message.
Here's an example (check the ObjDetails.swift
file):
// Send iOS Push notification
HDBSendiOSPush(message: "Hi there, this is a Push!", deviceToken: userPointer!["ST_iosDeviceToken"].string!, pushType: "chat") { (succ, e) in
if e == nil {
print("iOS PUSH SENT!")
}
}// ./ HDBSendiOSPush()
// Send Android Push notification
HDBSendAndroidPush(message: "Hi there, this is a Push!", deviceToken: userPointer!["ST_androidDeviceToken"].string!, pushType: "chat") { (succ, e) in
if e == nil {
print("ANDROID PUSH SENT!")
}
}// ./ HDBSendAndroidPush()
Don't forget to perform the Push Notification configurations in the HDBerver's _config.php
file, as explained in the beginning of this guide.
• Utility functions
The HelloDBSDK.swift
file contains a few utility functions like:
-
getImage(from : String)
-
HDBRemoveDuplicatesFromArray()
-
HDBUploadFile()
-
Custom alerts and more
• Conclusion
Check the HelloDB demo project included in the iOS
folder to learn how to use HelloDB API, and also use it as a starter project to build your own app.
[Android SDK]
The Android
folder contained in the package includes a demo Android Studio project written in Java language, for you to get started with the HelloDB API.
That Android Studio project contains the HelloDBSDK.java
file that hosts all the necessary code for the HelloDB backend, as well as the _FCMService.java
file to handle Push Notifications.
I recommend you to use the HelloDB project as your starter template to build your own application, you can easily rename the Android Studio project and edit it as you wish, from the Java code to the XML design.
Just make sure to keep a copy of the original HelloDB project folder somewhere in your computer.
• Configurations
Open the HelloDB
project with Android Studio, then open the HelloDBSDK.java
file and, as you've done for the API, you must set the path to where you've uploaded the api
folder in your server:
public static String DATABASE_PATH = "http://1.2.3.4/api/";
Then you must create an Android Google Sign In OAuth Key in your Google Cloud Platform, in order for the app to allow you to sing in with your Google account.
In order to obtain your own OAuth Google Sign In key, please refer to the Official Google Documentation, you can find a button to quickly create it
You can also change the app name from the strings.xml
file:
<string name="app_name">HelloDB</string>
• Data format
The objects data of the Android app are in JSON format and can be retrieved as follows:
Object Name.key("Column name").Data Type
Let's make a practical example: you have launched a query to get data form the Posts
table and need to get the text of an object.
myObj.key("ST_text").stringValue()
The code above will get the text string of your JSON object -> "ST_text"
column.
The JSON data types you can get are the following:
String
Integer
Double
Array
Boolean
Here are some examples about how to get data values:
pObj.key("ST_text").stringValue()
pObj.key("NU_number").intValue();
pObj.key("NU_number").doubleValue();
pObj.key("AR_array").getJSONArray();
pObj.key("BL_boolean").booleanValue();
• Current User - HDBCurrentUser
You can get the Current User data by calling the HDBCurrentUser()
function.
Here's an example (check the Home.java
file):
HDBCurrentUser(this, new HDBCurrentUserHandler() { @Override public void done(final JSON currentUser) {
// currentUse IS LOGGED IN!
if (currentUser != null) {
Log.i("hdb", currentUser.key("ST_username").stringValue());
// currentUse IS NOT LOGGED IN...
} else { startActivity(new Intent(ctx, Intro.class)); }
}}); // ./ HDBCurrentUser
• Sign In - HDBSignIn
The Sign In function can be called in a Login screen with a couple of EditText widgets: username
and password
.
Here's an example (check the Login.java
file):
HDBSignIn((Activity)ctx, usernameTxt.getText().toString(), passwordTxt.getText().toString(), new HDBSignInHandler() {
@Override
public void done(boolean success, String e) {
if (e == null) {
Log.i("hdb", "Signed in!");
// Go to the Home screen...
// error
} else { Log.i("hdb", e); }
}});// ./ HDBSignIn
• Sign Up - HDBSignUp
The Sign Up function can be called in a SignUp screen with 4 EditText widgets: username
, password
, email
and fullname
(this last one is optional, but we'll keep it for this example).
Here's an example (check the SignUp.java
file):
HDBSignUp((Activity) ctx, usernameTxt.getText().toString(), passwordTxt.getText().toString(), emailTxt.getText().toString(), "", new HDBSignUpHandler() {
@Override public void done(String result, String e) {
if (e == null) {
RequestParams params = new RequestParams();
params.put("tableName", "Users"); // <- Mandatory
params.put("ID_id", result); // <- Mandatory
// Add additional data
params.put("ST_fullname", fullnameTxt.getText().toString());
HDBObject((Activity)ctx, params, new HDBObjectHandler() { @Override public void done(boolean success, String e) {
if (e == null) {
Log.i("hdb", "Signed up!");
// Go to the Home screen...
// error
} else { Log.i("hdb", e);
}}});// ./ HDBObject
// error
} else { Log.i("hdb", e); }
}});// ./ HDBSignUp
As you may notice from the code above, it calls 2 functions: the HDBSignUp
one to store the default User's data, such as username, password and email, then the HDBObject
function. This updates the new User with additional data - in this case, it's the fullname
string, but you may need to add other data, as the demo project actually does.
So, in case you have additional data to store in the Users
table while signing up, you must add them as parameteres inside the HDBSignUp
function, as shown above, keeping in mind the the "tableName"
and the "ID_id"
parameters must be included.
If you also open the Intro.java
file, where you can sign in with Google, you'll see that the code calls the HDBObject
function with additional parameters in the handleSignInResult()
function.
So, remember to add the same parameters you did for the Sign Up screen in those functions as well - unless, of course, you'll remove the Google sing in function :)
• Reset Password - HDBResetPassword
Users have the ability to also request the app to reset their password, in case they forgot it (or just need to change it).
Here's an example (check the Login.java
file):
HDBResetPassword((Activity)ctx, "myemail@address.com", new ResetPasswordHandler() {
@Override
public void done(String result, String error) {
if (error == null) {
Log.i("hdb", result);
} else {
Log.i("hdb", error);
}
}});// ./ HDBResetPassword
The result
string you'll get will be "Thanks, an email with a link to reset your password has been sent. Check your Inbox in a little while.".
This is because the forgot-password.php
file of the api
folder returns that string in case of success.
If you want to change such message, just edit it in the forgot-password.php
file.
• Logout - HDBLogout
Users can logout from the app by calling the HDBLogout
function.
Here's an example (check the Account.java
file):
HDBLogout(new LogoutHandler() { @Override public void done(boolean success) {
if (success) {
Log.i("hdb", "Logged out!");
} else {
Log.i("hdb", Something went wrong. Try again");
}
}});
• Get Pointer Object - HDBGetPointer
If you've set a column of type Pointer in your Dashboard and you need to retrieve data of its object, use the HDBGetPointer
function and pass the object.key(columnName).stringValue()
and tableName
parameters.
Here's an example (check the Home.java
file):
HDBGetPointer((Activity) ctx, pObj.key("PO_userPointer_Users").stringValue(), "Users", new HDBPointerHandler() {
@SuppressLint("SetTextI18n")
@Override
public void done(final JSON userPointer, final String e) {
if (e == null) {
Log.i("hdb", userPointer.key("ST_username").stringValue());
Log.i("hdb", userPointer.key("NU_age").intValue());
Log.i("hdb", userPointer.key("AR_followers").getJsonArray());
// error
} else { log.i("hdb", e);
}}}); // ./ HDBGetPointer
As you can see by the code above, you got a full JSON object called userPointer
form the Users
table, and can get values like you did for any other object.
• Refresh Object Data - HDBRefreshObjectData
In order to get updated data of an object, call the HDBRefreshObjectData
function and pass the tableName
and Object
parameters.
Here's an example:
HDBRefreshObjectData((Activity)ctx, "Posts", postObj, new HDBRefreshObjectDataHandler() {
@Override
public void done(JSON postObj, String error) {
if (error == null){
// Use 'postObj' as you wish...
// error
} else { hideHUD(); simpleAlert(error, ctx); }
}}); // HDBRefreshObjectData
• Delete an Object - HDBDelete
You can delete an object by calling the HDBDelete
function and passing the tableName
and object.key("ID_id").stringValue()
parameters.
Here's an example (check the ObjDetails.java
file):
HDBDelete((Activity)ctx, "Posts", pObj.key("ID_id").stringValue(), new HDBDeleteHandler() {
@Override
public void done(boolean success, String e) {
if (e == null) {
Log.i("HDB", "Object successfully deleted!");
// error
} else { Log.i("hdb", e);
}}});// ./ HDBDelete
• Query Objects - HDBQuery
You can query objects by calling the HDBQuery
function and passing it the tableName
, columnName
and orderBy
parameters.
Then, inside a FOR
loop based on the length of a JSON objects array, set an IF
statement to filter your query by the desired columns and values.
Here's an example (check the Home.java
file):
HDBQuery((Activity)ctx, "Posts", "DT_createdAt", "descending", new HDBQueryHandler() {
@Override public void done(JSON objects, String error) {
if (error == null) {
for (int i = 0; i < objects.count(); i++) {
JSON pObj = objects.index(i);
// [Search for keywords] --------------------------
String[] keywords = searchText.toLowerCase().split(" ");
if (searchText.length() != 0) {
for (String keyword : keywords) {
if (
pObj.key("ST_text").stringValue().toLowerCase().contains(keyword.toLowerCase())
) { objectsArray.add(pObj); }
}// ./ For • Search
// [No keywords -> Query filters] ---------------------------
} else if (
pObj.key("ST_category").stringValue().matches("Fun")
|| pObj.key("NU_likes").intValue() == 789789
){ objectsArray.add(pObj); }
// [Finalize array of objects]
if (i == objects.count()-1) { objectsArray = HDBRemoveDuplicatesFromArray(objectsArray); }
}// ./ For
if (objectsArray.size() != 0) {
// Show data in a ListView or somewhere else...
} else {
Log.i("hdb", "No results!");
}// ./ If
// error
} else { Log.i("hdb", error); }
}});// /. HDBQuery
As you may notice at the end of the FOR
loop, we check when the loop ends and call a very important function called HDBRemoveDuplicatesFromArray
. That's because the IF statements may find the same object(s) more than once, so we must call that function to get the right data out of out query.
• Add/Edit an Object - HDBObject
In order to save a new Object you need to call the HDBObject
function and pass it the necessary parameters.
Here's an example (check the ObjDetails.java
and AddObject.java
files):
// Parameters
RequestParams params = new RequestParams();
params.put("tableName", "Posts");
params.put("ID_id", pObj.key("ID_id").stringValue()); // Used to update an object
params.put("ST_text", textTxt.getText().toString());
params.put("NU_number", numberTxt.getText().toString());
params.put("FL_file", fileURL);
params.put("DT_date", HDBGetStringFromDate(selectedDate)); // Used to save a Date()
params.put("AR_array", HDBGetStringFromArray(myArray)); // Used to save a ListArray<String>()
params.put("GPS_coords", latitudeTxt.getText().toString() + "," + longitudeTxt.getText().toString());
params.put("BL_bool", "1");
HDBObject((Activity)ctx, params, new HDBObjectHandler() { @Override public void done(String e, JSON obj) {
if (e == null) {
Log.i("hdb", "Object updated: " + obj.key("ID_id").stringValue());
// error
} else { Log.i("hdb", e);
}}});// ./ HDBObject
IMPORTANT:
-
In order to UPDATE an object, add its
ID
in the parameters array.
Example:
params.put("ID_id", myObj.key("ID_id").stringValue());
-
In order to save a File, make sure to get its URL with the
HDBUploadFile
function.
Example:
HDBUploadFile(filePath,"image.jpg", (Activity)ctx, new HDBFileHandler() {
@Override public void done(String fileURL, String e) {
if (fileURL != null) {
// Prepare data...
params.put("FL_file", fileURL);
// ...
// Upload failed
} else { Log.i("hdb", e); }
}});// ./ HDBUploadFile
-
In order to save a GPS value (a Location object with latitude and longitude coordinates), you must convert them into a String wiht the
HDBGetStringFromLocation
function.
Example:
params.put("GPS_coords", HDBGetStringFromLocation(myLocation));
-
In order to save a Pointer object, you must get its
ID
.
Example:
params.put("PO_userPointer_Users", currentUser.key("ID_id").stringValue());
-
In order to save an Array value, you must convert it into a String with the
HDBGetStringFromArray
function.
Example:
List<String> myArray = new ArrayList<>();
myArray.add("asD4e5Fo9");
params.put("AR_array", HDBGetStringFromArray(myArray));
-
In order to save a Number, you must convert it into a String.
Example:
params.put("NU_number", String.valueOf(1234));
// OR
params.put("NU_number", String.valueOf(45.678));
-
In order to save a Boolean value, you must convert it into a String and set it either as
"0"
or "1"
.
Example:
String bool = "1"; // "0" is False, "1" is True
params.put("BL_bool", bool);
-
In order to save a Date value, you must convert it into a String with the
HDBGetStringFromDate
function.
Example:
Date date = new Date();
params.put("DT_date", HDBGetStringFromDate(date));
-
In order to save a String value, just pass it as a String.
Example:
params.put("ST_text", "Lorem ipsum dolor sit amet quantum est sum");
• Send a Push Notification
You can send Push Notifications through the app to iOS and Android devices by calling the HDBSendiOSPush
and HDBSendAndroidPush
functions and passing the message
, deviceToken
and pushType
parameters.
The pushType
value can also be empty, like ""
, that's needed just in case you need to pass an extra parameter to your Push Notification.
Here's an example (check the ObjDetails.java
file):
// Send Android Push Notification
HDBSendAndroidPush((Activity)ctx, pushMessage, userPointer.key("ST_androidDeviceToken").stringValue(), "chat", new HDBAndroidPushHandler() {
@Override
public void done(boolean success, String e) {
if (e == null) {
Log.i("hdb", "ANDROID PUSH SENT!");
// error
} else { Log.i("hdb", e); }
}});// ./ HDBSendAndroidPush
// Send iOS Push Notification
HDBSendiOSPush((Activity)ctx, pushMessage, userPointer.key("ST_iosDeviceToken").stringValue(), "chat", new HDBiOSPushHandler() {
@Override
public void done(boolean success, String e) {
if (e == null) {
Log.i("hdb", "iOS PUSH SENT");
// error
} else { Log.i("hdb", e); }
}});// ./ HDBSendiOSPush
Of course you first have to configure both Push Notification settings in the HelloDB API, as explained in the beginning of this guide.
• Utility functions
The HelloDBSDK.java
file contains a few utility functions like:
- roundBigNumber();
- HDBRemoveDuplicatesFromArray();
- HDBUploadFile();
- etc...
• Conclusion
Check out the HelloDB demo project included in the Android
folder to learn how to use HelloDB API, and also use it as a starter project to build your own app.
[Unity SDK]
The Unity
folder contained in the package includes a demo Unity 3D project written in C# language, for you to get started with the HelloDB API.
The Unity project contains the HelloDBSDK
folder that hosts all the necessary files for the HelloDB backend - including the HelloDBSDK.cs
file.
You may use the HelloDB project as your starter template to build your own application, you can easily rename the project and edit it as you wish, from the C# code to the Scene layout.
Just make sure to keep a copy of the original HelloDB project folder somewhere in your computer.
• Configurations
Open the HelloDB
project with Unity, then open the HelloDBSDK.cs
file with your favorite editor and, as you've done for the API, you must set the path to where you've uploaded the api
folder in your server:
public static String DATABASE_PATH = "https://1.2.3.4/api/";
• Data format
The objects data of the Unity project are in JSON format and can be retrieved as follows:
Object Name["Column name"]
Let's make a practical example: you have launched a query to get data form the Posts
table and need to get the text of an object.
myObj["ST_text"]
The code above will get the text string of your JSON object -> "ST_text"
column.
The JSON data types you can get are the following:
String
Integer
Double
Array
Boolean
• Current User - HDBCurrentUser
You can get the Current User data by calling the HDBCurrentUser()
function.
Here's an example (check the Home.cs
file):
// Get Current User
JSONNode currentUser = HelloDBSDK.HDBCurrentUser();
if(currentUser == null) { Debug.Log("Current User is logged out...");
} else { Debug.Log(currentUser["ST_username"]); }
• Sign In - HDBSignIn
The Sign In function can be called in a Login scene, for instance, with a couple of InputField components: usernameField
and passwordField
.
Here's an example (check the SignInScreen.cs
file):
if (usernameField.text == "" || passwordField.text == ""){
HelloDBSDK.simpleAlert("Please type a username and a password.");
} else {
var result = HelloDBSDK.HDBSignIn(usernameField.text, passwordField.text);
if(result) { SceneManager.LoadScene("MainScene"); }
}
• Sign Up - HDBSignUp
The Sign Up function can be called in a SignUp scene with 4 InputField components: usernameField
, passwordField
, emailField
and fullnameField
(this last one is optional, but we'll keep it for this example).
Here's an example (check the SignUpScreen.cs
file):
if (usernameField.text == "" || passwordField.text == "" || emailField.text == "" || fullnameField.text == ""){
HelloDBSDK.simpleAlert("Please insert username, password, email and full name.");
} else {
var signUpResult = HelloDBSDK.HDBSignUp("unityUser", "qqq", "unity@mail.com", "");
// Additional user's data
List<String> parameters = new List<String>();
parameters.Add(HelloDBSDK.param("tableName", "Users"));
parameters.Add(HelloDBSDK.param("ID_id", signUpResult));
parameters.Add(HelloDBSDK.param("ST_fullname", "Unity Gamer"));
parameters.Add(HelloDBSDK.param("BL_bool", "0"));
parameters.Add(HelloDBSDK.param("FL_aFile", "0"));
var result = HelloDBSDK.HDBObject(parameters);
if (result.ToString().Contains("ID_id")){
Debug.Log("NEW USER SIGNED UP: " + result.ToString());
SceneManager.LoadScene("Home");
}
}
As you may notice from the code above, it calls 2 functions: the HDBSignUp
one to store the default User's data, such as username, password and email, then the HDBObject
function. This updates the new User with additional data - as the demo project actually does.
So, in case you have additional data to store in the Users
table while signing up, you must add them as parameteres inside the HDBSignUp
function, as shown above, keeping in mind the the "tableName"
and the "ID_id"
parameters must be included.
• Reset Password - HDBResetPassword
Users have the ability to also request the app to reset their password, in case they forgot it (or just need to change it).
Here's an example (check the SignInScreen.cs
file):
var result = HelloDBSDK.HDBResetPassword("my@email.com");
if(result != null){ HelloDBSDK.simpleAlert(result); }
The result
string you'll get will be "Thanks, an email with a link to reset your password has been sent. Check your Inbox in a little while.".
This is because the forgot-password.php
file of the api
folder returns that string in case of success.
If you want to change such message, just edit it in the forgot-password.php
file.
• Logout - HDBLogout
Users can logout from the app by calling the HDBLogout
function.
Here's an example (check the Home.cs
file):
if (EditorUtility.DisplayDialog(HelloDBSDK.GAME_NAME, "Are you sure you want to logout?", "Logout", "Cancel")){
// Logout
if(HelloDBSDK.HDBLogout()) { Debug.Log("Logged out!"); }
}
• Get Pointer Object - HDBGetPointer
If you've set a column of type Pointer in your Dashboard and you need to retrieve data of its object, use the HDBGetPointer
function and pass the object[columnName]
and tableName
parameters.
Here's an example (check the Home.cs
file):
var userPointer = HelloDBSDK.HDBGetPointer("Users", obj["PO_userPointer_Users"]);
Debug.Log("USERNAME: " + userPointer["ST_username"]);
Debug.Log("FULLNAME: " + userPointer["ST_fullname"]);
As you can see by the code above, you got a full JSON object called userPointer
form the Users
table, and can get values like you did for any other object.
• Refresh Object Data - HDBRefreshObjectData
In order to get updated data of an object, call the HDBRefreshObjectData
function and pass the tableName
and Object
parameters.
Here's an example (check the Home.cs
file):
var refreshedObj = HelloDBSDK.HDBRefreshObjectData("Posts", myObject);
Debug.Log("refreshed Object: " + refreshedObj.ToString());
• Delete an Object - HDBDelete
You can delete an object by calling the HDBDelete
function and passing the tableName
and object["ID_id"]
parameters.
Here's an example (check the Home.cs
file):
var result = HelloDBSDK.HDBDelete("Posts", "Abc123Def");
if(result) { Debug.Log("Object deleted!"); }
• Query Objects - HDBQuery
You can query objects by calling the HDBQuery
function and passing it the tableName
, columnName
and orderBy
parameters.
Then, inside a FOR
loop based on the length of a JSON objects array, set an IF
statement to filter your query by the desired columns and values.
Here's an example (check the Home.cs
file):
List<JSONNode> objectsArray = new List<JSONNode>();
var objects = HelloDBSDK.HDBQuery("Posts", "DT_createdAt", "");
for(int i=0; i<objects.Count; i++){
var obj = objects[i];
// [Query Filters]
if (obj["NU_likes"] == 123
|| obj["ST_category"] == "Game"
|| obj["AR_likedBy"].ToString().Contains("item1")
){ objectsArray.Add(obj); }
} //./ for
// Show data
if (objectsArray.Count != 0){
for (int i = 0; i < objectsArray.Count; i++){
var obj = objectsArray[i];
Debug.Log(obj["ST_text"]);
Debug.Log(obj["BL_aBool"]);
var date = HelloDBSDK.HDBGetDateFromString(obj["DT_myDate"]);
Debug.Log(date.ToString("MMM dd, yyyy"));
// Get image from URL
StartCoroutine(HelloDBSDK.GetImageFromURL(obj["FL_aFile"], myImage));
} //./ For
// No results
} else { Debug.Log("No results!"); }
• Add/Edit an Object - HDBObject
In order to save a new Object you need to call the HDBObject
function and pass it the necessary parameters.
Here's an example (check the Home.cs
file):
// Prepare Data
List parameters = new List();
parameters.Add(HelloDBSDK.param("tableName", "Posts"));
parameters.Add(HelloDBSDK.param("ST_text", "Text from Unity Game!"));
parameters.Add(HelloDBSDK.param("AR_likedBy", HelloDBSDK.HDBGetStringFromArray(myArray))); // Save a List<String>()
parameters.Add(HelloDBSDK.param("NU_likes", 12.12.ToString())); // Save a Number
parameters.Add(HelloDBSDK.param("GPS_coords", latitude.ToString() + "," + longitude.ToString())); // Save a GPS Location
parameters.Add(HelloDBSDK.param("DT_aDate", HelloDBSDK.HDBGetStringFromDate(myDate))); // Used to save DateTime
parameters.Add(HelloDBSDK.param("BL_aBool", "1")); // "0" -> FALSE | "1" -> TRUE
parameters.Add(HelloDBSDK.param("FL_aFile", fileURL));
// Save
var result = HelloDBSDK.HDBObject(parameters);
if (result.ToString().Contains("ID_id")){
Debug.Log("OBJECT SAVED/UPDATED: " + result.ToString());
}
IMPORTANT:
-
In order to UPDATE an object, add its
ID
in the parameters array.
Example:
parameters.Add(HelloDBSDK.param("ID_id", myObj["ID_id"]));
-
In order to save a File, make sure to get its URL with the
HDBUploadFile
function.
Example:
// Get bytes[] of file
byte[] bytes = File.ReadAllBytes(Application.dataPath + "/Res/image.jpg");
// Upload a file
StartCoroutine(HelloDBSDK.HDBUploadFile(bytes, "image.jpg", fileURL => {
Debug.Log("UPLOADED FILE URL: " + fileURL);
}})); //./ HDBUploadFile
-
In order to save a GPS value (a Location object with latitude and longitude coordinates), you must convert the coordinates into a String
Example:
parameters.Add(HelloDBSDK.param("GPS_coords", 12.123456.ToString() + "," + -14.654987.ToString()));
-
In order to save a Pointer object, you must get its
ID
.
Example:
parameters.Add(HelloDBSDK.param("PO_userPointer_Users", currentUser["ID_id"]));
-
In order to save an Array value, you must convert it into a String with the
HDBGetStringFromArray
function.
Example:
List<String> myArray = new ArrayList<>();
myArray.Add("asD4e5Fo9");
parameters.Add(HelloDBSDK.param("AR_array", HelloDBSDK.HDBGetStringFromArray(myArray)));
-
In order to save a Number, you must convert it into a String.
Example:
parameters.Add(HelloDBSDK.param("NU_number", 1234.ToString()));
-
In order to save a Boolean value, you must convert it into a String and set it either as
"0"
or "1"
.
Example:
String bool = "1"; // "0" is False, "1" is True
parameters.Add(HelloDBSDK.param("BL_bool", bool));
-
In order to save a Date value, you must convert it into a String with the
HDBGetStringFromDate
function.
Example:
var myDate = System.DateTime.Now;
parameters.Add(HelloDBSDK.param("DT_aDate", HelloDBSDK.HDBGetStringFromDate(myDate)));
-
In order to save a String value, just pass it as a String.
Example:
parameters.Add(HelloDBSDK.param("ST_text", "Lorem ipsum dolor sit amet"));
• Send a Push Notification
You can send Push Notifications through the game to iOS and Android devices by calling the HDBSendiOSPush
and HDBSendAndroidPush
functions and passing the message
, deviceToken
and pushType
parameters.
The pushType
value can also be empty, like ""
, that's needed just in case you need to pass an extra parameter to your Push Notification.
Here's an example (check the Home.cs
file):
var iosPush = HelloDBSDK.HDBSendiOSPush("Push from Unity!", userPointer["ST_ioDeviceToken"], "chat");
var anPush = HelloDBSDK.HDBSendAndroidPush("Push from Unity!", userPointer["ST_androidDeviceToken"], "chat");
Of course you first have to configure both Push Notification settings in the HelloDB API, as explained in the beginning of this guide.
• Utility functions
The HelloDBSDK.cs
file contains a few utility functions like:
- GetImageFromURL();
- HDBGetStringFromArray();
- HDBUploadFile();
- etc...
• Conclusion
Check out the HelloDB demo project included in the Unity
folder to learn how to use HelloDB API, and also use it as a starter project to build your own game.