Skip to content

Commit

Permalink
Collection nested queries (#44)
Browse files Browse the repository at this point in the history
* added collection nested query extension

* fixed save inverse relation bug

* added tests for nested queries

* refactoring
  • Loading branch information
KazaiMazai authored Aug 2, 2024
1 parent d8b9c0a commit 41469ee
Show file tree
Hide file tree
Showing 4 changed files with 734 additions and 2 deletions.
51 changes: 51 additions & 0 deletions Sources/SwiftletModel/Model/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,57 @@ public extension Query {
}
}

public extension Collection {
func with<Entity, Child, Directionality, Constraints>(
_ keyPath: WritableKeyPath<Entity, ToOneRelation<Child, Directionality, Constraints>>,
nested: @escaping QueryModifier<Child> = { $0 }) -> [Query<Entity>] where Element == Query<Entity> {

map { $0.with(keyPath, nested: nested) }
}

func with<Entity, Child, Directionality, Constraints>(
_ keyPath: WritableKeyPath<Entity, ToManyRelation<Child, Directionality, Constraints>>,
nested: @escaping QueryModifier<Child> = { $0 }

) -> [Query<Entity>] where Element == Query<Entity> {

map { $0.with(keyPath, nested: nested) }
}

func with<Entity, Child, Directionality, Constraints>(
fragment keyPath: WritableKeyPath<Entity, ToManyRelation<Child, Directionality, Constraints>>,
nested: @escaping QueryModifier<Child> = { $0 }

) -> [Query<Entity>] where Element == Query<Entity> {

map { $0.with(fragment: keyPath, nested: nested) }
}

func id<Entity, Child, Directionality, Constraints>(
_ keyPath: WritableKeyPath<Entity, ToOneRelation<Child, Directionality, Constraints>>

) -> [Query<Entity>] where Element == Query<Entity> {

map { $0.id(keyPath) }
}

func ids<Entity, Child, Directionality, Constraints>(
_ keyPath: WritableKeyPath<Entity, ToManyRelation<Child, Directionality, Constraints>>

) -> [Query<Entity>] where Element == Query<Entity> {

map { $0.ids(keyPath) }
}

func ids<Entity, Child, Directionality, Constraints>(
fragment keyPath: WritableKeyPath<Entity, ToManyRelation<Child, Directionality, Constraints>>

) -> [Query<Entity>] where Element == Query<Entity> {

map { $0.ids(fragment: keyPath) }
}
}

extension Context {
func query<Entity: EntityModel>(_ id: Entity.ID) -> Query<Entity> {
Query(context: self, id: id)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftletModel/Relation/Relation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ extension Relation {
}
}

var inverseLinkUpdateOption: Option {
static var inverseLinkUpdateOption: Option {
Cardinality.isToMany ? .append : .replace
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftletModel/Relation/RelationSave.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private extension EntityModel {
children: [id],
attribute: LinkAttribute(
name: inverse.name,
updateOption: relation(keyPath).inverseLinkUpdateOption
updateOption: MutualRelation<Self, InverseRelation, InverseConstraint>.inverseLinkUpdateOption
)
)
}
Expand Down
Loading

0 comments on commit 41469ee

Please sign in to comment.