Skip to main content

Storage

Storage Types

Few types of storage modules exist. They provide a similar interface, but each one is best for the specific use case.

Namespaced Storage

storage.namespaced is local key/value storage for storing data between runs of a single rule. Keys are isolated (namespaced) within a single rule.

storage.namespaced.write

-- @param key string
-- @param value string,number,table
-- @return error
function storage.namespaced.write(key, value)
end

Sets value of a given key.

Example

local err = storage.namespaced.write("string", "1")
if err then
enapter.log("Failed to write value to storage: "..err)
end

local err = storage.namespaced.write("table", { value = 1 })
if err then
enapter.log("Failed to write value to storage: "..err)
end

storage.namespaced.read

-- @param key string
-- @return string,number,table, error
function storage.namespaced.read(key)
end

Returns value for a given key.

Example

local value, err = storage.namespaced.read("string")
if err then
enapter.log("Failed to read value from storage: "..err)
end
enapter.log("string: " .. value)
-- string: 1

local value, err = storage.namespaced.read("table")
if err then
enapter.log("Failed to read value from storage: "..err)
end
enapter.log("table: " .. inspect(value))
-- table: {
-- value = 1
-- }

Shared Storage

storage.shared is global key/value storage for storing data between runs of all rules. Keys are shared between rules that runs on the same Enapter Gateway.

storage.shared.write

-- @param key string
-- @param value string,number,table
-- @return error
function storage.shared.write(key, value)
end

Sets value of a given key.

Example

local err = storage.shared.write("string", "1")
if err then
enapter.log("failed to write value to storage: "..err)
end

local err = storage.shared.write("table", { value = 1 })
if err then
enapter.log("failed to write value to storage: "..err)
end

storage.shared.read

-- @param key string
-- @return string,number,table, error
function storage.shared.read(key)
end

Returns value for a given key.

Example

local value, err = storage.shared.read("string")
if err then
enapter.log("failed to read value from storage: "..err)
end
enapter.log("string: " .. value)
-- string: 1

local value, err = storage.shared.read("table")
if err then
enapter.log("failed to read value from storage: "..err)
end
enapter.log("table: " .. inspect(value))
-- table: {
-- value = 1
-- }

Public Storage

storage.public is key/value storage for variables accessible over Public MQTT API. The common use case for this type of storage is implementing an external interface for your rule configuration and commands. See Enapter Gateway Public MQTT documentation for more details about how to get/set variables via MQTT API.

storage.public.write

-- @param key string
-- @param value string,number,table
-- @return error
function storage.public.write(key, value)
end

Sets value of a given key.

Example

local err = storage.public.write("string", "1")
if err then
enapter.log("failed to write value to storage: "..err)
end

local err = storage.public.write("table", { value = 1 })
if err then
enapter.log("failed to write value to storage: "..err)
end

storage.public.read

-- @param key string
-- @return string,number,table, error
function storage.public.read(key)
end

Returns value for a given key.

Example

local value, err = storage.public.read("string")
if err then
enapter.log("failed to read value from storage: "..err)
end
enapter.log("string: " .. value)
-- string: 1

local value, err = storage.public.read("table")
if err then
enapter.log("failed to read value from storage: "..err)
end
enapter.log("table: " .. inspect(value))
-- table: {
-- value = 1
-- }

All Rights Reserved © 2024 Enapter AG.