refactor(desktop): organize lib modules by domain
This commit is contained in:
1 parent
90f0594e93
commit
7e7c4944e9
700 files changed
+1292
-1264
No files matched your search
@@ -0,0 +1,415 @@
|
||||
export type MongoCompletionMode = "root" | "collection" | "method" | "cursorMethod" | "field" | "operator" | "stage";
|
||||
|
||||
export interface MongoCompletionField {
|
||||
name: string;
|
||||
type?: string;
|
||||
}
|
||||
|
||||
export interface MongoCompletionItem {
|
||||
label: string;
|
||||
type: "column" | "function" | "keyword" | "snippet" | "table";
|
||||
detail?: string;
|
||||
info?: string;
|
||||
apply?: string;
|
||||
boost: number;
|
||||
}
|
||||
|
||||
export interface MongoCompletionContext {
|
||||
mode: MongoCompletionMode;
|
||||
prefix: string;
|
||||
from: number;
|
||||
collection?: string;
|
||||
}
|
||||
|
||||
export interface MongoCompletionInput {
|
||||
collections?: string[];
|
||||
fields?: MongoCompletionField[];
|
||||
}
|
||||
|
||||
const COLLECTION_METHODS = [
|
||||
{ label: "find", detail: "Query matching documents", apply: "find({})" },
|
||||
{ label: "findOne", detail: "Query one matching document", apply: "findOne({})" },
|
||||
{ label: "aggregate", detail: "Run an aggregation pipeline", apply: "aggregate([])" },
|
||||
{ label: "countDocuments", detail: "Count matching documents", apply: "countDocuments({})" },
|
||||
{ label: "distinct", detail: "Return distinct field values", apply: 'distinct("${field}", {})' },
|
||||
{ label: "insertOne", detail: "Insert one document", apply: "insertOne({})" },
|
||||
{ label: "insertMany", detail: "Insert multiple documents", apply: "insertMany([{}])" },
|
||||
{ label: "updateOne", detail: "Update one matching document", apply: "updateOne({}, { $set: {} })" },
|
||||
{ label: "updateMany", detail: "Update all matching documents", apply: "updateMany({}, { $set: {} })" },
|
||||
{ label: "deleteOne", detail: "Delete one matching document", apply: "deleteOne({})" },
|
||||
{ label: "deleteMany", detail: "Delete all matching documents", apply: "deleteMany({})" },
|
||||
{ label: "getIndexes", detail: "List collection indexes", apply: "getIndexes()" },
|
||||
{ label: "stats", detail: "Show collection statistics", apply: "stats()" },
|
||||
{ label: "dataSize", detail: "Total size of documents in bytes", apply: "dataSize()" },
|
||||
{ label: "storageSize", detail: "Allocated storage size in bytes", apply: "storageSize()" },
|
||||
{ label: "totalIndexSize", detail: "Total size of all indexes in bytes", apply: "totalIndexSize()" },
|
||||
{ label: "createIndex", detail: "Create an index", apply: "createIndex({ ${field}: 1 })" },
|
||||
{ label: "dropIndex", detail: "Drop one index", apply: 'dropIndex("${indexName}")' },
|
||||
{ label: "dropIndexes", detail: "Drop collection indexes", apply: "dropIndexes()" },
|
||||
] as const;
|
||||
|
||||
const CURSOR_METHODS = [
|
||||
{ label: "sort", detail: "Sort cursor results", apply: "sort({ ${field}: 1 })" },
|
||||
{ label: "limit", detail: "Limit cursor results", apply: "limit(100)" },
|
||||
{ label: "skip", detail: "Skip cursor results", apply: "skip(0)" },
|
||||
] as const;
|
||||
|
||||
const ROOT_SNIPPETS = [
|
||||
{ label: "db.collection.find", detail: "Find documents", apply: "db.${collection}.find({})" },
|
||||
{ label: "db.collection.aggregate", detail: "Aggregation pipeline", apply: "db.${collection}.aggregate([\n { $match: {} }\n])" },
|
||||
{ label: "db.getCollection", detail: "Reference a collection by name", apply: 'db.getCollection("${collection}")' },
|
||||
] as const;
|
||||
|
||||
const FIELD_SNIPPETS = [
|
||||
{ label: "ObjectId", detail: "MongoDB ObjectId value", apply: 'ObjectId("${id}")' },
|
||||
{ label: "ISODate", detail: "MongoDB ISODate value", apply: 'ISODate("${date}")' },
|
||||
{ label: "$regex", detail: "Regular expression match", apply: '$regex: "${pattern}"' },
|
||||
{ label: "$in", detail: "Match any value in array", apply: "$in: []" },
|
||||
{ label: "$gte", detail: "Greater than or equal", apply: "$gte: ${value}" },
|
||||
{ label: "$lte", detail: "Less than or equal", apply: "$lte: ${value}" },
|
||||
] as const;
|
||||
|
||||
const QUERY_OPERATORS = ["$eq", "$ne", "$gt", "$gte", "$lt", "$lte", "$in", "$nin", "$exists", "$regex", "$and", "$or", "$nor", "$not", "$elemMatch"];
|
||||
const UPDATE_OPERATORS = ["$set", "$unset", "$inc", "$push", "$pull", "$addToSet", "$rename", "$currentDate", "$setOnInsert"];
|
||||
const PIPELINE_STAGES = ["$match", "$project", "$group", "$sort", "$limit", "$skip", "$unwind", "$lookup", "$addFields", "$count", "$facet"];
|
||||
const QUERY_METHODS = ["find", "findOne", "countDocuments", "updateOne", "updateMany", "deleteOne", "deleteMany", "dropIndex", "dropIndexes", "sort"];
|
||||
|
||||
export function getMongoCompletionContext(text: string, cursor: number): MongoCompletionContext {
|
||||
const safeCursor = Math.max(0, Math.min(cursor, text.length));
|
||||
const operatorPrefix = readOperatorPrefix(text, safeCursor);
|
||||
const collection = extractActiveCollection(text, safeCursor);
|
||||
|
||||
if (operatorPrefix) {
|
||||
return {
|
||||
mode: isLikelyAggregationPipeline(text, safeCursor) ? "stage" : "operator",
|
||||
prefix: operatorPrefix.prefix,
|
||||
from: operatorPrefix.from,
|
||||
collection,
|
||||
};
|
||||
}
|
||||
|
||||
const propertyPrefix = readPropertyPrefix(text, safeCursor);
|
||||
const beforeCursor = text.slice(0, safeCursor);
|
||||
|
||||
if (isInsideOperatorValueObject(beforeCursor)) {
|
||||
return { mode: "operator", prefix: propertyPrefix.prefix, from: propertyPrefix.from, collection };
|
||||
}
|
||||
|
||||
if (/db\.$/.test(beforeCursor)) {
|
||||
return { mode: "collection", prefix: "", from: safeCursor, collection };
|
||||
}
|
||||
|
||||
const collectionPrefix = matchDbCollectionPrefix(beforeCursor);
|
||||
if (collectionPrefix) {
|
||||
return { mode: "collection", prefix: collectionPrefix.prefix, from: collectionPrefix.from, collection };
|
||||
}
|
||||
|
||||
if (isAfterCollectionDot(beforeCursor)) {
|
||||
const methodPrefix = readMethodPrefix(beforeCursor);
|
||||
return { mode: "method", prefix: methodPrefix.prefix, from: methodPrefix.from, collection };
|
||||
}
|
||||
|
||||
if (isAfterCursorMethodDot(beforeCursor)) {
|
||||
const methodPrefix = readMethodPrefix(beforeCursor);
|
||||
return { mode: "cursorMethod", prefix: methodPrefix.prefix, from: methodPrefix.from, collection };
|
||||
}
|
||||
|
||||
if (isLikelyFieldPosition(text, safeCursor)) {
|
||||
return { mode: "field", prefix: propertyPrefix.prefix, from: propertyPrefix.from, collection };
|
||||
}
|
||||
|
||||
return { mode: "root", prefix: propertyPrefix.prefix, from: propertyPrefix.from, collection };
|
||||
}
|
||||
|
||||
export function buildMongoCompletionItems(text: string, cursor: number, input: MongoCompletionInput = {}): MongoCompletionItem[] {
|
||||
return buildMongoCompletionItemsFromContext(getMongoCompletionContext(text, cursor), input);
|
||||
}
|
||||
|
||||
export function buildMongoCompletionItemsFromContext(context: MongoCompletionContext, input: MongoCompletionInput = {}): MongoCompletionItem[] {
|
||||
if (context.mode === "collection") return collectionItems(context.prefix, input.collections ?? []);
|
||||
if (context.mode === "method") return methodItems(context.prefix);
|
||||
if (context.mode === "cursorMethod") return cursorMethodItems(context.prefix);
|
||||
if (context.mode === "field") return fieldItems(context.prefix, input.fields ?? []);
|
||||
if (context.mode === "operator") return operatorItems(context.prefix);
|
||||
if (context.mode === "stage") return stageItems(context.prefix);
|
||||
return rootItems(context.prefix);
|
||||
}
|
||||
|
||||
export function shouldAutoOpenMongoCompletion(text: string, cursor: number): boolean {
|
||||
const previousChar = text[cursor - 1];
|
||||
if (!previousChar) return false;
|
||||
if (/db\.$/.test(text.slice(0, cursor))) return true;
|
||||
if (previousChar === "$" || previousChar === "." || previousChar === '"') return true;
|
||||
if (/[{,[]/.test(previousChar)) return isLikelyFieldPosition(text, cursor) || isLikelyAggregationPipeline(text, cursor);
|
||||
if (/[\w_$.-]/.test(previousChar)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
export function getMongoCompletionResultValidFor(): RegExp {
|
||||
return /[\w_$.-]*$/;
|
||||
}
|
||||
|
||||
export function inferMongoCompletionFields(documents: unknown[]): MongoCompletionField[] {
|
||||
const typeByPath = new Map<string, Set<string>>();
|
||||
for (const doc of documents) collectFieldTypes(doc, "", typeByPath, 0);
|
||||
return [...typeByPath.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([name, types]) => ({ name, type: [...types].sort().join(" | ") }));
|
||||
}
|
||||
|
||||
function rootItems(prefix: string): MongoCompletionItem[] {
|
||||
const snippets = ROOT_SNIPPETS.filter((snippet) => matchesFuzzyPrefix(snippet.label, prefix)).map((snippet) => ({
|
||||
label: snippet.label,
|
||||
type: "snippet" as const,
|
||||
detail: snippet.detail,
|
||||
apply: snippet.apply,
|
||||
boost: 120,
|
||||
}));
|
||||
const methods = [...COLLECTION_METHODS, ...CURSOR_METHODS]
|
||||
.filter((method) => matchesFuzzyPrefix(method.label, prefix))
|
||||
.map((method) => ({
|
||||
label: method.label,
|
||||
type: "function" as const,
|
||||
detail: method.detail,
|
||||
apply: method.apply,
|
||||
boost: 100,
|
||||
}));
|
||||
return dedupeAndSort([...snippets, ...methods]);
|
||||
}
|
||||
|
||||
function collectionItems(prefix: string, collections: string[]): MongoCompletionItem[] {
|
||||
return collections
|
||||
.filter((collection) => matchesFuzzyPrefix(collection, prefix))
|
||||
.slice(0, 100)
|
||||
.map((collection) => ({
|
||||
label: collection,
|
||||
type: "table" as const,
|
||||
detail: "collection",
|
||||
apply: needsGetCollectionSyntax(collection) ? `getCollection("${escapeDoubleQuoted(collection)}")` : collection,
|
||||
boost: collection.toLowerCase().startsWith(prefix.toLowerCase()) ? 120 : 90,
|
||||
}));
|
||||
}
|
||||
|
||||
function methodItems(prefix: string): MongoCompletionItem[] {
|
||||
return dedupeAndSort(
|
||||
[...COLLECTION_METHODS, ...CURSOR_METHODS]
|
||||
.filter((method) => matchesFuzzyPrefix(method.label, prefix))
|
||||
.map((method) => ({
|
||||
label: method.label,
|
||||
type: "function" as const,
|
||||
detail: method.detail,
|
||||
apply: method.apply,
|
||||
boost: method.label === "find" || method.label === "aggregate" ? 130 : 100,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
function cursorMethodItems(prefix: string): MongoCompletionItem[] {
|
||||
return dedupeAndSort(
|
||||
CURSOR_METHODS.filter((method) => matchesFuzzyPrefix(method.label, prefix)).map((method) => ({
|
||||
label: method.label,
|
||||
type: "function" as const,
|
||||
detail: method.detail,
|
||||
apply: method.apply,
|
||||
boost: method.label === "limit" ? 130 : 110,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
function fieldItems(prefix: string, fields: MongoCompletionField[]): MongoCompletionItem[] {
|
||||
const normalizedPrefix = normalizeMongoKeyPrefix(prefix);
|
||||
const observedFields = fields
|
||||
.filter((field) => matchesFuzzyPrefix(field.name, normalizedPrefix))
|
||||
.slice(0, 100)
|
||||
.map((field) => ({
|
||||
label: field.name,
|
||||
type: "column" as const,
|
||||
detail: field.type ? `observed field · ${field.type}` : "observed field",
|
||||
apply: mongoFieldApplyText(field.name, prefix),
|
||||
boost: field.name.toLowerCase().startsWith(normalizedPrefix.toLowerCase()) ? 120 : 85,
|
||||
}));
|
||||
const snippets = FIELD_SNIPPETS.filter((snippet) => matchesFuzzyPrefix(snippet.label, normalizedPrefix)).map((snippet) => ({
|
||||
label: snippet.label,
|
||||
type: "snippet" as const,
|
||||
detail: snippet.detail,
|
||||
apply: snippet.apply,
|
||||
boost: 95,
|
||||
}));
|
||||
return dedupeAndSort([...observedFields, ...snippets]);
|
||||
}
|
||||
|
||||
function operatorItems(prefix: string): MongoCompletionItem[] {
|
||||
return [...QUERY_OPERATORS, ...UPDATE_OPERATORS]
|
||||
.filter((operator) => matchesFuzzyPrefix(operator, prefix))
|
||||
.map((operator) => ({
|
||||
label: operator,
|
||||
type: "keyword" as const,
|
||||
detail: operator.startsWith("$set") || UPDATE_OPERATORS.includes(operator) ? "update operator" : "query operator",
|
||||
apply: operator,
|
||||
boost: operator === "$set" || operator === "$match" ? 115 : 90,
|
||||
}));
|
||||
}
|
||||
|
||||
function stageItems(prefix: string): MongoCompletionItem[] {
|
||||
const stages = PIPELINE_STAGES.filter((stage) => matchesFuzzyPrefix(stage, prefix)).map((stage) => ({
|
||||
label: stage,
|
||||
type: "keyword" as const,
|
||||
detail: "aggregation stage",
|
||||
apply: stage,
|
||||
boost: stage === "$match" || stage === "$project" ? 120 : 95,
|
||||
}));
|
||||
const snippets = [
|
||||
{ label: "$match stage", apply: "$match: {}", detail: "Filter documents in a pipeline" },
|
||||
{ label: "$group stage", apply: '$group: { _id: "$${field}", count: { $sum: 1 } }', detail: "Group documents" },
|
||||
{ label: "$lookup stage", apply: '$lookup: { from: "${collection}", localField: "${field}", foreignField: "_id", as: "${as}" }', detail: "Join another collection" },
|
||||
]
|
||||
.filter((snippet) => matchesFuzzyPrefix(snippet.label, prefix))
|
||||
.map((snippet) => ({ ...snippet, type: "snippet" as const, boost: 110 }));
|
||||
return dedupeAndSort([...snippets, ...stages]);
|
||||
}
|
||||
|
||||
function readPropertyPrefix(text: string, cursor: number): { prefix: string; from: number } {
|
||||
let from = cursor;
|
||||
while (from > 0 && /[\w_$.-]/.test(text[from - 1] ?? "")) from--;
|
||||
if (text[from - 1] === '"' || text[from - 1] === "'") from--;
|
||||
return { prefix: text.slice(from, cursor), from };
|
||||
}
|
||||
|
||||
function readOperatorPrefix(text: string, cursor: number): { prefix: string; from: number } | null {
|
||||
let from = cursor;
|
||||
while (from > 0 && /[\w$]/.test(text[from - 1] ?? "")) from--;
|
||||
const prefix = text.slice(from, cursor);
|
||||
return prefix.startsWith("$") ? { prefix, from } : null;
|
||||
}
|
||||
|
||||
function readMethodPrefix(beforeCursor: string): { prefix: string; from: number } {
|
||||
const dot = beforeCursor.lastIndexOf(".");
|
||||
const from = dot >= 0 ? dot + 1 : beforeCursor.length;
|
||||
return { prefix: beforeCursor.slice(from), from };
|
||||
}
|
||||
|
||||
function matchDbCollectionPrefix(beforeCursor: string): { prefix: string; from: number } | null {
|
||||
const match = /(?:^|[\s;(])db\.([A-Za-z_][\w$-]*)$/.exec(beforeCursor);
|
||||
if (!match) return null;
|
||||
const prefix = match[1] ?? "";
|
||||
return { prefix, from: beforeCursor.length - prefix.length };
|
||||
}
|
||||
|
||||
function isAfterCollectionDot(beforeCursor: string): boolean {
|
||||
return /(?:^|[\s;(])db\.(?:[A-Za-z_][\w$-]*|getCollection\(["'][^"']+["']\))\.[\w$-]*$/.test(beforeCursor);
|
||||
}
|
||||
|
||||
function isAfterCursorMethodDot(beforeCursor: string): boolean {
|
||||
return /(?:^|[\s;(])db\.(?:[A-Za-z_][\w$-]*|getCollection\(["'][^"']+["']\))\.(?:find|aggregate)\s*\([\s\S]*\)(?:\s*\.\s*(?:sort|skip|limit)\s*\([\s\S]*\))*\s*\.\s*[\w$-]*$/.test(beforeCursor);
|
||||
}
|
||||
|
||||
function isLikelyFieldPosition(text: string, cursor: number): boolean {
|
||||
const before = text.slice(0, cursor);
|
||||
if (!/[{,]\s*["']?[\w$.-]*$/.test(before)) return false;
|
||||
const call = findInnermostMongoCall(before);
|
||||
if (!call) return false;
|
||||
if (call.method === "aggregate") return isLikelyAggregationPipeline(text, cursor);
|
||||
if (!QUERY_METHODS.includes(call.method)) return false;
|
||||
if (isInsideOperatorValueObject(before)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isLikelyAggregationPipeline(text: string, cursor: number): boolean {
|
||||
const before = text.slice(0, cursor);
|
||||
const aggregatePos = before.lastIndexOf(".aggregate");
|
||||
if (aggregatePos < 0) return false;
|
||||
const afterAggregate = before.slice(aggregatePos);
|
||||
return afterAggregate.lastIndexOf("[") > afterAggregate.lastIndexOf("]");
|
||||
}
|
||||
|
||||
function extractActiveCollection(text: string, cursor: number): string | undefined {
|
||||
const before = text.slice(0, cursor);
|
||||
const getCollectionMatches = [...before.matchAll(/db\.getCollection\(["']([^"']+)["']\)/g)];
|
||||
const directMatches = [...before.matchAll(/db\.([A-Za-z_][\w$-]*)\s*\./g)].filter((match) => match[1] !== "getCollection");
|
||||
const lastGetCollection = getCollectionMatches[getCollectionMatches.length - 1];
|
||||
const lastDirect = directMatches[directMatches.length - 1];
|
||||
const getCollectionIndex = lastGetCollection?.index ?? -1;
|
||||
const directIndex = lastDirect?.index ?? -1;
|
||||
if (getCollectionIndex > directIndex) return lastGetCollection?.[1];
|
||||
return lastDirect?.[1];
|
||||
}
|
||||
|
||||
function collectFieldTypes(value: unknown, prefix: string, out: Map<string, Set<string>>, depth: number) {
|
||||
if (depth > 4 || value == null || typeof value !== "object") return;
|
||||
if (Array.isArray(value)) {
|
||||
for (const item of value.slice(0, 3)) collectFieldTypes(item, prefix, out, depth + 1);
|
||||
return;
|
||||
}
|
||||
for (const [key, child] of Object.entries(value as Record<string, unknown>)) {
|
||||
const path = prefix ? `${prefix}.${key}` : key;
|
||||
if (!out.has(path)) out.set(path, new Set());
|
||||
out.get(path)?.add(describeMongoValueType(child));
|
||||
collectFieldTypes(child, path, out, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
function describeMongoValueType(value: unknown): string {
|
||||
if (value == null) return "null";
|
||||
if (Array.isArray(value)) return "array";
|
||||
if (value instanceof Date) return "date";
|
||||
return typeof value === "object" ? "object" : typeof value;
|
||||
}
|
||||
|
||||
function quoteMongoFieldName(field: string, prefix: string): string {
|
||||
if (prefix.startsWith('"')) return `"${escapeDoubleQuoted(field)}"`;
|
||||
if (prefix.startsWith("'")) return `'${field.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
|
||||
return field;
|
||||
}
|
||||
|
||||
function mongoFieldApplyText(field: string, prefix: string): string {
|
||||
const quoted = quoteMongoFieldName(field, prefix);
|
||||
return `${quoted}: `;
|
||||
}
|
||||
|
||||
function findInnermostMongoCall(beforeCursor: string): { method: string; openParenIndex: number } | null {
|
||||
const callPattern = /\.(find|findOne|countDocuments|updateOne|updateMany|deleteOne|deleteMany|aggregate|sort)\s*\(/g;
|
||||
let match: RegExpExecArray | null;
|
||||
let result: { method: string; openParenIndex: number } | null = null;
|
||||
while ((match = callPattern.exec(beforeCursor))) {
|
||||
const method = match[1];
|
||||
const openParenIndex = match.index + match[0].lastIndexOf("(");
|
||||
if (method) result = { method, openParenIndex };
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function isInsideOperatorValueObject(beforeCursor: string): boolean {
|
||||
const call = findInnermostMongoCall(beforeCursor);
|
||||
if (!call) return false;
|
||||
const callArgumentsBeforeCursor = beforeCursor.slice(call.openParenIndex + 1);
|
||||
return /(?:^|[{,])\s*["']?[\w$.-]+["']?\s*:\s*\{\s*["']?[\w$.-]*$/.test(callArgumentsBeforeCursor);
|
||||
}
|
||||
|
||||
function normalizeMongoKeyPrefix(prefix: string): string {
|
||||
return prefix.replace(/^["']/, "");
|
||||
}
|
||||
|
||||
function needsGetCollectionSyntax(collection: string): boolean {
|
||||
return !/^[A-Za-z_][\w$]*$/.test(collection);
|
||||
}
|
||||
|
||||
function escapeDoubleQuoted(value: string): string {
|
||||
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
||||
}
|
||||
|
||||
function matchesFuzzyPrefix(value: string, prefix: string): boolean {
|
||||
const normalizedValue = value.toLowerCase();
|
||||
const normalizedPrefix = prefix.toLowerCase().replace(/^["']/, "");
|
||||
if (!normalizedPrefix) return true;
|
||||
return normalizedValue.startsWith(normalizedPrefix) || normalizedValue.includes(normalizedPrefix);
|
||||
}
|
||||
|
||||
function dedupeAndSort(items: MongoCompletionItem[]): MongoCompletionItem[] {
|
||||
const seen = new Set<string>();
|
||||
const deduped: MongoCompletionItem[] = [];
|
||||
for (const item of items) {
|
||||
const key = `${item.type}:${item.label}`;
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
deduped.push(item);
|
||||
}
|
||||
return deduped.sort((a, b) => b.boost - a.boost || a.label.localeCompare(b.label));
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
export function mongoUrlParam(urlParams: string | undefined, key: string): string {
|
||||
const params = parseMongoUrlParams(urlParams);
|
||||
return params.get(key) || "";
|
||||
}
|
||||
|
||||
export function setMongoUrlParam(urlParams: string | undefined, key: string, value: string): string {
|
||||
const params = parseMongoUrlParams(urlParams);
|
||||
const normalized = value.trim();
|
||||
if (normalized) {
|
||||
params.set(key, normalized);
|
||||
} else {
|
||||
params.delete(key);
|
||||
}
|
||||
return params.toString();
|
||||
}
|
||||
|
||||
export function mongoUrlParamIsTrue(urlParams: string | undefined, key: string, defaultValue = false): boolean {
|
||||
const value = mongoUrlParam(urlParams, key);
|
||||
if (!value) return defaultValue;
|
||||
return ["true", "1", "yes"].includes(value.toLowerCase());
|
||||
}
|
||||
|
||||
export function setMongoUrlParamBoolean(urlParams: string | undefined, key: string, value: boolean, defaultWhenEnabled = false): string {
|
||||
if (value === defaultWhenEnabled) {
|
||||
return setMongoUrlParam(urlParams, key, "");
|
||||
}
|
||||
return setMongoUrlParam(urlParams, key, value ? "true" : "false");
|
||||
}
|
||||
|
||||
export function normalizeMongoTlsFormState(ssl: boolean, urlParams: string | undefined, caCertPath: string | undefined): { urlParams: string; caCertPath: string } {
|
||||
if (ssl) {
|
||||
const nextCaCertPath = caCertPath?.trim() || "";
|
||||
let nextUrlParams = urlParams || "";
|
||||
if (nextCaCertPath) {
|
||||
nextUrlParams = setMongoUrlParam(nextUrlParams, "tlsCAFile", "");
|
||||
}
|
||||
return { urlParams: nextUrlParams, caCertPath: nextCaCertPath };
|
||||
}
|
||||
const next = setMongoUrlParam(setMongoUrlParam(setMongoUrlParam(urlParams, "tlsAllowInvalidCertificates", ""), "tlsAllowInvalidHostnames", ""), "tlsCAFile", "");
|
||||
return { urlParams: next, caCertPath: "" };
|
||||
}
|
||||
|
||||
export function mongodbAuthFailureHint(message: string): string {
|
||||
const normalizedMessage = message.toLowerCase();
|
||||
if (normalizedMessage.includes("tlsallowinvalidhostnames is an invalid option")) {
|
||||
return `${message}\n\nDBX uses the Rust MongoDB driver (rustls), which does not accept tlsAllowInvalidHostnames in the connection string. Use tlsAllowInvalidCertificates=true instead. This is commonly required for AWS DocumentDB over an SSH tunnel because the local endpoint is 127.0.0.1 while the server certificate is issued for the DocumentDB hostname.`;
|
||||
}
|
||||
|
||||
if (message.includes('certificate not valid for name "127.0.0.1"') || (message.includes("invalid peer certificate") && message.includes("127.0.0.1") && message.includes("docdb"))) {
|
||||
return `${message}\n\nTLS hostname verification failed because the SSH tunnel connects to 127.0.0.1, but the DocumentDB certificate is issued for the cluster hostname. Add tlsAllowInvalidCertificates=true to URL params. Keep tlsCAFile configured so the certificate chain is still trusted when possible.`;
|
||||
}
|
||||
|
||||
if (message.includes("cannot specify multiple seeds with directConnection=true")) {
|
||||
return `${message}\n\nMongoDB directConnection=true can only be used with a single host. Remove directConnection=true when using a multi-host replica set URL, or keep only one reachable host if you need a direct connection.`;
|
||||
}
|
||||
|
||||
if (message.includes("no records found") && message.includes("_mongodb._tcp.")) {
|
||||
return `${message}\n\nMongoDB SRV URLs require a DNS hostname with SRV records. For an IP address or normal host:port endpoint, use mongodb://host:port instead of mongodb+srv://host.`;
|
||||
}
|
||||
|
||||
if (message.includes("ReplicaSetNoPrimary") && message.includes("127.0.0.1:27017")) {
|
||||
return `${message}\n\nThe replica set is advertising 127.0.0.1:27017, which points back to this app machine instead of the MongoDB server. Add directConnection=true for a single reachable endpoint, or reconfigure the replica set members to advertise addresses reachable from this app.`;
|
||||
}
|
||||
|
||||
if (message.includes("must be URL encoded") || message.includes("cannot contain unescaped %")) {
|
||||
return `${message}\n\nMongoDB URL mode requires reserved characters in usernames and passwords to be percent-encoded. For example, @ becomes %40, # becomes %23, / becomes %2F, : becomes %3A, and % becomes %25.`;
|
||||
}
|
||||
|
||||
if (message.includes("not authorized") && message.includes("listDatabases")) {
|
||||
return `${message}\n\nThis MongoDB user can authenticate but does not have permission to run listDatabases on admin. Grant listDatabases/cluster monitor privileges, or set a specific default database that the user can access.`;
|
||||
}
|
||||
|
||||
if (message.includes("Current authentication database:")) return message;
|
||||
|
||||
const source = message.match(/source='([^']+)'/)?.[1];
|
||||
if (!source || !message.includes("Exception authenticating MongoCredential")) return message;
|
||||
|
||||
return `${message}\n\nCurrent authentication database: ${source}. If this user was created in admin, set Authentication database to admin or add authSource=admin to URL params.`;
|
||||
}
|
||||
|
||||
function parseMongoUrlParams(urlParams: string | undefined): URLSearchParams {
|
||||
return new URLSearchParams((urlParams || "").trim().replace(/^\?/, ""));
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
export type MongoInputValue = string | number | boolean | null;
|
||||
|
||||
const MONGO_SHELL_DATE_PATTERN = /^(?:ISODate|new Date)\(\s*(["'])(.+)\1\s*\)$/;
|
||||
const LEGACY_MONGO_DATE_DISPLAY_PATTERN = /^(\d{4}-\d{2}-\d{2})[ T](\d{2}:\d{2}:\d{2})(?:\.(\d{1,3}))?$/;
|
||||
const MONGO_OBJECT_ID_PATTERN = /^[a-fA-F0-9]{24}$/;
|
||||
|
||||
export function mongoShellDateToExtendedJson(value: unknown): unknown {
|
||||
if (typeof value !== "string") return value;
|
||||
const match = value.trim().match(MONGO_SHELL_DATE_PATTERN);
|
||||
if (!match) return value;
|
||||
return { $date: match[2] };
|
||||
}
|
||||
|
||||
export function parseMongoDocumentInputValue(raw: MongoInputValue): unknown {
|
||||
if (raw === null || typeof raw === "number" || typeof raw === "boolean") return raw;
|
||||
|
||||
const trimmed = raw.trim();
|
||||
if (trimmed === "NULL") return null;
|
||||
if (/^(true|false|null)$/i.test(trimmed)) return JSON.parse(trimmed.toLowerCase());
|
||||
|
||||
const shellDate = mongoShellDateToExtendedJson(trimmed);
|
||||
if (shellDate !== trimmed) return shellDate;
|
||||
|
||||
const legacyDate = legacyMongoDateDisplayToExtendedJson(trimmed);
|
||||
if (legacyDate) return legacyDate;
|
||||
|
||||
if (/^-?\d+(?:\.\d+)?$/.test(trimmed)) return Number(trimmed);
|
||||
if (trimmed.startsWith("{") || trimmed.startsWith("[") || trimmed.startsWith('"')) {
|
||||
return mongoShellDateToExtendedJson(JSON.parse(trimmed));
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
|
||||
function legacyMongoDateDisplayToExtendedJson(value: string): { $date: string } | null {
|
||||
const match = value.match(LEGACY_MONGO_DATE_DISPLAY_PATTERN);
|
||||
if (!match) return null;
|
||||
const [, date, time, millis = "000"] = match;
|
||||
return { $date: `${date}T${time}.${millis.padEnd(3, "0")}Z` };
|
||||
}
|
||||
|
||||
export function buildMongoUpdateDocument(changes: Map<number, MongoInputValue>, columns: string[]): Record<string, unknown> {
|
||||
const setFields: Record<string, unknown> = {};
|
||||
const unsetFields: Record<string, unknown> = {};
|
||||
for (const [colIdx, newVal] of changes) {
|
||||
const col = columns[colIdx];
|
||||
if (!col || col === "_id") continue;
|
||||
if (newVal === null) {
|
||||
unsetFields[col] = "";
|
||||
} else {
|
||||
setFields[col] = parseMongoDocumentInputValue(newVal);
|
||||
}
|
||||
}
|
||||
const doc: Record<string, unknown> = {};
|
||||
if (Object.keys(setFields).length > 0) doc.$set = setFields;
|
||||
if (Object.keys(unsetFields).length > 0) doc.$unset = unsetFields;
|
||||
return doc;
|
||||
}
|
||||
|
||||
export function buildMongoInsertDocument(row: MongoInputValue[], columns: string[]): Record<string, unknown> {
|
||||
const doc: Record<string, unknown> = {};
|
||||
for (let ci = 0; ci < columns.length; ci++) {
|
||||
const col = columns[ci];
|
||||
if (!col || col === "_id") continue;
|
||||
const val = row[ci];
|
||||
if (val === null) continue;
|
||||
doc[col] = parseMongoDocumentInputValue(val);
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
export function buildMongoCopyInsertDocument(row: MongoInputValue[], columns: string[], options: { excludePrimaryKeys?: boolean } = {}): Record<string, unknown> {
|
||||
const doc: Record<string, unknown> = {};
|
||||
for (let ci = 0; ci < columns.length; ci++) {
|
||||
const col = columns[ci];
|
||||
if (!col || (options.excludePrimaryKeys && col === "_id")) continue;
|
||||
const val = row[ci];
|
||||
if (val === null) continue;
|
||||
if (col === "_id" && typeof val === "string" && MONGO_OBJECT_ID_PATTERN.test(val)) {
|
||||
doc[col] = { $oid: val };
|
||||
continue;
|
||||
}
|
||||
doc[col] = parseMongoDocumentInputValue(val);
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
export function formatMongoShellLiteral(value: unknown): string {
|
||||
if (value === null || value === undefined) return "null";
|
||||
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
||||
if (typeof value === "string") return JSON.stringify(value);
|
||||
if (Array.isArray(value)) return `[${value.map(formatMongoShellLiteral).join(",")}]`;
|
||||
if (typeof value === "object") {
|
||||
const object = value as Record<string, unknown>;
|
||||
const keys = Object.keys(object);
|
||||
if (keys.length === 1 && typeof object.$date === "string") {
|
||||
return `ISODate(${JSON.stringify(object.$date)})`;
|
||||
}
|
||||
if (keys.length === 1 && typeof object.$oid === "string" && MONGO_OBJECT_ID_PATTERN.test(object.$oid)) {
|
||||
return `ObjectId(${JSON.stringify(object.$oid)})`;
|
||||
}
|
||||
if (keys.length === 1 && typeof object.$numberLong === "string") {
|
||||
return `NumberLong(${JSON.stringify(object.$numberLong)})`;
|
||||
}
|
||||
return `{${keys.map((key) => `${JSON.stringify(key)}:${formatMongoShellLiteral(object[key])}`).join(",")}}`;
|
||||
}
|
||||
return JSON.stringify(String(value));
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
import { DEFAULT_SQL_FORMATTER_SETTINGS, type SqlFormatterSettings } from "@/lib/sql/sqlFormatterConfig";
|
||||
|
||||
export const MAX_MONGO_FORMAT_CHARS = 1_000_000;
|
||||
|
||||
interface FormatState {
|
||||
out: string;
|
||||
indentLevel: number;
|
||||
atLineStart: boolean;
|
||||
pendingSpace: boolean;
|
||||
chainIndent: boolean;
|
||||
pendingChainCall: boolean;
|
||||
stack: Array<{ char: string; expanded: boolean; chainCall?: boolean }>;
|
||||
}
|
||||
|
||||
const CHAIN_METHODS = new Set(["find", "findOne", "aggregate", "countDocuments", "distinct", "insertOne", "insertMany", "updateOne", "updateMany", "deleteOne", "deleteMany", "getIndexes", "createIndex", "dropIndex", "dropIndexes", "sort", "limit", "skip"]);
|
||||
|
||||
export function formatMongoShellText(text: string, settings: Partial<SqlFormatterSettings> = DEFAULT_SQL_FORMATTER_SETTINGS): string {
|
||||
if (!text.trim()) return text;
|
||||
if (text.length > MAX_MONGO_FORMAT_CHARS) {
|
||||
throw new Error("MongoDB query is too large to format safely.");
|
||||
}
|
||||
|
||||
const indentUnit = settings.useTabs ? "\t" : " ".repeat(settings.tabWidth ?? DEFAULT_SQL_FORMATTER_SETTINGS.tabWidth);
|
||||
const state: FormatState = { out: "", indentLevel: 0, atLineStart: true, pendingSpace: false, chainIndent: false, pendingChainCall: false, stack: [] };
|
||||
|
||||
for (let index = 0; index < text.length; index++) {
|
||||
const char = text[index] ?? "";
|
||||
const next = text[index + 1] ?? "";
|
||||
|
||||
if (char === '"' || char === "'" || char === "`") {
|
||||
const literal = readQuotedLiteral(text, index, char);
|
||||
appendToken(state, literal.value, indentUnit);
|
||||
index = literal.end;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "/" && next === "/") {
|
||||
const comment = readLineComment(text, index);
|
||||
appendToken(state, comment.value, indentUnit);
|
||||
index = comment.end;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "/" && next === "*") {
|
||||
const comment = readBlockComment(text, index);
|
||||
appendToken(state, comment.value, indentUnit);
|
||||
index = comment.end;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "/" && looksLikeRegexLiteral(text, index)) {
|
||||
const regex = readRegexLiteral(text, index);
|
||||
appendToken(state, regex.value, indentUnit);
|
||||
index = regex.end;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (/\s/.test(char)) {
|
||||
state.pendingSpace = !state.atLineStart;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "." && shouldBreakBeforeDot(text, index)) {
|
||||
newline(state, indentUnit);
|
||||
state.chainIndent = true;
|
||||
state.pendingChainCall = true;
|
||||
appendToken(state, ".", indentUnit);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "{" || char === "[" || char === "(") {
|
||||
appendToken(state, char, indentUnit);
|
||||
const expanded = char !== "(" && shouldExpandOpening(text, index);
|
||||
const isChainCall = char === "(" && state.pendingChainCall;
|
||||
state.stack.push({ char, expanded, chainCall: isChainCall });
|
||||
if (isChainCall) state.indentLevel++;
|
||||
state.pendingChainCall = false;
|
||||
if (expanded) {
|
||||
state.indentLevel++;
|
||||
newline(state, indentUnit);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "}" || char === "]" || char === ")") {
|
||||
const frame = popMatchingFrame(state, char);
|
||||
if (frame?.expanded) {
|
||||
if (!state.atLineStart) newline(state, indentUnit, -1);
|
||||
else state.indentLevel = Math.max(0, state.indentLevel - 1);
|
||||
}
|
||||
if (frame?.chainCall) {
|
||||
state.indentLevel = Math.max(0, state.indentLevel - 1);
|
||||
}
|
||||
appendRaw(state, char, indentUnit);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === ",") {
|
||||
appendToken(state, ",", indentUnit);
|
||||
newline(state, indentUnit);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === ":") {
|
||||
trimTrailingSpaces(state);
|
||||
appendRaw(state, ": ", indentUnit);
|
||||
state.pendingSpace = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
appendToken(state, char, indentUnit);
|
||||
}
|
||||
|
||||
return cleanupFormattedMongoText(state.out);
|
||||
}
|
||||
|
||||
function appendToken(state: FormatState, token: string, indentUnit: string) {
|
||||
if (state.atLineStart) {
|
||||
state.out += indentUnit.repeat(Math.max(0, state.indentLevel + (state.chainIndent ? 1 : 0)));
|
||||
state.atLineStart = false;
|
||||
} else if (state.pendingSpace && shouldInsertPendingSpace(state.out, token)) {
|
||||
state.out += " ";
|
||||
}
|
||||
state.out += token;
|
||||
state.pendingSpace = false;
|
||||
state.chainIndent = false;
|
||||
}
|
||||
|
||||
function appendRaw(state: FormatState, token: string, indentUnit: string) {
|
||||
if (state.atLineStart) {
|
||||
state.out += indentUnit.repeat(Math.max(0, state.indentLevel + (state.chainIndent ? 1 : 0)));
|
||||
state.atLineStart = false;
|
||||
}
|
||||
state.out += token;
|
||||
state.pendingSpace = false;
|
||||
state.chainIndent = false;
|
||||
}
|
||||
|
||||
function newline(state: FormatState, indentUnit: string, indentDelta = 0) {
|
||||
trimTrailingSpaces(state);
|
||||
if (!state.out.endsWith("\n")) state.out += "\n";
|
||||
state.indentLevel = Math.max(0, state.indentLevel + indentDelta);
|
||||
state.atLineStart = true;
|
||||
state.pendingSpace = false;
|
||||
state.chainIndent = false;
|
||||
void indentUnit;
|
||||
}
|
||||
|
||||
function shouldInsertPendingSpace(output: string, token: string): boolean {
|
||||
const previous = lastNonWhitespace(output);
|
||||
if (!previous) return false;
|
||||
if ([".", "(", "[", "{"].includes(previous)) return false;
|
||||
if ([".", ")", "]", "}", ",", ":"].includes(token)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function shouldExpandOpening(text: string, index: number): boolean {
|
||||
const close = matchingClose(text[index] ?? "");
|
||||
const nextNonSpace = findNextNonWhitespace(text, index + 1);
|
||||
if (nextNonSpace == null || text[nextNonSpace] === close) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function popMatchingFrame(state: FormatState, close: string): { char: string; expanded: boolean; chainCall?: boolean } | undefined {
|
||||
const expectedOpen = close === "}" ? "{" : close === "]" ? "[" : "(";
|
||||
for (let index = state.stack.length - 1; index >= 0; index--) {
|
||||
const frame = state.stack[index];
|
||||
state.stack.splice(index, state.stack.length - index);
|
||||
if (frame?.char === expectedOpen) return frame;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function shouldBreakBeforeDot(text: string, index: number): boolean {
|
||||
const method = readIdentifier(text, findNextNonWhitespace(text, index + 1) ?? index + 1);
|
||||
if (!method || !CHAIN_METHODS.has(method)) return false;
|
||||
const previousNonSpace = findPreviousNonWhitespace(text, index - 1);
|
||||
if (previousNonSpace == null) return false;
|
||||
return text[previousNonSpace] === ")";
|
||||
}
|
||||
|
||||
function readQuotedLiteral(text: string, start: number, quote: string): { value: string; end: number } {
|
||||
let index = start + 1;
|
||||
while (index < text.length) {
|
||||
const char = text[index] ?? "";
|
||||
if (char === "\\") {
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if (char === quote) return { value: text.slice(start, index + 1), end: index };
|
||||
index++;
|
||||
}
|
||||
return { value: text.slice(start), end: text.length - 1 };
|
||||
}
|
||||
|
||||
function readLineComment(text: string, start: number): { value: string; end: number } {
|
||||
const end = text.indexOf("\n", start);
|
||||
if (end < 0) return { value: text.slice(start), end: text.length - 1 };
|
||||
return { value: text.slice(start, end), end: end - 1 };
|
||||
}
|
||||
|
||||
function readBlockComment(text: string, start: number): { value: string; end: number } {
|
||||
const end = text.indexOf("*/", start + 2);
|
||||
if (end < 0) return { value: text.slice(start), end: text.length - 1 };
|
||||
return { value: text.slice(start, end + 2), end: end + 1 };
|
||||
}
|
||||
|
||||
function readRegexLiteral(text: string, start: number): { value: string; end: number } {
|
||||
let index = start + 1;
|
||||
let inCharClass = false;
|
||||
while (index < text.length) {
|
||||
const char = text[index] ?? "";
|
||||
if (char === "\\") {
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if (char === "[") inCharClass = true;
|
||||
else if (char === "]") inCharClass = false;
|
||||
else if (char === "/" && !inCharClass) {
|
||||
let end = index;
|
||||
while (/[a-z]/i.test(text[end + 1] ?? "")) end++;
|
||||
return { value: text.slice(start, end + 1), end };
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return { value: text.slice(start), end: text.length - 1 };
|
||||
}
|
||||
|
||||
function looksLikeRegexLiteral(text: string, index: number): boolean {
|
||||
const previous = findPreviousNonWhitespace(text, index - 1);
|
||||
if (previous == null) return true;
|
||||
return "({[,=:!&|?".includes(text[previous] ?? "");
|
||||
}
|
||||
|
||||
function readIdentifier(text: string, start: number): string {
|
||||
let index = start;
|
||||
while (index < text.length && /[A-Za-z0-9_$]/.test(text[index] ?? "")) index++;
|
||||
return text.slice(start, index);
|
||||
}
|
||||
|
||||
function matchingClose(open: string): string {
|
||||
if (open === "{") return "}";
|
||||
if (open === "[") return "]";
|
||||
return ")";
|
||||
}
|
||||
|
||||
function findNextNonWhitespace(text: string, start: number): number | null {
|
||||
for (let index = start; index < text.length; index++) {
|
||||
if (!/\s/.test(text[index] ?? "")) return index;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function findPreviousNonWhitespace(text: string, start: number): number | null {
|
||||
for (let index = start; index >= 0; index--) {
|
||||
if (!/\s/.test(text[index] ?? "")) return index;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function lastNonWhitespace(text: string): string | null {
|
||||
const index = findPreviousNonWhitespace(text, text.length - 1);
|
||||
return index == null ? null : (text[index] ?? null);
|
||||
}
|
||||
|
||||
function trimTrailingSpaces(state: FormatState) {
|
||||
state.out = state.out.replace(/[ \t]+$/g, "");
|
||||
}
|
||||
|
||||
function cleanupFormattedMongoText(text: string): string {
|
||||
return text
|
||||
.split("\n")
|
||||
.map((line) => line.replace(/[ \t]+$/g, ""))
|
||||
.join("\n")
|
||||
.replace(/\n{3,}/g, "\n\n")
|
||||
.trim();
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user