-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbStorage.fs
180 lines (168 loc) · 6.83 KB
/
DbStorage.fs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
namespace BackEnd
open FSharp.Data.Sql
open Npgsql.FSharp
open FSharpPlus
open Shared
open Shared.Utils
type json = string
type StorageEvent =
{
Event: json
Id: int
Timestamp: System.DateTime
}
type StorageSnapshot = {
Id: int
Snapshot: json
TimeStamp: System.DateTime
EventId: int
}
type IStorage =
abstract member DeleteAllEvents: unit -> unit
abstract member TryGetLastSnapshot: unit -> Option<int * int * json>
abstract member TryGetLastEventId: unit -> Option<int>
abstract member TryGetLastSnapshotEventId: unit -> Option<int>
abstract member TryGetLastSnapshotId: unit -> Option<int>
abstract member TryGetEvent: int -> Option<StorageEvent>
abstract member SetSnapshot: int * string -> Result<unit, string>
abstract member AddEvents: List<json> -> Result<unit, string>
abstract member GetEventsAfterId: int -> List<int * string >
module DbStorage =
let TPConnectionString = Conf.connectionString
let ceResult = CeResultBuilder()
type PgDb =
new() = {}
interface IStorage with
member this.DeleteAllEvents() =
if (Conf.isTestEnv) then
let _ =
TPConnectionString
|> Sql.connect
|> Sql.query "DELETE from snapshots"
|> Sql.executeNonQuery
let _ =
TPConnectionString
|> Sql.connect
|> Sql.query "DELETE from events"
|> Sql.executeNonQuery
()
else
failwith "operation allowed only in test db"
member this.TryGetLastSnapshot() =
TPConnectionString
|> Sql.connect
|> Sql.query "SELECT id, event_id, snapshot FROM snapshots ORDER BY id DESC LIMIT 1"
|> Sql.executeAsync (fun read ->
(
read.int "id",
read.int "event_id",
read.text "snapshot"
)
)
|> Async.AwaitTask
|> Async.RunSynchronously
|> Seq.tryHead
member this.TryGetLastSnapshotId() =
TPConnectionString
|> Sql.connect
|> Sql.query "SELECT id FROM snapshots ORDER BY id DESC LIMIT 1"
|> Sql.executeAsync (fun read ->
(
read.int "id"
)
)
|> Async.AwaitTask
|> Async.RunSynchronously
|> Seq.tryHead
member this.TryGetLastEventId() =
TPConnectionString
|> Sql.connect
|> Sql.query "SELECT id from events ORDER BY id DESC LIMIT 1"
|> Sql.executeAsync (fun read -> read.int "id")
|> Async.AwaitTask
|> Async.RunSynchronously
|> Seq.tryHead
member this.TryGetLastSnapshotEventId() =
TPConnectionString
|> Sql.connect
|> Sql.query "SELECT event_id from snapshots ORDER BY id DESC LIMIT 1"
|> Sql.executeAsync (fun read -> read.int "event_id")
|> Async.AwaitTask
|> Async.RunSynchronously
|> Seq.tryHead
member this.TryGetEvent id =
TPConnectionString
|> Sql.connect
|> Sql.query "SELECT * from events where id = @id"
|> Sql.parameters ["id", Sql.int id]
|> Sql.executeAsync
(
fun read ->
{
Id = read.int "id"
Event = read.string "event"
Timestamp = read.dateTime "timestamp"
}
)
|> Async.AwaitTask
|> Async.RunSynchronously
|> Seq.tryHead
member this.SetSnapshot (id: int, snapshot: json) =
ceResult
{
let! event = ((this :> IStorage).TryGetEvent id) |> optionToResult
let _ =
TPConnectionString
|> Sql.connect
|> Sql.executeTransactionAsync
[
"INSERT INTO snapshots (event_id, snapshot, timestamp) VALUES (@event_id, @snapshot, @timestamp)",
[
[
("@event_id", Sql.int event.Id);
("snapshot", Sql.jsonb snapshot);
("timestamp", Sql.timestamp event.Timestamp)
]
]
]
|> Async.AwaitTask
|> Async.RunSynchronously
return ()
}
member this.AddEvents (events: List<json>) =
try
let _ =
TPConnectionString
|> Sql.connect
|> Sql.executeTransactionAsync
[
"INSERT INTO events (event, timestamp) VALUES (@event, @timestamp)",
events
|> List.map
(
fun x ->
[
("@event", Sql.jsonb x);
("timestamp", Sql.timestamp (System.DateTime.Now))
]
)
]
|> Async.AwaitTask
|> Async.RunSynchronously
() |> Ok
with
| _ as ex -> (ex.ToString()) |> Error
member this.GetEventsAfterId id =
TPConnectionString
|> Sql.connect
|> Sql.query "SELECT id, event FROM events WHERE id > @id ORDER BY id"
|> Sql.parameters ["id", Sql.int id]
|> Sql.executeAsync ( fun read ->
(
read.int "id",
read.text "event"
)
)
|> Async.AwaitTask
|> Async.RunSynchronously
|> Seq.toList