Snapshot của vapor/postgres-nio: 430★ · Swift · Data. 🐘 Non-blocking, event-driven Swift client for PostgreSQL.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
let client = PostgresClient(configuration: config)
Once you have create your client, you must run() it:
await withTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask {
await client.run() // !important
}
// You can use the client while the `client.run()` method is not cancelled.
// To shutdown the client, cancel its run method, by cancelling the taskGroup.
taskGroup.cancelAll()
}
Querying
Once a client is running, queries can be sent to the server. This is straightforward:
let rows = try await client.query("SELECT id, username, birthday FROM users")
The query will return a PostgresRowSequence, which is an AsyncSequence of PostgresRows.
The rows can be iterated one-by-one:
for try await row in rows {
// do something with the row
}
Decoding from PostgresRow
However, in most cases it is much easier to request a row's fields as a set of Swift types:
for try await (id, username, birthday) in rows.decode((Int, String, Date).self) {
// do something with the datatypes.
}
A type must implement the PostgresDecodable protocol in order to be decoded from a row. PostgresNIO provides default implementations for most of Swift's builtin types, as well as some types provided by Foundation:
Bool
Bytes, Data, ByteBuffer
Date
UInt8, Int16, Int32, Int64, Int
Float, Double
String
UUID
Querying with parameters
Sending parameterized queries to the database is also supported (in the coolest way possible):
let id = 1
let username = "fancyuser"
let birthday = Date()
try await client.query("""
INSERT INTO users (id, username, birthday) VALUES (\(id), \(username), \(birthday))
""",
logger: logger
)
While this looks at first glance like a classic case of SQL injection 😱, PostgresNIO's API ensures that this usage is safe. The first parameter of the query(_:logger:) method is not a plain String, but a PostgresQuery, which implements Swift's ExpressibleByStringInterpolation protocol. PostgresNIO uses the literal parts of the provided string as the SQL query and replaces each interpolated value with a parameter binding. Only values which implement the PostgresEncodable protocol may be interpolated in this way. As with PostgresDecodable, PostgresNIO provides default implementations for most common types.
Some queries do not receive any rows from the server (most often INSERT, UPDATE, and DELETE queries with no RETURNING clause, not to mention most DDL queries). To support this, the query(_:logger:) method is marked @discardableResult, so that the compiler does not issue a warning if the return value is not used.
Security
Please see SECURITY.md for details on the security process.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/vapor/postgres-nio là nguồn chính thức.
vapor/postgres-nio có những chủ đề gì?
GitHub topics của vapor/postgres-nio: "postgresql", "server-side-swift", "swift", "swiftnio", "vapor". TopGit xếp repo vào nhóm Data.
vapor/postgres-nio có phải mã nguồn mở không?
Có — vapor/postgres-nio phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/vapor/postgres-nio.
vapor/postgres-nio có trang demo không?
Dự án có trang chủ ở https://api.vapor.codes/postgresnio/documentation/postgresnio/. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
vapor/postgres-nio còn đang phát triển không?
Commit gần nhất trên vapor/postgres-nio là 12 ngày trước (theo timestamp GitHub). Repo có 115 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
vapor/postgres-nio dùng license gì?
vapor/postgres-nio phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
Đọc đầy đủ README ở tab phía trên.
postgres-nio có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về postgres-nio.