This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.hs
54 lines (43 loc) · 1.83 KB
/
api.hs
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
47
48
49
50
51
52
53
54
-- Schemas are composed of fields
type FieldType a where
name :: String -- The name of the field in JS
dbName :: String -- The name of the field in the Database
validation :: a → Validation(FieldValidationError, a)
marshall :: a → Validation(MarshallingError, DatabaseType)
unmarshall :: DatabaseType → Validation(UnmarshallingError, a)
-- Which maps values to database types
type DatabaseType = DbNull
| DbText String
| DbDouble Number
| DbInt32 Number
| DbBoolean Boolean
| DbDate Date
| DbArray [DatabaseType]
| DbMap { String -> DatabaseType }
| DbObjectId String -- for MongoDB
| DbCustom Buffer -- Anything else
-- And we have a layer on top to provide something similar for all DBs
type Schema a where
name :: String -- Name of the table in the DB
fields :: [FieldType]
marshall :: a → Validation(MarshallingError, DatabaseType)
unmarshall :: DatabaseType → Validation(UnmarshallingError, a)
-- Collection and Cursor are specific to the database backend
type Collection a where
create :: Schema → Object → Future(DBError, a)
save :: a → Future(DBError, a)
delete :: a → Future(DBError, a)
find :: Query → Schema → Cursor
type Cursor a where
skip :: Int → Cursor → Cursor
limit :: Int → Cursor → Cursor
orderBy :: Ordering → Cursor → Cursor
values :: Cursor → [a]
first :: Cursor → Maybe(a)
type Ordering where
field :: FieldType
direction :: OrderingDirection
type OrderingDirection = Ascending | Descending
type Query = And(Query, Query)
| Or(Query, Query)
| Eq(FieldType, DatabaseType)