@umituz/web-hono 1.7.0 → 1.7.2
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 +2 -2
- package/src/index.ts +1 -1
- package/src/lib.ts +26 -0
- package/src/org.ts +12 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-hono",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.2",
|
|
4
4
|
"description": "API backend platform for Cloudflare Workers + Neon Postgres: createApi skeleton + error envelope layer, full-CRUD resource factory (opt-in create/remove, list params), schema→client type bridge (ClientRow), 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
|
@@ -6,7 +6,7 @@ import { neon } from '@neondatabase/serverless';
|
|
|
6
6
|
import type { Context } from 'hono';
|
|
7
7
|
import type { PgColumn, PgTable } from 'drizzle-orm/pg-core';
|
|
8
8
|
import { z } from 'zod';
|
|
9
|
-
import { pickPatch, stampUpdate, uid } from './lib';
|
|
9
|
+
import { normalizeRow, pickPatch, stampUpdate, uid } from './lib';
|
|
10
10
|
import type { ApiEnv } from './env';
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -160,7 +160,7 @@ export function crudRoutes(spec: CrudSpec): Hono<{ Bindings: ApiEnv }> {
|
|
|
160
160
|
if (!parsed.success) {
|
|
161
161
|
return c.json({ error: 'Invalid request body', details: parsed.error.flatten().fieldErrors }, 400);
|
|
162
162
|
}
|
|
163
|
-
const body = parsed.data as Record<string, unknown
|
|
163
|
+
const body = normalizeRow(table, parsed.data as Record<string, unknown>);
|
|
164
164
|
const db = getDb(c);
|
|
165
165
|
const [row] = await db
|
|
166
166
|
.insert(table)
|
package/src/index.ts
CHANGED
|
@@ -26,7 +26,7 @@ export { ApiError, apiOnError } from './errors';
|
|
|
26
26
|
export { crudRoutes, listRoutes } from './crud';
|
|
27
27
|
export type { CrudSpec, CrudTable, CrudDb, CrudDbFactory, CreateSpec, ListSpec } from './crud';
|
|
28
28
|
export { getDb } from './db';
|
|
29
|
-
export { uid, pickPatch, stampUpdate, trio, actor } from './lib';
|
|
29
|
+
export { uid, pickPatch, normalizeRow, stampUpdate, trio, actor } from './lib';
|
|
30
30
|
export { resolveOrg, orgHasMode, nextNumber } from './org';
|
|
31
31
|
export type { UsersLike, OrgsLike, NumberedTable } from './org';
|
|
32
32
|
export { writeAudit, writeNotification } from './activity';
|
package/src/lib.ts
CHANGED
|
@@ -79,3 +79,29 @@ export function pickPatch(table: PgTable, body: unknown): Record<string, unknown
|
|
|
79
79
|
}
|
|
80
80
|
return Object.keys(out).length > 0 ? out : null;
|
|
81
81
|
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Normalizes a create payload against the table before insert: drops keys
|
|
85
|
+
* that are not table columns, turns ISO-datetime strings destined for
|
|
86
|
+
* timestamptz columns into Dates, and maps '' to NULL on nullable columns
|
|
87
|
+
* (empty form fields). The insert-side twin of pickPatch.
|
|
88
|
+
*/
|
|
89
|
+
export function normalizeRow(table: PgTable, input: Record<string, unknown>): Record<string, unknown> {
|
|
90
|
+
const columns = getTableColumns(table) as unknown as Record<string, { columnType: string; notNull: boolean }>;
|
|
91
|
+
const out: Record<string, unknown> = {};
|
|
92
|
+
for (const [key, value] of Object.entries(input)) {
|
|
93
|
+
if (PROTECTED_KEYS.has(key)) continue;
|
|
94
|
+
const col = columns[key];
|
|
95
|
+
if (!col || value === undefined) continue;
|
|
96
|
+
if (typeof value === 'string' && col.columnType.includes('Timestamp')) {
|
|
97
|
+
const d = new Date(value);
|
|
98
|
+
if (Number.isNaN(d.getTime())) continue;
|
|
99
|
+
out[key] = d;
|
|
100
|
+
} else if (value === '' && !col.notNull) {
|
|
101
|
+
out[key] = null;
|
|
102
|
+
} else {
|
|
103
|
+
out[key] = value;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
package/src/org.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { CrudDb } from './crud';
|
|
|
4
4
|
|
|
5
5
|
export type UsersLike = PgTable & { id: PgColumn; organizationId: PgColumn };
|
|
6
6
|
export type OrgsLike = PgTable & { id: PgColumn; modes: PgColumn };
|
|
7
|
-
export type NumberedTable = PgTable & { number: PgColumn; organizationId
|
|
7
|
+
export type NumberedTable = PgTable & { number: PgColumn; organizationId?: PgColumn };
|
|
8
8
|
|
|
9
9
|
/** Organization of the acting user; missing actor → fallback. */
|
|
10
10
|
export async function resolveOrg(
|
|
@@ -29,19 +29,24 @@ export async function orgHasMode(db: CrudDb, orgs: OrgsLike, orgId: string, mode
|
|
|
29
29
|
return Array.isArray(org?.modes) && (org.modes as string[]).includes(mode);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
/** Next `PREFIX-n`
|
|
32
|
+
/** Next `PREFIX-n` (or `PREFIX-YYYY-NNNN` with year: true) document number:
|
|
33
|
+
* max numeric suffix +1 among the current year's live rows. Not a
|
|
33
34
|
* reservation — the partial-unique on number turns races into a 409. */
|
|
34
35
|
export async function nextNumber(
|
|
35
36
|
db: CrudDb,
|
|
36
37
|
table: NumberedTable,
|
|
37
38
|
prefix: string,
|
|
38
|
-
opts: { orgId?: string; base?: number } = {},
|
|
39
|
+
opts: { orgId?: string; base?: number; year?: boolean; pad?: number } = {},
|
|
39
40
|
): Promise<string> {
|
|
40
41
|
const q = db.select({ number: table.number }).from(table);
|
|
41
|
-
const rows = opts.orgId ? await q.where(eq(table.organizationId, opts.orgId)) : await q;
|
|
42
|
+
const rows = opts.orgId && table.organizationId ? await q.where(eq(table.organizationId, opts.orgId)) : await q;
|
|
43
|
+
const withYear = opts.year === true;
|
|
44
|
+
const year = new Date().getFullYear();
|
|
45
|
+
const pat = withYear ? new RegExp(`^${prefix}-${year}-(\\d+)$`) : new RegExp(`^${prefix}-(\\d+)$`);
|
|
42
46
|
const max = rows.reduce((m, r) => {
|
|
43
|
-
const n =
|
|
47
|
+
const n = pat.exec(String(r.number ?? ''))?.[1];
|
|
44
48
|
return n ? Math.max(m, Number(n)) : m;
|
|
45
|
-
}, opts.base ?? 2000);
|
|
46
|
-
|
|
49
|
+
}, opts.base ?? (withYear ? 0 : 2000));
|
|
50
|
+
const n = max + 1;
|
|
51
|
+
return withYear ? `${prefix}-${year}-${String(n).padStart(opts.pad ?? 4, '0')}` : `${prefix}-${n}`;
|
|
47
52
|
}
|