Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

fix: add instancenotfound error kind and handle it in getsingleton me… #509

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum ErrorKind {
MissingDriver = "MISSING_DRIVER",
ParseException = "PARSE_EXCEPTION",
InvalidType = "INVALID_TYPE",
InstanceNotFound = "INSTANCE_NOT_FOUND",
}

export class CustomError extends Error {
Expand Down
16 changes: 12 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,27 @@ export class QuickDB<D = any> {
* ```ts
* // If you have set a singleton instance
* // Otherwise it will return undefined
* const db = QuickDB.getSingletion("quickdb");
* const db = QuickDB.getSingleton("quickdb");
* await db.init();
* ```
**/
static getSingletion<T>(name: string): QuickDB<T> | undefined {
static getSingleton<T>(name: string): QuickDB<T> {
if (typeof name != "string") {
throw new QuickError(
`First argument (name) needs to be a string received "${typeof name}"`,
ErrorKind.InvalidType
);
}

return this.instances.get(name);

const instance = this.instances.get(name);
if (!instance) {
throw new QuickError(
`No singleton instance registered with the name "${name}"`,
ErrorKind.InstanceNotFound
);
}

return instance;
}

/**
Expand Down
Loading