在 Vapor 中手动在 SQLite 数据库中创建数据

Creating data in SQLite database manually in Vapor

如何在Vapor项目中手动向SQLite数据库中添加数据,以便在启动时已经有一些数据?

我用这个:

struct CreateAdminUser: Migration {

    static func prepare(on connection: SQLiteConnection) -> Future<Void> {
        let password = try? BCrypt.hash("secret")
        let user = User(email: "me@example.co.uk", password: password!)
        return user.save(on: connection).transform(to: ())
    }

    static func revert(on connection: SQLiteConnection) -> Future<Void> {
        return Future.map(on: connection) {}
    }
}

然后我将这一行放入 configure.swift 迁移:

migrations.add(migration: CreateAdminUser.self, database: .sqlite)