Skip to content

Documentation / @cashconnect-js/templates-dev / storage/base-store

StorageOpts ​

ts
type StorageOpts = object;

Defined in: packages/templates-dev/src/storage/base-store.ts:2

Properties ​

syncInMemory? ​

ts
optional syncInMemory: boolean;

Defined in: packages/templates-dev/src/storage/base-store.ts:3

encryption? ​

ts
optional encryption: Encrypter;

Defined in: packages/templates-dev/src/storage/base-store.ts:4


StoreRecords ​

ts
type StoreRecords = Record<string, unknown>;

Defined in: packages/templates-dev/src/storage/base-store.ts:8


StoreEvents ​

ts
type StoreEvents = object;

Defined in: packages/templates-dev/src/storage/base-store.ts:10

Indexable ​

ts
[key: `set:${string}`]: any

Properties ​

set ​

ts
set: object;

Defined in: packages/templates-dev/src/storage/base-store.ts:12

NameTypeDefined in
keystringpackages/templates-dev/src/storage/base-store.ts:12
valueanypackages/templates-dev/src/storage/base-store.ts:12

delete ​

ts
delete: object;

Defined in: packages/templates-dev/src/storage/base-store.ts:13

NameTypeDefined in
keystringpackages/templates-dev/src/storage/base-store.ts:13

clear ​

ts
clear: undefined;

Defined in: packages/templates-dev/src/storage/base-store.ts:14


StoreValueProxy ​

ts
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() ​

ts
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() ​

ts
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() ​

ts
set(newValue): Promise<void>;

Defined in: packages/templates-dev/src/storage/base-store.ts:53

Sets a new value in the store

Parameters ​
ParameterTypeDescription
newValueTThe value to store
Returns ​

Promise<void>


abstract BaseStorage ​

Defined in: packages/templates-dev/src/storage/base-store.ts:17

Constructors ​

Constructor ​

ts
new BaseStorage(): BaseStorage;
Returns ​

BaseStorage

Methods ​

listStores() ​

ts
abstract listStores(): Promise<string[]>;

Defined in: packages/templates-dev/src/storage/base-store.ts:18

Returns ​

Promise<string[]>

createStore() ​

ts
abstract createStore(storeName, opts): Promise<BaseStore>;

Defined in: packages/templates-dev/src/storage/base-store.ts:19

Parameters ​
ParameterType
storeNamestring
optsStorageOpts
Returns ​

Promise<BaseStore>

getStore() ​

ts
abstract getStore(storeName, opts): Promise<BaseStore | null>;

Defined in: packages/templates-dev/src/storage/base-store.ts:23

Parameters ​
ParameterType
storeNamestring
optsStorageOpts
Returns ​

Promise<BaseStore | null>

createOrGetStore() ​

ts
abstract createOrGetStore(storeName, opts): Promise<BaseStore>;

Defined in: packages/templates-dev/src/storage/base-store.ts:27

Parameters ​
ParameterType
storeNamestring
optsStorageOpts
Returns ​

Promise<BaseStore>

deleteStore() ​

ts
abstract deleteStore(storeName): Promise<void>;

Defined in: packages/templates-dev/src/storage/base-store.ts:31

Parameters ​
ParameterType
storeNamestring
Returns ​

Promise<void>

deleteDatabase() ​

ts
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 ​

typescript
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 ​

Extended by ​

Constructors ​

Constructor ​

ts
new BaseStore(): BaseStore;
Returns ​

BaseStore

Inherited from ​
ts
EventEmitter<StoreEvents>.constructor

Methods ​

keys() ​

ts
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() ​

ts
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 ParameterDescription
TThe type of the value to retrieve
Parameters ​
ParameterTypeDescription
keystringThe key to lookup
Returns ​

Promise<T | undefined>

set() ​

ts
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 ParameterDescription
TThe type of the value to store
Parameters ​
ParameterTypeDescription
keystringThe key under which to store the value
valueTThe value to store
Returns ​

Promise<void>

