Skip to main content

Store

This item only works when running on the server. Server

A store is a class that holds inner savable objects, Keep(s), from a datastore DataStoreService:GetDataStore()

Types

StoreInfo

type StoreInfo = {
Namestring,
Scopestring?
}

Table format for a store's info in .GetStore()

unreleasedHandler

type unreleasedHandler = (Session) → string

Used to determine how to handle an session locked Keep.

info

Check LoadMethods for more info.

StoreBase

type StoreBase = {
LoadKeep(
string,
) → Promise<Keep>,
ViewKeep(string) → Promise<Keep>,
PreLoad(callback(Data) → Data) → (),
PreSave(callback(Data) → Data) → (),
PostGlobalUpdate(
string,
(GlobalUpdates) → ()
) → Promise,
validate(dataData) → (
boolean,
string?
)
}

Stores are used to load and save Keeps from a DataStoreService:GetDataStore()

MockStore

type MockStore = StoreBase

MockStores are used to mirror the real store, but doesn't save data

Store

type Store = StoreBase&{MockMockStore}

Stores are used to load and save Keeps from a DataStoreService:GetDataStore()

Properties

Mock

Store.Mock: MockStore

A mock store that mirrors the real store, but doesn't save data

validate

Store.validate: ({[string]any}) → true | (false&string)

Used to validate data before saving. Ex. type guards

keepStore.validate = function(data)
	for key, value in data do
		local dataTempVersion = dataTemplate[key]

		if typeof(data[key]) ~= typeof(dataTempVersion) then
			return false, `Invalid type for key: {key}`
		end
	end

	return true
end

Functions

LoadKeep

Store:LoadKeep(
keystring,
unreleasedHandlerunreleasedHandler?
) → Promise<Keep?>

Loads a Keep from the store and returns a Keep object

keepStore:LoadKeep(`Player_{player.UserId}`, function()
	return DataKeep.LoadMethods.ForceLoad
end)):andThen(function(keep)
	if not keep then
		player:Kick("Session lock interrupted!")
		return
	end

	print(`Loaded {keep:Identify()}!`)
end)
info

Stores can be loaded multiple times as they are cached, that way you can call :LoadKeep() and get the same cached Keeps.

ViewKeep

Store:ViewKeep(
keystring,
versionstring?
) → Promise<Keep>

Loads a Keep from the store and returns a Keep object, but doesn't save it

View-only Keeps have the same functions as normal Keeps, but can not operate on data

keepStore:ViewKeep(`Player_{player.UserId}`):andThen(function(viewOnlyKeep)
	print(`Viewing {viewOnlyKeep:Identify()}!`)
end)
warning

View-only Keeps are not cached!

warning

Keep:Destroy() must be called when view-only Keep is not needed anymore.

PreLoad

Store:PreLoad(callback({[string]any}) → {[string]any}) → ()

Runs before loading a Keep, allowing you to modify the data before, like decompressing compressed data

caution

Callback must return a new data table.

warning

:PreLoad() can only be set once.

Decompression example:

keepStore:PreLoad(function(data)
	local newData = {}

	for key, value in data do
		newData[key] = HttpService:JSONDecode(value)
	end

	return newData
end)

PreSave

Store:PreSave(callback({[string]any}) → {[string]any}) → ()

Runs before saving a Keep, allowing you to modify the data before, like compressing data

caution

Callback must return a new data table.

warning

:PreSave() can only be set once.

Compression example:

keepStore:PreSave(function(data)
	local newData = {}

	for key, value in data do
		newData[key] = HttpService:JSONEncode(value)
	end

	return newData
end)

PostGlobalUpdate

Store:PostGlobalUpdate(
keystring,
updateHandler(GlobalUpdates) → ()
) → Promise<updatedData,DataStoreKeyInfo>

Posts a global update to a Keep

updateHandler reveals globalUpdates to the API

