We already have Firebase / Airtable/ Baserow for us to save our data on cloud.
Today I want to introduce another cloud server to you:
As far as I know, LeanCloud is supplying service like:
- data storage (insert/update/delete/select)
- user management (signup/login)
- leaderboard system
- file storage
- realtime communication
these function can be easily integrated in App Inventor/ Kodular/… using Web / Webviewer component.
And other functions maybe need an extension.
This post will show you how to use the LeanStorage.
Prepair work
- sign up and log in here: https://leancloud.app
- go inside Console
- Create an app:
- go inside the app, and create a Class
(at step 5, select No restrictions ) - copy and save keys for later use:
Read the documents
Leancloud has very detailed document, the rest api document can be found here:
https://docs.leancloud.app/rest_api.html
Create an object
An Object in LeanCloud is like a record in MySQL, it’s in a form of json object, like this:
{
"content": "Serverless cloud for lightning-fast development.",
"pubUser": "LeanCloud",
"pubTimestamp": 1435541999
}
the document the curl command to create object like this:
curl -X POST \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{appkey}}" \
-H "Content-Type: application/json" \
-d '{"content": "Serverless cloud for lightning-fast development.","pubUser": "LeanCloud","pubTimestamp": 1435541999}' \
https://{{first 8 digits of your App ID}}.api.lncldglobal.com/1.1/classes/Post
now we change this CURL command to App inventor block:
Now in the console, we have a record(or we should call it an Object) like this:
Retrieving Objects with constrains
document here: REST API Guide - LeanCloud Docs
in the document the curl command like this:
curl -X GET \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{appkey}}" \
-H "Content-Type: application/json" \
-G \
--data-urlencode 'where={"pubUser":"LeanCloud"}' \
https://{{first 8 digits of your App ID}}.api.lncldglobal.com/1.1/classes/Post
We change this to App inventor block:
We have a lot of Other operators available in the where
parameter:
Operator | Description |
---|---|
$ne |
not equal to |
$lt |
less than |
$lte |
less than or equal to |
$gt |
greater than |
$gte |
greater than or equal to |
$regex |
match a regular expression |
$in |
contain |
$nin |
not contain |
$all |
contain all (for array type) |
$exists |
the given key exists |
$select |
match the result of another query |
$dontSelect |
not match the result of another query |
for example To query posts published on 2015-06-29 :
curl -X GET \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{appkey}}" \
-H "Content-Type: application/json" \
-G \
--data-urlencode 'where={"createdAt":{"$gte":{"__type":"Date","iso":"2015-06-29T00:00:00.000Z"},"$lt":{"__type":"Date","iso":"2015-06-30T00:00:00.000Z"}}}' \
https://{{first 8 digits of your App ID}}.api.lncldglobal.com/1.1/classes/Post
the where clouse is a json string, which we can build it with Dictionary like this:
Now we know how to change curl command to blockly, in the same way we can update and delete Objects, just follow the Leancoud document.
to be continued for Leancloud Leaderboard