| 130 | } |
| 131 | |
| 132 | class DatabaseLogManager { |
| 133 | constructor(private readonly db: Kysely<LogDatabaseSchema>) {} |
| 134 | |
| 135 | async get() { |
| 136 | const logs = await this.db |
| 137 | .selectFrom("logs") |
| 138 | .select([ |
| 139 | "timestamp", |
| 140 | "message", |
| 141 | "level", |
| 142 | "scope", |
| 143 | "extras", |
| 144 | "elapsed", |
| 145 | "date" |
| 146 | ]) |
| 147 | .execute(); |
| 148 | const groupedLogs: Record<string, LogMessage[]> = {}; |
| 149 | |
| 150 | for (const log of logs) { |
| 151 | const key = log.date!; |
| 152 | if (!groupedLogs[key]) groupedLogs[key] = []; |
| 153 | groupedLogs[key].push(log as LogMessage); |
| 154 | } |
| 155 | |
| 156 | return Object.keys(groupedLogs) |
| 157 | .sort((a, b) => b.localeCompare(a, undefined, { numeric: true })) |
| 158 | .map((key) => ({ |
| 159 | key, |
| 160 | logs: groupedLogs[key]?.sort((a, b) => a.timestamp - b.timestamp) |
| 161 | })); |
| 162 | } |
| 163 | |
| 164 | async clear() { |
| 165 | await this.db.deleteFrom("logs").execute(); |
| 166 | } |
| 167 | |
| 168 | async delete(key: string) { |
| 169 | await this.db.deleteFrom("logs").where("date", "==", key).execute(); |
| 170 | } |
| 171 | |
| 172 | close() { |
| 173 | return this.db.destroy(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | async function initialize( |
| 178 | options: SQLiteOptions, |
nothing calls this directly
no outgoing calls
no test coverage detected