@pgxsinkit/server 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,32 @@
1
+ import type { SQLWrapper } from "drizzle-orm";
2
+
3
+ export type OpsLogSource = "crud" | "batch";
4
+
5
+ export type OpsLogBackend = "drizzle" | "bulk-dynamic" | "bulk-pregenerated" | "bulk-plpgsql" | "bulk-plpgsql-artifact";
6
+
7
+ export type OpsLogStatus = "succeeded" | "validation_failed" | "not_found" | "execution_failed";
8
+
9
+ export interface OperationsLogConfig {
10
+ enabled: boolean;
11
+ }
12
+
13
+ export interface OpsLogEntry {
14
+ source: OpsLogSource;
15
+ backend: OpsLogBackend;
16
+ tableName?: string | null;
17
+ operationKind?: "create" | "update" | "delete" | null;
18
+ userId?: string | null;
19
+ entityKey?: Record<string, string> | null;
20
+ payload?: unknown;
21
+ status: OpsLogStatus;
22
+ errorMessage?: string | null;
23
+ httpStatus?: number | null;
24
+ mutationId?: string | null;
25
+ mutationSeq?: number | null;
26
+ clientTimestampUs?: string | null;
27
+ requestPath?: string | null;
28
+ }
29
+
30
+ export interface SqlExecutor {
31
+ execute: (query: string | SQLWrapper<unknown>) => Promise<unknown>;
32
+ }
@@ -0,0 +1,72 @@
1
+ import { sql } from "drizzle-orm";
2
+
3
+ import type { OpsLogEntry, OperationsLogConfig, SqlExecutor } from "./types";
4
+
5
+ function toJson(value: unknown): string | null {
6
+ if (value === undefined) {
7
+ return null;
8
+ }
9
+
10
+ return JSON.stringify(value);
11
+ }
12
+
13
+ export async function logOperation(
14
+ executor: SqlExecutor,
15
+ config: OperationsLogConfig,
16
+ entry: OpsLogEntry,
17
+ ): Promise<void> {
18
+ if (!config.enabled) {
19
+ return;
20
+ }
21
+
22
+ const entityKeyJson = toJson(entry.entityKey ?? null);
23
+ const payloadJson = toJson(entry.payload);
24
+
25
+ await executor.execute(sql`
26
+ INSERT INTO operations_log (
27
+ source,
28
+ backend,
29
+ table_name,
30
+ operation_kind,
31
+ user_id,
32
+ entity_key_json,
33
+ payload_json,
34
+ status,
35
+ error_message,
36
+ http_status,
37
+ mutation_id,
38
+ mutation_seq,
39
+ client_timestamp_us,
40
+ request_path,
41
+ server_timestamp_us
42
+ ) VALUES (
43
+ ${entry.source},
44
+ ${entry.backend},
45
+ ${entry.tableName ?? null},
46
+ ${entry.operationKind ?? null},
47
+ ${entry.userId ?? null},
48
+ ${entityKeyJson}::jsonb,
49
+ ${payloadJson}::jsonb,
50
+ ${entry.status},
51
+ ${entry.errorMessage ?? null},
52
+ ${entry.httpStatus ?? null},
53
+ ${entry.mutationId ?? null},
54
+ ${entry.mutationSeq ?? null},
55
+ ${entry.clientTimestampUs ?? null},
56
+ ${entry.requestPath ?? null},
57
+ CAST(FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000000) AS BIGINT)
58
+ )
59
+ `);
60
+ }
61
+
62
+ export async function logOperationSafely(
63
+ executor: SqlExecutor,
64
+ config: OperationsLogConfig,
65
+ entry: OpsLogEntry,
66
+ ): Promise<void> {
67
+ try {
68
+ await logOperation(executor, config, entry);
69
+ } catch {
70
+ // Failure-path logging must not hide the original route response/error.
71
+ }
72
+ }