@pgxsinkit/server 0.1.27 → 0.1.28
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 +2004 -5550
- package/dist/index.js.map +13 -49
- package/package.json +3 -4
- package/src/index.ts +19 -49
- package/src/mutations/bulk/plpgsql-strategy.ts +5 -6
- package/src/mutations/bulk/route.ts +4 -4
- package/src/operations-log/ddl.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgxsinkit/server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.28",
|
|
4
4
|
"bin": {
|
|
5
5
|
"pgxsinkit-generate": "src/cli/generate.ts"
|
|
6
6
|
},
|
|
@@ -25,11 +25,10 @@
|
|
|
25
25
|
"build": "bun --cwd ../.. run build:public-packages"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@pgxsinkit/contracts": "^0.1.
|
|
28
|
+
"@pgxsinkit/contracts": "^0.1.28"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"drizzle-orm": "1.0.0-rc.2",
|
|
32
|
-
"hono": "^4.12.23"
|
|
33
|
-
"postgres": "^3.4.9"
|
|
32
|
+
"hono": "^4.12.23"
|
|
34
33
|
}
|
|
35
34
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { drizzle } from "drizzle-orm/postgres-js";
|
|
3
|
-
import { defineRelations } from "drizzle-orm/relations";
|
|
1
|
+
import type { PgAsyncDatabase } from "drizzle-orm/pg-core";
|
|
4
2
|
import { Hono } from "hono";
|
|
5
3
|
import { cors } from "hono/cors";
|
|
6
|
-
import postgres from "postgres";
|
|
7
4
|
|
|
8
5
|
import type {
|
|
9
6
|
RegistryRelations,
|
|
@@ -33,12 +30,12 @@ interface BunNamespace {
|
|
|
33
30
|
}) => BunServerHandle;
|
|
34
31
|
}
|
|
35
32
|
|
|
36
|
-
export interface CreateSyncServerOptions<
|
|
33
|
+
export interface CreateSyncServerOptions<
|
|
34
|
+
TRegistry extends SyncTableRegistry,
|
|
35
|
+
TDb extends PgAsyncDatabase<any, RegistryRelations<TRegistry>> = PgAsyncDatabase<any, RegistryRelations<TRegistry>>,
|
|
36
|
+
> {
|
|
37
37
|
registry: TRegistry;
|
|
38
|
-
|
|
39
|
-
db?: PostgresJsDatabase<RegistryRelations<TRegistry>>;
|
|
40
|
-
/** PostgreSQL connection URL. Mutually exclusive with db. */
|
|
41
|
-
databaseUrl?: string;
|
|
38
|
+
db: TDb;
|
|
42
39
|
/** Existing Hono app to register routes on. If omitted, a new Hono app is created. */
|
|
43
40
|
app?: Hono;
|
|
44
41
|
backend?: BulkMutationBackend;
|
|
@@ -60,8 +57,11 @@ export interface ServerDiagnostics<TRegistry extends SyncTableRegistry> {
|
|
|
60
57
|
modes: Record<string, TRegistry[keyof TRegistry]["mode"]>;
|
|
61
58
|
}
|
|
62
59
|
|
|
63
|
-
export interface SyncServer<
|
|
64
|
-
|
|
60
|
+
export interface SyncServer<
|
|
61
|
+
TRegistry extends SyncTableRegistry,
|
|
62
|
+
TDb extends PgAsyncDatabase<any, RegistryRelations<TRegistry>> = PgAsyncDatabase<any, RegistryRelations<TRegistry>>,
|
|
63
|
+
> {
|
|
64
|
+
drizzle: TDb;
|
|
65
65
|
fetch: (request: Request) => Promise<Response>;
|
|
66
66
|
request: (path: string, init?: RequestInit) => Promise<Response>;
|
|
67
67
|
start: () => Promise<void>;
|
|
@@ -71,28 +71,12 @@ export interface SyncServer<TRegistry extends SyncTableRegistry> {
|
|
|
71
71
|
diagnostics: () => ServerDiagnostics<TRegistry>;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
export function createSyncServer<
|
|
75
|
-
|
|
76
|
-
|
|
74
|
+
export function createSyncServer<
|
|
75
|
+
TRegistry extends SyncTableRegistry,
|
|
76
|
+
TDb extends PgAsyncDatabase<any, RegistryRelations<TRegistry>> = PgAsyncDatabase<any, RegistryRelations<TRegistry>>,
|
|
77
|
+
>(options: CreateSyncServerOptions<TRegistry, TDb>): SyncServer<TRegistry, TDb> {
|
|
77
78
|
const ownsApp = !options.app;
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
if (!ownsDb && !options.databaseUrl) {
|
|
81
|
-
throw new Error("createSyncServer requires either db or databaseUrl");
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
let pgClient: ReturnType<typeof postgres> | undefined;
|
|
85
|
-
let db: PostgresJsDatabase<RegistryRelations<TRegistry>>;
|
|
86
|
-
|
|
87
|
-
if (options.db) {
|
|
88
|
-
db = options.db;
|
|
89
|
-
} else {
|
|
90
|
-
pgClient = createPostgresClient(options.databaseUrl!);
|
|
91
|
-
const schema = buildSchema(options.registry);
|
|
92
|
-
const relations = defineRelations(schema) as RegistryRelations<TRegistry>;
|
|
93
|
-
db = drizzle({ client: pgClient, relations }) as unknown as PostgresJsDatabase<RegistryRelations<TRegistry>>;
|
|
94
|
-
}
|
|
95
|
-
|
|
79
|
+
const db = options.db;
|
|
96
80
|
const app = options.app ?? new Hono();
|
|
97
81
|
let bunServer: BunServerHandle | undefined;
|
|
98
82
|
|
|
@@ -203,7 +187,6 @@ export function createSyncServer<TRegistry extends SyncTableRegistry>(
|
|
|
203
187
|
bunServer?.stop();
|
|
204
188
|
bunServer = undefined;
|
|
205
189
|
status.isRunning = false;
|
|
206
|
-
await pgClient?.end();
|
|
207
190
|
options.onStatusChange?.(status);
|
|
208
191
|
},
|
|
209
192
|
status,
|
|
@@ -217,22 +200,9 @@ export function createSyncServer<TRegistry extends SyncTableRegistry>(
|
|
|
217
200
|
};
|
|
218
201
|
}
|
|
219
202
|
|
|
220
|
-
function
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return postgres({
|
|
225
|
-
host: url.hostname,
|
|
226
|
-
port: Number(url.port || "5432"),
|
|
227
|
-
database: url.pathname.replace(/^\//, ""),
|
|
228
|
-
user: decodeURIComponent(url.username),
|
|
229
|
-
password: decodeURIComponent(url.password),
|
|
230
|
-
ssl: sslmode === "disable" ? false : "prefer",
|
|
231
|
-
max: 10,
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
function buildSchema<TRegistry extends SyncTableRegistry>(registry: TRegistry) {
|
|
203
|
+
export function buildRegistrySchema<TRegistry extends SyncTableRegistry>(
|
|
204
|
+
registry: TRegistry,
|
|
205
|
+
): RegistryTables<TRegistry> {
|
|
236
206
|
return Object.fromEntries(
|
|
237
207
|
Object.entries(registry).map(([key, entry]) => [key, entry.table]),
|
|
238
208
|
) as RegistryTables<TRegistry>;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { getTableName, sql } from "drizzle-orm";
|
|
2
|
-
import { getTableConfig, type AnyPgTable } from "drizzle-orm/pg-core";
|
|
3
|
-
import type { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
2
|
+
import { getTableConfig, type AnyPgTable, type PgAsyncDatabase } from "drizzle-orm/pg-core";
|
|
4
3
|
import { getColumns } from "drizzle-orm/utils";
|
|
5
4
|
|
|
6
5
|
import {
|
|
@@ -273,7 +272,7 @@ $$;
|
|
|
273
272
|
}
|
|
274
273
|
|
|
275
274
|
export async function installPlpgsqlBatchFunction<TRegistry extends SyncTableRegistry>(
|
|
276
|
-
db:
|
|
275
|
+
db: PgAsyncDatabase<any, RegistryRelations<TRegistry>>,
|
|
277
276
|
registry: TRegistry,
|
|
278
277
|
options: {
|
|
279
278
|
functionSchema?: string;
|
|
@@ -288,7 +287,7 @@ type FunctionPresenceRow = {
|
|
|
288
287
|
};
|
|
289
288
|
|
|
290
289
|
export async function verifyPlpgsqlBatchFunction<TRegistry extends SyncTableRegistry>(
|
|
291
|
-
db:
|
|
290
|
+
db: PgAsyncDatabase<any, RegistryRelations<TRegistry>>,
|
|
292
291
|
options: {
|
|
293
292
|
functionSchema?: string;
|
|
294
293
|
} = {},
|
|
@@ -312,7 +311,7 @@ type AuthHelperPresenceRow = {
|
|
|
312
311
|
};
|
|
313
312
|
|
|
314
313
|
export async function verifyArtifactRlsAuthHelpers<TRegistry extends SyncTableRegistry>(
|
|
315
|
-
db:
|
|
314
|
+
db: PgAsyncDatabase<any, RegistryRelations<TRegistry>>,
|
|
316
315
|
): Promise<void> {
|
|
317
316
|
const result = await db.execute<AuthHelperPresenceRow>(sql`
|
|
318
317
|
SELECT
|
|
@@ -345,6 +344,6 @@ export async function executePlpgsqlBatch(
|
|
|
345
344
|
const functionName = qualifyIdent(options.functionSchema, "pgxsinkit_apply_batch_mutations");
|
|
346
345
|
|
|
347
346
|
await tx.execute(
|
|
348
|
-
sql`SELECT ${sql.raw(functionName)}(${JSON.stringify(batch)}::jsonb, ${requestPath}, ${logEnabled}, ${rlsEnabled}, ${JSON.stringify(normalizedClaims)}::jsonb)`,
|
|
347
|
+
sql`SELECT ${sql.raw(functionName)}(${JSON.stringify(batch)}::text::jsonb, ${requestPath}, ${logEnabled}, ${rlsEnabled}, ${JSON.stringify(normalizedClaims)}::text::jsonb)`,
|
|
349
348
|
);
|
|
350
349
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { and, eq, sql } from "drizzle-orm";
|
|
2
2
|
import { getTableConfig, type AnyPgTable } from "drizzle-orm/pg-core";
|
|
3
|
-
import type {
|
|
3
|
+
import type { PgAsyncDatabase } from "drizzle-orm/pg-core";
|
|
4
4
|
import { getColumns } from "drizzle-orm/utils";
|
|
5
5
|
import { createSchemaFactory } from "drizzle-orm/zod";
|
|
6
6
|
import type { Context, Hono } from "hono";
|
|
@@ -26,11 +26,11 @@ const { createInsertSchema: createMutationInsertSchema } = createSchemaFactory({
|
|
|
26
26
|
coerce: { date: true },
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
-
type RouteReadQueryClient = Pick<
|
|
29
|
+
type RouteReadQueryClient = Pick<PgAsyncDatabase<any, any>, "select">;
|
|
30
30
|
|
|
31
31
|
export function registerBulkMutationRoute<TRegistry extends SyncTableRegistry>(
|
|
32
32
|
app: Hono,
|
|
33
|
-
db:
|
|
33
|
+
db: PgAsyncDatabase<any, RegistryRelations<TRegistry>>,
|
|
34
34
|
registry: TRegistry,
|
|
35
35
|
backend: BulkMutationBackend,
|
|
36
36
|
operationsLogConfig: OperationsLogConfig,
|
|
@@ -567,7 +567,7 @@ async function readServerUpdatedAtUs(
|
|
|
567
567
|
}
|
|
568
568
|
|
|
569
569
|
async function installBulkStartupDdl<TRegistry extends SyncTableRegistry>(
|
|
570
|
-
db:
|
|
570
|
+
db: PgAsyncDatabase<any, RegistryRelations<TRegistry>>,
|
|
571
571
|
registry: TRegistry,
|
|
572
572
|
_backend: BulkMutationBackend,
|
|
573
573
|
): Promise<void> {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { sql } from "drizzle-orm";
|
|
2
|
-
import type {
|
|
2
|
+
import type { PgAsyncDatabase } from "drizzle-orm/pg-core";
|
|
3
3
|
|
|
4
4
|
import type { RegistryRelations, SyncTableRegistry } from "@pgxsinkit/contracts";
|
|
5
5
|
|
|
@@ -17,7 +17,7 @@ type OperationsLogPresenceRow = {
|
|
|
17
17
|
* in their Drizzle schema so `drizzle-kit generate`/`push` creates it.
|
|
18
18
|
*/
|
|
19
19
|
export async function ensureOperationsLogSchema<TRegistry extends SyncTableRegistry>(
|
|
20
|
-
db:
|
|
20
|
+
db: PgAsyncDatabase<any, RegistryRelations<TRegistry>>,
|
|
21
21
|
config: OperationsLogConfig,
|
|
22
22
|
): Promise<boolean> {
|
|
23
23
|
if (!config.enabled) {
|