PureScript中的全球资源(适用于AWS Lambda)



我想在AWS Lambda中运行PureScript代码。通常,如果lambda函数必须调用数据库,则连接存储在全局变量中。对不起,如果以下JavaScript代码语法错误,因为我不写JavaScript,但我想要的是下面这样的代码(这是AWS Lambda期望的代码(:

function createConnection() {
connection = SomeDBLibrary.createConnection(username, pwd)
return connection;
}
global.db_connection = createConnection();
handler = (event, context, callback) => {
// TODO use global.db_connection here to call the database
};
module.exports {
handler: handler
};

如何编写等效的PureScript来获得这样的代码?目前,我在每个CRUD方法中调用createConnection((函数,这是浪费。

编辑1:

下面是我实际的PureScript代码。我只想调用一次的函数是aPool。然而,我相信每次到达/list端点时都会调用它。我希望在第一次加载处理程序时只命中一次。

module Main where
import Prelude hiding (apply)
import Data.Bifunctor (lmap)
import Data.Either (Either)
import Database.Postgres (Client, ClientConfig, ConnectionInfo, Pool, Query(Query), connectionInfoFromConfig, defaultPoolConfig, mkPool, query_, withClient)
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Aff.Class (liftAff)
import Effect.Class (class MonadEffect, liftEffect)
import Effect.Console (log)
import Effect.Exception (Error, error)
import Foreign (Foreign)
import Node.Express.App (App, get)
import Node.Express.Handler (Handler)
import Node.Express.Response (sendJson)
import Simple.JSON as JSON
import Network.AWS.Lambda.Express as Lambda

clientConfig :: ClientConfig
clientConfig =
{ host: "localhost"
, database: "test"
, port: 5432
, user: "testuser"
, password: "test"
, ssl: false
}
type IndexedTodo = { id :: Int, description :: String, isdone :: Boolean }
connectionInfo :: ConnectionInfo
connectionInfo = connectionInfoFromConfig clientConfig defaultPoolConfig
println :: forall a. MonadEffect a => String -> a Unit
println str =  liftEffect $ log str
aPool :: Effect Pool
aPool = do
println "In Pooler"
mkPool connectionInfo
withConnectionPool :: forall a. Effect Pool -> (Client -> Aff a) -> Aff a
withConnectionPool somePool f = do
pool <- liftEffect somePool
withClient pool c -> f c
withConnection :: forall a. (Client -> Aff a) -> Aff a
withConnection = withConnectionPool aPool
read' :: forall a. JSON.ReadForeign a => Foreign -> Either Error a
read' = lmap (error <<< show) <<< JSON.read
readTodoList :: Aff (Array IndexedTodo)
readTodoList = do
let selectQuery = Query "select * from todos" :: Query IndexedTodo
withConnection $ query_ read' selectQuery
listTodosHandler :: Handler
listTodosHandler = do
userdata <- liftAff readTodoList
sendJson userdata

app :: App
app = do
get "/list"       listTodosHandler
-- Define the AWS Lambda handler
handler :: Lambda.HttpHandler
handler = Lambda.makeHandler app

我认为这需要像这样的FFI

foreign import dbConnection :: Effect Connection

实现:

exports.dbConnection = function() {
if(globals.dbConnection === undefined) {
globals.dbConnection = createConnection();
}
return globals.dbConnection;
}

相关内容

最新更新