Skip to main content

DataStoreStage

This item only works when running on the server. Server

Provides a data storage facility with an ability to get sub-stores. So you can write directly to this store, overwriting all children, or you can have more partial control at children level. This minimizes accidently overwriting. The big cost here is that we may leave keys that can't be removed.

Layers in priority order:

  1. Save data
  2. Substores
  3. Base layer

Functions

new

DataStoreStage.new(
loadNamestring,
loadParentDataStoreStage?
) → DataStoreStage

Constructs a new DataStoreStage to load from. Prefer to use DataStore because this doesn't have any way to retrieve this.

See DataStore, GameDataStoreService, and PlayerDataStoreService.

-- Data store inherits from DataStoreStage
local dataStore = serviceBag:GetService(PlayerDataStoreService):PromiseDataStore(player):Yield()

Store

DataStoreStage:Store(
keystring | number,
valueany
) → ()

Stores the value, firing off events and queuing the item for save.

dataStore:Store("money", 25)

Load

DataStoreStage:Load(
keystring | number,
defaultValueT?
) → Promise<T>

Loads the data at the key and returns a promise with that value

dataStore:Load():Then(function(data)
	print(data)
end)

LoadAll

DataStoreStage:LoadAll(defaultValueany) → Promise<any>

Promises the full content for the datastore

dataStore:LoadAll():Then(function(data)
	print(data)
end)

GetSubStore

DataStoreStage:GetSubStore(keystring | number) → DataStoreStage

Gets a sub-datastore that will write at the given key. This will have the same helper methods as any other data store object.

local dataStore = DataStore.new()

local saveslot = dataStore:GetSubStore("saveslot0")
saveslot:Store("Money", 0)

Delete

DataStoreStage:Delete(keystring | number) → ()

Explicitely deletes data at the key

Wipe

DataStoreStage:Wipe() → ()

Queues up a wipe of all values. This will completely set the data to nil.

Observe

DataStoreStage:Observe(
keystring | number | nil,
defaultValueT?
) → Observable<T>

Observes the current value for the stage itself

If no key is passed than it will observe the whole view snapshot

AddSavingCallback

DataStoreStage:AddSavingCallback(
callbackfunction--

May return a promise

) → function--

Call to remove

Adds a callback to be called before save. This may return a promise.

RemoveSavingCallback

DataStoreStage:RemoveSavingCallback(callbackfunction) → ()

Removes a saving callback from the data store stage

GetTopLevelDataStoredSignal

DataStoreStage:GetTopLevelDataStoredSignal() → Signal

Gets an event that will fire off whenever something is stored at this level

GetFullPath

DataStoreStage:GetFullPath() → string

Retrieves the full path of this datastore stage for diagnostic purposes.

PromiseKeyList

DataStoreStage:PromiseKeyList() → Promise<{string}>

Promises a list of keys in the data store stage

PromiseKeySet

DataStoreStage:PromiseKeySet() → Promise<{[string]true}>

Promises a set of keys in the data store stage

MergeDiffSnapshot

DataStoreStage:MergeDiffSnapshot(diffSnapshotany) → ()

This will always prioritize our own view of the world over incoming data.

tip

This is a helper method that helps load diff data into the data store.

MarkDataAsSaved

DataStoreStage:MarkDataAsSaved(parentWriterDataStoreWriter) → ()

Updates the base data to the saved / written data.

This will always prioritize our own view of the world over incoming data.

PromiseViewUpToDate

DataStoreStage:PromiseViewUpToDate() → Promise

Helper method that when invokes ensures the data view.

tip

This is a helper method. You probably want DataStore.LoadAll instead.

Overwrite

DataStoreStage:Overwrite(dataany) → ()

Ovewrites the full stage with the data specified.

tip

Use this method carefully as it can lead to data loss in ways that a specific :Store() call on the right stage would do better.

OverwriteMerge

DataStoreStage:OverwriteMerge(dataany) → ()

Ovewrites the full stage with the data specified. However, it will merge the data to help prevent data-loss.

tip

Use this method carefully as it can lead to data loss in ways that a specific :Store() call on the right stage would do better.

StoreOnValueChange

DataStoreStage:StoreOnValueChange(
namestring | number,
valueObjInstance--

ValueBase object to store on

) → MaidTask--

Cleanup to remove this writer and free the key.

Whenever the ValueObject changes, stores the resulting value in that entry.

HasWritableData

DataStoreStage:HasWritableData() → boolean

If these is data not yet written then this will return true

GetNewWriter

DataStoreStage:GetNewWriter() → DataStoreWriter

Constructs a writer which provides a snapshot of the current data state to write.

tip

This is automatically invoked during saving and is public so DataStore can invoke it.

PromiseInvokeSavingCallbacks

DataStoreStage:PromiseInvokeSavingCallbacks() → Promise

Invokes all saving callbacks

tip

