module Avers.Storage.Expressions where
import qualified Data.Vector as V
import Data.Text (Text)
import Database.RethinkDB as R
import Avers.Types hiding (Object)
import Prelude hiding (lookup)
objectsTable, sessionsTable, snapshotsTable, patchesTable, secretsTable, blobsTable :: Exp Table
blobsTable = Table Nothing $ lift ("blobs" :: Text)
objectsTable = Table Nothing $ lift ("objects" :: Text)
patchesTable = Table Nothing $ lift ("patches" :: Text)
secretsTable = Table Nothing $ lift ("secrets" :: Text)
sessionsTable = Table Nothing $ lift ("sessions" :: Text)
snapshotsTable = Table Nothing $ lift ("snapshots" :: Text)
primaryKeyField :: Text
primaryKeyField = "id"
primaryKeyFieldE :: Exp Text
primaryKeyFieldE = lift primaryKeyField
objectFieldE :: (IsDatum a) => Text -> Exp Object -> Exp a
objectFieldE field obj = GetField (lift field) obj
objectFieldEqE :: (ToDatum a) => Text -> a -> Exp Object -> Exp Bool
objectFieldEqE field value obj = Eq
(objectFieldE field obj :: Exp Datum)
(lift $ toDatum value)
primaryKeyEqE :: Text -> Exp Object -> Exp Bool
primaryKeyEqE = objectFieldEqE primaryKeyField
headE :: (IsSequence a, IsDatum r) => Exp a -> Exp r
headE = Nth 0
limitE :: (IsSequence s) => Int -> Exp s -> Exp s
limitE n s = Limit (fromIntegral n) s
mkBounds :: ObjectId -> Int -> Int -> (Bound, Bound)
mkBounds objId lo hi = (mkBound objId lo, mkBound objId hi)
mkBound :: ObjectId -> Int -> Bound
mkBound objId revId = Closed $ Array $ V.fromList
[String $ toPk objId, Number $ fromIntegral revId]
objectSnapshotSequenceE :: ObjectId -> Int -> Int -> Exp Table
objectSnapshotSequenceE objId lo hi =
BetweenIndexed "objectSnapshotSequence" (mkBounds objId lo hi) $
snapshotsTable
objectPatchSequenceE :: ObjectId -> Int -> Int -> Exp Table
objectPatchSequenceE objId lo hi =
BetweenIndexed "objectPatchSequence" (mkBounds objId lo hi) $
patchesTable