-- Visitor Sign-In database schema -- Source: https://personalsoftwarefactory.com/blueprints/visitor-sign-in (data model, section 5 of the blueprint) -- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite). All timestamps are UTC. -- License: MIT. Free to copy and adapt. -- The single settings row. id is fixed at 1 so the table can never hold a second row. CREATE TABLE settings ( id INTEGER PRIMARY KEY CHECK (id = 1), office_name TEXT NOT NULL, address TEXT, time_zone TEXT NOT NULL, -- IANA name; invite dates, automatic sign-out and the export use this zone retention_days INTEGER NOT NULL DEFAULT 90 CHECK (retention_days >= 1), -- visits signed out longer ago than this are anonymised auto_sign_out_time TEXT NOT NULL DEFAULT '19:00' CHECK (length(auto_sign_out_time) = 5 AND substr(auto_sign_out_time, 3, 1) = ':'), -- HH:MM in the office time zone privacy_notice TEXT, -- shown on the kiosk before Sign in reception_email TEXT, -- told about visits whose type needs no host badge_size TEXT NOT NULL DEFAULT '62 x 100 mm', -- width x height of the label, used as the CSS page size slack_webhook_url TEXT -- optional; a secret, never shown back in full ); -- A staff member with an account. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE CHECK (email = lower(email)), -- stored lowercase role TEXT NOT NULL CHECK (role IN ('admin', 'staff')), active BOOLEAN NOT NULL DEFAULT TRUE ); -- A reception tablet that signs in once with its token and can use only the kiosk. -- App rule (R8): a device can create visits, use invites, sign visits out and make the few reads those need, nothing else. CREATE TABLE devices ( id TEXT PRIMARY KEY, name TEXT NOT NULL, token TEXT NOT NULL UNIQUE, -- long random secret; the kiosk opens /kiosk with it once active BOOLEAN NOT NULL DEFAULT TRUE ); -- A document visitors sign, such as an NDA. Its text lives in document_versions. CREATE TABLE documents ( id TEXT PRIMARY KEY, title TEXT NOT NULL, active_version INTEGER NOT NULL CHECK (active_version >= 1), -- the version shown to sign. No foreign key: document_versions points back here (a cycle). re_sign_after_days INTEGER NOT NULL CHECK (re_sign_after_days >= 0) -- a visitor who signed the active version within this many days is not asked again ); -- One published version of a document. Versions are never edited. -- App rule (R2): publishing an edit creates the next version and makes it active. CREATE TABLE document_versions ( document_id TEXT NOT NULL REFERENCES documents(id), version INTEGER NOT NULL CHECK (version >= 1), -- 1, 2, 3... body TEXT NOT NULL, -- Markdown published_at TIMESTAMP NOT NULL, PRIMARY KEY (document_id, version) ); -- A kind of visit (Guest, Interview, Contractor, Delivery) and what the kiosk asks for it. CREATE TABLE visit_types ( id TEXT PRIMARY KEY, name TEXT NOT NULL, requires_host BOOLEAN NOT NULL, document_id TEXT REFERENCES documents(id), -- the document to sign, if any prints_badge BOOLEAN NOT NULL, asks_company BOOLEAN NOT NULL, -- the company field is shown; it is optional even then active BOOLEAN NOT NULL DEFAULT TRUE ); -- A visit a staff member arranged in advance, with a code the visitor scans at the kiosk. -- App rule (R5): the code works once, on the expected date in the office time zone only. -- App rule (R7): invites older than retention_days are anonymised; the name becomes Visitor and personal fields are cleared. CREATE TABLE invites ( id TEXT PRIMARY KEY, visitor_name TEXT NOT NULL, visitor_email TEXT, -- required when inviting; cleared when anonymised company TEXT, host_id TEXT NOT NULL REFERENCES people(id), visit_type_id TEXT NOT NULL REFERENCES visit_types(id), expected_at TIMESTAMP NOT NULL, code TEXT NOT NULL UNIQUE, -- random; shown as a QR code in the invite email status TEXT NOT NULL DEFAULT 'expected' CHECK (status IN ('expected', 'arrived', 'cancelled', 'no_show')) ); CREATE INDEX invites_host ON invites (host_id, expected_at); -- One visit: a person signed in at the kiosk, on site until signed out. -- App rule (R1): a host is required when the visit type requires one; the email must be valid when given. -- App rule (section 13): signing in again with the same email signs the earlier open visit out with method kiosk. -- App rule (R7): visits signed out more than retention_days ago are anonymised: name becomes Visitor, email and company are cleared. CREATE TABLE visits ( id TEXT PRIMARY KEY, visitor_name TEXT NOT NULL CHECK (length(visitor_name) BETWEEN 2 AND 80), -- for a delivery, the driver visitor_email TEXT, -- optional, except when the visit comes from an invite company TEXT, -- for a delivery, the carrier visit_type_id TEXT NOT NULL REFERENCES visit_types(id), host_id TEXT REFERENCES people(id), -- NULL for types that need no host invite_id TEXT UNIQUE REFERENCES invites(id), -- an invite is used at most once signed_in_at TIMESTAMP NOT NULL, signed_out_at TIMESTAMP, -- NULL while on site; for automatic sign-out, auto_sign_out_time on the day of the visit sign_out_method TEXT CHECK (sign_out_method IN ('kiosk', 'badge', 'host', 'admin', 'auto')), badge_code TEXT UNIQUE, -- random; printed as a QR code on the badge anonymized_at TIMESTAMP, CHECK (sign_out_method IS NULL OR signed_out_at IS NOT NULL), CHECK (signed_out_at IS NULL OR signed_out_at >= signed_in_at) ); CREATE INDEX visits_on_site ON visits (signed_in_at) WHERE signed_out_at IS NULL; CREATE INDEX visits_host ON visits (host_id, signed_in_at); CREATE INDEX visits_email ON visits (visitor_email); -- One document signed during one visit. Never edited or deleted; anonymising clears the typed name. -- App rule (section 6): typed_name must match the visitor name, ignoring case and extra spaces. -- App rule (R3): a visitor signs again unless they signed the active version within re_sign_after_days, looked up by email. CREATE TABLE signatures ( id TEXT PRIMARY KEY, visit_id TEXT NOT NULL REFERENCES visits(id), visitor_email TEXT, -- copied from the visit, for the R3 lookup document_id TEXT NOT NULL, version INTEGER NOT NULL, typed_name TEXT, -- cleared when anonymised signed_at TIMESTAMP NOT NULL, FOREIGN KEY (document_id, version) REFERENCES document_versions(document_id, version) ); CREATE INDEX signatures_email ON signatures (visitor_email, document_id, version); -- One change, written by every action. Append-only. -- App rule (R10): events are never edited or deleted. A run that changes nothing writes no event. CREATE TABLE events ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, actor TEXT NOT NULL, -- a person id, a device id, or the job type TEXT NOT NULL, -- e.g. visit_signed_in, visitor_details_deleted data TEXT -- details as JSON text; a deletion records who and how many records, never the email ); -- One email in the outbox. Every email is written here first, then sent. -- App rule (R7): outbox emails older than retention_days are blanked. CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL, -- which trigger in section 10, e.g. visitor_arrived, invite_created to_email TEXT NOT NULL, subject TEXT NOT NULL, body TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'failed', 'logged')), -- logged when no email provider is configured error TEXT );