-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclients.ts
40 lines (33 loc) · 1.14 KB
/
clients.ts
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
import * as Either from 'fp-ts/lib/Either'
import * as postgres from './postgres'
import { SchemaClient, schemaClient } from './schema'
import { TypeClient, typeClient } from './tstype'
export type Clients = {
postgres: postgres.Sql<{}>
schema: SchemaClient
types: TypeClient
}
export async function connect(
connectionString?: string | undefined
): Promise<Either.Either<string, Clients>> {
const postgresClient = connectionString
? postgres(connectionString)
: postgres()
return Either.right(await clients(postgresClient))
}
export async function clients(
postgresClient: postgres.Sql<{}>
): Promise<Clients> {
const schema = schemaClient(postgresClient)
const types = await typeClient(postgresClient)
return { postgres: postgresClient, schema, types }
}
export async function disconnect(clients: Clients): Promise<void> {
await clients.postgres.end({ timeout: 5 })
}
export async function clearCache(clients: Clients): Promise<Clients> {
// The type client caches stuff about user-defined SQL types.
// Recreate it to clear the cache.
const types = await typeClient(clients.postgres)
return { ...clients, types }
}