Documentation / @cashconnect-js/templates-dev / storage/base-store
StorageOpts
type StorageOpts = object;Defined in: packages/templates-dev/src/storage/base-store.ts:2
Properties
syncInMemory?
optional syncInMemory: boolean;Defined in: packages/templates-dev/src/storage/base-store.ts:3
encryption?
optional encryption: Encrypter;Defined in: packages/templates-dev/src/storage/base-store.ts:4
StoreRecords
type StoreRecords = Record<string, unknown>;Defined in: packages/templates-dev/src/storage/base-store.ts:8
StoreEvents
type StoreEvents = object;Defined in: packages/templates-dev/src/storage/base-store.ts:10
Indexable
[key: `set:${string}`]: anyProperties
set
set: object;Defined in: packages/templates-dev/src/storage/base-store.ts:12
| Name | Type | Defined in |
|---|---|---|
key | string | packages/templates-dev/src/storage/base-store.ts:12 |
value | any | packages/templates-dev/src/storage/base-store.ts:12 |
delete
delete: object;Defined in: packages/templates-dev/src/storage/base-store.ts:13
| Name | Type | Defined in |
|---|---|---|
key | string | packages/templates-dev/src/storage/base-store.ts:13 |
clear
clear: undefined;Defined in: packages/templates-dev/src/storage/base-store.ts:14
StoreValueProxy
type StoreValueProxy<T> = object;Defined in: packages/templates-dev/src/storage/base-store.ts:38
Represents a wrapper object for accessing and manipulating a stored value.
Type Parameters
| Type Parameter |
|---|
T |
Methods
get()
get(): Promise<T | undefined>;Defined in: packages/templates-dev/src/storage/base-store.ts:42
Gets the current value from the store
Returns
Promise<T | undefined>
getOrFail()
getOrFail(): Promise<T>;Defined in: packages/templates-dev/src/storage/base-store.ts:47
Gets the current value from the store or fails if undefined.
Returns
Promise<T>
set()
set(newValue): Promise<void>;Defined in: packages/templates-dev/src/storage/base-store.ts:53
Sets a new value in the store
Parameters
| Parameter | Type | Description |
|---|---|---|
newValue | T | The value to store |
Returns
Promise<void>
abstract BaseStorage
Defined in: packages/templates-dev/src/storage/base-store.ts:17
Constructors
Constructor
new BaseStorage(): BaseStorage;Returns
Methods
listStores()
abstract listStores(): Promise<string[]>;Defined in: packages/templates-dev/src/storage/base-store.ts:18
Returns
Promise<string[]>
createStore()
abstract createStore(storeName, opts): Promise<BaseStore>;Defined in: packages/templates-dev/src/storage/base-store.ts:19
Parameters
| Parameter | Type |
|---|---|
storeName | string |
opts | StorageOpts |
Returns
Promise<BaseStore>
getStore()
abstract getStore(storeName, opts): Promise<BaseStore | null>;Defined in: packages/templates-dev/src/storage/base-store.ts:23
Parameters
| Parameter | Type |
|---|---|
storeName | string |
opts | StorageOpts |
Returns
Promise<BaseStore | null>
createOrGetStore()
abstract createOrGetStore(storeName, opts): Promise<BaseStore>;Defined in: packages/templates-dev/src/storage/base-store.ts:27
Parameters
| Parameter | Type |
|---|---|
storeName | string |
opts | StorageOpts |
Returns
Promise<BaseStore>
deleteStore()
abstract deleteStore(storeName): Promise<void>;Defined in: packages/templates-dev/src/storage/base-store.ts:31
Parameters
| Parameter | Type |
|---|---|
storeName | string |
Returns
Promise<void>
deleteDatabase()
abstract deleteDatabase(): Promise<void>;Defined in: packages/templates-dev/src/storage/base-store.ts:32
Returns
Promise<void>
abstract BaseStore
Defined in: packages/templates-dev/src/storage/base-store.ts:81
Abstract base class for implementing key-value storage with event emission capabilities. Extends EventEmitter to provide event handling for store operations.
Example
class MyStore extends BaseStore {
async keys(): Promise<Array<string>> {
// Implementation
}
async get<T>(key: string): Promise<T | undefined> {
// Implementation
}
async set<T>(key: string, value: T): Promise<void> {
// Implementation
}
async delete(key: string): Promise<void> {
// Implementation
}
}Extends
EventEmitter<StoreEvents>
Extended by
Constructors
Constructor
new BaseStore(): BaseStore;Returns
Inherited from
EventEmitter<StoreEvents>.constructorMethods
keys()
abstract keys(): Promise<string[]>;Defined in: packages/templates-dev/src/storage/base-store.ts:85
Returns an array of all keys in the store.
Returns
Promise<string[]>
get()
abstract get<T>(key): Promise<T | undefined>;Defined in: packages/templates-dev/src/storage/base-store.ts:93
Retrieves a value from the store by its key.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the value to retrieve |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key to lookup |
Returns
Promise<T | undefined>
set()
abstract set<T>(key, value): Promise<void>;Defined in: packages/templates-dev/src/storage/base-store.ts:102
Stores a value in the store with the specified key.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the value to store |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key under which to store the value |
value | T | The value to store |
Returns
Promise<void>
delete()
abstract delete(key): Promise<void>;Defined in: packages/templates-dev/src/storage/base-store.ts:109
Removes a value from the store by its key.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key to remove |
Returns
Promise<void>
all()
all<T>(): Promise<{
[key: string]: T;
}>;Defined in: packages/templates-dev/src/storage/base-store.ts:116
Retrieves all key-value pairs from the store.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the values in the store |
Returns
Promise<{ [key: string]: T; }>
clear()
clear(): Promise<void>;Defined in: packages/templates-dev/src/storage/base-store.ts:132
Removes all key-value pairs from the store.
Returns
Promise<void>
has()
has(key): Promise<boolean>;Defined in: packages/templates-dev/src/storage/base-store.ts:142
Checks if a key exists in the store.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key to check |
Returns
Promise<boolean>
batchGet()
batchGet<T>(keys): Promise<Map<string, T>>;Defined in: packages/templates-dev/src/storage/base-store.ts:153
Retrieves multiple values from the store by their keys. Filters out undefined values from the results.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the values to retrieve |
Parameters
| Parameter | Type | Description |
|---|---|---|
keys | string[] | Array of keys to retrieve |
Returns
Promise<Map<string, T>>
batchSet()
batchSet<T>(items): Promise<void>;Defined in: packages/templates-dev/src/storage/base-store.ts:173
Stores multiple key-value pairs in the store. Emits 'set' events for each key-value pair.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the values to store |
Parameters
| Parameter | Type | Description |
|---|---|---|
items | Map<string, T> | Map of key-value pairs to store |
Returns
Promise<void>
close()
close(): Promise<void>;Defined in: packages/templates-dev/src/storage/base-store.ts:188
Closes the store connection. Override this method in implementing classes if needed.
Returns
Promise<void>
getOrFail()
getOrFail<T>(key): Promise<T>;Defined in: packages/templates-dev/src/storage/base-store.ts:196
Retrieves a value from the store by its key, throwing an error if the value doesn't exist.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the value to retrieve |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key to lookup |
Returns
Promise<T>
getOrSet()
getOrSet<T>(key, setter): Promise<T>;Defined in: packages/templates-dev/src/storage/base-store.ts:216
Retrieves a value from the store by its key, or sets it using the provided setter function if it doesn't exist.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the value to retrieve or set |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key to lookup |
setter | () => T | Promise<T> | Function to generate the value if it doesn't exist |
Returns
Promise<T>
proxy()
proxy<T>(key): StoreValueProxy<T>;Defined in: packages/templates-dev/src/storage/base-store.ts:245
Creates a wrapper object that provides convenient access to a stored value. The wrapper allows getting and setting the value using async property access.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the value to wrap |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key under which the value is stored |
Returns
An object with a getter and setter for the stored value
Example
const numValue = store.proxy<number>('myNumber');
const value = await numValue.get(); // gets the stored value
await numValue.set(42); // sets the stored valueon()
on<K>(
type,
listener,
debounceMilliseconds?): OffCallback;Defined in: packages/core/dist/primitives.d.ts:593
Registers a listener for the specified event type.
Type Parameters
| Type Parameter | Description |
|---|---|
K extends keyof StoreEvents | The event type key |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | K | The event type to listen for |
listener | Listener<StoreEvents[K]> | The function to call when the event is emitted |
debounceMilliseconds? | number | Optional debounce time in milliseconds |
Returns
OffCallback
A callback function that can be used to unregister this listener
Example
const off = emitter.on('data-change', (data) => {
console.log('Data updated:', data);
}, 300); // Debounce for 300ms
// Later, unregister the listener
off();Inherited from
EventEmitter.ononce()
once<K>(
type,
listener,
debounceMilliseconds?): OffCallback;Defined in: packages/core/dist/primitives.d.ts:611
Registers a one-time listener for the specified event type. The listener will be automatically removed after being called once.
Type Parameters
| Type Parameter | Description |
|---|---|
K extends keyof StoreEvents | The event type key |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | K | The event type to listen for |
listener | Listener<StoreEvents[K]> | The function to call when the event is emitted |
debounceMilliseconds? | number | Optional debounce time in milliseconds |
Returns
OffCallback
A callback function that can be used to unregister this listener before it fires
Example
emitter.once('user-login', ({ userId }) => {
console.log(`Welcome ${userId}! This will only show once.`);
});Inherited from
EventEmitter.onceoff()
off<K>(type, listener): void;Defined in: packages/core/dist/primitives.d.ts:626
Removes a specific listener for the given event type.
Type Parameters
| Type Parameter | Description |
|---|---|
K extends keyof StoreEvents | The event type key |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | K | The event type to remove the listener from |
listener | Listener<StoreEvents[K]> | The listener function to remove |
Returns
void
Example
const handler = (data) => console.log(data);
emitter.on('test', handler);
emitter.off('test', handler); // Removes the listenerInherited from
EventEmitter.offemit()
emit<K>(type, payload): boolean;Defined in: packages/core/dist/primitives.d.ts:648
Emits an event of the specified type with the given payload. All registered listeners for this event type will be called.
Type Parameters
| Type Parameter | Description |
|---|---|
K extends keyof StoreEvents | The event type key |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | K | The event type to emit |
payload | StoreEvents[K] | The data to pass to all listeners |
Returns
boolean
true if there were listeners for this event, false otherwise
Example
const hasListeners = emitter.emit('user-login', {
userId: '123',
timestamp: Date.now()
});
if (!hasListeners) {
console.log('No one was listening for user-login events');
}Inherited from
EventEmitter.emitremoveAllListeners()
removeAllListeners(): void;Defined in: packages/core/dist/primitives.d.ts:659
Removes all listeners for all events. This effectively resets the EventEmitter to its initial state.
Returns
void
Example
emitter.removeAllListeners();
// All previously registered listeners are now goneInherited from
EventEmitter.removeAllListenerswaitFor()
waitFor<K>(
type,
predicate,
timeoutMs?): Promise<StoreEvents[K]>;Defined in: packages/core/dist/primitives.d.ts:685
Returns a Promise that resolves when an event of the specified type is emitted and matches the given predicate.
Type Parameters
| Type Parameter | Description |
|---|---|
K extends keyof StoreEvents | The event type key |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | K | The event type to wait for |
predicate | (payload) => boolean | A function that determines if the event payload matches what we're waiting for |
timeoutMs? | number | Optional timeout in milliseconds. If specified, the promise will reject after this time |
Returns
Promise<StoreEvents[K]>
A Promise that resolves with the event payload when the condition is met
Throws
If the timeout is reached before a matching event occurs
Example
try {
const loginData = await emitter.waitFor(
'user-login',
({ userId }) => userId === 'admin',
5000 // Wait up to 5 seconds
);
console.log('Admin logged in:', loginData);
} catch (error) {
console.log('Timeout: Admin never logged in');
}Inherited from
EventEmitter.waitFor