diff --git a/Sources/VaporJsonApi/Resources/JsonApiChildrenModel.swift b/Sources/VaporJsonApi/Resources/JsonApiChildrenModel.swift index 4df384e..e5b784c 100644 --- a/Sources/VaporJsonApi/Resources/JsonApiChildrenModel.swift +++ b/Sources/VaporJsonApi/Resources/JsonApiChildrenModel.swift @@ -21,10 +21,16 @@ public struct JsonApiChildrenModel { public let adder: JsonApiChildrenAdder? public let replacer: JsonApiChildrenReplacer? - public init(childrenType: JsonApiResourceModel.Type, getter: @escaping JsonApiChildrenGetter, adder: JsonApiChildrenAdder? = nil, replacer: JsonApiChildrenReplacer? = nil) { + public let findInModel: (_ id: NodeRepresentable) throws -> JsonApiResourceModel? + + public init(childrenType: T.Type, getter: @escaping JsonApiChildrenGetter, adder: JsonApiChildrenAdder? = nil, replacer: JsonApiChildrenReplacer? = nil) { self.type = childrenType self.getter = getter self.adder = adder self.replacer = replacer + + self.findInModel = { id in + return try T.find(id) + } } } diff --git a/Sources/VaporJsonApi/Resources/JsonApiParentModel.swift b/Sources/VaporJsonApi/Resources/JsonApiParentModel.swift index d2f1108..6501dd6 100644 --- a/Sources/VaporJsonApi/Resources/JsonApiParentModel.swift +++ b/Sources/VaporJsonApi/Resources/JsonApiParentModel.swift @@ -19,9 +19,15 @@ public struct JsonApiParentModel { public let getter: JsonApiParentGetter public let setter: JsonApiParentSetter? - public init(parentType: JsonApiResourceModel.Type, getter: @escaping JsonApiParentGetter, setter: JsonApiParentSetter? = nil) { + public let findInModel: (_ id: NodeRepresentable) throws -> JsonApiResourceModel? + + public init(parentType: T.Type, getter: @escaping JsonApiParentGetter, setter: JsonApiParentSetter? = nil) { self.getter = getter self.setter = setter self.type = parentType + + self.findInModel = { id in + return try T.find(id) + } } } diff --git a/Sources/VaporJsonApi/Resources/JsonApiSiblingsModel.swift b/Sources/VaporJsonApi/Resources/JsonApiSiblingsModel.swift index 4086572..701b683 100644 --- a/Sources/VaporJsonApi/Resources/JsonApiSiblingsModel.swift +++ b/Sources/VaporJsonApi/Resources/JsonApiSiblingsModel.swift @@ -21,11 +21,17 @@ public struct JsonApiSiblingsModel { public let adder: JsonApiSiblingsAdder? public let replacer: JsonApiSiblingsReplacer? - public init(siblingType: JsonApiResourceModel.Type, getter: @escaping JsonApiSiblingsGetter, adder: JsonApiSiblingsAdder? = nil, replacer: JsonApiSiblingsReplacer? = nil) { + public let findInModel: (_ id: NodeRepresentable) throws -> JsonApiResourceModel? + + public init(siblingType: T.Type, getter: @escaping JsonApiSiblingsGetter, adder: JsonApiSiblingsAdder? = nil, replacer: JsonApiSiblingsReplacer? = nil) { self.getter = getter self.adder = adder self.replacer = replacer self.type = siblingType + + self.findInModel = { id in + return try T.find(id) + } } public init(model: S, siblingType: T.Type, localKey: String? = nil, foreignKey: String? = nil) { @@ -51,5 +57,9 @@ public struct JsonApiSiblingsModel { } self.type = siblingType + + self.findInModel = { id in + return try T.find(id) + } } }