@umituz/web-hono 1.8.0 → 1.8.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/docstore.ts +27 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-hono",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.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/docstore.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Hono } from 'hono';
|
|
2
|
-
import { and, eq, isNull } from 'drizzle-orm';
|
|
2
|
+
import { and, eq, isNull, sql } from 'drizzle-orm';
|
|
3
3
|
import { drizzle } from 'drizzle-orm/neon-http';
|
|
4
4
|
import { neon } from '@neondatabase/serverless';
|
|
5
5
|
import { validator } from 'hono/validator';
|
|
@@ -40,6 +40,8 @@ export interface DocCollectionSpec {
|
|
|
40
40
|
singletonId?: string;
|
|
41
41
|
/** Status state machine: allowed targets per current status. */
|
|
42
42
|
transitions?: Record<string, string[]>;
|
|
43
|
+
/** Doc field the transitions guard reads. Default 'status' (fuelora: state, franchisia: stage). */
|
|
44
|
+
statusKey?: string;
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
export interface DocStoreSpec {
|
|
@@ -101,14 +103,15 @@ export function docStore(spec: DocStoreSpec) {
|
|
|
101
103
|
}
|
|
102
104
|
const guard = col.transitions;
|
|
103
105
|
if (guard) {
|
|
106
|
+
const statusKey = col.statusKey ?? 'status';
|
|
104
107
|
const [existing] = await db
|
|
105
108
|
.select({ data: col.table.data })
|
|
106
109
|
.from(col.table)
|
|
107
110
|
.where(and(eq(col.table.id, String(doc.id)), isNull(col.table.deletedAt)))
|
|
108
111
|
.limit(1);
|
|
109
112
|
if (existing) {
|
|
110
|
-
const from = (existing.data as Doc)
|
|
111
|
-
const to = doc
|
|
113
|
+
const from = (existing.data as Doc)[statusKey];
|
|
114
|
+
const to = doc[statusKey];
|
|
112
115
|
if (typeof from === 'string' && typeof to === 'string' && !canTransition(guard, from, to)) {
|
|
113
116
|
throw new WorkflowError(`Invalid ${key} transition ${from} → ${to}`);
|
|
114
117
|
}
|
|
@@ -124,6 +127,26 @@ export function docStore(spec: DocStoreSpec) {
|
|
|
124
127
|
});
|
|
125
128
|
}
|
|
126
129
|
|
|
130
|
+
/** Trusted bulk load for seed scripts: chunked multi-row upsert with the
|
|
131
|
+
* same revive semantics as putDoc, without per-row workflow/singleton
|
|
132
|
+
* guards (seeds write fresh, consistent datasets by construction). */
|
|
133
|
+
async function putDocs(db: CrudDb, key: string, docs: readonly Doc[], chunkSize = 200): Promise<number> {
|
|
134
|
+
const col = requireSpec(key);
|
|
135
|
+
let count = 0;
|
|
136
|
+
for (let i = 0; i < docs.length; i += chunkSize) {
|
|
137
|
+
const chunk = docs.slice(i, i + chunkSize).map((doc) => rowValues(col, doc, Date.now()));
|
|
138
|
+
await db
|
|
139
|
+
.insert(col.table)
|
|
140
|
+
.values(chunk as never)
|
|
141
|
+
.onConflictDoUpdate({
|
|
142
|
+
target: [col.table.id],
|
|
143
|
+
set: { updatedAt: sql`excluded.updated_at`, deletedAt: null, data: sql`excluded.data` } as never,
|
|
144
|
+
});
|
|
145
|
+
count += chunk.length;
|
|
146
|
+
}
|
|
147
|
+
return count;
|
|
148
|
+
}
|
|
149
|
+
|
|
127
150
|
async function patchDoc(db: CrudDb, key: string, id: string, patch: Doc): Promise<Doc | undefined> {
|
|
128
151
|
requireSpec(key);
|
|
129
152
|
const col = byKey.get(key)!;
|
|
@@ -274,6 +297,7 @@ export function docStore(spec: DocStoreSpec) {
|
|
|
274
297
|
router,
|
|
275
298
|
collectionOf,
|
|
276
299
|
putDoc,
|
|
300
|
+
putDocs,
|
|
277
301
|
patchDoc,
|
|
278
302
|
softDeleteDoc,
|
|
279
303
|
listDocs,
|