Any way to register a database client to the application command

Let's take this example:
import { Command } from "@sapphire/framework";

export default class extends Command {
public constructor(context: Command.LoaderContext, options: Command.Options) {
super(context, { ...options });
}

public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand((builder) =>
builder.setName("ping").setDescription("Ping bot to see if it is alive")
);
}
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
await interaction.reply("Yup");
}
}
import { Command } from "@sapphire/framework";

export default class extends Command {
public constructor(context: Command.LoaderContext, options: Command.Options) {
super(context, { ...options });
}

public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand((builder) =>
builder.setName("ping").setDescription("Ping bot to see if it is alive")
);
}
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
await interaction.reply("Yup");
}
}
It registers nice. Now let's say we register out DB client:
const db = new DB(); //For testing
const db = new DB(); //For testing
Now since I don't wanna create new instances everytime a command is invoked. I need a way to register this. (With type safety). My question: How can I register the db client onto the context of the command? Singleton class? Well, if there are no other possibilites. I'd rather just instantiate it in main and keep references to it throughout the commands where I can invoke.
Solution:
Sapphire Framework
Using and extending container | Sapphire
The container is a way in which Sapphire achieves Dependency Injection. This is a very useful feature, since it
Jump to solution
1 Reply
Solution
Favna
Favna4mo ago
Sapphire Framework
Using and extending container | Sapphire
The container is a way in which Sapphire achieves Dependency Injection. This is a very useful feature, since it