Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 7d232f1

Browse files
PostgreSQLDemo
1 parent 81e126c commit 7d232f1

File tree

9 files changed

+247
-0
lines changed

9 files changed

+247
-0
lines changed

Examples/PostgreSQLDemo/Package.resolved

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/PostgreSQLDemo/Package.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// swift-tools-version:5.1
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "PostgreSQLDemo",
8+
dependencies: [
9+
// Dependencies declare other packages that this package depends on.
10+
// .package(url: /* package url */, from: "1.0.0"),
11+
.package(url: "https://github.com/swift-sprinter/aws-lambda-swift-sprinter-nio-plugin", from: "1.0.0-alpha.3"),
12+
.package(url: "https://github.com/vapor/postgres-nio.git", from: "1.0.0-alpha.1.6"),
13+
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
14+
],
15+
targets: [
16+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
17+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
18+
.target(
19+
name: "PostgreSQLDemo",
20+
dependencies: ["LambdaSwiftSprinterNioPlugin", "Logging", "PostgresNIO"]),
21+
.testTarget(
22+
name: "PostgreSQLDemoTests",
23+
dependencies: ["PostgreSQLDemo"]),
24+
]
25+
)

Examples/PostgreSQLDemo/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# PostgreSQLDemo
2+
3+
A description of this package.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import AsyncHTTPClient
2+
import Foundation
3+
#if canImport(FoundationNetworking)
4+
import FoundationNetworking
5+
#endif
6+
import LambdaSwiftSprinter
7+
import LambdaSwiftSprinterNioPlugin
8+
import Logging
9+
import NIO
10+
import NIOFoundationCompat
11+
import PostgresNIO
12+
13+
struct Event: Codable {
14+
let query: String
15+
}
16+
17+
struct Response: Codable {
18+
let value: String
19+
}
20+
21+
enum LambdaError: Error {
22+
case connectionFailed
23+
}
24+
25+
let logger = Logger(label: "AWS.Lambda.Redis")
26+
let endpoint = "<yourdb>.rds.amazonaws.com"
27+
do {
28+
let eventLoop = httpClient.eventLoopGroup.next()
29+
let connection = try PostgresConnection.connect(
30+
to: try .makeAddressResolvingHost(endpoint,
31+
port: 5432),
32+
on: eventLoop
33+
).wait()
34+
35+
logger.error("after connection")
36+
37+
try connection.authenticate(username: "<username>",
38+
database: "<db>",
39+
password: "<password>").wait()
40+
41+
42+
let syncCodableNIOLambda: SyncCodableNIOLambda<Event, Response> = { (event, context) throws -> EventLoopFuture<Response> in
43+
44+
let future = connection.query(event.query).map { (rows) -> Response in
45+
return Response(value: "\(rows)")
46+
47+
}
48+
return future
49+
}
50+
51+
52+
let sprinter = try SprinterNIO()
53+
//Note amend this line in case if it's required to use a different lambda handler.
54+
sprinter.register(handler: "query", lambda: syncCodableNIOLambda)
55+
56+
try sprinter.run()
57+
} catch {
58+
logger.error("\(String(describing: error))")
59+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import XCTest
2+
3+
import PostgreSQLDemoTests
4+
5+
var tests = [XCTestCaseEntry]()
6+
tests += PostgreSQLDemoTests.allTests()
7+
XCTMain(tests)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import XCTest
2+
import class Foundation.Bundle
3+
4+
final class PostgreSQLDemoTests: XCTestCase {
5+
func testExample() throws {
6+
// This is an example of a functional test case.
7+
// Use XCTAssert and related functions to verify your tests produce the correct
8+
// results.
9+
10+
// Some of the APIs that we use below are available in macOS 10.13 and above.
11+
guard #available(macOS 10.13, *) else {
12+
return
13+
}
14+
15+
let fooBinary = productsDirectory.appendingPathComponent("PostgreSQLDemo")
16+
17+
let process = Process()
18+
process.executableURL = fooBinary
19+
20+
let pipe = Pipe()
21+
process.standardOutput = pipe
22+
23+
try process.run()
24+
process.waitUntilExit()
25+
26+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
27+
let output = String(data: data, encoding: .utf8)
28+
29+
XCTAssertEqual(output, "Hello, world!\n")
30+
}
31+
32+
/// Returns path to the built products directory.
33+
var productsDirectory: URL {
34+
#if os(macOS)
35+
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
36+
return bundle.bundleURL.deletingLastPathComponent()
37+
}
38+
fatalError("couldn't find the products directory")
39+
#else
40+
return Bundle.main.bundleURL
41+
#endif
42+
}
43+
44+
static var allTests = [
45+
("testExample", testExample),
46+
]
47+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import XCTest
2+
3+
#if !canImport(ObjectiveC)
4+
public func allTests() -> [XCTestCaseEntry] {
5+
return [
6+
testCase(PostgreSQLDemoTests.allTests),
7+
]
8+
}
9+
#endif

Examples/PostgreSQLDemo/event.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"query": "SELECT version()"
3+
}

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ LAMBDA_HANDLER?=$(SWIFT_EXECUTABLE).helloWorld
3636
# LAMBDA_FUNCTION_NAME?=RedisDemo
3737
# LAMBDA_HANDLER?=$(SWIFT_EXECUTABLE).setGet
3838

39+
# PostgreSQLDemo Example Configuration
40+
# SWIFT_EXECUTABLE=PostgreSQLDemo
41+
# SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo
42+
# LAMBDA_FUNCTION_NAME=PostgreSQLDemo
43+
# LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query
44+
3945
# AWS Configuration
4046
IAM_ROLE_NAME?=lambda_sprinter_basic_execution
4147
AWS_PROFILE?=default

0 commit comments

Comments
 (0)