@umituz/web-hono 1.10.0 → 1.10.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/docstore.ts +13 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/web-hono",
3
- "version": "1.10.0",
3
+ "version": "1.10.1",
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
@@ -83,9 +83,13 @@ export function canTransition(map: Record<string, string[]>, from: string, to: s
83
83
  return from === to || (map[from] ?? []).includes(to);
84
84
  }
85
85
 
86
- /** ISO string → epoch ms; anything unparsable falls back to now. */
86
+ /** ISO string → epoch ms; anything unparsable falls back to now. The
87
+ * epoch-0 instant (1970-01-01T00:00:00Z) parses to 0 and must stay 0 —
88
+ * a truthiness check would silently rewrite it to "now". */
87
89
  export function epoch(iso: unknown): number {
88
- return typeof iso === 'string' && Date.parse(iso) ? Date.parse(iso) : Date.now();
90
+ if (typeof iso !== 'string') return Date.now();
91
+ const ms = Date.parse(iso);
92
+ return Number.isNaN(ms) ? Date.now() : ms;
89
93
  }
90
94
 
91
95
  function defaultDb(c: RequestContext): CrudDb {
@@ -145,12 +149,14 @@ export function docStore(spec: DocStoreSpec) {
145
149
  return and(...conds);
146
150
  }
147
151
 
148
- function rowValues(col: DocCollectionSpec, doc: DocInput, nowMs: number): Record<string, unknown> {
152
+ function rowValues(col: DocCollectionSpec, doc: DocInput): Record<string, unknown> {
149
153
  const id = docIdOf(col, doc);
150
154
  const values: Record<string, unknown> = {
151
155
  id,
152
- createdAt: epoch(doc.createdAt) || nowMs,
153
- updatedAt: epoch(doc.updatedAt) || nowMs,
156
+ // epoch() already falls back to now for unparsable input; the extra
157
+ // `|| now` would rewrite a legitimate 0 (epoch-0 instant) to now.
158
+ createdAt: epoch(doc.createdAt),
159
+ updatedAt: epoch(doc.updatedAt),
154
160
  deletedAt: null as number | null,
155
161
  data: doc,
156
162
  ...col.columns?.(doc),
@@ -185,7 +191,7 @@ export function docStore(spec: DocStoreSpec) {
185
191
  }
186
192
  await col.validate?.({ db, doc, existing });
187
193
  }
188
- const values = rowValues(col, doc, Date.now());
194
+ const values = rowValues(col, doc);
189
195
  await db
190
196
  .insert(col.table)
191
197
  .values(values as never)
@@ -203,7 +209,7 @@ export function docStore(spec: DocStoreSpec) {
203
209
  const col = requireSpec(key);
204
210
  let count = 0;
205
211
  for (let i = 0; i < docs.length; i += chunkSize) {
206
- const chunk = docs.slice(i, i + chunkSize).map((doc) => rowValues(col, doc, Date.now()));
212
+ const chunk = docs.slice(i, i + chunkSize).map((doc) => rowValues(col, doc));
207
213
  await db
208
214
  .insert(col.table)
209
215
  .values(chunk as never)