1
A
答えて
4
解決策を見つけました。ここでは、多くの車を所有する所有者の簡単な例ですが、多分誰かに役立つでしょう。
オーナー:
final class Owner: Model {
static let idKey = "id"
static let nameKey = "name"
static let carsKey = "cars"
var name: String
let storage = Storage()
var cars: Children<Owner, Car> {
return children()
}
init(name: String) {
self.name = name
}
init(row: Row) throws {
name = try row.get(Owner.nameKey)
}
func makeRow() throws -> Row {
var row = Row()
try row.set(Owner.nameKey, name)
return row
}
}
extension Owner: Preparation {
static func prepare(_ database: Database) throws {
try database.create(self) { builder in
builder.id()
builder.string(Owner.nameKey)
}
}
static func revert(_ database: Database) throws {
try database.delete(self)
}
}
extension Owner: JSONConvertible {
convenience init(json: JSON) throws {
try self.init(
name: json.get(Owner.nameKey)
)
}
func makeJSON() throws -> JSON {
var json = JSON()
try json.set(Owner.idKey, id)
try json.set(Owner.nameKey, name)
try json.set(Owner.carsKey, try cars.all())
return json
}
}
extension Owner: ResponseRepresentable { }
extension Owner: Updateable {
public static var updateableKeys: [UpdateableKey<Owner>] {
return [
UpdateableKey(Owner.nameKey, String.self) { owner, text in
owner.name = name
}
]
}
}
車:
final class Car: Model {
static let idKey = "id"
static let makeKey = "make"
static let modelKey = "model"
static let ownerIdKey = "owner_id"
var make: String
var model: String
var ownerId: Identifier
let storage = Storage()
var owner: Parent<Car, Owner> {
return parent(id: ownerId)
}
init(make: String, model: String, ownerId: Identifier) {
self.make = make
self.model = model
self.ownerId = ownerId
}
init(row: Row) throws {
make = try row.get(Car.makeKey)
model = try row.get(Car.modelKey)
ownerId = try row.get(Car.ownerIdKey)
}
func makeRow() throws -> Row {
var row = Row()
try row.set(Car.makeKey, make)
try row.set(Car.modelKey, model)
try row.set(Car.ownerIdKey, ownerId)
return row
}
}
extension Car: JSONConvertible {
convenience init(json: JSON) throws {
try self.init(
make: json.get(Car.makeKey),
model: json.get(Car.modelKey),
ownerId: json.get(Car.ownerIdKey)
)
}
func makeJSON() throws -> JSON {
var json = JSON()
try json.set(Car.idKey, id)
try json.set(Car.makeKey, make)
try json.set(Car.modelKey, model)
try json.set(Car.ownerIdKey, ownerId)
return json
}
}
extension Car: ResponseRepresentable {}
extension Car: Preparation {
static func prepare(_ database: Database) throws {
try database.create(self) { builder in
builder.id()
builder.string(Car.makeKey)
builder.string(Car.modelKey)
builder.foreignId(for: Owner.self)
}
}
static func revert(_ database: Database) throws {
try database.delete(self)
}
}
extension Car: Updateable {
public static var updateableKeys: [UpdateableKey<Car>] {
return [
UpdateableKey(Car.makeKey, String.self) { car, make in
car.make = make
}
]
}
}
関連する問題
- 1. 1対1と1対多の関係
- 2. laravel 1対多対多の関係
- 3. AutoMapper 1対多の関係
- 4. MvcScaffolding 1対多の関係
- 5. エンティティフレームワーク1対多の関係
- 6. Laravel多対1の関係
- 7. JPA多対1の関係
- 8. django 1対多の関係
- 9. Kinvey 1対多の関係
- 10. 1対多の関係mongoDB
- 11. 2対多の関係
- 12. JPA多対2の関係
- 13. 1対1の関係から多対多の関係への移動
- 14. doctrine 2多対多の関係
- 15. 1対1の関係を1対多の関係で返す方法
- 16. エンティティフレームワークの1対多および多対多の関係
- 17. 2対1の関係EF
- 18. 私の蒸気アプリケーション用蒸気
- 19. 多対1のクラス関係の作成
- 20. 特定の1対多の関係
- 21. Railsの1対多の関係
- 22. mongooseの1対多の関係
- 23. symfony3の1対多の関係__toStringエラー
- 24. エンティティフレームワークのモデリングと1対多の関係
- 25. スイフトのレルムと1対多の関係
- 26. Embeddable、Hibernate内の多対1の関係
- 27. ActiveRecordの協会:1対多の関係
- 28. データストアの1対多の関係
- 29. REST APIの1対多のリソース関係
- 30. laravelの1対多の関係
[蒸気としてラボ(https://github.com/VaporExamplesLab)蒸気に古いバージョン蒸気例の移行を有します2 + Swift 4.「1対多」(親子関係)の例は、['ServerSideSwift_J_RelationsAwt'](https://github.com/VaporExamplesLab/ServerSideSwift_J_RelationsAwt)の例にあります。 –