-- | equivalent to $each
--
-- > eachOp push field []
--
-- @eachOp pull@ will get translated to @$pullAll@
eachOp :: forall record typ.
       ( PersistField typ, PersistEntityBackend record ~ DB.MongoContext)
       => (EntityField record [typ] -> typ -> Update record)
       -> EntityField record [typ] -> [typ]
       -> Update record
eachOp haskellOp fld val = case haskellOp fld (error "eachOp: undefined") of
    BackendUpdate (ArrayUpdate _ (UpdateValueOp (Left _) (Right (MongoSimple op)))) -> each op
    BackendUpdate (ArrayUpdate{})  -> error "eachOp: unexpected ArrayUpdate"
    BackendUpdate (NestedUpdate{}) -> error "eachOp: did not expect NestedUpdate"
    Update{} -> error "eachOp: did not expect Update"
  where
    each op = BackendUpdate $ ArrayUpdate fld $
      UpdateValueOp (Right val) (Right $ MongoEach op)
eachOp :: forall record typ.
       ( PersistField typ, PersistEntityBackend record ~ DB.MongoContext)
       => (EntityField record [typ] -> typ -> Update record)
       -> EntityField record [typ] -> [typ]
       -> Update record
eachOp haskellOp fld val = case haskellOp fld (error "eachOp: undefined") of
    BackendUpdate (ArrayUpdate _ (UpdateValueOp (Left _) (Right (MongoSimple op)))) -> each op
    BackendUpdate (ArrayUpdate{})  -> error "eachOp: unexpected ArrayUpdate"
    BackendUpdate (NestedUpdate{}) -> error "eachOp: did not expect NestedUpdate"
    Update{} -> error "eachOp: did not expect Update"
  where
    each op = BackendUpdate $ ArrayUpdate fld $
      UpdateValueOp (Right val) (Right $ MongoEach op)
eachOp haskellOp fld val = case haskellOp fld (error "eachOp: undefined") of
    BackendUpdate (ArrayUpdate _ (UpdateValueOp (Left _) (Right (MongoSimple op)))) -> each op
    BackendUpdate (ArrayUpdate{})  -> error "eachOp: unexpected ArrayUpdate"
    BackendUpdate (NestedUpdate{}) -> error "eachOp: did not expect NestedUpdate"
    Update{} -> error "eachOp: did not expect Update"
  where
    each op = BackendUpdate $ ArrayUpdate fld $
      UpdateValueOp (Right val) (Right $ MongoEach op)