我正在使用 Xcode 11.2 和 Swift 5.1 创建基于 Vapor 3.1.10 的后端,数据库是 PostgreSQL 12.我有一个问题:如何在没有 POST 和 GET 请求的情况下与数据库 (CRUD) 交互.所有教程都展示了如何仅基于通过 HTTPS 的请求进行 CRUD.但是如果我的应用程序需要在数据库中保存一些东西而不与网络交互怎么办?看看我的代码:
I am creating backend based on Vapor 3.1.10 using Xcode 11.2 and Swift 5.1, database is PostgreSQL 12. I have a question: how to interact with database (CRUD) without POST and GET requests. All tutorials show how to CRUD only based on Request through HTTPS. But what if my app needs to save something in database without interacting with network? Look at my code:
import Vapor import FluentPostgreSQL final class Device: PostgreSQLModel { var id: Int? var isWorking: Bool var serial: Int init(isWorking: Bool, serial: Int) { self.isWorking = isWorking self.serial = serial } } extension Device: Content {} extension Device: Migration {} extension Device: Parameter {}写或读的经典方法是:
import Vapor final class DeviceController { func readAll(_ req: Request) throws -> Future<[Device]> { return Device.query(on: req).all() } func create(_ req: Request) throws -> Future<Device> { return try req.content.decode(Device.self).flatMap { device in return device.save(on: req) } } }如何将 req 替换为另一个可以在本地创建的后台安全线程?
How to replace req to another background, safe thread, which I can create locally?
例如:
let device = Device(isWorking: true, serial: 54321) device.save(on: <#T##DatabaseConnectable#>)如何替换?
我将感谢您的任何帮助或建议.
I will be thankful for any help or advice.
推荐答案基于这个问题和答案 (是否可以在独立脚本中使用 Vapor 3 Postgres Fluent?)我实现了这样的 CRUD:
Based on this question and answers (Is is possible to use Vapor 3 Postgres Fluent in a standalone script?) I realized CRUD like this:
import Vapor import FluentPostgreSQL final class Device: PostgreSQLModel { var id: Int? var isWorking: Bool var serial: Int init(isWorking: Bool, serial: Int) { self.isWorking = isWorking self.serial = serial } } extension Device: Content {} extension Device: Migration {} extension Device: Parameter {} final class WorkWithPostgres { let databaseConfig = PostgreSQLDatabaseConfig(hostname: "localhost", port: 5432, username: "username", database: "testestest", password: nil) let database: PostgreSQLDatabase static let shared = WorkWithPostgres() private init() { database = PostgreSQLDatabase(config: databaseConfig) } func readAll<T: PostgreSQLModel>(postgreSQLModel: T.Type, completion: (([T]) -> Void)?) { let worker = MultiThreadedEventLoopGroup(numberOfThreads: 1) let conn = database.newConnection(on: worker) let _ = conn.map { connection in postgreSQLModel.query(on: connection).all().map { databaseData in worker.shutdownGracefully { _ in } completion?(databaseData) } } } func create<T: PostgreSQLModel>(postgreSQLModel: T) { let worker = MultiThreadedEventLoopGroup(numberOfThreads: 1) let conn = database.newConnection(on: worker) let _ = conn.map { connection in let _ = postgreSQLModel.save(on: connection).whenComplete { worker.shutdownGracefully { _ in } } } } } final class DeviceController { func readAll(completion: (([Device]) -> Void)?) { WorkWithPostgres.shared.readAll(postgreSQLModel: Device.self) { devices in completion?(devices) } } func create(isWorking: Bool, serial: Int) { let device = Device(isWorking: isWorking, serial: serial) WorkWithPostgres.shared.create(postgreSQLModel: device) } }它正在工作,但我不确定这是不是这样做的好方法.有人知道吗?
It is working, but I am not sure is it good way to do this. Does somebody know?
更多推荐
Vapor 3 PostgreSQL CRUD 没有请求 http
发布评论