// // Role.swift // // // Created by Michiel Carman on 10/24/21. // import Foundation import Vapor import Fluent import FluentPostgresDriver final class mdlRole: Model, Content{ struct Input: Content{ let id: Int? let name: String let description: String let users: [mdlUser]? } struct Output: Content{ let id: Int? let name: String let description: String let users: [mdlUser]? } static let schema = "Role" @ID(custom: "id", generatedBy: .database) var id: Int? @Field(key: "name") var name: String @Field(key: "description") var description: String @Siblings(through: mdlXUserRole.self, from: \.$role, to: \.$user) public var users: [mdlUser] init(){} init(id: Int? = nil){ self.id = id } init(id: Int? = nil, name: String, description: String, users: [mdlUser]?){ self.id = id self.name = name self.description = description self.$users.value = users } } extension mdlRole { struct create: Migration{ func prepare(on database: Database) -> EventLoopFuture { return database.schema("Role") .field("id", .int, .identifier(auto: true)) .field("name", .string) .field("description", .string) .create() } func revert(on database: Database) -> EventLoopFuture { database.schema("Role").delete() } } struct seed: Migration { func prepare(on database: Database) -> EventLoopFuture { // 1 let array: [mdlRole] = [ .init(id: -1, name: "SuperAdmin", description: "System User Account", users: []) ] // 2 return array.map { obj in obj.save(on: database) } .flatten(on: database.eventLoop) } func revert(on database: Database) -> EventLoopFuture { // 3 mdlRole.query(on: database).delete() } } }