Skip to main content

Class: AliasStore<T, StoreName>

Defined in: projects/pieces/src/lib/structures/AliasStore.ts:9

The store class which contains AliasPieces.

Extends

Type Parameters

Type ParameterDefault type
T extends AliasPiece-
StoreName extends StoreRegistryKeyStoreRegistryKey

Constructors

new AliasStore()

new AliasStore<T, StoreName>(constructor: AbstractConstructor<T>, options: StoreOptions<T, StoreName>): AliasStore<T, StoreName>

Defined in: projects/pieces/src/lib/structures/Store.ts:80

Parameters

ParameterTypeDescription
constructorAbstractConstructor<T>The piece constructor this store loads.
optionsStoreOptions<T, StoreName>The options for the store.

Returns

AliasStore<T, StoreName>

Inherited from

Store.constructor

Properties

aliases

readonly aliases: Collection<string, T>

Defined in: projects/pieces/src/lib/structures/AliasStore.ts:13

The aliases referencing to pieces.


Constructor

readonly Constructor: AbstractConstructor<T>

Defined in: projects/pieces/src/lib/structures/Store.ts:56

Inherited from

Store.Constructor


name

readonly name: StoreName

Defined in: projects/pieces/src/lib/structures/Store.ts:57

Inherited from

Store.name


paths

readonly paths: Set<string>

Defined in: projects/pieces/src/lib/structures/Store.ts:58

Inherited from

Store.paths


strategy

readonly strategy: ILoaderStrategy<T>

Defined in: projects/pieces/src/lib/structures/Store.ts:59

Inherited from

Store.strategy


defaultStrategy

static defaultStrategy: ILoaderStrategy<any>

Defined in: projects/pieces/src/lib/structures/Store.ts:377

The default strategy, defaults to LoaderStrategy, which is constructed on demand when a store is constructed, when none was set beforehand.

Inherited from

Store.defaultStrategy


logger

static logger: null | StoreLogger = null

Defined in: projects/pieces/src/lib/structures/Store.ts:382

The default logger, defaults to null.

Inherited from

Store.logger

Accessors

container

Get Signature

get container(): Container

Defined in: projects/pieces/src/lib/structures/Store.ts:97

A reference to the Container object for ease of use.

See

container

Returns

Container

Inherited from

Store.container

Methods

construct()

construct(Ctor: ILoaderResultEntry<T>, data: HydratedModuleData): T

Defined in: projects/pieces/src/lib/structures/Store.ts:335

Constructs a Piece instance.

Parameters

ParameterTypeDescription
CtorILoaderResultEntry<T>The Piece's constructor used to build the instance.
dataHydratedModuleDataThe module's information

Returns

T

An instance of the constructed piece.

Inherited from

Store.construct


get()

get(key: string): undefined | T

Defined in: projects/pieces/src/lib/structures/AliasStore.ts:19

Looks up the name by the store, falling back to an alias lookup.

Parameters

ParameterTypeDescription
keystringThe key to look for.

Returns

undefined | T

Overrides

Store.get


has()

has(key: string): boolean

Defined in: projects/pieces/src/lib/structures/AliasStore.ts:27

Checks whether a key is in the store, or is an alias

Parameters

ParameterTypeDescription
keystringThe key to check

Returns

boolean

Overrides

Store.has


insert()

insert(piece: T): Promise<T>

Defined in: projects/pieces/src/lib/structures/AliasStore.ts:54

Inserts a piece into the store, and adds all the aliases.

Parameters

ParameterTypeDescription
pieceTThe piece to be inserted into the store.

Returns

Promise<T>

The inserted piece.

Overrides

Store.insert


load()

load(root: string, path: string): Promise<T[]>

Defined in: projects/pieces/src/lib/structures/Store.ts:182

Loads one or more pieces from a path.

Parameters

ParameterTypeDescription
rootstringThe root directory the file is from.
pathstringThe path of the file to load, relative to the root.

Returns

Promise<T[]>

All the loaded pieces.

Inherited from

Store.load


loadAll()

loadAll(): Promise<void>

Defined in: projects/pieces/src/lib/structures/Store.ts:241

Loads all pieces from all directories specified by paths.

Returns

Promise<void>

Inherited from

Store.loadAll


loadPiece()

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

Defined in: projects/pieces/src/lib/structures/Store.ts:154

Adds a piece into the store's list of manually registered pieces. If () was called, the piece will be loaded immediately, otherwise it will be queued until () is called.

All manually registered pieces will be kept even after they are loaded to ensure they can be loaded again if () is called again.

Parameters

ParameterTypeDescription
entryStoreManuallyRegisteredPiece<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 always throw a TypeError if entry.piece is not a class.
  • This method will always throw a LoaderError if the piece does not extend the 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.get('commands').loadPiece({
name: 'ping',
piece: PingCommand
});

Inherited from

Store.loadPiece


registerPath()

registerPath(path: Path): this

Defined in: projects/pieces/src/lib/structures/Store.ts:111

Registers a directory into the store.

Parameters

ParameterTypeDescription
pathPathThe path to be added.

Returns

this

Example

store
.registerPath(resolve('commands'))
.registerPath(resolve('third-party', 'commands'));

Inherited from

Store.registerPath


resolve()

resolve(name: string | T): T

Defined in: projects/pieces/src/lib/structures/Store.ts:282

Resolves a piece by its name or its instance.

Parameters

ParameterTypeDescription
namestring | TThe name of the piece or the instance itself.

Returns

T

The resolved piece.

Inherited from

Store.resolve


unload()

unload(name: string | T): Promise<T>

Defined in: projects/pieces/src/lib/structures/AliasStore.ts:36

Unloads a piece given its instance or its name, and removes all the aliases.

Parameters

ParameterTypeDescription
namestring | TThe name of the file to load.

Returns

Promise<T>

Returns the piece that was unloaded.

Overrides

Store.unload


unloadAll()

unloadAll(): Promise<T[]>

Defined in: projects/pieces/src/lib/structures/Store.ts:225

Unloads all pieces from the store.

Returns

Promise<T[]>

Inherited from

Store.unloadAll