Ola
The BANanoRelax library allows one to work offline, sync the crud data to cloud storage and basically relax. For more details, see this thread.
Download
Getting Started
1. Include the attached BANanoRelax Class
2. In AppStart, include the following lines
See Examples on Main (output is on console log)
For more examples, see this post:
www.b4x.com
BANanoRelax
The BANanoRelax library allows one to work offline, sync the crud data to cloud storage and basically relax. For more details, see this thread.
Download
Getting Started
- Install CouchDB (specify admin and password names - keep these safe, you will need for sync
- Check CouchDB is running; http://127.0.0.1:5984/ (observe the JSON result)
- Open Fauxton on: http://127.0.0.1:5984/_utils/
- Enable CORS : http://127.0.0.1:5984/_utils/#_config/couchdb@localhost/cors (this helps with sync and replication)
1. Include the attached BANanoRelax Class
2. In AppStart, include the following lines
B4X:
BANano.header.AddJavascriptFile("pouchdb.min.js")
BANano.Header.AddJavascriptFile("pouchdb.find.js")
See Examples on Main (output is on console log)
For more examples, see this post:
[BANanoRelax] 1. Perform CRUD Offline, 2. Sync to Cloud and 3. Relax
Ola With the upcoming updates to BANano 7, with cron jobs, background workers, it makes it more awesome to work with. So i guess we can only be limited by our own imagination. Hand clap to Alain for such an amazing software package. So what is BANanoRelax? BANanoRelax is a DB lib, made of...

BANanoRelax
- Operators
B4X:
Public const OP_EQ As String = "$eq"
Public const OP_LTE As String = "$lte"
Public const OP_LT As String = "$lt"
Public const OP_GT As String = "$gt"
Public const OP_GTE As String = "$gte"
Public const OP_EXISTS As String = "$exists"
Public const OP_NE As String = "$ne"
Public const OP_IN As String = "$in"
Public const OP_NOT_IN As String = "$nin"
Public const OP_SIZE As String = "$size"
Public const OP_MOD As String = "$mod"
Public const OP_REGEX As String = "$regex"
Public const OP_ELEM_MATCH As String = "$elemMatch"
Public const OP_TYPEOF As String = "$type"
Public const OP_ALL As String = "$all"
- setHost (vHost As String)
set the remote host for synch
B4X:db.Host = "127.0.0.1"
- setPort (vPort As String)
set the remote port
B4X:db.Port = "5984"
- setUserName (vUserName As String)
set the admin username
B4X:db.UserName = "xxx"
- setPassword (vPwd As String)
set admin password
B4X:db.Password = "xxx"
- INITIALIZE (Module As Object, name As String, auto As Boolean) As BANanoRelax
B4X:'open the database for processing, true ensures BANano.GUIDs are generated 'name - name of database 'auto - own _id / generated by database, set false for own Dim db As BANanoRelax db.Initialize(Me, "mydb", true) db.WORK_OFFLINE
- WORK_OFFLINE As BANanoRelax
this creates an offline IndexDB database - WORK_ONLINE
connects directly with the online DB for your CRUD (does not sync, just opens an online connection) - SYNC (live As Boolean, retry As Boolean)
sync with remote DB
B4X:db.Host = "localhost" db.Port = "5984" db.UserName = "xxx" db.Password = "xxx" 'live - true (continuously) 'retry - when true retries if offline db.Sync(True, True) 'the callback for the sync errors Sub mydb_syncerror() End Sub
- WATCH_CHANGES (sinceWhen As String, live As Boolean, includedocs As Boolean) As BANanoRelax
B4X:'watch when changes take place db.WatchChanges("now", True, True) 'changes callback Sub db_change(change As Map) End Sub 'complete callback Sub db_complete(info As Map) End Sub 'error callback Sub db_error(err As Map) End Sub
- INSERT (nrec As Map) As BANanoRelax
'the callback for the insert -
B4X:
Dim person As Map = CreateMap() person.Put("_id", "1") person.Put("firstname", "Anele") person.Put("lastname", "Mbanga") [*]db.INSERT(person) [*] [*]Sub mydb_insert(Err As RelaxErr, Rec As RelaxRec) End Sub [/code' [*]PutWait (nrec As Map) As Map 'same as insert but uses wait [code] Dim person As Map = CreateMap() person.Put("_id", "1") person.Put("firstname", "Anele") person.Put("lastname", "Mbanga") Dim res As Map = BANano.Await(db.PutWait(person))
- GetAllWait (desc As Boolean) As List
'works the same as SELECT_ALL but uses wait
B4X:Dim recs As List = BANano.Await(db.GetAllWait(True)) Log(recs)
- GetWait (docID As String) As Map
same as read by uses wait
B4X:Dim rg As Map = BANano.Await(db.GetWait("1"))
- RemoveWait (docID As String) As Map
same as delete but uses wait
B4X:Dim rd As Map = BANano.Await(db.RemoveWait("1"))
- UpdateWait (docID As String, nrec As Map, force As Boolean) As Map
same as update but uses wait
B4X:rg.Put("firstname", "Usibabale") Dim ru As Map = BANano.Await(db.UpdateWait("1", rg, True))
- READ (docID As String) As BANanoRelax
B4X:'read a record from a table using the id db.READ("mashy") 'the callback for the insert Sub mydb_read(Err As RelaxErr, Doc As Map) End Sub
- DELETE (nrec As Map) As BANanoRelax
B4X:'delete the database record (use record with id and rev) db.DELETE(rec) 'the callback for the delete Sub mydb_delete(Err As RelaxErr, Rec As RelaxRec) End Sub
- UPDATE (nrec As Map, force As Boolean) As BANanoRelax
B4X:'update the database record (use record with id and rev) db.UPDATE(rec) 'the callback for the update Sub mydb_update(Err As RelaxErr, Rec As RelaxRec) End Sub
- INSERT_BULK (nrecs As List) As BANanoRelax
B4X:'insert bulk records, _id can be BANano.GenerateUUID Dim lst As List lst.Initialize lst.Add(CreateMap("title" : "Lisa Says", "_id": "doc1")) lst.Add(CreateMap("title" : "Space Oddity", "_id": "doc2")) db.INSERT_BULK(lst) 'the callback for the insert Sub mydb_insert(Err As RelaxErr, Rec As RelaxRec) End Sub
- InsertBulkWait (recs As List) As List
works like insert bulk but using Wait
B4X:'insert bulk records, _id can be BANano.GenerateUUID Dim lst As List lst.Initialize lst.Add(CreateMap("title" : "Lisa Says", "_id": "doc1")) lst.Add(CreateMap("title" : "Space Oddity", "_id": "doc2")) Dim res As Map = BANano.Await(db.InsertBulkWait(lst))
- SELECT_ALL (desc As Boolean) As BANanoRelax
B4X:'select all records from the data-base 'desc of True means show new-west on top sorted by ID, useful for datenow ids db.SELECT_ALL(True) 'the callback for the select all Sub mydb_selectall(Err As RelaxErr, Documents As List) For each rec As Map in Documents Next End Sub
- CREATE_INDEX (xFields As List) As BANanoRelax
B4X:'create an index based on fields db.CREATE_INDEX(Array("firstname", "lastname")) 'the callback for the select all Sub mydb_createindex(Err As RelaxErr, Documents As List) For each rec As Map in Documents Next End Sub
- CreateIndexWait (xFields As List) As Map
creates an index before a search is done
B4X:Dim res As Map = BANano.Await(db.CreateIndexWait(Array("firstname", "lastname"))) Log(res)
- SelectWhereWait As List
B4X:'specify the fields and the values to find, this will create an index on the SortBy fields '* means all fields else specify the field names to return 'the operators indicates the condition that field should meet 'SELECT * FROM db WHERE firstname = 'Usibabale' ORDER BY _id desc db.NEW_QUERY db.ADD_FIELDS(Array("*")) db.ADD_WHERE("firstname", db.EQ, "Usibabale") db.ADD_ORDER_BY(Array("_id:desc")) Dim docs As List = db.SelectWhereWait() for each rec As Map in docs next
- SELECT_WHERE As BANanoRelax
B4X:'SELECT * FROM db WHERE firstname = 'Usibabale' ORDER BY _id desc 'specify the fields and the values to find, this will create an index based on the sort by fields '* means all fields else specify the field names to return db.NEW_QUERY db.ADD_FIELDS(Array("*")) db.ADD_WHERE("firstname", db.EQ, "Usibabale") db.ADD_ORDER_BY(Array("_id:desc")) db.SELECT_WHERE 'the callback for the find all Sub mydb_selectwhere(docs As List) For each rec As Map in docs Next End Sub
- NEW_QUERY As BANanoRelax
clear where clause
B4X:'create a new query db.NEW_QUERY
- ADD_FIELDS (flds As List) As BANanoRelax
'set the fields for the select
'add all fields
'db.ADD_FIELDS(array("*")
'add particular fields
'db.ADD_FIELDS(array("firstname", "lastname")) - ADD_ORDER_BY (flds As List) As BANanoRelax
set the sort order
order in asc order .ADD_ORDER_BY("firstname")
order in desc order .ADD_ORDER_BY("firstname:desc") - ADD_WHERE (fld As String, operator As String, value As Object) As BANanoRelax
add a where clause for your select where
B4X:db.ADD_WHERE("firstname", db.EQ, "Usibabale")
- CLOSE
close the database connection
Last edited: