Skip to content

Persistence

A key/value store for data that must outlive a session — player profiles, saved levels, high scores. Available in both roles; the runtime routes to the appropriate backing store. All operations are asynchronous: you pass a callback that the runtime invokes on completion.

Callbacks, briefly

Munos functions aren't first-class, but a builtin's callback slot accepts either an inline function literal or the name of a top-level function. You may declare the callback with any prefix of its full parameter list — trailing parameters you don't need are dropped.

The optional trailing ctx: i32 is an integer you supply at the call site and the runtime passes back to your callback unchanged — a lightweight way to route one shared callback by context (which key, which request).

Reading

munos
db_get(namespace: string, key: string, cb, ctx: i32 = 0)
// cb(success: bool, data: u8[], ctx: i32)
munos
db_get("profiles", "player42", got_profile)

function got_profile(success: bool, data: u8[]) {
    if !success { return }
    // ...decode data...
}

Writing and deleting

munos
db_set(namespace: string, key: string, data: u8[], cb?, ctx?)
// cb(success: bool, ctx: i32)

db_del(namespace: string, key: string, cb?, ctx?)
// cb(success: bool, ctx: i32)

For db_set and db_del the callback and its trailing ctx are jointly optional — omit both for fire-and-forget:

munos
db_set("profiles", "player42", encoded)            // fire-and-forget
db_set("profiles", "player42", encoded, on_saved)  // with confirmation

Listing keys

munos
db_list(namespace: string, prefix: string, cb, ctx: i32 = 0)
// cb(success: bool, keys: string[], ctx: i32)

Pass an empty prefix to list the whole namespace.

munos
db_list("levels", "world1-", got_levels)

function got_levels(success: bool, keys: string[]) {
    for (var i = 0; i < len(keys); i = i + 1) { print(keys[i]) }
}

The error event

munos
event db_error(op: string, namespace: string, key: string, msg: string)

Fires in addition to the success = false callback path when an operation hit infrastructure trouble (store unreachable, quota overflow). Declaring it is optional — failures are still observable through success = false — but it gives you the failure reason in one place. Available in both roles.

TIP

Data goes in and out as u8[]. Use pack/unpack or base64_decode / string_from_bytes to turn your records into bytes and back.

Part of the MultiNostalgia project.