Decoding the response from a SQL query

We have a requirement to query the metadata field from ALL our users at a regular time interval. The approach we are taking is to execute an SQL query in nakama. If there is a better approach, please advise.

A simple example wrapped inside our custom RPC, we are running:

const query = `SELECT id,metadata FROM users`;
let parameters: any[] = [];
let Result : any = {};

rows = nk.sqlQuery(query, parameters);
           
 for (let row of rows) {
     Result[row.id] ={
        all_data : row
     };
 }
    
return JSON.stringify({
        code: "success",
        players: Result
    });

The non system account metadata you will see in the response for example is

and the return payload in the API explorer looks like

  "code": "success",
  "players": {
    "00000000-0000-0000-0000-000000000000": {
      "all_data": {
        "id": "00000000-0000-0000-0000-000000000000",
        "metadata": [
          123,
          125
        ]
      }
    },
    "2d770cdc-cc23-4dbf-ae19-661a475176d7": {
      "all_data": {
        "id": "2d770cdc-cc23-4dbf-ae19-661a475176d7",
        "metadata": [
          123,
          34,
          104,
          101,
          114,
          111,
          95,
          105,
          100,
          34,
          58,
          32,
          50,
          125
        ]
      }
    },

What format is the metadata ? how do we decode this response ?
Thanks !

ChatGPT helped figure it out :slight_smile:

JSON.parse(String.fromCharCode(...Result[row.id].all_data.metadata))

turns it into a json object I was able to traverse.

2 Likes