Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

JS: Local database, transport and token helpers

Local database, transport and token helpers

Source code: src/helpers.js


jdb, a small database in memory

A jdb is a JSON object with a schema, a set of topics and a hook name. It holds the records of a view while the page is open. It is not TreeDB, which is the graph database of the backend.

{
    hook:   "data",
    type:   [],
    schema: { app_menu: [], account_menu: [], … },
    topics: { app_menu: [ {id: …}, … ] }
}

jdb_init(jdb, prefix, duplicate)

Prepares a jdb, and builds one topic for each entry of the schema. With duplicate set to true the function works on a deep copy, and the caller keeps its own object. It gives the jdb back.

A key of the schema that begins with two underscores is metadata, and the function builds no topic for it.

jdb_update(jdb, topic_name, path, kw)

Writes a record in a topic. The function writes a log error when the topic does not exist.

jdb_delete(jdb, topic_name, path, kw)

Deletes a record from a topic.

jdb_get(jdb, topic_name, id, recursive)

Gives the record with an identifier. The search goes down into the children through the hook. With recursive not given the function goes down.

jdb_get_by_idx(jdb, topic_name, idx)

Gives the record at an index of a topic.

jdb_get_topic(jdb, topic_name)

Gives a whole topic. Returns null and writes a log error when the topic does not exist.


Files and HTTP

load_json_file(url, on_success, on_error)

Reads a JSON file from a URL, and calls on_success with the object. It calls on_error when the read fails.

send_http_json_post(url, data, on_response)

Sends a POST request with a JSON body. It calls on_response(status, response_data).


Tokens

jwtDecode(jwt)

Reads a JWT, and gives its three parts back.

jwt2json(jwt, what)

Reads one part of a JWT as JSON. what accepts "header", "payload" and "signature", and the default is "payload".