UNPKG

mongodb-rag-core

Version:

Common elements used by MongoDB Chatbot Framework components.

91 lines (90 loc) 2.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractMongoDbQueryOperators = exports.extractMongoDbMethods = exports.commonMongoDbNodeJsMethods = void 0; // Common MongoDB Node.js methods exports.commonMongoDbNodeJsMethods = new Set([ "find", "findOne", "insertOne", "insertMany", "updateOne", "updateMany", "deleteOne", "deleteMany", "aggregate", "count", "countDocuments", "distinct", "watch", "bulkWrite", "replaceOne", "findOneAndUpdate", "findOneAndReplace", "findOneAndDelete", "createIndex", "createIndexes", "dropIndex", "dropIndexes", "listIndexes", "indexExists", "indexInformation", "stats", "estimatedDocumentCount", "mapReduce", "group", "rename", "drop", "options", "isCapped", "createCollection", "listCollections", "indexExists", "indexes", // Cursor methods often used in chaining "sort", "limit", "skip", "project", "projection", "toArray", "next", "hasNext", "forEach", "explain", "hint", "collation", "batchSize", "maxTimeMS", ]); /** Extracts MongoDB method calls from code. */ function extractMongoDbMethods(code, methods = exports.commonMongoDbNodeJsMethods) { // Regular expression to match method calls on MongoDB collections // This pattern looks for method calls in the format collection.method() or chain.method() // We need to capture both direct collection methods and chained methods const methodRegex = /\.(\w+)\s*\(/g; // Extract all method matches const matches = Array.from(code.matchAll(methodRegex)); // Filter to only include known MongoDB methods and remove duplicates const methodsFound = matches .map((match) => match[1]) // Extract the method name from the regex match .filter((method) => methods.has(method)); // Remove duplicates by converting to Set and back to Array return [...new Set(methodsFound)]; } exports.extractMongoDbMethods = extractMongoDbMethods; /** Extracts MongoDB query operators from code. */ function extractMongoDbQueryOperators(code) { // Regular expression to match MongoDB query operators // This pattern looks for operators in the format $operatorName in various contexts // 1. As an object key: { $match: {...} } const operatorAsKeyRegex = /[{,]\s*["']?(\$[a-zA-Z0-9]+)["']?\s*:/g; // Extract the operator names from the matches and remove duplicates const operatorsFound = Array.from(new Set(Array.from(code.matchAll(operatorAsKeyRegex)).map((match) => match[1]))); return operatorsFound; } exports.extractMongoDbQueryOperators = extractMongoDbQueryOperators; //# sourceMappingURL=extractMongoDbQueryMetadata.js.map