|
| 1 | +# PostgreSQLDemo |
| 2 | + |
| 3 | +[](https://swift.org/download/) [](https://swift.org/download/) |
| 4 | + |
| 5 | +This example shows the usage of the [LambdaSwiftSprinter](https://github.com/swift-sprinter/aws-lambda-swift-sprinter-core) framework and the plugin [LambdaSwiftSprinterNioPlugin](https://github.com/swift-sprinter/aws-lambda-swift-sprinter-nio-plugin) to build a lambda capable to perform an Postgres query using |
| 6 | +[PostgresNIO](https://github.com/vapor/postgres-nio.git). |
| 7 | + |
| 8 | +## Swift code |
| 9 | + |
| 10 | +Define an Event and a Response as Codable. |
| 11 | +```swift |
| 12 | +import AsyncHTTPClient |
| 13 | +import Foundation |
| 14 | +#if canImport(FoundationNetworking) |
| 15 | +import FoundationNetworking |
| 16 | +#endif |
| 17 | +import LambdaSwiftSprinter |
| 18 | +import LambdaSwiftSprinterNioPlugin |
| 19 | +import Logging |
| 20 | +import NIO |
| 21 | +import NIOFoundationCompat |
| 22 | +import PostgresNIO |
| 23 | + |
| 24 | +struct Event: Codable { |
| 25 | + let query: String |
| 26 | +} |
| 27 | + |
| 28 | +struct Response: Codable { |
| 29 | + let value: String |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +Add a loger: |
| 36 | +```swift |
| 37 | +let logger = Logger(label: "AWS.Lambda.Postgres") |
| 38 | +``` |
| 39 | + |
| 40 | +Add a redis connection, define the Lambda and run it: |
| 41 | +```swift |
| 42 | +enum LambdaError: Error { |
| 43 | + case connectionFailed |
| 44 | +} |
| 45 | + |
| 46 | +//let endpoint = "<yourdb>.rds.amazonaws.com" |
| 47 | +let endpoint = "postgres" |
| 48 | + |
| 49 | +do { |
| 50 | + let eventLoop = httpClient.eventLoopGroup.next() |
| 51 | + let connection = try PostgresConnection.connect( |
| 52 | + to: try .makeAddressResolvingHost(endpoint, |
| 53 | + port: 5432), |
| 54 | + on: eventLoop |
| 55 | + ).wait() |
| 56 | + |
| 57 | + logger.error("after connection") |
| 58 | + |
| 59 | + try connection.authenticate(username: "username1", |
| 60 | + database: "demoDB", |
| 61 | + password: "password1").wait() |
| 62 | + |
| 63 | + |
| 64 | + let syncCodableNIOLambda: SyncCodableNIOLambda<Event, Response> = { (event, context) throws -> EventLoopFuture<Response> in |
| 65 | + |
| 66 | + let future = connection.query(event.query).map { (rows) -> Response in |
| 67 | + return Response(value: "\(rows)") |
| 68 | + |
| 69 | + } |
| 70 | + return future |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + let sprinter = try SprinterNIO() |
| 75 | + sprinter.register(handler: "query", lambda: syncCodableNIOLambda) |
| 76 | + |
| 77 | + try sprinter.run() |
| 78 | +} catch { |
| 79 | + logger.error("\(String(describing: error))") |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +If there are not error, the Event will be automatically decoded inside the lambda and then used to perform a `query` to Postgres. |
| 84 | +The result of the `query` is returned into the Response. |
| 85 | + |
| 86 | + |
| 87 | +Note |
| 88 | + |
| 89 | +In this example we used [swift-log](https://github.com/apple/swift-log.git) to log the output. |
| 90 | + |
| 91 | +## Deployment |
| 92 | + |
| 93 | +To build this example make sure the following lines on the `Makefile` are not commented and the other example configurations are commented. |
| 94 | + |
| 95 | +``` |
| 96 | +... |
| 97 | +
|
| 98 | +# HelloWorld Example Configuration |
| 99 | +# SWIFT_EXECUTABLE=HelloWorld |
| 100 | +# SWIFT_PROJECT_PATH=Examples/HelloWorld |
| 101 | +# LAMBDA_FUNCTION_NAME=HelloWorld |
| 102 | +# LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).helloWorld |
| 103 | +
|
| 104 | +.... |
| 105 | +
|
| 106 | +# PostgreSQLDemo Example Configuration |
| 107 | +SWIFT_EXECUTABLE=PostgreSQLDemo |
| 108 | +SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo |
| 109 | +LAMBDA_FUNCTION_NAME=PostgreSQLDemo |
| 110 | +LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query |
| 111 | +
|
| 112 | +... |
| 113 | +``` |
| 114 | + |
| 115 | +Then follow the main [README](https://github.com/swift-sprinter/aws-lambda-swift-sprinter) to build and test the code. |
| 116 | + |
| 117 | +## Test |
| 118 | + |
| 119 | +The test event is defined in the file `event.json` |
| 120 | +```json |
| 121 | +{ |
| 122 | + "query": "SELECT 1+1 as result;" |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +expected response: |
| 127 | + |
| 128 | +```json |
| 129 | +{"value":"[[\"result\": 2]]"} |
| 130 | +``` |
| 131 | + |
| 132 | +Change it to try different output and error conditions. |
| 133 | + |
| 134 | +# LambdaSwiftSprinter |
| 135 | + |
| 136 | +To know more refer to [LambdaSwiftSprinter](https://github.com/swift-sprinter/aws-lambda-swift-sprinter-core). |
0 commit comments