I've been trying to link up my PostgreSQL database to a Swift Vapor project so I create routes to it. The first table I want to access is a table in my_database
calls users
. It has the properties user_id
(primary integer key) and created_on
(timestamp with time zone).
I've linked my Vapor project to my_database
so that I can create new models. However, what if I want access to the pre-existing table users
. Here's what I've cobbled together from the documentation and a few tutorials:
My initial model:
final class Users: Model {
static let name = "users"
typealias ID = Int
typealias Database = PostgreSQLDatabase
static let idKey: WritableKeyPath<Users, Int?> = \.user_id
var user_id: Int?
var created_on: Date
}
extension Users: Content { }
My migration (which I think is just a 'blank' migration, just to hook the project to the table?):
struct FirstMigration: PostgreSQLMigration {
static func prepare(on conn: PostgreSQLConnection) -> EventLoopFuture<Void> {
return conn.future()
}
static func revert(on conn: PostgreSQLConnection) -> EventLoopFuture<Void> {
return Future<Void>.done(on: conn)
}
}
And my configuration:
var migrations = MigrationConfig()
migrations.add(migration: FirstMigration.self, database: .psql)
services.register(migrations)
Any guidance much appreciated!