private _removeFromIndices()

in src/InMemoryProvider.ts [424:451]


  private _removeFromIndices(
    key: string,
    item: ItemType,
    removePrimaryKey: boolean
  ) {
    // Don't need to remove from primary key on Puts because set is enough
    // 1. If it's an existing key then it will get overwritten
    // 2. If it's a new key then we need to add it
    if (removePrimaryKey) {
      (this.openPrimaryKey() as InMemoryIndex).remove(key);
    }

    each(this._storeSchema.indexes, (index: IndexSchema) => {
      const ind = this.openIndex(index.name) as InMemoryIndex;
      const indexKeys = ind.internal_getKeysFromItem(item);

      // when it's a unique index, value is the item.
      // in case of a non-unique index, value is an array of items,
      // and we want to only remove items that have the same primary key
      if (ind.isUniqueIndex()) {
        each(indexKeys, (indexKey: string) => ind.remove(indexKey));
      } else {
        each(indexKeys, (idxKey: string) =>
          ind.remove({ idxKey, primaryKey: key })
        );
      }
    });
  }