@goatlab/typesense
Version:
Modern TypeScript wrapper for Typesense search engine API
59 lines • 2.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.importDocuments = importDocuments;
const export_formatter_1 = require("../../components/export-formatter");
const typesense_model_1 = require("../../typesense.model");
async function importDocuments(ctx, documents, format = 'jsonl', importOptions, collectionOptions) {
// Validate format
const supportedFormats = ['jsonl', 'json', 'csv'];
if (!supportedFormats.includes(format)) {
throw new typesense_model_1.TypesenseError('Unsupported format', 400);
}
const collectionName = collectionOptions?.collection || ctx.fqcn();
let body;
if (documents instanceof ReadableStream) {
body = documents;
}
else if (typeof documents === 'string') {
if (format === 'csv') {
throw new typesense_model_1.TypesenseError('CSV import requires conversion', 400);
}
if (format === 'json') {
// Convert JSON array string to JSONL
const parsedDocuments = JSON.parse(documents);
const jsonlData = parsedDocuments
.map((doc) => JSON.stringify(doc))
.join('\n');
body = jsonlData;
}
else {
// Assume it's already JSONL
body = documents;
}
}
else {
// Array of documents
if (format === 'csv') {
throw new typesense_model_1.TypesenseError('CSV import requires conversion', 400);
}
const formatted = export_formatter_1.ExportFormatter.formatDocuments(documents, format);
body = formatted;
}
const searchParams = {
...importOptions,
action: importOptions?.action || 'create',
};
// Send body directly to HTTP for large files
const response = await ctx.httpClient.requestTextWithRawBody(`/collections/${collectionName}/documents/import`, {
method: 'POST',
body,
searchParams,
timeout: ctx.httpClient.importTimeout,
});
// Parse JSONL response to array
return response
.split('\n')
.filter(line => line.trim())
.map(line => JSON.parse(line));
}
//# sourceMappingURL=importDocuments.js.map