diff --git a/src/Typesense/Documents.ts b/src/Typesense/Documents.ts index 77ed9c57..683ddb37 100644 --- a/src/Typesense/Documents.ts +++ b/src/Typesense/Documents.ts @@ -214,12 +214,15 @@ export default class Documents super(collectionName, apiCall, configuration); } - async create(document: T, options: DocumentWriteParameters = {}): Promise { + async create( + document: T, + options: Omit = {}, + ): Promise { if (!document) throw new Error("No document provided"); return this.apiCall.post(this.endpointPath(), document, options); } - async upsert(document: T, options: DocumentWriteParameters = {}): Promise { + async upsert(document: T, options: Omit = {}): Promise { if (!document) throw new Error("No document provided"); return this.apiCall.post( this.endpointPath(), @@ -232,10 +235,10 @@ export default class Documents document: T, options: UpdateByFilterParameters, ): Promise; - async update(document: T, options: DocumentWriteParameters): Promise; + async update(document: T, options: Omit): Promise; async update( document: T, - options: DocumentWriteParameters | UpdateByFilterParameters = {}, + options: Omit | UpdateByFilterParameters = {}, ): Promise { if (!document) throw new Error("No document provided"); @@ -254,6 +257,35 @@ export default class Documents } } + async emplace( + document: T, + options: UpdateByFilterParameters, + ): Promise; + async emplace( + document: T, + options: Omit, + ): Promise; + async emplace( + document: T, + options: Omit | UpdateByFilterParameters = {}, + ): Promise { + if (!document) throw new Error("No document provided"); + + if (options["filter_by"] != null) { + return this.apiCall.patch( + this.endpointPath(), + document, + Object.assign({}, options), + ); + } else { + return this.apiCall.post( + this.endpointPath(), + document, + Object.assign({}, options, { action: "emplace" }), + ); + } + } + async delete( query: DeleteQuery = {} as DeleteQuery, ): Promise> { diff --git a/src/Typesense/Types.ts b/src/Typesense/Types.ts index 5270c1f9..d9ac8c94 100644 --- a/src/Typesense/Types.ts +++ b/src/Typesense/Types.ts @@ -202,9 +202,22 @@ export interface SearchableDocuments< } export interface WriteableDocuments { - create(document: T, options: DocumentWriteParameters): Promise; - upsert(document: T, options: DocumentWriteParameters): Promise; - update(document: T, options: DocumentWriteParameters): Promise; + create( + document: T, + options: Omit, + ): Promise; + upsert( + document: T, + options: Omit, + ): Promise; + update( + document: T, + options: Omit, + ): Promise; + emplace( + document: T, + options: Omit, + ): Promise; delete(query: DeleteQuery): Promise; import( documents: T[] | string, @@ -263,7 +276,14 @@ export type MultiSearchRequestsSchema< export interface UnionSearchResponse extends Omit, "request_params"> { - union_request_params: SearchResponseRequestParams[]; + union_request_params: UnionSearchResponseRequestParams[]; +} + +type AllRequiredBut = Required> & Pick; + +export interface UnionSearchResponseRequestParams + extends AllRequiredBut { + found: number; } export type MultiSearchResponse<