This is automatically invoked before saving and is public so DataStore can invoke it.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Constructs a new DataStoreStage to load from. Prefer to use DataStore because this doesn't\nhave any way to retrieve this.\n\nSee [DataStore], [GameDataStoreService], and [PlayerDataStoreService].\n\n```lua\n-- Data store inherits from DataStoreStage\nlocal dataStore = serviceBag:GetService(PlayerDataStoreService):PromiseDataStore(player):Yield()\n```",
            "params": [
                {
                    "name": "loadName",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "loadParent",
                    "desc": "",
                    "lua_type": "DataStoreStage?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "DataStoreStage"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 53,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "Store",
            "desc": "Stores the value, firing off events and queuing the item for save.\n\n```lua\ndataStore:Store(\"money\", 25)\n```",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string | number"
                },
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 92,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "Load",
            "desc": "Loads the data at the `key` and returns a promise with that value\n\n```lua\ndataStore:Load():Then(function(data)\n\tprint(data)\nend)\n```",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string | number"
                },
                {
                    "name": "defaultValue",
                    "desc": "",
                    "lua_type": "T?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<T>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 118,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "LoadAll",
            "desc": "Promises the full content for the datastore\n\n```lua\ndataStore:LoadAll():Then(function(data)\n\tprint(data)\nend)\n```",
            "params": [
                {
                    "name": "defaultValue",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<any>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 147,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "GetSubStore",
            "desc": "Gets a sub-datastore that will write at the given key. This will have the same\nhelper methods as any other data store object.\n\n```lua\nlocal dataStore = DataStore.new()\n\nlocal saveslot = dataStore:GetSubStore(\"saveslot0\")\nsaveslot:Store(\"Money\", 0)\n```",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string | number"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "DataStoreStage"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 171,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "Delete",
            "desc": "Explicitely deletes data at the key",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string | number"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 226,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "Wipe",
            "desc": "Queues up a wipe of all values. This will completely set the data to nil.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 235,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "Observe",
            "desc": "Observes the current value for the stage itself\n\nIf no key is passed than it will observe the whole view snapshot",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string | number | nil"
                },
                {
                    "name": "defaultValue",
                    "desc": "",
                    "lua_type": "T?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Observable<T>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 248,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "AddSavingCallback",
            "desc": "Adds a callback to be called before save. This may return a promise.",
            "params": [
                {
                    "name": "callback",
                    "desc": "May return a promise",
                    "lua_type": "function"
                }
            ],
            "returns": [
                {
                    "desc": "Call to remove",
                    "lua_type": "function"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 307,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "RemoveSavingCallback",
            "desc": "Removes a saving callback from the data store stage",
            "params": [
                {
                    "name": "callback",
                    "desc": "",
                    "lua_type": "function"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 323,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "GetTopLevelDataStoredSignal",
            "desc": "Gets an event that will fire off whenever something is stored at this level",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Signal"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 337,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "GetFullPath",
            "desc": "Retrieves the full path of this datastore stage for diagnostic purposes.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 346,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "PromiseKeyList",
            "desc": "Promises a list of keys in the data store stage",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<{ string }>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 363,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "PromiseKeySet",
            "desc": "Promises a set of keys in the data store stage",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<{ [string]: true }>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 379,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "MergeDiffSnapshot",
            "desc": "This will always prioritize our own view of the world over\nincoming data.\n\n:::tip\nThis is a helper method that helps load diff data into the data store.\n:::",
            "params": [
                {
                    "name": "diffSnapshot",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 399,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "MarkDataAsSaved",
            "desc": "Updates the base data to the saved / written data.\n\nThis will always prioritize our own view of the world over\nincoming data.",
            "params": [
                {
                    "name": "parentWriter",
                    "desc": "",
                    "lua_type": "DataStoreWriter"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 416,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "PromiseViewUpToDate",
            "desc": "Helper method that when invokes ensures the data view.\n\n:::tip\nThis is a helper method. You probably want [DataStore.LoadAll] instead.\n:::",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 485,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "Overwrite",
            "desc": "Ovewrites the full stage with the data specified.\n\n:::tip\nUse this method carefully as it can lead to data loss in ways that a specific :Store() call\non the right stage would do better.\n:::",
            "params": [
                {
                    "name": "data",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 506,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "OverwriteMerge",
            "desc": "Ovewrites the full stage with the data specified. However, it will merge the data\nto help prevent data-loss.\n\n:::tip\nUse this method carefully as it can lead to data loss in ways that a specific :Store() call\non the right stage would do better.\n:::",
            "params": [
                {
                    "name": "data",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 559,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "StoreOnValueChange",
            "desc": "Whenever the ValueObject changes, stores the resulting value in that entry.",
            "params": [
                {
                    "name": "name",
                    "desc": "",
                    "lua_type": "string | number"
                },
                {
                    "name": "valueObj",
                    "desc": "ValueBase object to store on",
                    "lua_type": "Instance"
                }
            ],
            "returns": [
                {
                    "desc": "Cleanup to remove this writer and free the key.",
                    "lua_type": "MaidTask"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 581,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "HasWritableData",
            "desc": "If these is data not yet written then this will return true",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 606,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "GetNewWriter",
            "desc": "Constructs a writer which provides a snapshot of the current data state to write.\n\n:::tip\nThis is automatically invoked during saving and is public so [DataStore] can invoke it.\n:::",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "DataStoreWriter"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 634,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        },
        {
            "name": "PromiseInvokeSavingCallbacks",
            "desc": "Invokes all saving callbacks\n\n:::tip\nThis is automatically invoked before saving and is public so [DataStore] can invoke it.\n:::",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 670,
                "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "DataStoreStage",
    "desc": "Provides a data storage facility with an ability to get sub-stores. So you can write\ndirectly to this store, overwriting all children, or you can have more partial control\nat children level. This minimizes accidently overwriting.\nThe big cost here is that we may leave keys that can't be removed.\n\nLayers in priority order:\n\n1. Save data\n2. Substores\n3. Base layer",
    "realm": [
        "Server"
    ],
    "source": {
        "line": 16,
        "path": "src/datastore/src/Server/Modules/DataStoreStage.lua"
    }
}