Skip to content

Add global kv#25

Merged
dimiro1 merged 4 commits intodimiro1:mainfrom
adamcrossland:add_global_kv
Feb 22, 2026
Merged

Add global kv#25
dimiro1 merged 4 commits intodimiro1:mainfrom
adamcrossland:add_global_kv

Conversation

@adamcrossland
Copy link
Contributor

This change adds a global kv store that can be accessed by multiple
functions.

To make this possible, this change adds three new functions to the kv
interface: getGlobal, setGlobal and deleteGlobal. These functions map
the behavior of get, set, and delete.

For example, let's say that I want to have an HTTP handler function
that increments a counter each time that it is called:

function handler(ctx, event)
    local count = kv.getGlobal("visitCounter") or 0
    count = count + 1
    kv.setGlobal("visitCounter", count)

    return {
        statusCode = 200,
        headers = { ["Content-Type"] = "application/json" },
        body = json.encode({
            message = "Visit count = " .. count
        })
    }
end

I also want to have a function that runs every minute to email a
report of the number of times that the first function has been
called:

function handler(ctx, event)
    -- Get data to report
    local count = kv.getGlobal("visitCounter") or 0

    -- Send email via Resend
    local result, err = email.send({
        from = "reports@fakeurl.dev",
        to = "reportreceivingperson@gmail.com",
        subject = "Current visit counter value",
        html = "<p>Current visit counter value is " .. count .. "</p>",
    })

    if err then
        log.error("Email error: " .. err)
        return {
            statusCode = 500,
            headers = { ["Content-Type"] = "application/json" },
            body = json.encode({ error = err })
        }
    end

    log.info("Email sent: " .. result.id)

    return {
        statusCode = 200,
        headers = { ["Content-Type"] = "application/json" },
        body = json.encode({
            success = true,
            email_id = result.id
        })
    }
end

The kv.getGlobal, kv.setGlobal and kv.deleteGlobal functions allow access to a kv store that is shared by all function.

This change adds the ability to have kv stores that are not scoped
to a function, allowing data to be shared between multiple functions.

To make this possible, this change adds two new functions to the kv
interface: openNamed and closeNamed. The openNamed function takes a
string parameter that specifies a shared kv store to use for all futher
kv.get, kv.set and kv.delete calls until the kv.closeNamed function
is called, which returns to using to the default, function-scoped
kv store.

For example, let's say that I want to have an HTTP handler function
that increments a counter each time that it is called:

function handler(ctx, event)

    kv.openNamed("shared")
    local count = kv.get("visitCounter") or 0
    count = count + 1
    kv.set("visitCounter", count)

    return {
        statusCode = 200,
        headers = { ["Content-Type"] = "application/json" },
        body = json.encode({
            message = "Visit count = " .. count
        })
    }
end

I also want to have a function that runs every minute to email a
report of the number of times that the first function has been
called:

function handler(ctx, event)
    -- Get data to report
    kv.openNamed("shared")
    local count = kv.get("visitCounter") or 0
    kv.closeNamed()

    -- Send email via Resend
    local result, err = email.send({
        from = "reports@fakeurl.dev",
        to = "reportreceivingperson@gmail.com",
        subject = "Current visit counter value",
        html = "<p>Current visit counter value is " .. count .. "</p>",
    })

    if err then
        log.error("Email error: " .. err)
        return {
            statusCode = 500,
            headers = { ["Content-Type"] = "application/json" },
            body = json.encode({ error = err })
        }
    end

    log.info("Email sent: " .. result.id)

    return {
        statusCode = 200,
        headers = { ["Content-Type"] = "application/json" },
        body = json.encode({
            success = true,
            email_id = result.id
        })
    }
end

By using kv.openNamed("shared") and kv.closeNamed(), both functions can
access the same kv store.
There shouldn't be any reason for that to be accessed outside of
this file.
This change adds a global kv store that can be accessed by multiple
functions.

To make this possible, this change adds three new functions to the kv
interface: getGlobal, setGlobal and deleteGlobal. These functions map
the behavior of get, set, and delete.

For example, let's say that I want to have an HTTP handler function
that increments a counter each time that it is called:

function handler(ctx, event)
    local count = kv.getGlobal("visitCounter") or 0
    count = count + 1
    kv.setGlobal("visitCounter", count)

    return {
        statusCode = 200,
        headers = { ["Content-Type"] = "application/json" },
        body = json.encode({
            message = "Visit count = " .. count
        })
    }
end

I also want to have a function that runs every minute to email a
report of the number of times that the first function has been
called:

function handler(ctx, event)
    -- Get data to report
    local count = kv.getGlobal("visitCounter") or 0

    -- Send email via Resend
    local result, err = email.send({
        from = "reports@fakeurl.dev",
        to = "reportreceivingperson@gmail.com",
        subject = "Current visit counter value",
        html = "<p>Current visit counter value is " .. count .. "</p>",
    })

    if err then
        log.error("Email error: " .. err)
        return {
            statusCode = 500,
            headers = { ["Content-Type"] = "application/json" },
            body = json.encode({ error = err })
        }
    end

    log.info("Email sent: " .. result.id)

    return {
        statusCode = 200,
        headers = { ["Content-Type"] = "application/json" },
        body = json.encode({
            success = true,
            email_id = result.id
        })
    }
end
@dimiro1
Copy link
Owner

dimiro1 commented Feb 22, 2026

Thank you!

@dimiro1 dimiro1 merged commit 6c5d5a4 into dimiro1:main Feb 22, 2026
1 of 2 checks passed
@adamcrossland adamcrossland deleted the add_global_kv branch February 22, 2026 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants