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
Collection
<StoreRegistryKey
,StoreRegistryValue
>
Constructors
new StoreRegistry()
new StoreRegistry(
entries
?:null
| readonly readonly [never
,never
][]):StoreRegistry
Parameters
Parameter | Type |
---|---|
entries ? | null | readonly readonly [never , never ][] |
Returns
Inherited from
Collection<StoreRegistryKey, StoreRegistryValue>.constructor
Defined in
node_modules/typescript/lib/lib.es2015.collection.d.ts:50
new StoreRegistry()
new StoreRegistry(
iterable
?:null
|Iterable
<readonly [never
,never
]>):StoreRegistry
Parameters
Parameter | Type |
---|---|
iterable ? | null | Iterable <readonly [never , never ]> |
Returns
Inherited from
Collection<StoreRegistryKey, StoreRegistryValue>.constructor
Defined in
node_modules/typescript/lib/lib.es2015.collection.d.ts:49
Methods
deregister()
deregister<
T
>(store
:Store
<T
,never
>):this
Deregisters a store.
Type Parameters
Type Parameter |
---|
T extends Piece <PieceOptions , never > |
Parameters
Parameter | Type | Description |
---|---|---|
store | Store <T , never > | The store to deregister. |
Returns
this
Since
2.1.0
Defined in
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
Parameter | Type |
---|---|
key | K |
Returns
Inherited from
Collection.get
Defined in
projects/pieces/src/lib/structures/StoreRegistry.ts:184
get(key)
get(
key
:string
):undefined
Parameters
Parameter | Type |
---|---|
key | string |
Returns
undefined
Inherited from
Collection.get
Defined in
projects/pieces/src/lib/structures/StoreRegistry.ts:185
has()
has(key)
has(
key
:never
):true
Parameters
Parameter | Type |
---|---|
key | never |
Returns
true
Inherited from
Collection.has
Defined in
projects/pieces/src/lib/structures/StoreRegistry.ts:186
has(key)
has(
key
:string
):false
Parameters
Parameter | Type |
---|---|
key | string |
Returns
false
Inherited from
Collection.has
Defined in
projects/pieces/src/lib/structures/StoreRegistry.ts:187
load()
load():
Promise
<void
>
Loads all the registered stores.
Returns
Promise
<void
>
Since
2.1.0
Defined in
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
Parameter | Type | Description |
---|---|---|
entry | StoreManagerManuallyRegisteredPiece <StoreName > | The entry to load. |
Returns
Promise
<void
>
Remarks
- Pieces loaded this way will have their
root
andpath
set toVirtualPath
, 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 registeredstore'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
});
Defined in
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
Parameter | Type | Description |
---|---|---|
store | Store <T , never > | The store to register. |
Returns
this
Remarks
- This method will allow
StoreRegistry
to manage the store, meaning:()
will call the store's() registerPath()
method on call.()
will call the store's() load()
method on call.()
will call the store's() loadPiece()
method on call.
- 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
Defined in
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
Parameter | Type | Description |
---|---|---|
rootDirectory | Path | The root directory to register pieces at. |
Returns
void
Since
2.1.0