weaviate-client
Version:
JS/TS client for Weaviate
56 lines (55 loc) • 2.21 kB
JavaScript
import { ClientError } from 'nice-grpc';
import { BatchObjectsRequest, BatchStreamRequest, } from '../proto/v1/batch.js';
import { WeaviateBatchError, WeaviateBatchStreamError, WeaviateDeleteManyError } from '../errors.js';
import { BatchDeleteRequest } from '../proto/v1/batch_delete.js';
import Base from './base.js';
import { retryOptions } from './retry.js';
export default class Batcher extends Base {
static use(connection, collection, metadata, timeout, consistencyLevel, tenant) {
return new Batcher(connection, collection, metadata, timeout, consistencyLevel, tenant);
}
withStream = (reqs) => this.callStream(reqs);
withDelete = (args) => this.callDelete(BatchDeleteRequest.fromPartial(args));
withObjects = (args) => this.callObjects(BatchObjectsRequest.fromPartial(args));
async *callStream(reqs) {
const consistencyLevel = this.consistencyLevel;
async function* generate() {
yield BatchStreamRequest.create({ start: { consistencyLevel } });
for await (const req of reqs) {
yield req;
}
}
try {
for await (const res of this.connection.batchStream(generate(), { metadata: this.metadata })) {
yield res;
}
}
catch (err) {
if (err instanceof ClientError) {
throw new WeaviateBatchStreamError(err.message);
}
throw err;
}
}
callDelete(message) {
return this.sendWithTimeout((signal) => this.connection.batchDelete({
...message,
collection: this.collection,
consistencyLevel: this.consistencyLevel,
tenant: this.tenant,
}, {
metadata: this.metadata,
signal,
}), (err) => new WeaviateDeleteManyError(err.message));
}
callObjects(message) {
return this.sendWithTimeout((signal) => this.connection.batchObjects({
...message,
consistencyLevel: this.consistencyLevel,
}, {
metadata: this.metadata,
signal,
...retryOptions,
}), (err) => new WeaviateBatchError(err.message));
}
}