B4J Question [BANano] What is the best approach to build data-intensive BANanoServer WebApps?

Mashiane

Expert
Licensed User
Longtime User
Hi there...

The image below depicts my implementation of the BrowserIndex and ServerIndex symbiotic for my CRUD WebApps.

When one wants to load a combobox, load a data-table, perform some CRUD on a table, the following steps are undertaken.

1. In the BrowserIndex (client) there is a method named "XOnBrowser". This sends whatever payload I need to execute on the database to the ServerIndex (server) using ws.RunFunctionWithResult. This happens on a click of a button (SAVE/DELETE/UPDATE/INSERT), opening of a page (data-table) etc.

Example Code that runs on client.

B4X:
Sub DirectMySQLOnBrowser(payload As Map)            'ignoreDeadCode
    'only send to the server what is needed
    Dim jsonPayload As String = BANano.ToJson(payload)
    'push the payload to the server and return the result to JRDCOnBrowser_Result
    ws.RunFunctionWithResult("DirectMySQLOnServer", Array(jsonPayload))
End Sub


2. The payload is received by ServerIndex "XOnServer", which the processes the payload, by executing whatever CRUD functionality I need and sends the "result" back to the client using ws.RunFunction

Example Code that runs on Server

B4X:
'receive the payload from BROWSERIndex And process it-p
Sub DirectMySQLOnServer(jsonPayload As String)            'ignore
    Dim returnList As List
    returnList.Initialize
    Dim payload2browser As Map = CreateMap()
    'extract the payloader
    Dim payload As Map = MapFromJSON(jsonPayload)
    'get the query
    Dim query As String = payload.Get("query")
    'get the command
    Dim scommand As String = payload.Get("command")
    'get the arguements
    Dim args As List = payload.Get("args")
    'get the dbpath
    Dim dbname As String = payload.Get("dbname")
    'get the tag
    Dim stag As String = payload.Get("tag")
    'get the username
    Dim susername As String = payload.Get("username")
    Dim spassword As String = payload.Get("password")
    Dim sport As String = payload.Get("port")
    Dim shost As String = payload.Get("host")
  
    'set the tag for the payload
    payload2browser.Put("tag", stag)
  
    If dDB.OpenMySQL(shost, sport, dbname, susername, spassword) Then
        Select Case scommand
            Case "insert", "update", "delete", "createtable"
                dDB.SQLRunQuery(query, args)
                payload2browser.Put("error", dDB.error)
                payload2browser.Put("ok", dDB.ok)
                payload2browser.Put("result", returnList)
            Case Else
                returnList = dDB.SQLExecuteMaps(query, args)
                payload2browser.Put("error", dDB.error)
                payload2browser.Put("ok", dDB.ok)
                payload2browser.Put("result", returnList)
        End Select
        dDB.SQLClose
    Else
        payload2browser.Put("error", "Cannot connect to the database!")
        payload2browser.Put("ok", False)
        payload2browser.Put("result", returnList)
    End If
    ws.RunFunction("DirectMySQLOnBrowser_Result", Array(payload2browser))
    ws.Flush
End Sub

NB: If there is no connection pool the first time, the connection is made and the connection pool is used each time. I close everytime I finish executing SQL statements

3. The XOnServer result is fired on the BrowserIndex page, this receives the payload and passes it to a "PayloadManager". The payload manager is available on any other page that has CRUD functionality and these are linked to the BrowserIndex page. This UI update could be updating contents of a combobox, updating a data-table etc, after a CRUD action is performed.

B4X:
Sub DirectMySQLOnBrowser_Result (payload As Map)
    'save the payload for later
    vuetify.SetData("payload", payload)
    'each page should have a payload manager
    vuetify.RunMethodOnActive("PayloadManager")
End Sub

BANanoServer.jpg


This works well, but...

1. I have combo boxes in forms that need to be refreshed before the form is shown. The contents of these combo boxes might come from different tables in the database. So what happens is, before I show the form/dialog, I load the first combo (the process depicted above happens), then I load the second combo, after the first combo is done (the process depicted above happens again) etc etc. Then after all the combos are loaded, I show the form. The user updates the form with whatever changes they want, then click Save. (the process depicted above happens). After the save, depending on what I want to do, the payload manager runs a query to fetch all records and loads them into a data-table (the process depicted above happens).

2. In most cases, I experience a connection pool problem, this can be a timeout or whatever problem. If I can limit the number of "trips" to the server, it would make a lot of stuff go away, but the thing is at times the data is constantly dynamic and needs to be refreshed in the UI, so the BrowserIndex>ServerIndex to and fro is rather at a larger scale.

What are the best approaches for BANanoServer CRUD webapps that I can apply to ensure a smooth run across my webapps?

Thanks

PS: For now please dont pay attention of the passing of db credentials via the payload, that is not important for now. I plan to use aes encryption between client and server in the future.
 
Last edited:
Top