问题描述
我正在使用 Swift 5 和 Vapor 4 编写后端,数据库是 PostgreSQL 12.我有一个问题:如何在没有 POST 或 GET 请求的情况下在本地与数据库 (CRUD) 交互?所有教程再次展示了如何仅基于请求通过 HTTPS 进行 CRUD.我已经问过这个问题:Vapor 3 PostgreSQL CRUD without requests http但这不是一个重复的问题,Vapor 4 的工作原理完全不同!请看看我目前的 Vapor 3 工作班级(基于这个答案:是否可以在独立脚本中使用 Vapor 3 Postgres Fluent?):
I am writing backend using Swift 5 and Vapor 4, database is PostgreSQL 12. I have a question: how to interact with database (CRUD) locally, without POST or GET requests? All tutorials show how to CRUD only based on Request through HTTPS again. I already asked this question: Vapor 3 PostgreSQL CRUD without requests httpBUT IT'S NOT A DUPLICATE QUESTION, Vapor 4 works completely different! Please, look at my current, working class for Vapor 3 (based on this answer: Is is possible to use Vapor 3 Postgres Fluent in a standalone script?):
import Vapor
import FluentPostgreSQL
final class WorkWithPostgres {
private let databaseConfig = PostgreSQLDatabaseConfig(hostname: "localhost", port: 5432, username: "RutrackerTech", database: "vaporpostgres", password: "ru628trac4er")
let postgres: PostgreSQLDatabase
static let shared = WorkWithPostgres()
private init() {
postgres = PostgreSQLDatabase(config: databaseConfig)
}
/// Will it create undeleted memory leaks?
func shutdownGracefully(worker: MultiThreadedEventLoopGroup, completion: (() -> Void)?) {
worker.shutdownGracefully { error in
completion?()
}
}
/// Will it create udeleted memory leaks?
func connect(completion: ((MultiThreadedEventLoopGroup, PostgreSQLConnection) -> Void)?) {
let worker = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let eventLoopFuturePostgreSQLConnection = postgres.newConnection(on: worker)
let _ = eventLoopFuturePostgreSQLConnection.map { postgreSQLConnection in
completion?(worker, postgreSQLConnection)
}
}
func readAll<T: PostgreSQLModel>(postgreSQLModel: T.Type, completion: (([T]) -> Void)?) {
connect { worker, connection in
let _ = postgreSQLModel.query(on: connection).all().map { databaseData in
self.shutdownGracefully(worker: worker) {
completion?(databaseData)
}
}
}
}
func create<T: PostgreSQLModel>(postgreSQLModel: T) {
connect { worker, connection in
let _ = postgreSQLModel.save(on: connection).whenComplete {
self.shutdownGracefully(worker: worker, completion: nil)
}
}
}
}
最大的问题:我在 FluentPostgresDriver
或 Fluent
中找不到 PostgreSQLDatabase
的替代品.或者,也许,应该是解决此任务的另一种方法?
The biggest problem: I cannot find replacement to PostgreSQLDatabase
in FluentPostgresDriver
or in Fluent
.Or, maybe, should be another approach to solve this task?
感谢您的阅读,如果您有任何帮助或建议,我将不胜感激!
Thank you for reading, I will be thankful for any help or advice!
推荐答案
所以,正确的想法是不要像使用 Vapor 3 那样使用数据库.现在,在 Vapor 4 中,您应该使用 Database
实例.从哪里得到它?我可以为您推荐 2 个选项.首先是 boot.swift
中的 boot(_ app: Application)
或 configure.swift 中的
configure(_ app: Application)
代码>.所以 app
将有选项来处理这样的数据库:<Model>.query(on: app.db)
.第二个选项是任何请求中的 req
.req
应该是 Request
类型,像这样:.query(on: req.db)
.通过Model
,你可以取任何继承自Model
类的类,例如:final class User: Model, Content {
你应该组织你的代码从 boot.swift
或 configure.swift
或任何请求调用,例如,在 routes.swift
So, correct idea not to work with database like I did with Vapor 3. Now, in Vapor 4, you should use Database
instance. Where to get it? I can suggest 2 options for you. First is boot(_ app: Application)
in boot.swift
or in configure(_ app: Application)
in configure.swift
. So app
will have options to work with database like this: <Model>.query(on: app.db)
. Second option is req
in any request. req
should be of type Request
, like this: <Model>.query(on: req.db)
. By Model
, you can take any class inherited of class Model
, for example: final class User: Model, Content {
You should organise your code to be called from boot.swift
or configure.swift
or from any request, for example, in routes.swift
这篇关于没有 http 请求的 Vapor 4 PostgreSQL CRUD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!