All files / src/server/db columns.ts

100% Statements 15/15
100% Branches 0/0
100% Functions 8/8
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                    67x 1453x   1x     595x           67x     67x             1915x     1521x           529x        
import { integer, text } from 'drizzle-orm/sqlite-core'
 
// Column helpers for the storage conventions in CLAUDE.md. They use only
// integer and text columns, so a later move to PostgreSQL is a schema
// rewrite with the same values.
 
/** A calendar date as ISO `YYYY-MM-DD`. */
export type IsoDate = string
 
/** Primary key: a random UUID as text, generated in the app. */
export const id = () =>
  text('id')
    .primaryKey()
    .$defaultFn(() => crypto.randomUUID())
 
/** Money in integer cents (`123456` = 1 234,56 €). */
export const cents = (name: string) => integer(name, { mode: 'number' })
 
/**
 * A quantity with one decimal as integer tenths (`485` = 48,5), e.g. a floor
 * area in m². Exact, like cents.
 */
export const tenths = (name: string) => integer(name, { mode: 'number' })
 
/** A share in integer basis points (`5000` = 50.00 %); shares sum to 10000. */
export const basisPoints = (name: string) => integer(name, { mode: 'number' })
 
/**
 * A point in time (e.g. when a session expires) as integer epoch
 * milliseconds, read and written as a `Date`. Not for calendar dates: those
 * are `isoDate()`.
 */
export const instant = (name: string) => integer(name, { mode: 'timestamp_ms' })
 
/** A calendar date stored as ISO `YYYY-MM-DD` text. */
export const isoDate = (name: string) => text(name).$type<IsoDate>()
 
/**
 * A validity period: `valid_from` is required, `valid_to` is open-ended when
 * null. Periods are never overwritten; a change ends one and starts the next.
 */
export const validity = () => ({
  validFrom: isoDate('valid_from').notNull(),
  validTo: isoDate('valid_to'),
})