mirror of https://github.com/wg-easy/wg-easy
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.0 KiB
78 lines
2.0 KiB
import type { ClientRepository, Client } from './client';
|
|
import type { System, SystemRepository } from './system';
|
|
import type { User, UserRepository } from './user';
|
|
|
|
// Represent data structure
|
|
export type Database = {
|
|
migrations: string[];
|
|
system: System;
|
|
users: User[];
|
|
clients: Record<string, Client>;
|
|
};
|
|
|
|
export const DEFAULT_DATABASE: Database = {
|
|
migrations: [],
|
|
system: null as never,
|
|
users: [],
|
|
clients: {},
|
|
};
|
|
|
|
/**
|
|
* Abstract class for database operations.
|
|
* Provides methods to connect, disconnect, and interact with system and user data.
|
|
*
|
|
* **Note :** Always throw `DatabaseError` to ensure proper API error handling.
|
|
*
|
|
*/
|
|
export abstract class DatabaseProvider {
|
|
/**
|
|
* Connects to the database.
|
|
*/
|
|
abstract connect(): Promise<void>;
|
|
|
|
/**
|
|
* Disconnects from the database.
|
|
*/
|
|
abstract disconnect(): Promise<void>;
|
|
|
|
abstract system: SystemRepository;
|
|
abstract user: UserRepository;
|
|
abstract client: ClientRepository;
|
|
}
|
|
|
|
/**
|
|
* Represents a specialized error class for database-related operations.
|
|
* This class is designed to work with internationalization (i18n) by using message keys.
|
|
* The actual error messages are expected to be retrieved using these keys from an i18n system.
|
|
*
|
|
* ### Usage:
|
|
* When throwing a `DatabaseError`, you provide an i18n key as the message.
|
|
* The key will be used by the i18n system to retrieve the corresponding localized error message.
|
|
*
|
|
* Example:
|
|
* ```typescript
|
|
* throw new DatabaseError(DatabaseError.ERROR_INIT);
|
|
* ...
|
|
* // event handler routes
|
|
* if (error instanceof DatabaseError) {
|
|
* const t = await useTranslation(event);
|
|
* throw createError({
|
|
* statusCode: 400,
|
|
* statusMessage: t(error.message),
|
|
* message: error.message,
|
|
* });
|
|
* } else {
|
|
* throw createError('Something happened !');
|
|
* }
|
|
* ```
|
|
*
|
|
* @extends {Error}
|
|
*/
|
|
export class DatabaseError extends Error {
|
|
static readonly ERROR_INIT = 'errorInit';
|
|
|
|
constructor(message: string) {
|
|
super(message);
|
|
this.name = 'DatabaseError';
|
|
}
|
|
}
|
|
|