Vapor 3 PostgreSQL CRUD 没有请求 http

编程入门 行业动态 更新时间:2024-10-26 11:13:57
本文介绍了Vapor 3 PostgreSQL CRUD 没有请求 http的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 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

本文发布于:2023-10-22 23:59:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519122.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PostgreSQL   Vapor   http   CRUD

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!