Repository navigation
Commit c70ff8e
committed
feat(core): add ULID identifiers and the org/location/register/device model
Phase 1 foundations for multi-device, offline and eventual synchronisation.
All three migrations are strictly additive — no existing table, column or row
is modified, so no current hospitality workflow is affected.
## main/core/ids.ts — collision-safe identifiers
orders/order_items/bills use INTEGER PRIMARY KEY AUTOINCREMENT. Two tills
trading offline both allocate from 1, and when they converge two different
sales claim one identity — nothing downstream can tell them apart.
ULID over UUIDv4 because both are collision-safe without coordination, but
ULID also sorts lexicographically by time: SQLite indexes stay dense instead
of scattering inserts, "recent sales" and future sync cursors become range
scans on the key itself, and logs are readable. Implemented in-repo rather
than adding a dependency — the spec is short and stable, it is financial
identity code a commercial product should own, and it keeps the licence-clean
dependency tree we just achieved. Randomness is crypto.randomBytes, never
Math.random.
## Migration v68 — Organization > Location > Register > Device
A till's local database holds exactly ONE organization, location and register:
single-tenant by construction, because a shop-floor machine must be physically
incapable of holding another merchant's data. Multi-tenancy proper (many
organizations in one database) is a CLOUD concern and is not modelled here.
Seeded from existing settings, with settings pointers so "which register am I"
is one cheap read.
## Migration v69 — uid columns, not a primary-key conversion
Converting the primary keys outright would break every foreign key, join,
report and the KDS WebSocket contract in a single commit. Instead each row
gains a ULID `uid` beside its existing key: current code is untouched, new
distributed code keys on uid. The backfill seeds each ULID's timestamp from
the row's own created_at, so generated ids sort in true historical order
rather than clustering at migration time.
The unique index is PARTIAL (WHERE uid IS NOT NULL) on purpose: unique because
a duplicate would silently merge two distinct sales during a future sync,
partial so insert paths not yet taught to populate uid keep working.
## Migration v70 — audit_events
One narrow append-only table, not an audit framework: who did what, when,
where. The inherited codebase only has fragments (tax_config_audit,
print_logs, payment_transaction_ref_conflicts) with no common shape, and a
commercial EPOS must be able to answer "who authorised this refund".
## Verification
New: tests/plemmo-identifiers.test.ts (55 checks) — format, 100k-ID
uniqueness, two independent offline generators producing zero collisions,
monotonic ordering, a backwards clock step still yielding increasing ids, the
seeded hierarchy with a clean foreign_key_check, and the partial unique index
actually rejecting a duplicate while still allowing NULLs.
Extended tests/upgrade-path.test.ts to assert the backfill against the REAL
v1.5.0-era fixture database — every legacy row gets a distinct well-formed
ULID that sorts in created_at order — which is where a backfill normally
breaks. schema-health still reports zero drift between a migrated old install
and a fresh one, so both paths converge on an identical schema.
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011HjBK8R8ArAjzGRLS7gjUj1 parent fe54969 commit c70ff8e
5 files changed
Lines changed: 609 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
0 commit comments