Files
xenia-pos-local/waiter_pwa/src/db/posdb.js
T
bonaminandClaude Opus 5.5 18012c2c95 feat(waiter): configurable server/venue layer for native app and plain-HTTP browser mode
New waiter_pwa/src/config/server.js is the single place that knows where the
backend is. Served by the venue's server (https domain or http://<LAN IP>)
nothing changes: same-origin URLs and the original storage keys, so existing
installs keep their token and unsynced offline queue. With an active venue
(native app, or dev builds with VITE_SERVER_URL) URLs become absolute and all
venue data is namespaced by siteId: token/savedUsername keys, the Dexie DB
(pos_snapshot__<siteId>, which also covers the WS cursor), favorites and
table-view prefs. Switching venue reloads the app.

- api client baseURL, WebSocket and SSE URLs routed through the layer
- product images / waiter avatars rendered via assetUrl()
- service-worker update prompt skipped in native builds
- InstallAppBanner: shown only in plain-HTTP browser mode and only when
  VITE_APP_DOWNLOAD_URL is set at build time (dismiss for 7 days)
- VITE_SERVER_URL override is DEV-only (a URL-controlled server in prod would
  let a crafted link capture PINs)
- pack README: rule CS-8 on never assuming same-origin

Verified with Playwright/Edge against a local backend: prod build same-origin,
prod build via LAN IP over plain HTTP (insecure context, no SW, banner shown),
and dev build pointed at the backend by URL - all three log in, reach /tables
and receive the WebSocket 'ready' frame; storage keys and IndexedDB names are
as expected. Lint: no new problems (103 before/after). Build passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 11:38:42 +03:00

121 lines
4.3 KiB
JavaScript

import Dexie from 'dexie'
import { dbName } from '../config/server'
// NOTE: do NOT use 'tables' as a store name — Dexie 4.x has a built-in
// `.tables` getter on the prototype that returns all registered stores as an
// array, so an own-property store named 'tables' gets shadowed by it.
// We use pos_ prefix on store names to avoid all such collisions.
// One database per venue (see config/server.js); same-origin keeps 'pos_snapshot'.
const db = new Dexie(dbName())
db.version(1).stores({
tables: 'id, group_id, is_active',
orders: 'id, table_id, status',
offline_payments: '++localId, uuid, synced',
})
db.version(2).stores({
tables: 'id, group_id, is_active',
orders: 'id, table_id, status',
offline_payments: '++localId, uuid, synced',
products: 'id, category_id, is_available',
categories: 'id, parent_id',
})
db.version(3).stores({
tables: 'id, group_id, is_active',
orders: 'id, table_id, status',
offline_payments: '++localId, uuid, synced',
products: 'id, category_id, is_available',
categories: 'id, parent_id',
offline_orders: '++localId, uuid, synced, table_id',
})
db.version(4).stores({
tables: 'id, group_id, is_active',
orders: 'id, table_id, status',
offline_payments: '++localId, uuid, synced',
products: 'id, category_id, is_available',
categories: 'id, parent_id',
offline_orders: '++localId, uuid, synced, table_id',
table_groups: 'id',
flag_defs: 'id',
flag_assignments: '++localId, table_id, flag_id',
})
// Version 5: rename 'tables' → 'pos_tables' and 'orders' → 'pos_orders'
db.version(5).stores({
tables: null,
orders: null,
offline_payments: '++localId, uuid, synced',
products: 'id, category_id, is_available',
categories: 'id, parent_id',
offline_orders: '++localId, uuid, synced, table_id',
table_groups: 'id',
flag_defs: 'id',
flag_assignments: '++localId, table_id, flag_id',
pos_tables: 'id, group_id, is_active',
pos_orders: 'id, table_id, status',
}).upgrade(tx => {
return tx.table('tables').toArray().then(rows => {
if (rows.length > 0) return tx.table('pos_tables').bulkPut(rows)
}).then(() =>
tx.table('orders').toArray().then(rows => {
if (rows.length > 0) return tx.table('pos_orders').bulkPut(rows)
})
).catch(() => {})
})
// Version 6: add offline_ops queue for splits and payments taken offline
db.version(6).stores({
tables: null,
orders: null,
offline_payments: '++localId, uuid, synced',
products: 'id, category_id, is_available',
categories: 'id, parent_id',
offline_orders: '++localId, uuid, synced, table_id',
table_groups: 'id',
flag_defs: 'id',
flag_assignments: '++localId, table_id, flag_id',
pos_tables: 'id, group_id, is_active',
pos_orders: 'id, table_id, status',
offline_ops: '++localId, orderId, synced, seq',
})
// Version 7: add kv store for persisting WebSocket cursor (last seen seq_id)
db.version(7).stores({
tables: null,
orders: null,
offline_payments: '++localId, uuid, synced',
products: 'id, category_id, is_available',
categories: 'id, parent_id',
offline_orders: '++localId, uuid, synced, table_id',
table_groups: 'id',
flag_defs: 'id',
flag_assignments: '++localId, table_id, flag_id',
pos_tables: 'id, group_id, is_active',
pos_orders: 'id, table_id, status',
offline_ops: '++localId, orderId, synced, seq',
kv: 'key',
})
// Version 8: add uuid index to offline_ops (needed for legacy-drain dedup check)
db.version(8).stores({
tables: null,
orders: null,
offline_payments: '++localId, uuid, synced',
products: 'id, category_id, is_available',
categories: 'id, parent_id',
offline_orders: '++localId, uuid, synced, table_id',
table_groups: 'id',
flag_defs: 'id',
flag_assignments: '++localId, table_id, flag_id',
pos_tables: 'id, group_id, is_active',
pos_orders: 'id, table_id, status',
offline_ops: '++localId, uuid, orderId, synced, seq',
kv: 'key',
})
export default db