如何在 Typed Racket 中连接数据库?

How to connect db in Typed Racket?

根据official docs,通过

连接postgresql很方便
(require db)
(define pgc
  (postgresql-connect #:user "example"
                      #:database "exampledb"
                      #:password "password"))

但是,这在 Typed Racket 中不起作用(我有 (require typed/db)):

postgresql-connect: unbound identifier in module in: postgresql-connect

对... 原来typed/db 只适用于sqlite3。下面是一些适用于我的代码示例:

#lang typed/racket

(require/typed
 db
 [#:opaque Connection connection?]
 [postgresql-connect
  (#:port Number #:user String #:database String #:password String
   -> Connection)]
 [query-rows
  (Connection String Any * -> (Listof (Vectorof Any)))]
 [query-list
  (Connection String Any * -> (Listof Any))])

(define conn
    (postgresql-connect #:port 13432
                        #:user db-username
                        #:database "scheduling"
                        #:password db-password))
(define rows
  (query-rows
   conn
   (~a "SELECT * FROM course_mappings")))

请注意,我只为我想使用的函数提供了类型。例如,要使用 query-exec,您需要为此提供一个单独的类型。