@umituz/web-hono 1.1.0 → 1.2.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.
- package/package.json +1 -1
- package/src/crud.ts +32 -7
- package/src/index.ts +3 -3
- package/src/lib.ts +22 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-hono",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Hono backend contract for Cloudflare Workers + Neon Postgres: crudRoutes factory, pickPatch, validateJson, apiFetch/apiWrite client, reactive fetch-backed store (+React hooks), and db lifecycle scripts (reset / snapshot / seed)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
package/src/crud.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { and, asc, desc, eq, isNull } from 'drizzle-orm';
|
|
|
3
3
|
import { drizzle } from 'drizzle-orm/neon-http';
|
|
4
4
|
import { neon } from '@neondatabase/serverless';
|
|
5
5
|
import type { PgColumn, PgTable } from 'drizzle-orm/pg-core';
|
|
6
|
-
import { pickPatch } from './lib';
|
|
6
|
+
import { pickPatch, stampUpdate } from './lib';
|
|
7
7
|
import type { ApiEnv } from './env';
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -11,6 +11,9 @@ import type { ApiEnv } from './env';
|
|
|
11
11
|
* PATCH /:id (pickPatch-filtered body, updatedAt stamp, 404 on miss).
|
|
12
12
|
* Mount it onto an area router with `.route('/', crudRoutes({ ... }))`.
|
|
13
13
|
*
|
|
14
|
+
* listRoutes is its GET-only sibling for append-only collections (event
|
|
15
|
+
* logs, activities, users) whose rows are written server-side only.
|
|
16
|
+
*
|
|
14
17
|
* Entities with business rules (role immutability guards, goods-receipt
|
|
15
18
|
* restock, order transitions…) stay hand-written in their area router —
|
|
16
19
|
* the factory is for plain settings/catalog rows only.
|
|
@@ -18,14 +21,35 @@ import type { ApiEnv } from './env';
|
|
|
18
21
|
|
|
19
22
|
export type CrudTable = PgTable & { id: PgColumn; deletedAt: PgColumn };
|
|
20
23
|
|
|
21
|
-
export interface
|
|
22
|
-
/** Path under /api, e.g. '/
|
|
24
|
+
export interface ListSpec {
|
|
25
|
+
/** Path under /api, e.g. '/activities'. */
|
|
23
26
|
path: string;
|
|
27
|
+
table: PgTable & { deletedAt: PgColumn };
|
|
28
|
+
/** List order; defaults to insertion order. */
|
|
29
|
+
orderBy?: { col: PgColumn; dir?: 'asc' | 'desc' };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface CrudSpec extends ListSpec {
|
|
24
33
|
table: CrudTable;
|
|
25
34
|
/** Human label for 404s — 'Product' → 'Product not found'. */
|
|
26
35
|
label: string;
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** GET-only list (soft-deleted hidden) for append-only collections — event
|
|
39
|
+
* logs, activities, users. No PATCH: these rows are written server-side. */
|
|
40
|
+
export function listRoutes(spec: ListSpec): Hono<{ Bindings: ApiEnv }> {
|
|
41
|
+
const { path, table, orderBy } = spec;
|
|
42
|
+
const router = new Hono<{ Bindings: ApiEnv }>();
|
|
43
|
+
|
|
44
|
+
router.get(path, async (c) => {
|
|
45
|
+
// Schema-less drizzle: see the note in crudRoutes.
|
|
46
|
+
const db = drizzle(neon(c.env.DATABASE_URL));
|
|
47
|
+
const q = db.select().from(table).where(isNull(table.deletedAt)).$dynamic();
|
|
48
|
+
if (orderBy) q.orderBy(orderBy.dir === 'desc' ? desc(orderBy.col) : asc(orderBy.col));
|
|
49
|
+
return c.json(await q);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return router;
|
|
29
53
|
}
|
|
30
54
|
|
|
31
55
|
export function crudRoutes(spec: CrudSpec): Hono<{ Bindings: ApiEnv }> {
|
|
@@ -49,8 +73,9 @@ export function crudRoutes(spec: CrudSpec): Hono<{ Bindings: ApiEnv }> {
|
|
|
49
73
|
const [row] = await db
|
|
50
74
|
.update(table)
|
|
51
75
|
// Generic tables can't carry per-table set types — values are already
|
|
52
|
-
// column-filtered by pickPatch, so the cast is purely type-level.
|
|
53
|
-
|
|
76
|
+
// column-filtered by pickPatch, so the cast is purely type-level. The
|
|
77
|
+
// updatedAt stamp matches the table's column type (date vs timestamp).
|
|
78
|
+
.set({ ...patch, ...stampUpdate(table) } as never)
|
|
54
79
|
.where(and(eq(table.id, c.req.param('id')), isNull(table.deletedAt)))
|
|
55
80
|
.returning();
|
|
56
81
|
return row ? c.json(row) : c.json({ error: `${label} not found` }, 404);
|
package/src/index.ts
CHANGED
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
* `/client` apiFetch / apiWrite — the same-origin API client
|
|
15
15
|
* `/lifecycle` reset / snapshot / seed — the shared-Neon db lifecycle
|
|
16
16
|
*/
|
|
17
|
-
export { crudRoutes } from './crud';
|
|
18
|
-
export type { CrudSpec, CrudTable } from './crud';
|
|
17
|
+
export { crudRoutes, listRoutes } from './crud';
|
|
18
|
+
export type { CrudSpec, CrudTable, ListSpec } from './crud';
|
|
19
19
|
export { getDb } from './db';
|
|
20
|
-
export { uid, pickPatch } from './lib';
|
|
20
|
+
export { uid, pickPatch, stampUpdate } from './lib';
|
|
21
21
|
export { apiNotFound, spaFallback } from './handlers';
|
|
22
22
|
export type { ApiEnv } from './env';
|
package/src/lib.ts
CHANGED
|
@@ -11,6 +11,28 @@ export function uid(prefix: string): string {
|
|
|
11
11
|
/** Fields no client may ever patch. */
|
|
12
12
|
const PROTECTED_KEYS = new Set(['id', 'createdAt', 'updatedAt', 'deletedAt']);
|
|
13
13
|
|
|
14
|
+
/** Local calendar day as 'YYYY-MM-DD' — the stamp for date-string columns. */
|
|
15
|
+
function localDateStamp(): string {
|
|
16
|
+
const d = new Date();
|
|
17
|
+
const mm = String(d.getMonth() + 1).padStart(2, '0');
|
|
18
|
+
const dd = String(d.getDate()).padStart(2, '0');
|
|
19
|
+
return `${d.getFullYear()}-${mm}-${dd}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* updatedAt stamp matching the table's column type. Fleet-standard tables
|
|
24
|
+
* (timestamp withTimezone) get a Date — unchanged from previous versions.
|
|
25
|
+
* Domains that model updatedAt as a calendar date column (date string,
|
|
26
|
+
* e.g. polaris) get today's local 'YYYY-MM-DD' instead of a Date that the
|
|
27
|
+
* column cannot carry.
|
|
28
|
+
*/
|
|
29
|
+
export function stampUpdate(table: PgTable): Record<string, unknown> {
|
|
30
|
+
const columns = getTableColumns(table) as unknown as Record<string, { columnType: string }>;
|
|
31
|
+
return columns.updatedAt?.columnType.includes('Timestamp')
|
|
32
|
+
? { updatedAt: new Date() }
|
|
33
|
+
: { updatedAt: localDateStamp() };
|
|
34
|
+
}
|
|
35
|
+
|
|
14
36
|
/**
|
|
15
37
|
* Filters a JSON patch body down to the table's own columns (drizzle speaks
|
|
16
38
|
* the camelCase schema PROPERTY names — which are the domain field names).
|