forked from lucaong/minisearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace.ts
30 lines (28 loc) · 1.03 KB
/
replace.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { type SearchIndex } from "./SearchIndex.js";
import { add } from "./add.js";
import { discard } from "./remove.js";
/**
* It replaces an existing document with the given updated version
*
* It works by discarding the current version and adding the updated one, so
* it is functionally equivalent to calling [[discard]] followed by
* [[add]]. The ID of the updated document should be the same as
* the original one.
*
* Since it uses [[discard]] internally, this method relies on
* vacuuming to clean up obsolete document references from the index, allowing
* memory to be released (see [[discard]]).
*
* @param searchIndex The search Index
* @param updatedDocument The updated document to replace the old version
* with
*/
export const replace = <Document, ID>(
searchIndex: SearchIndex<Document, ID>,
updatedDocument: Document,
): void => {
const { idField, extractField } = searchIndex._options;
const id = <ID>extractField(updatedDocument, idField);
discard(searchIndex, id);
add(searchIndex, updatedDocument);
};