POS app database schema
The tables behind the POS app, as SQL, as an ER diagram and as a Prisma schema.
A working POS app to start from
The database schema
Written by the free Database Schema Generator from a one-paragraph brief of the app.
The model centres on sale, which holds line items, simulated payments and a receipt, and is rung up by a staff member. Products sit in categories, and prices are copied onto each line item so later price changes never alter past sales or reports.
Tables
staff: one person who can sign in to the till, with a role of either cashier or manager.category: one product group shown on the till screen.product: one item for sale, belonging to a category and carrying its current price.sale: one checkout by a staff member, from open cart to completed or voided, with its totals.sale_item: one product line within a sale, with the quantity and the name and price at the time of sale.payment: one simulated cash or card payment recorded against a sale.receipt: the numbered receipt issued for a completed sale, so it can be viewed again.
SQL (PostgreSQL)
CREATE TABLE staff (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name text NOT NULL,
email text NOT NULL UNIQUE,
password_hash text NOT NULL,
role text NOT NULL DEFAULT 'cashier' CHECK (role IN ('cashier', 'manager')),
active boolean NOT NULL DEFAULT true,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE category (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name text NOT NULL UNIQUE,
sort_order integer NOT NULL DEFAULT 0,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE product (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
category_id uuid NOT NULL REFERENCES category(id) ON DELETE RESTRICT, -- groups
name text NOT NULL,
price_cents integer NOT NULL CHECK (price_cents >= 0),
active boolean NOT NULL DEFAULT true,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE sale (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
staff_id uuid NOT NULL REFERENCES staff(id) ON DELETE RESTRICT, -- rings up
status text NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'completed', 'voided')),
total_cents integer NOT NULL DEFAULT 0 CHECK (total_cents >= 0),
completed_at timestamptz,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE sale_item (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
sale_id uuid NOT NULL REFERENCES sale(id) ON DELETE CASCADE, -- contains
product_id uuid NOT NULL REFERENCES product(id) ON DELETE RESTRICT, -- is sold as
product_name text NOT NULL,
unit_price_cents integer NOT NULL CHECK (unit_price_cents >= 0),
quantity integer NOT NULL DEFAULT 1 CHECK (quantity > 0),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (sale_id, product_id)
);
CREATE TABLE payment (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
sale_id uuid NOT NULL REFERENCES sale(id) ON DELETE CASCADE, -- is paid by
method text NOT NULL CHECK (method IN ('cash', 'card')),
amount_cents integer NOT NULL CHECK (amount_cents > 0),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE receipt (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
sale_id uuid NOT NULL UNIQUE REFERENCES sale(id) ON DELETE CASCADE, -- is documented by
receipt_number bigint GENERATED ALWAYS AS IDENTITY UNIQUE,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX product_category_id_idx ON product (category_id);
CREATE INDEX sale_completed_at_idx ON sale (completed_at);
CREATE INDEX sale_item_product_id_idx ON sale_item (product_id);
CREATE INDEX payment_sale_id_idx ON payment (sale_id);
ERD
erDiagram
staff {
uuid id PK
text name
text email
text password_hash
text role
boolean active
timestamptz created_at
timestamptz updated_at
}
category {
uuid id PK
text name
integer sort_order
timestamptz created_at
timestamptz updated_at
}
product {
uuid id PK
uuid category_id FK
text name
integer price_cents
boolean active
timestamptz created_at
timestamptz updated_at
}
sale {
uuid id PK
uuid staff_id FK
text status
integer total_cents
timestamptz completed_at
timestamptz created_at
timestamptz updated_at
}
sale_item {
uuid id PK
uuid sale_id FK
uuid product_id FK
text product_name
integer unit_price_cents
integer quantity
timestamptz created_at
timestamptz updated_at
}
payment {
uuid id PK
uuid sale_id FK
text method
integer amount_cents
timestamptz created_at
timestamptz updated_at
}
receipt {
uuid id PK
uuid sale_id FK
bigint receipt_number
timestamptz created_at
timestamptz updated_at
}
category ||--o{ product : "groups"
staff ||--o{ sale : "rings up"
sale ||--o{ sale_item : "contains"
product ||--o{ sale_item : "is sold as"
sale ||--o{ payment : "is paid by"
sale ||--o| receipt : "is documented by"
Prisma
model Staff {
id String @id @default(uuid()) @db.Uuid
name String
email String @unique
passwordHash String @map("password_hash")
role String @default("cashier")
active Boolean @default(true)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
sales Sale[]
@@map("staff")
}
model Category {
id String @id @default(uuid()) @db.Uuid
name String @unique
sortOrder Int @default(0) @map("sort_order")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
products Product[]
@@map("category")
}
model Product {
id String @id @default(uuid()) @db.Uuid
categoryId String @map("category_id") @db.Uuid
name String
priceCents Int @map("price_cents")
active Boolean @default(true)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
category Category @relation(fields: [categoryId], references: [id], onDelete: Restrict)
saleItems SaleItem[]
@@index([categoryId])
@@map("product")
}
model Sale {
id String @id @default(uuid()) @db.Uuid
staffId String @map("staff_id") @db.Uuid
status String @default("open")
totalCents Int @default(0) @map("total_cents")
completedAt DateTime? @map("completed_at") @db.Timestamptz
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
staff Staff @relation(fields: [staffId], references: [id], onDelete: Restrict)
items SaleItem[]
payments Payment[]
receipt Receipt?
@@index([completedAt])
@@map("sale")
}
model SaleItem {
id String @id @default(uuid()) @db.Uuid
saleId String @map("sale_id") @db.Uuid
productId String @map("product_id") @db.Uuid
productName String @map("product_name")
unitPriceCents Int @map("unit_price_cents")
quantity Int @default(1)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
sale Sale @relation(fields: [saleId], references: [id], onDelete: Cascade)
product Product @relation(fields: [productId], references: [id], onDelete: Restrict)
@@unique([saleId, productId])
@@index([productId])
@@map("sale_item")
}
model Payment {
id String @id @default(uuid()) @db.Uuid
saleId String @map("sale_id") @db.Uuid
method String
amountCents Int @map("amount_cents")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
sale Sale @relation(fields: [saleId], references: [id], onDelete: Cascade)
@@index([saleId])
@@map("payment")
}
model Receipt {
id String @id @default(uuid()) @db.Uuid
saleId String @unique @map("sale_id") @db.Uuid
receiptNumber BigInt @unique @default(autoincrement()) @map("receipt_number")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
sale Sale @relation(fields: [saleId], references: [id], onDelete: Cascade)
@@map("receipt")
}