Datenbankschema: Kassen-App
Die Tabellen hinter der App, als SQL, als ER-Diagramm und als Prisma-Schema.
Kassen-App als fertige Vorlage
Das Datenbankschema
Erstellt vom kostenlosen Datenbank-Schema-Generator, nach einer Kurzbeschreibung der App.
Das Modell dreht sich um Verkäufe (sale), die aus Positionen bestehen und per Zahlung abgeschlossen werden. Produkte hängen an Kategorien, Mitarbeitende melden sich an und führen Verkäufe aus; Preise und Namen werden in der Verkaufsposition festgehalten, damit Belege später unverändert bleiben.
Tabellen
staff_user: ein Mitarbeitender oder eine Leitungsperson mit Login und Rolle.category: eine Produktkategorie, nach der der Kassenbildschirm gruppiert.product: ein verkaufbares Produkt mit aktuellem Preis, zugeordnet zu einer Kategorie.sale: ein Verkauf (Warenkorb bzw. abgeschlossener Beleg) mit Belegnummer, Status und Summe.sale_item: eine Position eines Verkaufs mit Menge sowie Name und Preis zum Verkaufszeitpunkt.payment: eine erfasste (simulierte) Bar- oder Kartenzahlung zu einem Verkauf.
SQL (PostgreSQL)
CREATE TABLE staff_user (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
email text NOT NULL UNIQUE,
password_hash text NOT NULL,
name text NOT NULL,
role text NOT NULL DEFAULT 'staff' CHECK (role IN ('staff', 'manager')),
is_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),
is_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(),
receipt_number bigint GENERATED ALWAYS AS IDENTITY UNIQUE,
staff_user_id uuid NOT NULL REFERENCES staff_user(id) ON DELETE RESTRICT, -- rings up
status text NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'paid', 'cancelled')),
total_cents integer NOT NULL DEFAULT 0 CHECK (total_cents >= 0),
paid_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 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 settled by
method text NOT NULL CHECK (method IN ('cash', 'card')),
amount_cents integer NOT NULL CHECK (amount_cents > 0),
cash_given_cents integer CHECK (cash_given_cents >= 0),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX idx_product_category ON product (category_id);
CREATE INDEX idx_sale_paid_at ON sale (paid_at);
CREATE INDEX idx_sale_item_product ON sale_item (product_id);
CREATE INDEX idx_payment_sale ON payment (sale_id);
ERD
erDiagram
staff_user {
uuid id PK
text email
text password_hash
text name
text role
boolean is_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 is_active
timestamptz created_at
timestamptz updated_at
}
sale {
uuid id PK
bigint receipt_number
uuid staff_user_id FK
text status
integer total_cents
timestamptz paid_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
integer cash_given_cents
timestamptz created_at
timestamptz updated_at
}
category ||--o{ product : "groups"
staff_user ||--o{ sale : "rings up"
sale ||--o{ sale_item : "contains"
product ||--o{ sale_item : "is sold as"
sale ||--o{ payment : "is settled by"
Prisma
model StaffUser {
id String @id @default(uuid()) @db.Uuid
email String @unique
passwordHash String @map("password_hash")
name String
role String @default("staff")
isActive Boolean @default(true) @map("is_active")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
sales Sale[]
@@map("staff_user")
}
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")
isActive Boolean @default(true) @map("is_active")
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
receiptNumber BigInt @unique @default(autoincrement()) @map("receipt_number")
staffUserId String @map("staff_user_id") @db.Uuid
status String @default("open")
totalCents Int @default(0) @map("total_cents")
paidAt DateTime? @map("paid_at") @db.Timestamptz
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
staffUser StaffUser @relation(fields: [staffUserId], references: [id], onDelete: Restrict)
items SaleItem[]
payments Payment[]
@@index([paidAt])
@@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
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")
cashGivenCents Int? @map("cash_given_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")
}