about summary refs log tree commit diff
path: root/bskylink/src/context.ts
blob: 1520513ceb2efafb9dd1726f91a4455efc95fc09 (plain) (blame)
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
import {SafelinkClient} from './cache/safelinkClient.js'
import {type Config} from './config.js'
import Database from './db/index.js'

export type AppContextOptions = {
  cfg: Config
  db: Database
}

export class AppContext {
  cfg: Config
  db: Database
  safelinkClient: SafelinkClient
  abortController = new AbortController()

  constructor(private opts: AppContextOptions) {
    this.cfg = this.opts.cfg
    this.db = this.opts.db
    this.safelinkClient = new SafelinkClient({
      cfg: this.opts.cfg.service,
      db: this.opts.db,
    })
  }

  static async fromConfig(cfg: Config, overrides?: Partial<AppContextOptions>) {
    const db = Database.postgres({
      url: cfg.db.url,
      schema: cfg.db.schema,
      poolSize: cfg.db.pool.size,
      poolMaxUses: cfg.db.pool.maxUses,
      poolIdleTimeoutMs: cfg.db.pool.idleTimeoutMs,
    })
    return new AppContext({
      cfg,
      db,
      ...overrides,
    })
  }
}