仅当父级存在于使用 Fluent 的 Vapor 中时才创建记录
Create record only if parent exists in Vapor using Fluent
我有 3 个表,LastName、MiddleName 和 FirstName,仅当 lastName 存在时,我想在 FirstName、MiddleName 中创建新记录或更新记录。如果姓氏不存在,那么想 return 返回错误。
final class LastName: Model, Content {
static let schema = "lastnames"
@ID(key: .id)
var id: UUID?
@Field(key: "name")
var name: String
@Children(for: \.$lastname)
var middle_names: [MiddleName]
init() { }
init(id: UUID? = nil, name: String) {
self.id = id
self.name = name
}
}
final class MiddleName: Model, Content {
static let schema = "middlenames"
@ID(key: .id)
var id: UUID?
@Parent(key: "last_name_id")
var lastname: LastName
@Field(key: "name")
var name: String
@Children(for: \.$middleNameId)
var firstNames: [FirstName]
init() { }
init(id: UUID? = nil, lastname: LastName, name: String ) {
self.id = id
self.lastname = lastname
self.name = name
}
}
final class FirstName: Model, Content {
static let schema = "firstnames"
@ID(key: .id)
var id: UUID?
@Parent(key: "middle_name_id")
var middleNameId: MiddleName
@Field(key: "name")
var name: String
init() { }
init(id: UUID? = nil, middleNameId: MiddleName, name: String) {
self.id = id
self.middleNameId = testBundleId
self.name = name
}
}
Fluent 文档很少介绍通过嵌套连接创建新记录,因为在创建姓氏时会自动生成 ID,我如何获取用于创建 MiddleName/FirstName 的 ID。还有什么好的例子可以根据关系在多个表中进行更新或创建?
基本上我想要流利的做一些像
LastName.query(on: req.db).filter(\.$name == "Smith")
.first()
.unwrap(or: Abort(.notFound))
// Now Find if middle name exists, if exists use the ID to create first name and maybe even update middle name table,
// if middle name does not exist, create middle name record, then use the new ID to create first name record
// return the new record(s) as some new Swift Codable DTO
你可以这样做
LastName.query(on: req.db).filter(\.$name == "Smith")
.first()
.unwrap(or: Abort(.notFound))
.flatMap { lastName in
let middlenameQuery = MiddleName.query(on: req.db).filter(\.$name == "Jane").first().flatMap { middlenameFound in
if let middleName = middlenameFound {
return req.eventLoop.future(middleName)
} else {
let newMiddlename = Middlename(name: "Jane")
return newMiddlename.create(on: req.db).transform(to: newMiddlename)
}
}
return middlenameQuery.flatMap { middlename in
// Use last name and middle name here
}
}
我有 3 个表,LastName、MiddleName 和 FirstName,仅当 lastName 存在时,我想在 FirstName、MiddleName 中创建新记录或更新记录。如果姓氏不存在,那么想 return 返回错误。
final class LastName: Model, Content {
static let schema = "lastnames"
@ID(key: .id)
var id: UUID?
@Field(key: "name")
var name: String
@Children(for: \.$lastname)
var middle_names: [MiddleName]
init() { }
init(id: UUID? = nil, name: String) {
self.id = id
self.name = name
}
}
final class MiddleName: Model, Content {
static let schema = "middlenames"
@ID(key: .id)
var id: UUID?
@Parent(key: "last_name_id")
var lastname: LastName
@Field(key: "name")
var name: String
@Children(for: \.$middleNameId)
var firstNames: [FirstName]
init() { }
init(id: UUID? = nil, lastname: LastName, name: String ) {
self.id = id
self.lastname = lastname
self.name = name
}
}
final class FirstName: Model, Content {
static let schema = "firstnames"
@ID(key: .id)
var id: UUID?
@Parent(key: "middle_name_id")
var middleNameId: MiddleName
@Field(key: "name")
var name: String
init() { }
init(id: UUID? = nil, middleNameId: MiddleName, name: String) {
self.id = id
self.middleNameId = testBundleId
self.name = name
}
}
Fluent 文档很少介绍通过嵌套连接创建新记录,因为在创建姓氏时会自动生成 ID,我如何获取用于创建 MiddleName/FirstName 的 ID。还有什么好的例子可以根据关系在多个表中进行更新或创建?
基本上我想要流利的做一些像
LastName.query(on: req.db).filter(\.$name == "Smith")
.first()
.unwrap(or: Abort(.notFound))
// Now Find if middle name exists, if exists use the ID to create first name and maybe even update middle name table,
// if middle name does not exist, create middle name record, then use the new ID to create first name record
// return the new record(s) as some new Swift Codable DTO
你可以这样做
LastName.query(on: req.db).filter(\.$name == "Smith")
.first()
.unwrap(or: Abort(.notFound))
.flatMap { lastName in
let middlenameQuery = MiddleName.query(on: req.db).filter(\.$name == "Jane").first().flatMap { middlenameFound in
if let middleName = middlenameFound {
return req.eventLoop.future(middleName)
} else {
let newMiddlename = Middlename(name: "Jane")
return newMiddlename.create(on: req.db).transform(to: newMiddlename)
}
}
return middlenameQuery.flatMap { middlename in
// Use last name and middle name here
}
}