Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/Typesense/Documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,15 @@ export default class Documents<T extends DocumentSchema = object>
super(collectionName, apiCall, configuration);
}

async create(document: T, options: DocumentWriteParameters = {}): Promise<T> {
async create(
document: T,
options: Omit<DocumentWriteParameters, "action"> = {},
): Promise<T> {
if (!document) throw new Error("No document provided");
return this.apiCall.post<T>(this.endpointPath(), document, options);
}

async upsert(document: T, options: DocumentWriteParameters = {}): Promise<T> {
async upsert(document: T, options: Omit<DocumentWriteParameters, "action"> = {}): Promise<T> {
if (!document) throw new Error("No document provided");
return this.apiCall.post<T>(
this.endpointPath(),
Expand All @@ -232,10 +235,10 @@ export default class Documents<T extends DocumentSchema = object>
document: T,
options: UpdateByFilterParameters,
): Promise<UpdateByFilterResponse>;
async update(document: T, options: DocumentWriteParameters): Promise<T>;
async update(document: T, options: Omit<DocumentWriteParameters, "action">): Promise<T>;
async update(
document: T,
options: DocumentWriteParameters | UpdateByFilterParameters = {},
options: Omit<DocumentWriteParameters, "action"> | UpdateByFilterParameters = {},
): Promise<UpdateByFilterResponse | T> {
if (!document) throw new Error("No document provided");

Expand All @@ -254,6 +257,35 @@ export default class Documents<T extends DocumentSchema = object>
}
}

async emplace(
document: T,
options: UpdateByFilterParameters,
): Promise<UpdateByFilterResponse>;
async emplace(
document: T,
options: Omit<DocumentWriteParameters, "action">,
): Promise<T>;
async emplace(
document: T,
options: Omit<DocumentWriteParameters, "action"> | UpdateByFilterParameters = {},
): Promise<UpdateByFilterResponse | T> {
if (!document) throw new Error("No document provided");

if (options["filter_by"] != null) {
return this.apiCall.patch<T>(
this.endpointPath(),
document,
Object.assign({}, options),
);
} else {
return this.apiCall.post<T>(
this.endpointPath(),
document,
Object.assign({}, options, { action: "emplace" }),
);
}
}

async delete(
query: DeleteQuery = {} as DeleteQuery,
): Promise<DeleteResponse<T>> {
Expand Down
28 changes: 24 additions & 4 deletions src/Typesense/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,22 @@ export interface SearchableDocuments<
}

export interface WriteableDocuments<T> {
create(document: T, options: DocumentWriteParameters): Promise<T>;
upsert(document: T, options: DocumentWriteParameters): Promise<T>;
update(document: T, options: DocumentWriteParameters): Promise<T>;
create(
document: T,
options: Omit<DocumentWriteParameters, "action">,
): Promise<T>;
upsert(
document: T,
options: Omit<DocumentWriteParameters, "action">,
): Promise<T>;
update(
document: T,
options: Omit<DocumentWriteParameters, "action">,
): Promise<T>;
emplace(
document: T,
options: Omit<DocumentWriteParameters, "action">,
): Promise<T>;
delete(query: DeleteQuery): Promise<DeleteResponse>;
import(
documents: T[] | string,
Expand Down Expand Up @@ -263,7 +276,14 @@ export type MultiSearchRequestsSchema<

export interface UnionSearchResponse<T extends DocumentSchema>
extends Omit<SearchResponse<T>, "request_params"> {
union_request_params: SearchResponseRequestParams[];
union_request_params: UnionSearchResponseRequestParams[];
}

type AllRequiredBut<T, K extends keyof T> = Required<Omit<T, K>> & Pick<T, K>;

export interface UnionSearchResponseRequestParams
extends AllRequiredBut<SearchResponseRequestParams, "voice_query"> {
found: number;
}

export type MultiSearchResponse<
Expand Down
Loading