@goatlab/typesense
Version:
Modern TypeScript wrapper for Typesense search engine API
37 lines • 1.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.insertDocument = insertDocument;
const typesense_model_1 = require("../../typesense.model");
const getOrCreateCollection_1 = require("../collections/getOrCreateCollection");
async function insertDocument(ctx, document, options) {
if (!(0, typesense_model_1.isValidDocumentId)(document.id)) {
throw new typesense_model_1.TypesenseError('Document must have a valid id', 400);
}
const collectionName = options?.collection || ctx.fqcn();
try {
return await ctx.httpClient.request(`/collections/${collectionName}/documents`, {
method: 'POST',
body: document,
});
}
catch (error) {
// Auto-create collection if enabled
if (ctx.autoCreateCollection &&
(error.status === 404 || error.response?.status === 404)) {
// Infer schema from document
const inferredSchema = ctx.schemaManager.inferSchemaFromDocument(document, collectionName);
// Create collection
await (0, getOrCreateCollection_1.getOrCreateCollection)(ctx, {
...inferredSchema,
name: collectionName,
});
// Retry insert
return await ctx.httpClient.request(`/collections/${collectionName}/documents`, {
method: 'POST',
body: document,
});
}
throw error;
}
}
//# sourceMappingURL=insertDocument.js.map