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 {}
classical method to write or read is:
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)
}
}
}
How to replace req
to another background, safe thread, which I can create locally?
For example:
let device = Device(isWorking: true, serial: 54321)
device.save(on: <#T##DatabaseConnectable#>)
How to replace <#T##DatabaseConnectable#>
?
I will be thankful for any help or advice.