delete() ​

ts
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 ​
ParameterTypeDescription
keystringThe key to remove
Returns ​

Promise<void>

all() ​

ts
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 ParameterDescription
TThe type of the values in the store
Returns ​

Promise<{ [key: string]: T; }>

clear() ​

ts
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() ​

ts
has(key): Promise<boolean>;

Defined in: packages/templates-dev/src/storage/base-store.ts:142

Checks if a key exists in the store.

Parameters ​
ParameterTypeDescription
keystringThe key to check
Returns ​

Promise<boolean>

batchGet() ​

ts
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 ParameterDescription
TThe type of the values to retrieve
Parameters ​
ParameterTypeDescription
keysstring[]Array of keys to retrieve
Returns ​

Promise<Map<string, T>>

batchSet() ​

ts
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 ParameterDescription
TThe type of the values to store
Parameters ​
ParameterTypeDescription
itemsMap<string, T>Map of key-value pairs to store
Returns ​

Promise<void>

close() ​

ts
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() ​

ts
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 ParameterDescription
TThe type of the value to retrieve
Parameters ​
ParameterTypeDescription
keystringThe key to lookup
Returns ​

Promise<T>

getOrSet() ​

ts
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 ParameterDescription
TThe type of the value to retrieve or set
Parameters ​
ParameterTypeDescription
keystringThe key to lookup
setter() => T | Promise<T>Function to generate the value if it doesn't exist
Returns ​

Promise<T>

proxy() ​

ts
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 ParameterDescription
TThe type of the value to wrap
Parameters ​
ParameterTypeDescription
keystringThe key under which the value is stored
Returns ​

StoreValueProxy<T>

An object with a getter and setter for the stored value

Example ​
typescript
const numValue = store.proxy<number>('myNumber');
const value = await numValue.get(); // gets the stored value
await numValue.set(42); // sets the stored value

on() ​

ts
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 ParameterDescription
K extends keyof StoreEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to listen for
listenerListener<StoreEvents[K]>The function to call when the event is emitted
debounceMilliseconds?numberOptional debounce time in milliseconds
Returns ​

OffCallback

A callback function that can be used to unregister this listener

Example ​
typescript
const off = emitter.on('data-change', (data) => {
  console.log('Data updated:', data);
}, 300); // Debounce for 300ms

// Later, unregister the listener
off();
Inherited from ​
ts
EventEmitter.on

once() ​

ts
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 ParameterDescription
K extends keyof StoreEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to listen for
listenerListener<StoreEvents[K]>The function to call when the event is emitted
debounceMilliseconds?numberOptional debounce time in milliseconds
Returns ​

OffCallback

A callback function that can be used to unregister this listener before it fires

Example ​
typescript
emitter.once('user-login', ({ userId }) => {
  console.log(`Welcome ${userId}! This will only show once.`);
});
Inherited from ​
ts
EventEmitter.once

off() ​

ts
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 ParameterDescription
K extends keyof StoreEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to remove the listener from
listenerListener<StoreEvents[K]>The listener function to remove
Returns ​

void

Example ​
typescript
const handler = (data) => console.log(data);
emitter.on('test', handler);
emitter.off('test', handler); // Removes the listener
Inherited from ​
ts
EventEmitter.off

emit() ​

ts
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 ParameterDescription
K extends keyof StoreEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to emit
payloadStoreEvents[K]The data to pass to all listeners
Returns ​

boolean

true if there were listeners for this event, false otherwise

Example ​
typescript
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 ​
ts
EventEmitter.emit

removeAllListeners() ​

ts
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 ​
typescript
emitter.removeAllListeners();
// All previously registered listeners are now gone
Inherited from ​
ts
EventEmitter.removeAllListeners

waitFor() ​

ts
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 ParameterDescription
K extends keyof StoreEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to wait for
predicate(payload) => booleanA function that determines if the event payload matches what we're waiting for
timeoutMs?numberOptional 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 ​
typescript
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 ​
ts
EventEmitter.waitFor