keepStore:PostGlobalUpdate(`Player_{player.UserId}`, function(globalUpdates)
	globalUpdates:AddGlobalUpdate({
		Hello = "World!",
	}):andThen(function(updateId)
		print("Added Global Update!")
	end)
end)
Show raw api
{
    "functions": [
        {
            "name": "LoadKeep",
            "desc": "Loads a Keep from the store and returns a Keep object\n\n```lua\nkeepStore:LoadKeep(`Player_{player.UserId}`, function()\n\treturn DataKeep.LoadMethods.ForceLoad\nend)):andThen(function(keep)\n\tif not keep then\n\t\tplayer:Kick(\"Session lock interrupted!\")\n\t\treturn\n\tend\n\n\tprint(`Loaded {keep:Identify()}!`)\nend)\n```\n\n:::info\nStores can be loaded multiple times as they are cached, that way you can call [:LoadKeep()](#LoadKeep) and get the same cached Keeps.\n:::info",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "unreleasedHandler",
                    "desc": "",
                    "lua_type": "unreleasedHandler?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<Keep?>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 143,
                "path": "src/Store.luau"
            }
        },
        {
            "name": "ViewKeep",
            "desc": "Loads a Keep from the store and returns a Keep object, but doesn't save it\n\nView-only Keeps have the same functions as normal Keeps, but can not operate on data\n\n```lua\nkeepStore:ViewKeep(`Player_{player.UserId}`):andThen(function(viewOnlyKeep)\n\tprint(`Viewing {viewOnlyKeep:Identify()}!`)\nend)\n```\n\n:::warning\nView-only Keeps are not cached!\n:::warning\n\n:::warning\n[Keep:Destroy()](Keep#Destroy) must be called when view-only Keep is not needed anymore.\n:::warning",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "version",
                    "desc": "",
                    "lua_type": "string?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<Keep>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 423,
                "path": "src/Store.luau"
            }
        },
        {
            "name": "PreLoad",
            "desc": "Runs before loading a Keep, allowing you to modify the data before, like decompressing compressed data\n\n:::caution\nCallback **must** return a new data table.\n:::caution\n\n:::warning\n```:PreLoad()``` can only be set once.\n:::warning\n\nDecompression example:\n\n```lua\nkeepStore:PreLoad(function(data)\n\tlocal newData = {}\n\n\tfor key, value in data do\n\t\tnewData[key] = HttpService:JSONDecode(value)\n\tend\n\n\treturn newData\nend)\n```",
            "params": [
                {
                    "name": "callback",
                    "desc": "",
                    "lua_type": "({ [string]: any }) -> { [string]: any }"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 515,
                "path": "src/Store.luau"
            }
        },
        {
            "name": "PreSave",
            "desc": "Runs before saving a Keep, allowing you to modify the data before, like compressing data\n\n:::caution\nCallback **must** return a new data table.\n:::caution\n\n:::warning\n```:PreSave()``` can only be set once.\n:::warning\n\nCompression example:\n\n```lua\nkeepStore:PreSave(function(data)\n\tlocal newData = {}\n\n\tfor key, value in data do\n\t\tnewData[key] = HttpService:JSONEncode(value)\n\tend\n\n\treturn newData\nend)\n```",
            "params": [
                {
                    "name": "callback",
                    "desc": "",
                    "lua_type": "({ [string]: any }) -> { [string]: any }"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 553,
                "path": "src/Store.luau"
            }
        },
        {
            "name": "PostGlobalUpdate",
            "desc": "Posts a global update to a Keep\n\n```updateHandler``` reveals globalUpdates to the API\n\n```lua\nkeepStore:PostGlobalUpdate(`Player_{player.UserId}`, function(globalUpdates)\n\tglobalUpdates:AddGlobalUpdate({\n\t\tHello = \"World!\",\n\t}):andThen(function(updateId)\n\t\tprint(\"Added Global Update!\")\n\tend)\nend)\n```",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "updateHandler",
                    "desc": "",
                    "lua_type": "(GlobalUpdates) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<updatedData,DataStoreKeyInfo>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 584,
                "path": "src/Store.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "Mock",
            "desc": "A mock store that mirrors the real store, but doesn't save data",
            "lua_type": "MockStore",
            "source": {
                "line": 41,
                "path": "src/Store.luau"
            }
        },
        {
            "name": "validate",
            "desc": "Used to validate data before saving. Ex. type guards\n\n```lua\nkeepStore.validate = function(data)\n\tfor key, value in data do\n\t\tlocal dataTempVersion = dataTemplate[key]\n\n\t\tif typeof(data[key]) ~= typeof(dataTempVersion) then\n\t\t\treturn false, `Invalid type for key: {key}`\n\t\tend\n\tend\n\n\treturn true\nend\n```",
            "lua_type": "({ [string]: any }) -> true | (false&string)",
            "source": {
                "line": 62,
                "path": "src/Store.luau"
            }
        }
    ],
    "types": [
        {
            "name": "StoreInfo",
            "desc": "Table format for a store's info in [.GetStore()](#GetStore)",
            "lua_type": "{ Name: string, Scope: string? }",
            "source": {
                "line": 13,
                "path": "src/Types.luau"
            }
        },
        {
            "name": "unreleasedHandler",
            "desc": "Used to determine how to handle an session locked Keep.\n\n:::info\nCheck [LoadMethods] for more info.\n:::info",
            "lua_type": "(Session) -> string",
            "source": {
                "line": 29,
                "path": "src/Types.luau"
            }
        },
        {
            "name": "StoreBase",
            "desc": "Stores are used to load and save Keeps from a [DataStoreService:GetDataStore()](https://create.roblox.com/docs/reference/engine/classes/DataStoreService#GetDataStore)",
            "lua_type": "{ LoadKeep: (string, unreleasedHandler?) -> Promise<Keep>, ViewKeep: (string) -> Promise<Keep>, PreLoad: (callback: (Data) -> Data) -> (), PreSave: (callback: (Data) -> Data) -> (), PostGlobalUpdate: (string, (GlobalUpdates) -> ()) -> Promise, validate: (data: Data) -> (boolean, string?) }",
            "source": {
                "line": 42,
                "path": "src/Types.luau"
            }
        },
        {
            "name": "MockStore",
            "desc": "MockStores are used to mirror the real store, but doesn't save data",
            "lua_type": "StoreBase",
            "source": {
                "line": 72,
                "path": "src/Types.luau"
            }
        },
        {
            "name": "Store",
            "desc": "Stores are used to load and save Keeps from a [DataStoreService:GetDataStore()](https://create.roblox.com/docs/reference/engine/classes/DataStoreService#GetDataStore)",
            "lua_type": "StoreBase & { Mock: MockStore }",
            "source": {
                "line": 81,
                "path": "src/Types.luau"
            }
        }
    ],
    "name": "Store",
    "desc": "A store is a class that holds inner savable objects, Keep(s), from a datastore [DataStoreService:GetDataStore()](https://create.roblox.com/docs/reference/engine/classes/DataStoreService#GetDataStore)",
    "realm": [
        "Server"
    ],
    "source": {
        "line": 34,
        "path": "src/Store.luau"
    }
}