Is it possible to call JS websql from go

I’m trying to learn deeply the GO "syscall/js" and came a long WebSQl, where I’ve the below function signature and want to replicate it in GO:

tx.executeSql(SQLstatement, [], successCallback, errorCallback);

Where tx is passed through the db, the full code is:

<script> 
  var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
  db.transaction(function (tx) { 
       tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) { 
          var len = results.rows.length, i; 
          msg = "<p>Found rows: " + len + "</p>"; 
          document.querySelector('#status').innerHTML +=  msg; 
      
          for (i = 0; i < len; i++) { 
             msg = "<p><b>" + results.rows.item(i).log + "</b></p>"; 
             document.querySelector('#status').innerHTML +=  msg; 
          } 
       }, null); 
  }); 
</script> 

My approach was as below:
=> Creating js.Global() for each function then Invoke it, so Itried with something like:

package main

import "syscall/js"

var (
	document js.Value
)

func init() {
	document = js.Global().Get("document")

}

var openDatabase = js.Global().Get("openDatabase")
var transaction = js.Global().Get("transaction")
var executeSql = js.Global().Get("executeSql")

type DB struct{}

func (db *DB) Open(file string) {
	db = openDatabase.Invoke(file, "1.0", "Test DB", 2*1024*1024)
}

func (db *DB) Query(tx string){
	db.transaction.Invoke(function(tx) { 
		db.executeSql.Invoke(tx, [], successCallback, errorCallback); 
	 });
}

func main() {
	db := DB.Open('mydb')
	db.Query('SELECT * FROM LOGS')
}

Then I stuck as couldn’t know how to manage the Callback functions.

Any thought?

Communication between Go and Javascript is a challenge IMHO.

My experience is that when you query a database the result must be included in a html template and rendered at the server. SSR (server side rendering). I have found no other way to communicate with the webpage (other than Websockets - maybe)

Both SSR (Server Side Rendering) and CSR (Client Side Rendering) have their benefits. But a mix of them is SSG (Static Site Generation) may be more beneficial. Which means that Go renders the HTML at build time from templates and all dynamic content provided by AJAX (Javascript). I connect an API created by Go (with SQL queries) and AJAX to call this API. I think you get the best of the two worlds. Go is good as an API and AJAX is good for REST queries.

SSG with AJAX reduce the page flickering as it only renders a part of the page. So my conclusion is to use AJAX to communicate with the API.

These are my conclusions. Hope to get proven wrong :slight_smile:

var url = "https://api2.go4webdev.org/test/1";

var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.setRequestHeader("Accept", "application/json");

xhr.onload = function () {
   if (xhr.readyState === 4) {
      document.getElementById("put").innerHTML = this.responseText
   }};

xhr.send();

Thanks, actually I’m trying to get done offline functionality in case of lost connection.

Local SQLite?

I’ve the so at shall server capacity that is not supporting database, so looking for database in the browser.

The only browser database I am aware of is localStorage and it is hard to reach from Go.