2023-01-05 22:16:06 -05:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DerivingStrategies #-}
|
|
|
|
{-# LANGUAGE EmptyDataDecls #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE GADTs #-}
|
2022-12-11 17:51:11 -05:00
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
2023-01-05 22:16:06 -05:00
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE StandaloneDeriving #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
2023-01-28 22:58:05 -05:00
|
|
|
{-# LANGUAGE NoImplicitPrelude #-}
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
module Internal.Database.Model where
|
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
import Database.Esqueleto.Experimental
|
|
|
|
import Database.Persist.TH
|
|
|
|
import Internal.Types
|
|
|
|
import RIO
|
|
|
|
import qualified RIO.Map as M
|
|
|
|
import qualified RIO.Text as T
|
|
|
|
import RIO.Time
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
share
|
|
|
|
[mkPersist sqlSettings, mkMigrate "migrateAll"]
|
|
|
|
[persistLowerCase|
|
2022-12-11 17:51:11 -05:00
|
|
|
CommitR sql=commits
|
|
|
|
hash Int
|
|
|
|
type ConfigType
|
|
|
|
deriving Show Eq
|
|
|
|
CurrencyR sql=currencies
|
|
|
|
symbol T.Text
|
|
|
|
fullname T.Text
|
|
|
|
deriving Show Eq
|
|
|
|
AccountR sql=accounts
|
|
|
|
name T.Text
|
|
|
|
fullpath T.Text
|
|
|
|
desc T.Text
|
|
|
|
deriving Show Eq
|
|
|
|
AccountPathR sql=account_paths
|
|
|
|
parent AccountRId OnDeleteCascade
|
|
|
|
child AccountRId OnDeleteCascade
|
|
|
|
depth Int
|
|
|
|
deriving Show Eq
|
|
|
|
TransactionR sql=transactions
|
|
|
|
commit CommitRId OnDeleteCascade
|
|
|
|
date Day
|
|
|
|
description T.Text
|
|
|
|
deriving Show Eq
|
|
|
|
SplitR sql=splits
|
|
|
|
transaction TransactionRId OnDeleteCascade
|
|
|
|
currency CurrencyRId OnDeleteCascade
|
|
|
|
account AccountRId OnDeleteCascade
|
|
|
|
memo T.Text
|
|
|
|
value Rational
|
|
|
|
deriving Show Eq
|
2023-01-30 22:48:16 -05:00
|
|
|
BudgetLabelR sql=budget_labels
|
2023-02-05 11:34:22 -05:00
|
|
|
split SplitRId OnDeleteCascade
|
2023-01-30 22:48:16 -05:00
|
|
|
budgetName T.Text
|
2023-01-30 21:47:17 -05:00
|
|
|
deriving Show Eq
|
2023-01-30 22:48:16 -05:00
|
|
|
ExpenseBucketR sql=expense_buckets
|
2023-02-05 11:34:22 -05:00
|
|
|
budgetLabel BudgetLabelRId OnDeleteCascade
|
2023-01-30 21:47:17 -05:00
|
|
|
bucket ExpenseBucket
|
|
|
|
deriving Show Eq
|
2023-01-30 22:48:16 -05:00
|
|
|
IncomeBucketR sql=income_buckets
|
2023-02-05 11:34:22 -05:00
|
|
|
budgetLabel BudgetLabelRId OnDeleteCascade
|
2023-01-30 21:47:17 -05:00
|
|
|
bucket IncomeBucket
|
|
|
|
deriving Show Eq
|
2022-12-11 17:51:11 -05:00
|
|
|
|]
|
|
|
|
|
|
|
|
type AccountMap = M.Map AcntID (AccountRId, AcntSign)
|
|
|
|
|
|
|
|
type CurrencyMap = M.Map CurID CurrencyRId
|
|
|
|
|
|
|
|
data DBState = DBState
|
2023-01-05 22:16:06 -05:00
|
|
|
{ kmCurrency :: !CurrencyMap
|
|
|
|
, kmAccount :: !AccountMap
|
2023-02-05 10:34:26 -05:00
|
|
|
, kmBudgetInterval :: !Bounds
|
|
|
|
, kmStatementInterval :: !Bounds
|
2023-01-05 22:16:06 -05:00
|
|
|
, kmNewCommits :: ![Int]
|
2023-01-30 22:57:42 -05:00
|
|
|
, kmConfigDir :: !FilePath
|
2023-02-01 20:56:29 -05:00
|
|
|
, kmBoundsCache :: !(MVar (M.Map (Bounds, DatePat) [Day]))
|
2022-12-11 17:51:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type MappingT m a = ReaderT DBState (SqlPersistT m) a
|
|
|
|
|
|
|
|
type KeySplit = Split AccountRId Rational CurrencyRId
|
|
|
|
|
|
|
|
type KeyTx = Tx KeySplit
|
|
|
|
|
|
|
|
type TreeR = Tree ([T.Text], AccountRId)
|