Skip to content

Commit abefc46

Browse files
committed
feat: update version to 0.2.0 and enhance BrightClient with document and search operations
1 parent 27aad34 commit abefc46

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bright-client",
3-
"version": "0.1.2",
3+
"version": "0.2.0",
44
"description": "TypeScript client library for Bright search database",
55
"main": "dist/index.cjs",
66
"module": "dist/index.mjs",

src/index.ts

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ export interface BrightClientOptions {
2424
fetch?: typeof fetch;
2525
}
2626

27+
export interface IndexHandle<T = Record<string, any>> {
28+
readonly id: string;
29+
30+
// Index operations
31+
update(config: Partial<IndexConfig>): Promise<IndexConfig>;
32+
delete(): Promise<void>;
33+
34+
// Document operations
35+
addDocuments(documents: T[], format?: 'jsoneachrow'): Promise<{ indexed: number }>;
36+
updateDocument(documentId: string, updates: Partial<T>): Promise<T>;
37+
deleteDocument(documentId: string): Promise<void>;
38+
deleteDocuments(options: { ids?: string[]; filter?: string }): Promise<void>;
39+
40+
// Search
41+
search(params?: SearchParams): Promise<SearchResponse<T>>;
42+
}
43+
2744
export class BrightClient {
2845
private baseUrl: string;
2946
private fetchFn: typeof fetch;
@@ -84,26 +101,26 @@ export class BrightClient {
84101

85102
// Document Operations
86103

87-
async addDocuments(
104+
async addDocuments<T = Record<string, any>>(
88105
indexId: string,
89-
documents: Record<string, any>[],
106+
documents: T[],
90107
format: 'jsoneachrow' = 'jsoneachrow'
91108
): Promise<{ indexed: number }> {
92109
const body = documents.map(doc => JSON.stringify(doc)).join('\n');
93110
const params = new URLSearchParams({ format });
94-
111+
95112
return this.request<{ indexed: number }>(`/indexes/${indexId}/documents?${params}`, {
96113
method: 'POST',
97114
body,
98115
});
99116
}
100117

101-
async updateDocument(
118+
async updateDocument<T = Record<string, any>>(
102119
indexId: string,
103120
documentId: string,
104-
updates: Record<string, any>
105-
): Promise<Record<string, any>> {
106-
return this.request<Record<string, any>>(`/indexes/${indexId}/documents/${documentId}`, {
121+
updates: Partial<T>
122+
): Promise<T> {
123+
return this.request<T>(`/indexes/${indexId}/documents/${documentId}`, {
107124
method: 'PATCH',
108125
body: JSON.stringify(updates),
109126
});
@@ -141,32 +158,50 @@ export class BrightClient {
141158
params?: SearchParams
142159
): Promise<SearchResponse<T>> {
143160
const searchParams = new URLSearchParams();
144-
161+
145162
if (params?.q) searchParams.append('q', params.q);
146163
if (params?.offset) searchParams.append('offset', params.offset.toString());
147164
if (params?.limit) searchParams.append('limit', params.limit.toString());
148165
if (params?.page) searchParams.append('page', params.page.toString());
149-
166+
150167
if (params?.sort) {
151168
params.sort.forEach(s => searchParams.append('sort[]', s));
152169
}
153-
170+
154171
if (params?.attributesToRetrieve) {
155-
params.attributesToRetrieve.forEach(attr =>
172+
params.attributesToRetrieve.forEach(attr =>
156173
searchParams.append('attributesToRetrieve[]', attr)
157174
);
158175
}
159-
176+
160177
if (params?.attributesToExclude) {
161-
params.attributesToExclude.forEach(attr =>
178+
params.attributesToExclude.forEach(attr =>
162179
searchParams.append('attributesToExclude[]', attr)
163180
);
164181
}
165-
182+
166183
return this.request<SearchResponse<T>>(`/indexes/${indexId}/searches?${searchParams}`, {
167184
method: 'POST',
168185
});
169186
}
187+
188+
// Typed Index Handle
189+
190+
index<T = Record<string, any>>(indexId: string): IndexHandle<T> {
191+
return {
192+
id: indexId,
193+
194+
update: (config) => this.updateIndex(indexId, config),
195+
delete: () => this.deleteIndex(indexId),
196+
197+
addDocuments: (documents, format) => this.addDocuments<T>(indexId, documents, format),
198+
updateDocument: (documentId, updates) => this.updateDocument<T>(indexId, documentId, updates),
199+
deleteDocument: (documentId) => this.deleteDocument(indexId, documentId),
200+
deleteDocuments: (options) => this.deleteDocuments(indexId, options),
201+
202+
search: (params) => this.search<T>(indexId, params),
203+
};
204+
}
170205
}
171206

172207
// Convenience function

0 commit comments

Comments
 (0)