@goatlab/typesense
Version:
Modern TypeScript wrapper for Typesense search engine API
51 lines • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.exportDocuments = exportDocuments;
exports.exportDocumentsStream = exportDocumentsStream;
const stream_1 = require("stream");
const typesense_model_1 = require("../../typesense.model");
const export_formatter_1 = require("../../components/export-formatter");
async function exportDocuments(ctx, format = 'jsonl', options) {
// Validate format
const supportedFormats = ['jsonl', 'json', 'csv'];
if (!supportedFormats.includes(format)) {
throw new typesense_model_1.TypesenseError(`Unsupported export format: ${format}`, 400);
}
const collectionName = options?.collection || ctx.fqcn();
const { collection, ...exportOptions } = options || {};
const searchParams = {
...exportOptions
// Note: Typesense export always returns JSONL regardless of format param
};
// Get response as text (JSONL format)
const response = await ctx.httpClient.requestText(`/collections/${collectionName}/documents/export`, { searchParams });
if (format === 'json') {
// Parse JSONL to JSON array
return response
.split('\n')
.filter(line => line.trim())
.map(line => JSON.parse(line));
}
else if (format === 'csv') {
// For small datasets, convert JSONL to CSV
const documents = response
.split('\n')
.filter(line => line.trim())
.map(line => JSON.parse(line));
return export_formatter_1.ExportFormatter.formatCSV(documents);
}
return response;
}
async function exportDocumentsStream(ctx, options) {
const collectionName = options?.collection || ctx.fqcn();
const { collection, ...exportOptions } = options || {};
const searchParams = {
...exportOptions
};
return ctx.httpClient
.stream(`/collections/${collectionName}/documents/export`, {
searchParams
})
.then(stream => stream_1.Readable.fromWeb(stream));
}
//# sourceMappingURL=exportDocuments.js.map