UNPKG

@goatlab/typesense

Version:

Modern TypeScript wrapper for Typesense search engine API

58 lines 2.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.importDocuments = importDocuments; const stream_1 = require("stream"); const typesense_model_1 = require("../../typesense.model"); const export_formatter_1 = require("../../components/export-formatter"); 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 bodyStream; if (documents instanceof stream_1.Readable) { bodyStream = documents; } else if (typeof documents === 'string') { if (format === 'csv') { throw new typesense_model_1.TypesenseError('CSV import requires conversion', 400); } else if (format === 'json') { // Convert JSON array string to JSONL const parsedDocuments = JSON.parse(documents); const jsonlData = parsedDocuments.map((doc) => JSON.stringify(doc)).join('\n'); bodyStream = stream_1.Readable.from([jsonlData]); } else { // Assume it's already JSONL bodyStream = stream_1.Readable.from([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); bodyStream = stream_1.Readable.from([formatted]); } const searchParams = { ...importOptions, action: importOptions?.action || 'create' }; // Stream directly to HTTP body for large files const response = await ctx.httpClient.requestTextWithRawBody(`/collections/${collectionName}/documents/import`, { method: 'POST', body: bodyStream, searchParams, timeout: ctx.httpClient['options'].importTimeout }); // Parse JSONL response to array return response .split('\n') .filter(line => line.trim()) .map(line => JSON.parse(line)); } //# sourceMappingURL=importDocuments.js.map