Skip to content

Commit

Permalink
add setField for optional in updatequery
Browse files Browse the repository at this point in the history
  • Loading branch information
JaapWijnen committed Sep 6, 2022
1 parent aabce0b commit 7b44033
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Sources/Meow/KeyPathModel/MeowCollection+KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ public struct ModelUpdateQuery<M: KeyPathQueryableModel & MutableModel> {
set[path] = newValue
}

public mutating func setField<P: Primitive>(at keyPath: WritableKeyPath<M, QueryableField<P?>>, to newValue: P?) {
if let newValue = newValue {
setField(at: keyPath, to: newValue)
} else {
unsetField(at: keyPath)
}
}

/// Adds an atomic `$unset` to the update query that updates the field corresponding to `keyPath` to be removed
public mutating func unsetField<Value>(at keyPath: WritableKeyPath<M, QueryableField<Value?>>) {
let path = M.resolveFieldPath(keyPath).joined(separator: ".")
Expand Down

0 comments on commit 7b44033

Please sign in to comment.