Share My Creation [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 PouchDB (Client) + CouchDB (Server).

1. You can work offline - it creates an indexedDB for you and you can use like how how you usually use BANanoSQL. As its not compatible with BANanoSQL,you cant use BANanoSQL commands.
2. This stores your data using an "_id" and a "_rev", this can either be your own specified id or use the BANano.GUID with (BANano.GenerateUUID). The "_rev" is generated automatically.
3. Using your own IDs is recommended.
4. Has a simple API for CRUD functionality.

CRUD Example - The BANano.Await() Version

1. Initialize the lib and tell it you want to work offline. By working offline, a local IndexedDB "todos" database is created. This is the recommended approach. And yes you can just work online with db.WORK_ONLINE.

B4X:
'initialize the db and use own ids
Public db As BANanoRelax
    db.INITIALIZE(Me, "todos", False)
    'specify connecction details for online sync / work online
    db.Host = "127.0.0.1"
    db.Port = "5984"
    db.UserName = "XXX"
    db.Password = "XXX"
    db.WORK_OFFLINE

2. Create a record - think of it as the PUT

When we initialized, we said we want to use own ids, so our id here is "1"

B4X:
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))
    Log(res)

Result:

1633225635692.png


3. Read a record (using own id) - this of it as the GET

B4X:
Dim rg As Map = BANano.Await(db.GetWait("1"))
    Log(rg)

Result

1633225739725.png


4. Update a record (using own id) - another PUT
'* here we are using the previous record we read, you can pass it any map object

Result

1633225766432.png


B4X:
rg.Put("firstname", "Usibabale")
    Dim ru As Map = BANano.Await(db.UpdateWait("1", rg))
    Log(ru)
'

5. List all records

B4X:
Dim recs As List = BANano.Await(db.GetAllWait(True))
    Log(recs)

Result

1633225819900.png


6. Delete a record (using own id) - think of it as the REMOVE

B4X:
Dim rd As Map = BANano.Await(db.RemoveWait("1"))
Log(rd)

Result

1633225867599.png



7. Sync with clould DB (this is 2 way to cloud and from cloud)

B4X:
db.SYNC(True)

8. Relax

So far tests are yielding good results.

CRUD Example - The BANano.CallBack() Version

For each CRUD call you make, you need to trap the result with a callback.

[Coming Soon]

So how do you get started with this?

We are still running tests. The lib will be available next week sometime, if however you want to test it out. Send a ping on this thread and will send as soon as available.

Ta

#SharingTheGoodNess
 

Attachments

  • 1633225319759.png
    1633225319759.png
    5.7 KB · Views: 302
  • 1633225707023.png
    1633225707023.png
    8.7 KB · Views: 310
Last edited:

alwaysbusy

Expert
Licensed User
Longtime User
This one was on my bucket list for quite a while but never found the time to wrap it, so I'm glad you tackled it. This is an interesting page for SQL buffs on how CouchDB compares to classic SQL: https://docs.couchdb.org/en/stable/ddocs/views/nosql.html. I always planned to write an extra SQL Query parser that could automatically convert them to NoSql commands and, when time permits, I still do. Good job! Looking forward to play with it.

Alwaysbusy
 

Mashiane

Expert
Licensed User
Longtime User
After adding the find plugin, executing statements like SELECT WHERE are possible. Here is the implementation, however you need to Create an index first on the fields to search on and the order of fields in your index is important.

9. Creating Indexes for searches (below we create an index using the first and lastname)

B4X:
Dim res As Map = BANano.Await(db.CreateIndexWait(Array("firstname", "lastname")))
Log(res)

10. Executing an equivalent of a SELECT WHERE statement

Selecting records where the firstname is equal to. You can specify which fields to select on array(*) [this selects all fields], the other array is the sort order and 0 is limit.
You can specify which fields to select [array(*)], [this selects all fields *], the other array is the sort order and 0 is limit.

B4X:
db.CLEAR_WHERE
    db.ADD_WHERE("firstname", db.EQ, "Usibabale")
    Dim res As List = db.SelectWhereWait(Array("*"), Array("firstname"), 0)
    Log(res)

Output:

1633274935399.png


11. Bulk Inserts

B4X:
'insert bulk records, _id can be BANano.GenerateUUID
Dim lst As List
lst.Initialize
lst.Add(CreateMap("firstname" : "Lisa Says", "_id": "doc1"))
lst.Add(CreateMap("firstname" : "Space Oddity", "_id": "doc2"))
Dim recs As List = BANano.Await(db.InsertBulkWait(lst))
 
Last edited:
  • Like
Reactions: LJG

Mashiane

Expert
Licensed User
Longtime User
Release:

 
  • Like
Reactions: LJG
Top