Skip to content

Database schema

24 tables in src/server/db/schema.ts, with the migrations in migrations/. Money is integer cents, dates are ISO text and shares are basis points; the column helpers are in src/server/db/columns.ts:

Helper Stores
id() UUID text, generated in the app
cents() money in integer cents
tenths() one decimal as integer tenths
basisPoints() a share in basis points, 5000 = 50 %
instant() a point in time as epoch milliseconds
isoDate() a calendar date, YYYY-MM-DD
validity() a validity period, ISO dates

Key columns only; every column is in Tables.

The references to user (who owns, created or uploaded a row) are left out of this diagram.

erDiagram
  direction LR
  properties {
    id id PK
  }
  property_access {
    text property_id PK, FK
    text user_id PK
  }
  property_invitations {
    id id PK
    text property_id FK
  }
  ownership_periods {
    id id PK
    text property_id FK
  }
  financing_charges {
    id id PK
    text property_id FK
  }
  recurring_amounts {
    id id PK
    text property_id FK
    text financing_charge_id FK
  }
  financing_treatments {
    id id PK
    text property_id FK
  }
  transactions {
    id id PK
    text property_id FK
  }
  receipts {
    id id PK
    text transaction_id FK, UK
  }
  tenancies {
    id id PK
    text property_id FK
  }
  deposit_returns {
    id id PK
    text tenancy_id FK, UK
  }
  tenancy_rents {
    id id PK
    text tenancy_id FK
  }
  property_documents {
    id id PK
    text property_id FK
    text tenancy_id FK
  }
  usage_charges {
    id id PK
    text property_id FK
  }
  usage_charge_amounts {
    id id PK
    text usage_charge_id FK
  }
  tenancy_occupants {
    id id PK
    text tenancy_id FK
  }
  tenant_fees {
    id id PK
    text tenancy_id FK
  }
  tenant_fee_amounts {
    id id PK
    text tenant_fee_id FK
  }
  rent_payments {
    id id PK
    text tenancy_id FK
    text deposit_return_id FK
  }
  properties ||--o{ property_access : "property_id"
  properties ||--o{ property_invitations : "property_id"
  properties ||--o{ ownership_periods : "property_id"
  properties ||--o{ financing_charges : "property_id"
  properties ||--o{ recurring_amounts : "property_id"
  financing_charges |o--o{ recurring_amounts : "financing_charge_id"
  properties ||--o{ financing_treatments : "property_id"
  properties ||--o{ transactions : "property_id"
  transactions ||--o| receipts : "transaction_id"
  properties ||--o{ tenancies : "property_id"
  tenancies ||--o| deposit_returns : "tenancy_id"
  tenancies ||--o{ tenancy_rents : "tenancy_id"
  properties ||--o{ property_documents : "property_id"
  tenancies |o--o{ property_documents : "tenancy_id"
  properties ||--o{ usage_charges : "property_id"
  usage_charges ||--o{ usage_charge_amounts : "usage_charge_id"
  tenancies ||--o{ tenancy_occupants : "tenancy_id"
  tenancies ||--o{ tenant_fees : "tenancy_id"
  tenant_fees ||--o{ tenant_fee_amounts : "tenant_fee_id"
  tenancies ||--o{ rent_payments : "tenancy_id"
  deposit_returns |o--o{ rent_payments : "deposit_return_id"
erDiagram
  direction LR
  user {
    id id PK
    text email UK
  }
  session {
    id id PK
    text user_id FK
    text token UK
  }
  account {
    id id PK
    text user_id FK
  }
  verification {
    id id PK
  }
  rate_limit {
    text key PK
  }
  user ||--o{ session : "user_id"
  user ||--o{ account : "user_id"

Rows are periods (valid_from / valid_to) that are never overwritten: a change closes the open period and starts the next.

Deleting a row deletes the rows that reference it with ON DELETE CASCADE, and theirs in turn. Deleting a flat relies on this alone.

A row that one of these references can’t be deleted while the reference exists:

Defined in src/server/db/schema.ts:27 as user.

Column Type Keys References Notes
id id() TEXT PK Default generated.
name text not null
email text unique, not null
email_verified integer (boolean) not null Default false.
image text
locale text not null The user’s language: ‘fi’, ‘sv’ or ‘en’.
created_at instant() INTEGER not null
updated_at instant() INTEGER not null

Defined in src/server/db/schema.ts:41 as session.

Column Type Keys References Notes
id id() TEXT PK Default generated.
user_id text not null user.id on delete cascade
token text unique, not null
expires_at instant() INTEGER not null
ip_address text
user_agent text
created_at instant() INTEGER not null
updated_at instant() INTEGER not null
  • index session_user_id_idx on user_id

Linked sign-in methods (passwords, social providers). Unused by email codes. Defined in src/server/db/schema.ts:59 as account.

Column Type Keys References Notes
id id() TEXT PK Default generated.
user_id text not null user.id on delete cascade
account_id text not null
provider_id text not null
access_token text
refresh_token text
id_token text
access_token_expires_at instant() INTEGER
refresh_token_expires_at instant() INTEGER
scope text
password text
created_at instant() INTEGER not null
updated_at instant() INTEGER not null
  • index account_user_id_idx on user_id

One-time codes, stored hashed. Defined in src/server/db/schema.ts:82 as verification.

Column Type Keys References Notes
id id() TEXT PK Default generated.
identifier text not null
value text not null
expires_at instant() INTEGER not null
created_at instant() INTEGER not null
updated_at instant() INTEGER not null
  • index verification_identifier_idx on identifier

Request counters for abuse limits (platform/limits.server.ts), e.g. sign-in code requests per IP address and per hashed email address. Defined in src/server/db/schema.ts:99 as rateLimit.

Column Type Keys References Notes
key text PK
count integer not null
window_start instant() INTEGER not null

Defined in src/server/db/schema.ts:108 as properties.

Column Type Keys References Notes
id id() TEXT PK Default generated.
street_address text not null E.g. “Kuusikuja 3 B 12”.
postcode text not null Five digits, as text: leading zeros.
city text not null
room_count integer not null 1, 2, 3 or 4, where 4 means four rooms or more.
floor_area tenths() INTEGER not null In tenths of m²: 485 = 48,5 m².
housing_company_name text The housing company’s name, e.g. “As Oy Kuusikuja 3”. Form 7H asks for it.
business_id text The housing company’s Business ID (Y-tunnus), “2345678-0”.
apartment_number text The apartment number, e.g. “B 12”.
created_at instant() INTEGER not null
updated_at instant() INTEGER not null

Defined in src/server/db/schema.ts:137 as propertyAccess.

Column Type Keys References Notes
property_id text PK properties.id on delete cascade
user_id text PK user.id on delete cascade
role text not null One of owner, member.
created_at instant() INTEGER not null
archived_at instant() INTEGER When this user archived the flat: hidden from their own views only.
  • primary key on property_id, user_id
  • index property_access_user_id_idx on user_id

Pending invitations to a flat, by email address: the invitee may have no account yet. Accepting turns one into a property_access row; accepted, declined and cancelled invitations are deleted (openspec: sharing). Defined in src/server/db/schema.ts:162 as propertyInvitations.

Column Type Keys References Notes
id id() TEXT PK Default generated.
property_id text not null properties.id on delete cascade
email text not null Lower-cased, as accounts store it.
invited_by text not null user.id on delete cascade
created_at instant() INTEGER not null
expires_at instant() INTEGER not null
  • unique property_invitations_property_email on property_id, email
  • index property_invitations_email_idx on email

Validity periods. Who owns how much of a flat, from when (openspec: ownership). One row per owner and period; a period is the rows with the same valid_from. A change closes the open rows and adds the new split, so rows are never overwritten. A 0 % share has no row: that person is a member. Defined in src/server/db/schema.ts:199 as ownershipPeriods.

Column Type Keys References Notes
id id() TEXT PK Default generated.
property_id text not null properties.id on delete cascade
user_id text not null user.id on delete cascade
share basisPoints() INTEGER not null Basis points: 5000 = 50 %. A period’s shares add up to 10000.
valid_from validity() TEXT not null The first day of the period.
valid_to validity() TEXT The last day of the period; null while it is open-ended.
reason text One of sale, gift, inheritance, division, other. Null for the first period, which records how the flat was acquired.
  • index ownership_periods_property_idx on property_id, valid_from
  • index ownership_periods_user_idx on user_id

A flat’s financing charges (openspec: recurring-amounts): a housing company can collect several at once, e.g. one per renovation loan. Their amounts are periods in recurring_amounts; the treatment is the flat’s, one for all. Defined in src/server/db/schema.ts:236 as financingCharges.

Column Type Keys References Notes
id id() TEXT PK Default generated.
property_id text not null properties.id on delete cascade
name text Null only for the charge each flat got when financing charges became several; the UI shows it as the term’s word in the user’s language.
  • index financing_charges_property_idx on property_id

Validity periods. Charges over time (openspec: recurring-amounts). One row per sequence and period: the maintenance charge, or one financing charge (financing_charge_id, set on every financing_charge row). Every period starts on the first of a month. A change closes the open row the day before and adds the next, so rows are never overwritten. Monthly amounts are computed from these rows, never stored. Defined in src/server/db/schema.ts:260 as recurringAmounts.

Column Type Keys References Notes
id id() TEXT PK Default generated.
property_id text not null properties.id on delete cascade
kind text not null One of maintenance_charge, financing_charge.
financing_charge_id text financing_charges.id on delete cascade The financing charge a financing_charge row belongs to; else null.
amount cents() INTEGER not null Per month, in cents.
valid_from validity() TEXT not null The first day of the period.
valid_to validity() TEXT The last day of the period; null while it is open-ended.
  • index recurring_amounts_property_idx on property_id, kind, valid_from
  • index recurring_amounts_financing_charge_idx on financing_charge_id, valid_from

Validity periods. The financing charge’s treatment over time (openspec: recurring-amounts): income-recognised charges are deductible, capitalised ones add to the acquisition cost. Periods like recurring_amounts. Defined in src/server/db/schema.ts:302 as financingTreatments.

Column Type Keys References Notes
id id() TEXT PK Default generated.
property_id text not null properties.id on delete cascade
treatment text not null One of income_recognised, capitalised.
valid_from validity() TEXT not null The first day of the period.
valid_to validity() TEXT The last day of the period; null while it is open-ended.
  • index financing_treatments_property_idx on property_id, valid_from

Logged income and expenses of a flat (openspec: transactions). Plain rows, edited and deleted in place; rent and the monthly charges are periods in recurring_amounts, never entries. The amount is positive; the type gives the sign. Defined in src/server/db/schema.ts:344 as transactions.

Column Type Keys References Notes
id id() TEXT PK Default generated.
property_id text not null properties.id on delete cascade
type text not null One of income, expense.
category text not null One of tenant_water_charge, other_income, annual_repair, improvement, water_charge, insurance, other_expense.
amount cents() INTEGER not null Positive, in cents.
date isoDate() TEXT not null
note text
created_by text not null user.id Who logged it; kept when another owner edits it.
created_at instant() INTEGER not null
updated_at instant() INTEGER not null
  • index transactions_property_date_idx on property_id, date

An entry’s receipt (openspec: transactions): the file is in FileStorage under key, generated by newFileKey(); D1 keeps the metadata. One per entry. uploadedBy counts the file against that user’s storage quota. Defined in src/server/db/schema.ts:372 as receipts.

Column Type Keys References Notes
id id() TEXT PK Default generated.
transaction_id text unique, not null transactions.id on delete cascade
key text not null
content_type text not null
size integer not null In bytes.
filename text not null The name the browser gave, shown and used for downloads only.
uploaded_by text not null user.id
created_at instant() INTEGER not null
  • index receipts_uploaded_by_idx on uploaded_by

Validity periods. A flat’s tenancies (openspec: tenancies), each a period: valid_to is the last day (the fixed term’s end, or the last rent day once ended), null while an open-ended tenancy runs. term_end keeps a fixed term’s end so that cancelling an early ending restores it. A flat’s tenancies never overlap. The tenant’s details are a third party’s personal data: never logged. Defined in src/server/db/schema.ts:421 as tenancies.

Column Type Keys References Notes
id id() TEXT PK Default generated.
property_id text not null properties.id on delete cascade
tenant_name text not null
tenant_phone text
tenant_email text
contract_type text not null One of open_ended, fixed_term.
contract_made isoDate() TEXT not null Decides the landlord’s notice period (CONCEPT.md §4.2).
valid_from validity() TEXT not null The first day of the period.
valid_to validity() TEXT The last day of the period; null while it is open-ended.
term_end isoDate() TEXT A fixed term’s end; null for an open-ended tenancy.
due_day integer not null Day of the month, 1–31; the month’s last day in shorter months.
notice_given_by text One of tenant, landlord, agreed, court, other.
notice_date isoDate() TEXT The date notice was received, or the ending agreed or permitted.
deposit cents() INTEGER The deposit in cents (openspec: deposits); null when none is set.
created_by text not null user.id
created_at instant() INTEGER not null
updated_at instant() INTEGER not null
  • index tenancies_property_idx on property_id, valid_from

The return of a tenancy’s deposit (openspec: deposits), one per tenancy. The amount returned is computed: the deposit less the two amounts kept. The rent kept is also recorded as rent payments linked to the return (rent_payments.deposit_return_id). Defined in src/server/db/schema.ts:459 as depositReturns.

Column Type Keys References Notes
id id() TEXT PK Default generated.
tenancy_id text unique, not null tenancies.id on delete cascade
returned_on isoDate() TEXT not null
rent_kept cents() INTEGER not null Kept for unpaid rent, in cents.
damage_kept cents() INTEGER not null Kept for damage and cleaning, in cents.
note text
created_by text not null user.id
created_at instant() INTEGER not null
updated_at instant() INTEGER not null

Validity periods. A tenancy’s rent over time (openspec: tenancies, “The rent of a tenancy”). The first period starts on the tenancy’s start day, later ones on the first of a month. A change closes the open row the day before and adds the next; the latest row stays open, bounded by the tenancy’s own dates. Defined in src/server/db/schema.ts:484 as tenancyRents.

Column Type Keys References Notes
id id() TEXT PK Default generated.
tenancy_id text not null tenancies.id on delete cascade
amount cents() INTEGER not null Per month, in cents.
valid_from validity() TEXT not null The first day of the period.
valid_to validity() TEXT The last day of the period; null while it is open-ended.
  • index tenancy_rents_tenancy_idx on tenancy_id, valid_from

A file kept with a flat (openspec: documents): the file is in FileStorage under key, generated by newFileKey(); D1 keeps the metadata. tenancyId is set only for a lease, and cleared when its tenancy is removed, so the paper stays with the flat. uploadedBy counts the file against that user’s storage quota, together with receipts. Defined in src/server/db/schema.ts:518 as propertyDocuments.

Column Type Keys References Notes
id id() TEXT PK Default generated.
property_id text not null properties.id on delete cascade
tenancy_id text tenancies.id on delete set null
kind text not null One of lease, financial_statement, charge_notice, minutes, other.
name text not null
date isoDate() TEXT not null
key text not null
content_type text not null
size integer not null In bytes.
filename text not null The name the browser gave, shown and used for downloads only.
uploaded_by text not null user.id
created_at instant() INTEGER not null
updated_at instant() INTEGER not null
  • index property_documents_property_date_idx on property_id, date
  • index property_documents_uploaded_by_idx on uploaded_by

A usage charge the housing company bills the flat’s owners monthly (openspec: usage-charges). description is set only for the kind other. Its amounts are periods in usage_charge_amounts. Defined in src/server/db/schema.ts:564 as usageCharges.

Column Type Keys References Notes
id id() TEXT PK Default generated.
property_id text not null properties.id on delete cascade
kind text not null One of water, parking, sauna, other.
basis text not null One of fixed, per_occupant.
description text
  • index usage_charges_property_idx on property_id

Validity periods. A usage charge’s amount over time, per month or per occupant per month. Periods like recurring_amounts: from the first of a month, the latest open. Defined in src/server/db/schema.ts:582 as usageChargeAmounts.

Column Type Keys References Notes
id id() TEXT PK Default generated.
usage_charge_id text not null usage_charges.id on delete cascade
amount cents() INTEGER not null
valid_from validity() TEXT not null The first day of the period.
valid_to validity() TEXT The last day of the period; null while it is open-ended.
  • index usage_charge_amounts_charge_idx on usage_charge_id, valid_from

Validity periods. The number of persons living in the flat during a tenancy, as periods (openspec: tenancies, “Occupants of a tenancy”). The first starts on the tenancy’s start day, changes on the first of a month. Defined in src/server/db/schema.ts:602 as tenancyOccupants.

Column Type Keys References Notes
id id() TEXT PK Default generated.
tenancy_id text not null tenancies.id on delete cascade
persons integer not null
valid_from validity() TEXT not null The first day of the period.
valid_to validity() TEXT The last day of the period; null while it is open-ended.
  • index tenancy_occupants_tenancy_idx on tenancy_id, valid_from

A fee the tenant pays the owner on top of the rent (openspec: tenancies, “Tenant fees”). Not linked to the usage charges. Amounts are periods in tenant_fee_amounts inside the tenancy. Defined in src/server/db/schema.ts:620 as tenantFees.

Column Type Keys References Notes
id id() TEXT PK Default generated.
tenancy_id text not null tenancies.id on delete cascade
kind text not null One of water, parking, sauna, other.
basis text not null One of fixed, per_occupant.
description text
  • index tenant_fees_tenancy_idx on tenancy_id

Validity periods. A tenant fee’s amount over time, per month or per occupant per month. Defined in src/server/db/schema.ts:635 as tenantFeeAmounts.

Column Type Keys References Notes
id id() TEXT PK Default generated.
tenant_fee_id text not null tenant_fees.id on delete cascade
amount cents() INTEGER not null
valid_from validity() TEXT not null The first day of the period.
valid_to validity() TEXT The last day of the period; null while it is open-ended.
  • index tenant_fee_amounts_fee_idx on tenant_fee_id, valid_from

Rent received for a month of a tenancy (openspec: rent-payments). month (YYYY-MM) is the rent’s month, paid_on the day it was received, which decides its tax year. A month may have several payments. Never edited: a wrong one is deleted and recorded again. Payments stay inside their tenancy’s months; text comparison of month orders it correctly. Defined in src/server/db/schema.ts:655 as rentPayments.

Column Type Keys References Notes
id id() TEXT PK Default generated.
tenancy_id text not null tenancies.id on delete cascade
month text not null
amount cents() INTEGER not null Positive, in cents.
paid_on isoDate() TEXT not null
deposit_return_id text deposit_returns.id on delete cascade Set when the rent was kept from the deposit (openspec: deposits).
created_by text not null user.id
created_at instant() INTEGER not null
  • index rent_payments_tenancy_month_idx on tenancy_id, month