@mistriwork/search-typesense 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,6 +37,7 @@ export declare class TypesenseClient implements SearchIndex {
37
37
  */
38
38
  ensureCollection(name: string, fields: readonly string[]): Promise<void>;
39
39
  upsert(collection: string, document: SearchDocument): Promise<void>;
40
+ upsertBatch(collection: string, documents: readonly SearchDocument[]): Promise<void>;
40
41
  /** Gone from the index; a document that was never there is gone already. With `scope`, only if it is that tenant's (a delete by filter, so it is one step). */
41
42
  remove(collection: string, id: string, scope?: SearchScope): Promise<void>;
42
43
  /** The keys of the best matches for `phrase` across `fields`, best first;
@@ -58,6 +58,35 @@ export class TypesenseClient {
58
58
  const reply = await this.call('POST', `collections/${encodeURIComponent(collection)}/documents`, new URLSearchParams({ action: 'upsert' }), document);
59
59
  this.expect(reply, `index a record in "${collection}"`);
60
60
  }
61
+ async upsertBatch(collection, documents) {
62
+ if (!documents.length)
63
+ return;
64
+ if (documents.length === 1)
65
+ return this.upsert(collection, documents[0]);
66
+ const jsonl = documents.map((doc) => JSON.stringify(doc)).join('\n');
67
+ const url = new URL(`collections/${encodeURIComponent(collection)}/documents/import`, this.connection.base);
68
+ url.search = new URLSearchParams({ action: 'upsert' }).toString();
69
+ const headers = { accept: 'text/plain' };
70
+ if (this.connection.apiKey)
71
+ headers['x-typesense-api-key'] = this.connection.apiKey;
72
+ let reply;
73
+ try {
74
+ reply = await request(url, { method: 'POST', headers, text: { type: 'text/plain', body: jsonl } }, this.connection.policy);
75
+ }
76
+ catch (error) {
77
+ throw new SearchUnavailable(`search "${this.connection.label}": ${error instanceof HttpSourceError ? error.message : 'the request failed.'}`);
78
+ }
79
+ if (reply.status === 404) {
80
+ for (const document of documents)
81
+ await this.upsert(collection, document);
82
+ return;
83
+ }
84
+ this.expect(reply, `bulk-index records in "${collection}"`);
85
+ const result = reply.json;
86
+ if (result && typeof result === 'object' && result.success === false) {
87
+ throw new SearchUnavailable(`search "${this.connection.label}" could not bulk-index records in "${collection}": ${result.error ?? 'rejected.'}`);
88
+ }
89
+ }
61
90
  /** Gone from the index; a document that was never there is gone already. With `scope`, only if it is that tenant's (a delete by filter, so it is one step). */
62
91
  async remove(collection, id, scope) {
63
92
  if (scope) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mistriwork/search-typesense",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Typesense search provider pack for mistri Apps, over core's egress with SSRF protection and zero extra SDK dependencies.",
5
5
  "keywords": [
6
6
  "mistri",
@@ -32,10 +32,10 @@
32
32
  }
33
33
  },
34
34
  "dependencies": {
35
- "@mistriwork/engine": "1.2.0"
35
+ "@mistriwork/engine": "1.3.0"
36
36
  },
37
37
  "peerDependencies": {
38
- "@mistriwork/mistri": "1.2.0"
38
+ "@mistriwork/mistri": "1.3.0"
39
39
  },
40
40
  "engines": {
41
41
  "node": ">=24"