Skip to content

Commit 3876f0f

Browse files
author
Jamil Maqdis Anton
committed
Add docs. Remove redundant functionality. Streamline api.
1 parent 475bda2 commit 3876f0f

File tree

1 file changed

+28
-38
lines changed

1 file changed

+28
-38
lines changed

src/Postgres.fs

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@ type PostgresConfig =
77
port: string
88
username: string
99
password: string
10-
database: string }
11-
12-
type PoolingConfig =
13-
{ minPoolSize: int option
14-
maxPoolSize: int option
15-
connectionIdleLifeTime: int option
16-
connectionPruningInterval: int option }
10+
database: string
11+
schema: string option }
1712

1813
module Postgres =
19-
let private storeSettings (config: PostgresConfig): string =
14+
let private settingsStringFromConfig (config: PostgresConfig): string =
2015
sprintf
2116
"Host=%s;Port=%s;User Id=%s;Password=%s;Database=%s"
2217
config.host
@@ -25,47 +20,42 @@ module Postgres =
2520
config.password
2621
config.database
2722

28-
let private poolingSettings (pooling: PoolingConfig): string =
29-
let minSize =
30-
pooling.minPoolSize |> Option.defaultValue 0
31-
32-
let maxSize =
33-
pooling.maxPoolSize |> Option.defaultValue 100
34-
35-
let connectionPruningInterval =
36-
pooling.connectionPruningInterval
37-
|> Option.defaultValue 300
23+
let private setSchema (schema: string option)
24+
(settings: SqlStreamStore.PostgresStreamStoreSettings)
25+
: SqlStreamStore.PostgresStreamStoreSettings =
26+
match schema with
27+
| None -> ()
28+
| Some schema -> settings.Schema <- schema
3829

39-
let connectionIdleLifeTime =
40-
pooling.connectionIdleLifeTime
41-
|> Option.defaultValue 10
42-
43-
sprintf
44-
"Minimum Pool Size=%d;Maximum Pool Size=%d;Connection Idle Lifetime=%d;Connection Pruning Interval%d"
45-
minSize
46-
maxSize
47-
connectionPruningInterval
48-
connectionIdleLifeTime
30+
settings
4931

50-
let private storeSettingsWithPooling (config: PostgresConfig) (pooling: PoolingConfig): string =
51-
sprintf "%s;%s" (storeSettings config) (poolingSettings pooling)
32+
/// Connects to a postgres database given a configuration record and an optional schema.
33+
/// If no schema is provided the tables will be created directly in the public one.
34+
let connect (config: PostgresConfig) (schema: string option): SqlStreamStore.PostgresStreamStore =
35+
let storeSettings =
36+
SqlStreamStore.PostgresStreamStoreSettings(settingsStringFromConfig config)
37+
|> setSchema schema
5238

53-
let createStore (config: PostgresConfig): SqlStreamStore.PostgresStreamStore =
54-
new SqlStreamStore.PostgresStreamStore(SqlStreamStore.PostgresStreamStoreSettings(storeSettings config))
39+
new SqlStreamStore.PostgresStreamStore(storeSettings)
5540

56-
let createStoreWithPoolingConfig (config: PostgresConfig) (pooling: PoolingConfig): SqlStreamStore.PostgresStreamStore =
57-
new SqlStreamStore.PostgresStreamStore(SqlStreamStore.PostgresStreamStoreSettings
58-
(storeSettingsWithPooling config pooling))
41+
/// Connects to a postgres database given a Npgsql configuration string and an optional schema.
42+
/// If no schema is provided the tables will be created directly in the public one.
43+
let createStoreWithConfigString (config: string) (schema: string option): SqlStreamStore.PostgresStreamStore =
44+
let storeSettings =
45+
SqlStreamStore.PostgresStreamStoreSettings(config)
46+
|> setSchema schema
5947

60-
let createStoreWithConfigString (config: string): SqlStreamStore.PostgresStreamStore =
61-
new SqlStreamStore.PostgresStreamStore(SqlStreamStore.PostgresStreamStoreSettings(config))
48+
new SqlStreamStore.PostgresStreamStore(storeSettings)
6249

50+
/// Creates messages, and streams tables that house the data.
51+
/// Can throw exceptions.
6352
let createSchemaRaw (store: SqlStreamStore.PostgresStreamStore): Async<unit> =
6453
async {
6554
return! store.CreateSchemaIfNotExists()
6655
|> Async.awaitTaskWithInnerException'
6756
}
6857

69-
let createSchema (store: SqlStreamStore.PostgresStreamStore): Async<Result<unit, string>> =
58+
/// Creates messages, and streams tables that house the data.
59+
let createSchema (store: SqlStreamStore.PostgresStreamStore): Async<Result<unit, exn>> =
7060
createSchemaRaw store
7161
|> ExceptionsHandler.asyncExceptionHandler

0 commit comments

Comments
 (0)