@umituz/web-hono 1.7.5 → 1.7.6

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/org.ts +19 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/web-hono",
3
- "version": "1.7.5",
3
+ "version": "1.7.6",
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\u2192client 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/org.ts CHANGED
@@ -31,12 +31,22 @@ export async function orgHasMode(db: CrudDb, orgs: OrgsLike, orgId: string, mode
31
31
 
32
32
  /** Next `PREFIX-n` (or `PREFIX-YYYY-NNNN` with year: true) document number:
33
33
  * max numeric suffix +1 among the current year's live rows. Not a
34
- * reservation — the partial-unique on number turns races into a 409. */
34
+ * reservation — the partial-unique on number turns races into a 409.
35
+ * digitsOnly: true serves prefix-less columns that store the bare integer
36
+ * ("101") — prefix is ignored, non-numeric rows don't count. */
35
37
  export async function nextNumber(
36
38
  db: CrudDb,
37
39
  table: NumberedTable,
38
40
  prefix: string,
39
- opts: { orgId?: string; base?: number; year?: boolean; pad?: number; column?: PgColumn } = {},
41
+ opts: {
42
+ orgId?: string;
43
+ base?: number;
44
+ year?: boolean;
45
+ pad?: number;
46
+ column?: PgColumn;
47
+ digitsOnly?: boolean;
48
+ separator?: string;
49
+ } = {},
40
50
  ): Promise<string> {
41
51
  const numberCol = opts.column ?? table.number;
42
52
  if (!numberCol) throw new Error('nextNumber: no number column (pass opts.column for orderNo-style tables)');
@@ -47,13 +57,16 @@ export async function nextNumber(
47
57
  .select({ number: numberCol })
48
58
  .from(table)
49
59
  .where(conds.length > 0 ? and(...conds) : undefined);
50
- const withYear = opts.year === true;
60
+ const bare = opts.digitsOnly === true;
61
+ const withYear = !bare && opts.year === true;
51
62
  const year = new Date().getFullYear();
52
- const pat = withYear ? new RegExp(`^${prefix}-${year}-(\\d+)$`) : new RegExp(`^${prefix}-(\\d+)$`);
63
+ const sep = bare ? '' : (opts.separator ?? '-').replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
64
+ const pat = bare ? /^(\d+)$/ : withYear ? new RegExp(`^${prefix}-${year}-(\\d+)$`) : new RegExp(`^${prefix}${sep}(\\d+)$`);
53
65
  const max = rows.reduce((m, r) => {
54
66
  const n = pat.exec(String(r.number ?? ''))?.[1];
55
67
  return n ? Math.max(m, Number(n)) : m;
56
- }, opts.base ?? (withYear ? 0 : 2000));
68
+ }, opts.base ?? (bare ? 0 : withYear ? 0 : 2000));
57
69
  const n = max + 1;
58
- return withYear ? `${prefix}-${year}-${String(n).padStart(opts.pad ?? 4, '0')}` : `${prefix}-${n}`;
70
+ if (bare) return opts.pad ? String(n).padStart(opts.pad, '0') : String(n);
71
+ return withYear ? `${prefix}-${year}-${String(n).padStart(opts.pad ?? 4, '0')}` : `${prefix}${opts.separator ?? '-'}${n}`;
59
72
  }