docs: function writeCollection(collectionState, action) {
    const { meta, merge = { doc: true, collections: true } } = action;
    if (meta.storeAs) {
      return action.payload.ordered;
    }
    const collectionStateSize = size(collectionState);
    const payloadExists = !!size(action.payload.ordered);
  
    // Handle doc update (update item in array instead of whole array)
    if (meta.doc && merge.doc && collectionStateSize) {
      // Merge if data already exists
      // Merge with existing ordered array if collection merge enabled
      return modifyDoc(collectionState, action);
    }
  
    // Merge with existing ordered array (existing as source) if collection merge enabled
    if (collectionStateSize && merge.collections) {
      // Listener response is empty - empty state
      if (!payloadExists) {
        return [];
      }
      // Key existing state collection by id (used to prevent multiple array lookups)
      const existingKeys = collectionState && keyBy(collectionState, 'id');
      // Map new doc data with existing doc data (preserves existing sub-collections)
      return map(action.payload.ordered, newDocObj => {
        const existingDoc = get(existingKeys, newDocObj.id);
        // merge with existing doc if existing doc is not equal
        return !!existingDoc && !isEqual(existingDoc, newDocObj)
          ? { ...existingDoc, ...newDocObj }
          : newDocObj;
      });
    }
  
    // Handle subcollections (only when storeAs is not being used)
    if (meta.doc && meta.subcollections) {
      const subcollectionConfig = meta.subcollections[0];
      if (!collectionStateSize) {
        // Collection state does not already exist, create it with item containing
        // subcollection
        return [
          {
            id: meta.doc,
            [subcollectionConfig.collection]: action.payload.ordered,
          },
        ];
      }
      // Merge with existing document if collection state exists
      return updateItemInArray(collectionState, meta.doc, item =>
        // check if action contains ordered payload
        payloadExists
          ? // merge with existing subcollection
            {
              ...item,
              [subcollectionConfig.collection]: unionBy(
                get(item, subcollectionConfig.collection, []),
                action.payload.ordered,
                'id',
              ),
            }
          : // remove subcollection if payload is empty
            omit(item, [subcollectionConfig.collection]),
      );
    }
  
    if (meta.doc && collectionStateSize) {
      // Update item in array
      return updateItemInArray(collectionState, meta.doc, item =>
        mergeObjects(item, action.payload.ordered[0]),
      );
    }
  
    return action.payload.ordered;
  }