B4J Library [BANanoKeyStore] A KeyValue Store for BANano on top of IndexedDB

Hi there

The BANanoKeyStore is a Key Value store class that works like the b4j Key Value Store b4xlib.

How to use...
 

Attachments

  • BANanoKeyStore.bas
    5.8 KB · Views: 139
  • BANanoALASQLE.bas
    56.4 KB · Views: 141

Mashiane

Expert
Licensed User
Longtime User
Step 1. Initialize the class

B4X:
BANano.Await(kvs.Initialize("mykvs"))

This creates an IndexedDB database...

1657983987006.png
 

Mashiane

Expert
Licensed User
Longtime User
Step 2: Put Some Values

B4X:
'put a value
    BANano.Await(kvs.Put("name", "Anele"))
    BANano.Await(kvs.Put("dob", "1973-04-15"))
    BANano.Await(kvs.Put("doing", "Creating BANanoVuetifyAD3"))

You can also use Maps

B4X:
Dim nuser As Map = CreateMap()
    nuser.Put("first_name", "Usibabale")
    nuser.Put("last_name", "Mbanga")
    nuser.Put("dob", "2003-02-27")
    nuser.Put("doing", "Grade 12")
    BANano.Await(kvs.Put("usi", nuser))

And also use PutMapAsync

B4X:
Dim b4x As Map = CreateMap()
    b4x.Put("b4a", "Android Development")
    b4x.Put("b4j", "Desktop & Web Development")
    b4x.Put("b4i", "iOS Development")
    b4x.Put("b4r", "Arduino & IoT")
    BANano.Await(kvs.PutMapAsync(b4x))
 

Mashiane

Expert
Licensed User
Longtime User
Step 3: Get Some Values

B4X:
'get a value
    Dim res As String = BANano.Await(kvs.Get("name"))
    Log(res)

1657984271604.png


You can also get all records in the store

B4X:
Dim Result As Map = BANano.Await(kvs.GetMapAllAsync)
    Log(Result)

or get records with particular keys

B4X:
Dim Result As Map = BANano.Await(kvs.GetMapAsync(Array("dob")))
    Log(Result)

or get a list of all keys

B4X:
'get keys
    Dim lkeys As List = BANano.Await(kvs.ListKeys)
    Log(lkeys)

1657984415626.png


or GetDefault

B4X:
'get default
    Dim res1 As String = BANano.Await(kvs.GetDefault("lastname", "Mbanga"))
    Log(res1)
 

Mashiane

Expert
Licensed User
Longtime User
Complete Code for Example

B4X:
'Static code module
Sub Process_Globals
    Public BANano As BANano        'ignore
    Private kvs As BANanoKeyStore
End Sub

Sub Init                    'ignoreDeadCode
    BANano.Await(kvs.Initialize("mykvs"))
    'put a value
    BANano.Await(kvs.Put("name", "Anele"))
    BANano.Await(kvs.Put("dob", "1973-04-15"))
    BANano.Await(kvs.Put("doing", "Creating BANanoVuetifyAD3"))
    
    'get a value
    Dim res As String = BANano.Await(kvs.Get("name"))
    Log(res)
    'get default
    Dim res1 As String = BANano.Await(kvs.GetDefault("lastname", "Mbanga"))
    Log(res1)
    '
    'put a value to be removed
    BANano.Await(kvs.Put("toremove", "Must be removed"))
    'get keys
    Dim lkeys As List = BANano.Await(kvs.ListKeys)
    Log(lkeys)
    'remove a value
    BANano.Await(kvs.Remove("toremove"))
    
    'check if it contains
    Dim bres As Boolean = BANano.Await(kvs.ContainsKey("toremove"))
    Log(bres)
    
    Dim Result As Map = BANano.Await(kvs.GetMapAllAsync)
    Log(Result)
    
    Dim Result As Map = BANano.Await(kvs.GetMapAsync(Array("dob")))
    Log(Result)
    
    Dim nuser As Map = CreateMap()
    nuser.Put("first_name", "Usibabale")
    nuser.Put("last_name", "Mbanga")
    nuser.Put("dob", "2003-02-27")
    nuser.Put("doing", "Grade 12")
    BANano.Await(kvs.Put("usi", nuser))
    '
    Dim b4x As Map = CreateMap()
    b4x.Put("b4a", "Android Development")
    b4x.Put("b4j", "Desktop & Web Development")
    b4x.Put("b4i", "iOS Development")
    b4x.Put("b4r", "Arduino & IoT")
    BANano.Await(kvs.PutMapAsync(b4x))
    'get keys
    Dim lkeys As List = BANano.Await(kvs.ListKeys)
    Log(lkeys)

    Dim usi As Map = BANano.Await(kvs.Get("usi"))
    Log(usi)

End Sub
 
Top