PlayerStorageAdapter
Class representing a player storage adapter.
Signature
export declare class PlayerStorageAdapterJSDoc
Examples
class MyPlayerStorageAdapter extends PlayerStorageAdapter {};
const storage = new MyPlayerStorageAdapter();
await storage.set("key", "value");
const value = await storage.get("key");
console.log(value); // "value"Properties
3Methods
16Properties
namespace: string;The namespace of the storage.
Examples
console.log(storage.namespace); // "hoshimiplayer"readonly guildId: string;the guild id of the player storage adapter. This is used to identify the player storage adapter.
Examples
console.log(storage.guildId); // "123456789012345678"prefixGetterget prefix(): string;Get the prefix for the storage keys. This is used to prevent key collisions between different player storage adapters.
Examples
console.log(storage.prefix); // "hoshimiplayer:123456789012345678"
const key = storage.buildKey("key");
console.log(key); // "hoshimiplayer:123456789012345678:key"Methods
constructorConstructorconstructor(guildId: string);strip(key: string): string;Strip the prefix from the key. This is used to get the original key from the stored key.
Parameters
key- The key to strip the prefix from.
Returns
The original key without the prefix.
Examples
const key = storage.buildKey("key");
console.log(key); // "hoshimiplayer:123456789012345678:key"
const originalKey = storage.strip(key);
console.log(originalKey); // "key"isInternal(key: string): boolean;Check if the key is an internal key. Internal keys are used to store internal data for the player storage adapter and should not be exposed to the user.
Parameters
key- The key to check.
Returns
Return true if the key is an internal key.
Examples
const internalKey = "internal_key";
console.log(storage.isInternal(internalKey)); // true
const userKey = "user_key";
console.log(storage.isInternal(userKey)); // falseabstract get<K extends StorageKeys, V extends StorageValues<K>>(key: K): Awaitable<V | undefined>;Get the value using the key.
Parameters
key- The key to get the value from.
Returns
The value of the key.
Examples
const value = await storage.get("key");
console.log(value); // "value"abstract set<K extends StorageKeys, V extends StorageValues<K>>(key: K, value: V): Awaitable<void>;Set the value using the key.
Parameters
key- The key to set the value to.
value- The value to set.
Returns
Did you know this can be async?
Examples
await storage.set("key", "value");abstract delete<K extends StorageKeys>(key: K): Awaitable<boolean>;Delete the value using the key.
Parameters
key- The key to delete the value from.
Returns
Returns true if the key was deleted.
Examples
const success = await storage.delete("key");
console.log(success); // trueabstract clear(): Awaitable<void>;Clear the storage.
Returns
Scary, right?
Examples
await storage.clear();abstract has<K extends StorageKeys>(key: K): Awaitable<boolean>;Check if the storage has the key.
Parameters
key- The key to check.
Returns
Return true if the key exists.
Examples
const exists = await storage.has("key");
console.log(exists); // trueabstract keys<K extends StorageKeys[]>(): Awaitable<K[]>;Get all keys in the storage.
Returns
The keys in the storage.
Examples
const keys = await storage.keys();
console.log(keys); // ["key1", "key2"]abstract values<K extends StorageKeys[], V extends StorageValues<K[number]>>(): Awaitable<V[]>;Get all values in the storage.
Returns
The values in the storage.
Examples
const values = await storage.values();
console.log(values); // ["value1", "value2"]abstract entries<K extends StorageKeys[], V extends StorageValues<K[number]>>(): Awaitable<[ K, V][]>;Get all entries in the storage.
Returns
The entries in the storage.
Examples
const entries = await storage.entries();
console.log(entries); // [["key1", "value1"], ["key2", "value2"]]abstract all<K extends StorageKeys[], V extends StorageValues<K[number]>>(): Awaitable<Record<K[number], V>>;Get all key-value pairs in the storage.
Returns
An object containing all key-value pairs in the storage, excluding internal keys.
Examples
const all = await storage.all();
console.log(all); // { key1: "value1", key2: "value2" }abstract size(): Awaitable<number>;Get the size of the storage.
Returns
The size of the storage.
Examples
const size = await storage.size();
console.log(size); // 2abstract destroy(): Awaitable<void>;Destroy the storage. This is called when the player is destroyed.
Returns
Did you know this can be async?
Examples
await storage.destroy();abstract setIfAbsent<K extends StorageKeys, V extends StorageValues<K>>(key: K, value: V): Awaitable<boolean>;Set the value if the key does not exist in the storage.
Parameters
key- The key to set the value to if it does not exist.
value- The value to set if the key does not exist.
Returns
Returns true if the value was set, false if the key already exists.
Examples
const success = await storage.setIfAbsent("key", "value");
console.log(success); // true
const success2 = await storage.setIfAbsent("key", "newValue");
console.log(success2); // false
const value = await storage.get("key");
console.log(value); // "value"buildKey(...parts: RestOrArray<string>): string;Build a key from the given parts.
Parameters
parts- The parts to build the key from.
Returns
The built key.
Examples
const key = storage.buildKey("part1", "part2", "part3");