feat: Implement repositories for authentication, certificates, metrics, and platform services
- Added AuthRepository for user and settings management with CRUD operations. - Introduced CertificateRepository to handle domains, certificates, and requirements. - Created MetricsRepository for managing metrics and logs. - Developed PlatformRepository for platform services and resources management. - Established RegistryRepository for registry and token operations. - Implemented ServiceRepository for CRUD operations on services. - Defined types and interfaces in types.ts for database interactions.
This commit is contained in:
83
ts/database/repositories/auth.repository.ts
Normal file
83
ts/database/repositories/auth.repository.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Auth Repository
|
||||
* Handles CRUD operations for users and settings tables
|
||||
*/
|
||||
|
||||
import { BaseRepository } from '../base.repository.ts';
|
||||
import type { IUser } from '../../types.ts';
|
||||
|
||||
export class AuthRepository extends BaseRepository {
|
||||
// ============ Users ============
|
||||
|
||||
async createUser(user: Omit<IUser, 'id'>): Promise<IUser> {
|
||||
const now = Date.now();
|
||||
this.query(
|
||||
'INSERT INTO users (username, password_hash, role, created_at, updated_at) VALUES (?, ?, ?, ?, ?)',
|
||||
[user.username, user.passwordHash, user.role, now, now]
|
||||
);
|
||||
|
||||
return this.getUserByUsername(user.username)!;
|
||||
}
|
||||
|
||||
getUserByUsername(username: string): IUser | null {
|
||||
const rows = this.query('SELECT * FROM users WHERE username = ?', [username]);
|
||||
return rows.length > 0 ? this.rowToUser(rows[0]) : null;
|
||||
}
|
||||
|
||||
getAllUsers(): IUser[] {
|
||||
const rows = this.query('SELECT * FROM users ORDER BY created_at DESC');
|
||||
return rows.map((row) => this.rowToUser(row));
|
||||
}
|
||||
|
||||
updateUserPassword(username: string, passwordHash: string): void {
|
||||
this.query('UPDATE users SET password_hash = ?, updated_at = ? WHERE username = ?', [
|
||||
passwordHash,
|
||||
Date.now(),
|
||||
username,
|
||||
]);
|
||||
}
|
||||
|
||||
deleteUser(username: string): void {
|
||||
this.query('DELETE FROM users WHERE username = ?', [username]);
|
||||
}
|
||||
|
||||
private rowToUser(row: any): IUser {
|
||||
return {
|
||||
id: Number(row.id || row[0]),
|
||||
username: String(row.username || row[1]),
|
||||
passwordHash: String(row.password_hash || row[2]),
|
||||
role: String(row.role || row[3]) as IUser['role'],
|
||||
createdAt: Number(row.created_at || row[4]),
|
||||
updatedAt: Number(row.updated_at || row[5]),
|
||||
};
|
||||
}
|
||||
|
||||
// ============ Settings ============
|
||||
|
||||
getSetting(key: string): string | null {
|
||||
const rows = this.query('SELECT value FROM settings WHERE key = ?', [key]);
|
||||
if (rows.length === 0) return null;
|
||||
|
||||
const value = (rows[0] as any).value || rows[0][0];
|
||||
return value ? String(value) : null;
|
||||
}
|
||||
|
||||
setSetting(key: string, value: string): void {
|
||||
const now = Date.now();
|
||||
this.query(
|
||||
'INSERT OR REPLACE INTO settings (key, value, updated_at) VALUES (?, ?, ?)',
|
||||
[key, value, now]
|
||||
);
|
||||
}
|
||||
|
||||
getAllSettings(): Record<string, string> {
|
||||
const rows = this.query('SELECT key, value FROM settings');
|
||||
const settings: Record<string, string> = {};
|
||||
for (const row of rows) {
|
||||
const key = (row as any).key || row[0];
|
||||
const value = (row as any).value || row[1];
|
||||
settings[String(key)] = String(value);
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user