@pgxsinkit/server 0.1.25 → 0.1.26
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.
- package/dist/index.js +7034 -194
- package/dist/index.js.map +110 -6
- package/package.json +2 -2
- package/src/mutations/bulk/route.ts +19 -16
- package/src/operations-log/types.ts +4 -0
- package/src/operations-log/writer.ts +27 -42
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgxsinkit/server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.26",
|
|
4
4
|
"bin": {
|
|
5
5
|
"pgxsinkit-generate": "src/cli/generate.ts"
|
|
6
6
|
},
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"build": "bun --cwd ../.. run build:public-packages"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@pgxsinkit/contracts": "^0.1.
|
|
28
|
+
"@pgxsinkit/contracts": "^0.1.26"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"drizzle-orm": "1.0.0-rc.2",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { and, eq, sql } from "drizzle-orm";
|
|
2
2
|
import { getTableConfig, type AnyPgTable } from "drizzle-orm/pg-core";
|
|
3
3
|
import type { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
4
4
|
import { getColumns } from "drizzle-orm/utils";
|
|
@@ -26,6 +26,8 @@ const { createInsertSchema: createMutationInsertSchema } = createSchemaFactory({
|
|
|
26
26
|
coerce: { date: true },
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
+
type RouteReadQueryClient = Pick<PostgresJsDatabase<any>, "select">;
|
|
30
|
+
|
|
29
31
|
export function registerBulkMutationRoute<TRegistry extends SyncTableRegistry>(
|
|
30
32
|
app: Hono,
|
|
31
33
|
db: PostgresJsDatabase<RegistryRelations<TRegistry>>,
|
|
@@ -237,7 +239,7 @@ export function registerBulkMutationRoute<TRegistry extends SyncTableRegistry>(
|
|
|
237
239
|
|
|
238
240
|
for (const mutation of batchRequest.mutations) {
|
|
239
241
|
const serverUpdatedAtUs = await readServerUpdatedAtUs(
|
|
240
|
-
tx
|
|
242
|
+
tx,
|
|
241
243
|
registry[mutation.tableName as keyof TRegistry] as SyncTableEntry,
|
|
242
244
|
mutation.entityKey,
|
|
243
245
|
);
|
|
@@ -526,7 +528,7 @@ function resolveServerUpdatedAtColumnName(entry: SyncTableEntry): string {
|
|
|
526
528
|
}
|
|
527
529
|
|
|
528
530
|
async function readServerUpdatedAtUs(
|
|
529
|
-
tx:
|
|
531
|
+
tx: RouteReadQueryClient,
|
|
530
532
|
entry: SyncTableEntry,
|
|
531
533
|
entityKey: Record<string, string>,
|
|
532
534
|
): Promise<string | undefined> {
|
|
@@ -545,22 +547,23 @@ async function readServerUpdatedAtUs(
|
|
|
545
547
|
throw new Error(`Missing entity key value for primary key column ${primaryKeyColumn}`);
|
|
546
548
|
}
|
|
547
549
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
+
const primaryKeyTableColumn = Object.values(columns).find((column) => column.name === primaryKeyColumn);
|
|
551
|
+
|
|
552
|
+
if (!primaryKeyTableColumn) {
|
|
553
|
+
throw new Error(`Missing table column metadata for primary key column ${primaryKeyColumn}`);
|
|
554
|
+
}
|
|
550
555
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
FROM ${quoteIdentifier(getTableName(entry.table as AnyPgTable))}
|
|
554
|
-
WHERE ${sql.join(conditions, sql` AND `)}
|
|
555
|
-
LIMIT 1
|
|
556
|
-
`)) as Iterable<{ updatedAtUs: string | null }>;
|
|
556
|
+
return eq(primaryKeyTableColumn, rawValue);
|
|
557
|
+
});
|
|
557
558
|
|
|
558
|
-
const
|
|
559
|
-
|
|
560
|
-
}
|
|
559
|
+
const whereClause = conditions.length === 1 ? conditions[0]! : and(...conditions);
|
|
560
|
+
const rows = await tx
|
|
561
|
+
.select({ updatedAtUs: sql<string>`${updatedAtColumn}::text` })
|
|
562
|
+
.from(entry.table as AnyPgTable)
|
|
563
|
+
.where(whereClause)
|
|
564
|
+
.limit(1);
|
|
561
565
|
|
|
562
|
-
|
|
563
|
-
return sql.raw(`"${identifier.replace(/"/g, '""')}"`);
|
|
566
|
+
return rows[0]?.updatedAtUs ?? undefined;
|
|
564
567
|
}
|
|
565
568
|
|
|
566
569
|
async function installBulkStartupDdl<TRegistry extends SyncTableRegistry>(
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SQLWrapper } from "drizzle-orm";
|
|
2
|
+
import type { AnyPgTable } from "drizzle-orm/pg-core";
|
|
2
3
|
|
|
3
4
|
export type OpsLogSource = "crud" | "batch";
|
|
4
5
|
|
|
@@ -29,4 +30,7 @@ export interface OpsLogEntry {
|
|
|
29
30
|
|
|
30
31
|
export interface SqlExecutor {
|
|
31
32
|
execute: (query: string | SQLWrapper<unknown>) => Promise<unknown>;
|
|
33
|
+
insert: (table: AnyPgTable) => {
|
|
34
|
+
values: (values: Record<string, unknown>) => PromiseLike<unknown>;
|
|
35
|
+
};
|
|
32
36
|
}
|
|
@@ -1,13 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { operationsLogTable } from "./schema";
|
|
3
2
|
import type { OpsLogEntry, OperationsLogConfig, SqlExecutor } from "./types";
|
|
4
3
|
|
|
5
|
-
function
|
|
4
|
+
function toJsonValue(value: unknown): unknown {
|
|
6
5
|
if (value === undefined) {
|
|
7
6
|
return null;
|
|
8
7
|
}
|
|
9
8
|
|
|
10
|
-
return
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function toBigIntValue(value: string | null | undefined): bigint | null {
|
|
13
|
+
if (value === undefined || value === null) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return BigInt(value);
|
|
11
18
|
}
|
|
12
19
|
|
|
13
20
|
export async function logOperation(
|
|
@@ -19,44 +26,22 @@ export async function logOperation(
|
|
|
19
26
|
return;
|
|
20
27
|
}
|
|
21
28
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
`);
|
|
29
|
+
await executor.insert(operationsLogTable).values({
|
|
30
|
+
source: entry.source,
|
|
31
|
+
backend: entry.backend,
|
|
32
|
+
tableName: entry.tableName ?? null,
|
|
33
|
+
operationKind: entry.operationKind ?? null,
|
|
34
|
+
userId: entry.userId ?? null,
|
|
35
|
+
entityKeyJson: toJsonValue(entry.entityKey ?? null),
|
|
36
|
+
payloadJson: toJsonValue(entry.payload),
|
|
37
|
+
status: entry.status,
|
|
38
|
+
errorMessage: entry.errorMessage ?? null,
|
|
39
|
+
httpStatus: entry.httpStatus ?? null,
|
|
40
|
+
mutationId: entry.mutationId ?? null,
|
|
41
|
+
mutationSeq: entry.mutationSeq ?? null,
|
|
42
|
+
clientTimestampUs: toBigIntValue(entry.clientTimestampUs),
|
|
43
|
+
requestPath: entry.requestPath ?? null,
|
|
44
|
+
});
|
|
60
45
|
}
|
|
61
46
|
|
|
62
47
|
export async function logOperationSafely(
|