Skip to main content

Class: StoreRegistry

A strict-typed store registry. This is available in container.

Since

2.1.0

Example

// Adding new stores

// Register the store:
container.stores.register(new RouteStore());

// Augment Sapphire to add the new store, in case of a JavaScript
// project, this can be moved to an `Augments.d.ts` (or any other name)
// file somewhere:
declare module '@sapphire/pieces' {
export interface StoreRegistryEntries {
routes: RouteStore;
}
}

Extends

Constructors

new StoreRegistry()

new StoreRegistry(entries?: null | readonly readonly [never, never][]): StoreRegistry

Parameters

ParameterType
entries?null | readonly readonly [never, never][]

Returns

StoreRegistry

Inherited from

Collection<StoreRegistryKey, StoreRegistryValue>.constructor

Source

node_modules/typescript/lib/lib.es2015.collection.d.ts:50

new StoreRegistry()

new StoreRegistry(iterable?: null | Iterable<readonly [never, never]>): StoreRegistry

Parameters

ParameterType
iterable?null | Iterable<readonly [never, never]>

Returns

StoreRegistry

Inherited from

Collection<StoreRegistryKey, StoreRegistryValue>.constructor

Source

node_modules/typescript/lib/lib.es2015.collection.d.ts:49

Properties

#pendingManuallyRegisteredPieces

private readonly #pendingManuallyRegisteredPieces: Collection<never, StoreManuallyRegisteredPiece<never>[]>

The queue of pieces to load.

Source

projects/pieces/src/lib/structures/StoreRegistry.ts:35

Methods

deregister()

deregister<T>(store: Store<T, never>): this

Deregisters a store.

Type parameters

Type parameter
T extends Piece <PieceOptions, never>

Parameters

ParameterTypeDescription
storeStore<T, never>The store to deregister.

Returns

this

Since

2.1.0

Source

projects/pieces/src/lib/structures/StoreRegistry.ts:126


get()

get(key)

get<K>(key: K): StoreRegistryEntries[K]

Type parameters
Type parameter
K extends never
Parameters
ParameterType
keyK
Returns

StoreRegistryEntries[K]

Inherited from

Collection.get

Source

projects/pieces/src/lib/structures/StoreRegistry.ts:184

get(key)

get(key: string): undefined

Parameters
ParameterType
keystring
Returns

undefined

Inherited from

Collection.get

Source

projects/pieces/src/lib/structures/StoreRegistry.ts:185


has()

has(key)

has(key: never): true

Parameters
ParameterType
keynever
Returns

true

Inherited from

Collection.has

Source

projects/pieces/src/lib/structures/StoreRegistry.ts:186

has(key)

has(key: string): false

Parameters
ParameterType
keystring
Returns

false

Inherited from

Collection.has

Source

projects/pieces/src/lib/structures/StoreRegistry.ts:187


load()

load(): Promise<void>

Loads all the registered stores.

Returns

Promise<void>

Since

2.1.0

Source

projects/pieces/src/lib/structures/StoreRegistry.ts:41


loadPiece()

loadPiece<StoreName>(entry: StoreManagerManuallyRegisteredPiece<StoreName>): Promise<void>

If the store was registered, this method will call the store's () loadPiece() method.

If it was called, the entry will be loaded immediately without queueing.

Type parameters

Type parameter
StoreName extends never

Parameters

ParameterTypeDescription
entryStoreManagerManuallyRegisteredPiece<StoreName>The entry to load.

Returns

Promise<void>

Remarks

  • Pieces loaded this way will have their root and path set to VirtualPath, and as such, cannot be reloaded.
  • This method is useful in environments where file system access is limited or unavailable, such as when using Serverless Computing.
  • This method will not throw an error if a store with the given name does not exist, it will simply be queued until it's registered.
  • This method will always throw a TypeError if entry.piece is not a class.
  • If the store is registered, this method will always throw a LoaderError if the piece does not extend the registered store's piece constructor.
  • This operation is atomic, if any of the above errors are thrown, the piece will not be loaded.

Seealso

()

Since

3.8.0

Example

import { container } from '@sapphire/pieces';

class PingCommand extends Command {
// ...
}

container.stores.loadPiece({
store: 'commands',
name: 'ping',
piece: PingCommand
});

Source

projects/pieces/src/lib/structures/StoreRegistry.ts:168


register()

register<T>(store: Store<T, never>): this

Registers a store.

Type parameters

Type parameter
T extends Piece <PieceOptions, never>

Parameters

ParameterTypeDescription
storeStore<T, never>The store to register.

Returns

this

Remarks

  • This method will allow StoreRegistry to manage the store, meaning:
  • This will also add all the manually registered pieces by () in the store.

It is generally recommended to register a store as early as possible, before any of the aforementioned methods are called, otherwise you will have to manually call the aforementioned methods for the store to work properly.

If there were manually registered pieces for this store with (), this method will add them to the store and delete the queue. Note, however, that this method will not call the store's () loadPiece() method, and as such, the pieces will not be loaded until () is called.

Since

2.1.0

Source

projects/pieces/src/lib/structures/StoreRegistry.ts:105


registerPath()

registerPath(rootDirectory: Path): void

Registers all user directories from the process working directory, the default value is obtained by assuming CommonJS (high accuracy) but with fallback for ECMAScript Modules (reads package.json's main entry, fallbacks to process.cwd()).

By default, if you have this folder structure:

/home/me/my-bot
├─ src
│ ├─ commands
│ ├─ events
│ └─ main.js
└─ package.json

And you run node src/main.js, the directories /home/me/my-bot/src/commands and /home/me/my-bot/src/events will be registered for the commands and events stores respectively, since both directories are located in the same directory as your main file.

Note: this also registers directories for all other stores, even if they don't have a folder, this allows you to create new pieces and hot-load them later anytime.

Parameters

ParameterTypeDescription
rootDirectoryPathThe root directory to register pieces at.

Returns

void

Since

2.1.0

Source

projects/pieces/src/lib/structures/StoreRegistry.ts:74