feat(db): перевести хранилище с SQLite на PostgreSQL
Docker images / prepare-release (push) Successful in 12s
Docker images / backend-test (push) Successful in 4m9s
Docker images / frontend-image (push) Successful in 4m28s
Docker images / updater-image (push) Successful in 58s
Docker images / backend-image (push) Successful in 2m49s
Docker images / notify-webhook (push) Skipped
Docker images / publish-release (push) Successful in 11s
Docker images / prepare-release (push) Successful in 12s
Docker images / backend-test (push) Successful in 4m9s
Docker images / frontend-image (push) Successful in 4m28s
Docker images / updater-image (push) Successful in 58s
Docker images / backend-image (push) Successful in 2m49s
Docker images / notify-webhook (push) Skipped
Docker images / publish-release (push) Successful in 11s
При старте backend накатывает схему PostgreSQL 18 и, если база пустая, один раз импортирует mikrotik.db с тома. Повторный старт не копирует данные. Бэкап в UI идёт через pg_dump. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
0e5fb065e2
commit
ec43591a99
128 files changed
+4304
-3639
No files matched your search
@@ -24,18 +24,7 @@ export type ListEventsParams = {
|
||||
to?: string
|
||||
}
|
||||
|
||||
function parsePayload(payloadJson: string | null): Record<string, unknown> {
|
||||
if (!payloadJson) return {}
|
||||
try {
|
||||
const parsed = JSON.parse(payloadJson) as unknown
|
||||
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
||||
return parsed as Record<string, unknown>
|
||||
}
|
||||
} catch {
|
||||
// keep backwards compatibility with malformed legacy payloads
|
||||
}
|
||||
return {}
|
||||
}
|
||||
import { parseJsonObject } from "../../../db/json.js"
|
||||
|
||||
function mapEventRow(row: typeof events.$inferSelect): EventItem {
|
||||
return {
|
||||
@@ -48,13 +37,13 @@ function mapEventRow(row: typeof events.$inferSelect): EventItem {
|
||||
message: row.message,
|
||||
entityType: row.entityType ?? null,
|
||||
entityId: row.entityId ?? null,
|
||||
payload: parsePayload(row.payloadJson ?? null),
|
||||
payload: parseJsonObject(row.payloadJson),
|
||||
}
|
||||
}
|
||||
|
||||
export function insertEventsBatch(items: EventInsertInput[]) {
|
||||
export async function insertEventsBatch(items: EventInsertInput[]) {
|
||||
if (items.length === 0) return
|
||||
db.insert(events)
|
||||
await db.insert(events)
|
||||
.values(
|
||||
items.map((item) => ({
|
||||
id: item.id,
|
||||
@@ -66,25 +55,23 @@ export function insertEventsBatch(items: EventInsertInput[]) {
|
||||
message: item.message,
|
||||
entityType: item.entityType ?? null,
|
||||
entityId: item.entityId ?? null,
|
||||
payloadJson: item.payload ? JSON.stringify(item.payload) : null,
|
||||
payloadJson: item.payload ?? null,
|
||||
})),
|
||||
)
|
||||
.run()
|
||||
}
|
||||
|
||||
export function listEvents(params: ListEventsParams): EventItem[] {
|
||||
export async function listEvents(params: ListEventsParams): Promise<EventItem[]> {
|
||||
const where = and(
|
||||
params.level ? eq(events.level, params.level) : undefined,
|
||||
params.sourceModule ? eq(events.sourceModule, params.sourceModule) : undefined,
|
||||
params.from ? gte(events.createdAt, params.from) : undefined,
|
||||
params.to ? lte(events.createdAt, params.to) : undefined,
|
||||
)
|
||||
const rows = db
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(events)
|
||||
.where(where)
|
||||
.orderBy(desc(events.createdAt))
|
||||
.limit(params.limit)
|
||||
.all()
|
||||
return rows.map(mapEventRow)
|
||||
}
|
||||
@@ -23,17 +23,17 @@ function normalizeEventInput(input: AppendEventBody): EventInsertInput {
|
||||
}
|
||||
}
|
||||
|
||||
export function appendEvent(input: AppendEventBody) {
|
||||
export async function appendEvent(input: AppendEventBody) {
|
||||
const parsed = appendEventSchema.parse(input)
|
||||
insertEventsBatch([normalizeEventInput(parsed)])
|
||||
await insertEventsBatch([normalizeEventInput(parsed)])
|
||||
}
|
||||
|
||||
export function appendEvents(inputs: AppendEventBody[]) {
|
||||
export async function appendEvents(inputs: AppendEventBody[]) {
|
||||
const rows = inputs.map((entry) => normalizeEventInput(appendEventSchema.parse(entry)))
|
||||
insertEventsBatch(rows)
|
||||
await insertEventsBatch(rows)
|
||||
}
|
||||
|
||||
export function readEvents(query: Partial<ListEventsQuery>): EventItem[] {
|
||||
export async function readEvents(query: Partial<ListEventsQuery>): Promise<EventItem[]> {
|
||||
const parsed = listEventsQuerySchema.parse(query)
|
||||
return listEvents(parsed)
|
||||
return await listEvents(parsed)
|
||||
}
|
||||
Reference in new issue
Block a user