Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adirburke/5ff2ffa3663bda2a8664bf1afd5883a7 to your computer and use it in GitHub Desktop.
Save adirburke/5ff2ffa3663bda2a8664bf1afd5883a7 to your computer and use it in GitHub Desktop.
Fluent4 model with automigration example (automigration like in Fluent3)
// configure.swift
func configure(_ app: Application) throws {
app.migrations.add(Todo(), to: .psql)
try app.migrator.setupIfNeeded().wait()
try app.migrator.prepareBatch().wait()
}
// Todo.swift
final class Todo: Model, Content {
static let schema = "todos"
@ID(key: "id")
var id: UUID?
@Field(key: "title")
var title: String
@Timestamp(key: "created_at", on: .create)
public var createdAt: Date?
@Timestamp(key: "updated_at", on: .update)
public var updatedAt: Date?
init() { }
init(id: UUID? = nil, title: String) {
self.id = id
self.title = title
}
}
extension Todo: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema(Self.schema)
.field("id", .uuid, .identifier(auto: false))
.field("title", .datetime, .required)
.field("created_at", .datetime)
.field("updated_at", .datetime)
.create()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema(Self.schema).delete()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment