-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmongodb.ts
More file actions
595 lines (530 loc) · 19.4 KB
/
mongodb.ts
File metadata and controls
595 lines (530 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/**
* MongoDB driver using the `mongodb` package.
*
* Maps MongoDB concepts to the Connector interface:
* - listSchemas() → lists databases
* - listTables(schema) → lists collections in a database
* - describeTable(s, t) → samples documents to infer field types
* - execute(query) → parses and executes MQL commands
*
* Query format (JSON string):
* { "database": "mydb", "collection": "users", "command": "find", "filter": { "age": { "$gt": 25 } } }
* { "database": "mydb", "collection": "orders", "command": "aggregate", "pipeline": [...] }
* { "database": "mydb", "collection": "users", "command": "insertMany", "documents": [...] }
* { "database": "mydb", "collection": "users", "command": "countDocuments", "filter": {} }
*/
import type { ConnectionConfig, Connector, ConnectorResult, SchemaColumn } from "./types"
/** Supported MQL commands. */
type MqlCommand =
| "find"
| "aggregate"
| "countDocuments"
| "distinct"
| "insertOne"
| "insertMany"
| "updateOne"
| "updateMany"
| "deleteOne"
| "deleteMany"
| "createCollection"
| "dropCollection"
| "createIndex"
| "listIndexes"
| "ping"
interface MqlQuery {
database?: string
collection?: string
command: MqlCommand
// find
filter?: Record<string, unknown>
projection?: Record<string, unknown>
sort?: Record<string, unknown>
limit?: number
skip?: number
// aggregate
pipeline?: Record<string, unknown>[]
// insert
document?: Record<string, unknown>
documents?: Record<string, unknown>[]
// update
update?: Record<string, unknown>
// distinct
field?: string
// createIndex
keys?: Record<string, unknown>
options?: Record<string, unknown>
// createCollection
name?: string
}
/**
* Infer a human-readable type name from a JavaScript value.
*/
function inferType(value: unknown): string {
if (value === null || value === undefined) return "null"
if (Array.isArray(value)) return "array"
if (value instanceof Date) return "date"
// mongodb BSON types
const ctor = (value as any)?._bsontype
if (ctor) {
switch (ctor) {
case "ObjectId":
case "ObjectID":
return "objectId"
case "Decimal128":
return "decimal128"
case "Long":
return "int64"
case "Int32":
return "int32"
case "Double":
return "double"
case "Binary":
return "binary"
case "Timestamp":
return "timestamp"
case "MinKey":
return "minKey"
case "MaxKey":
return "maxKey"
case "BSONRegExp":
return "regex"
case "Code":
return "javascript"
case "BSONSymbol":
return "symbol"
case "UUID":
return "uuid"
default:
return ctor.toLowerCase()
}
}
const t = typeof value
if (t === "number") return Number.isInteger(value as number) ? "int32" : "double"
if (t === "boolean") return "bool"
if (t === "string") return "string"
if (t === "object") return "object"
return "unknown"
}
/**
* Extract field names and their observed types from a set of documents.
* Only inspects top-level fields — nested objects are reported as type "object".
*/
function extractFields(docs: Record<string, unknown>[]): Map<string, Set<string>> {
const fieldTypes = new Map<string, Set<string>>()
for (const doc of docs) {
for (const [key, value] of Object.entries(doc)) {
const types = fieldTypes.get(key) ?? new Set()
types.add(inferType(value))
fieldTypes.set(key, types)
}
}
return fieldTypes
}
export async function connect(config: ConnectionConfig): Promise<Connector> {
let mongoModule: any
try {
mongoModule = await import("mongodb")
mongoModule = mongoModule.default || mongoModule
} catch {
throw new Error("MongoDB driver not installed. Run: npm install mongodb")
}
const MongoClient = mongoModule.MongoClient
let client: any
const explicitDb = config.database as string | undefined
/** Resolve which database to use: query-specified, config-specified, or URI default. */
function resolveDb(queryDb?: string): any {
if (queryDb) return client.db(queryDb)
if (explicitDb) return client.db(explicitDb)
// Fall back to the database embedded in the connection string URI, or MongoDB's default
return client.db()
}
/**
* Serialize a value for tabular display.
* BSON types are converted to strings; nested objects are JSON-serialized.
*/
function serializeValue(val: unknown): unknown {
if (val === null || val === undefined) return val
if (typeof val !== "object") return val
// BSON ObjectId
if ((val as any)._bsontype === "ObjectId" || (val as any)._bsontype === "ObjectID") {
return (val as any).toString()
}
// BSON Decimal128, Long, Int32, Double
if (
(val as any)._bsontype === "Decimal128" ||
(val as any)._bsontype === "Long" ||
(val as any)._bsontype === "Int32" ||
(val as any)._bsontype === "Double"
) {
return (val as any).toString()
}
// BSON UUID
if ((val as any)._bsontype === "UUID") {
return (val as any).toString()
}
// BSON Binary
if ((val as any)._bsontype === "Binary") {
return `Binary(${(val as any).length()})`
}
// BSON Timestamp
if ((val as any)._bsontype === "Timestamp") {
return (val as any).toString()
}
// Date
if (val instanceof Date) {
return val.toISOString()
}
// Arrays, plain objects, and remaining BSON types — JSON-serialize for tabular display
return JSON.stringify(val)
}
return {
async connect() {
// Support connection_string or individual fields
// SECURITY: URI may contain credentials — never log it
let uri: string
if (config.connection_string) {
uri = config.connection_string as string
} else {
const host = (config.host as string) ?? "127.0.0.1"
const port = (config.port as number) ?? 27017
const user = config.user as string | undefined
const password = config.password as string | undefined
// Include database in URI for correct auth-source resolution
const dbPath = explicitDb ? `/${encodeURIComponent(explicitDb)}` : ""
if (user && password) {
uri = `mongodb://${encodeURIComponent(user)}:${encodeURIComponent(password)}@${host}:${port}${dbPath}`
} else {
uri = `mongodb://${host}:${port}${dbPath}`
}
}
const connectOptions: Record<string, unknown> = {
connectTimeoutMS: (config.connect_timeout as number) ?? 10000,
serverSelectionTimeoutMS: (config.server_selection_timeout as number) ?? 10000,
}
if (config.auth_source) {
connectOptions.authSource = config.auth_source
}
if (config.replica_set) {
connectOptions.replicaSet = config.replica_set
}
if (config.tls !== undefined) {
connectOptions.tls = config.tls
}
if (config.direct_connection !== undefined) {
connectOptions.directConnection = config.direct_connection
}
client = new MongoClient(uri, connectOptions)
await client.connect()
},
async execute(query: string, limit?: number, _binds?: any[]): Promise<ConnectorResult> {
let parsed: MqlQuery
try {
parsed = JSON.parse(query) as MqlQuery
} catch (e) {
throw new Error(`Invalid MQL query — must be valid JSON. Error: ${(e as Error).message}`)
}
if (!parsed.command) {
throw new Error("MQL query must include a 'command' field")
}
const db = resolveDb(parsed.database)
const effectiveLimit = limit ?? 1000
const cmd = parsed.command
// Commands that don't need a collection
if (cmd === "ping") {
const result = await db.command({ ping: 1 })
return { columns: ["ok"], rows: [[result.ok]], row_count: 1, truncated: false }
}
if (cmd === "createCollection") {
const name = parsed.name ?? parsed.collection
if (!name) {
throw new Error("createCollection requires 'name' or 'collection'")
}
await db.createCollection(name, parsed.options ?? {})
return { columns: ["result"], rows: [["ok"]], row_count: 1, truncated: false }
}
if (cmd === "dropCollection") {
if (!parsed.collection) {
throw new Error("dropCollection requires 'collection'")
}
const dropped = await db
.collection(parsed.collection)
.drop()
.catch((e: any) => {
if (e.codeName === "NamespaceNotFound" || e.code === 26) return false
throw e
})
return {
columns: ["dropped"],
rows: [[dropped]],
row_count: 1,
truncated: false,
}
}
if (!parsed.collection) {
throw new Error(`Command '${cmd}' requires a 'collection' field`)
}
const coll = db.collection(parsed.collection)
switch (cmd) {
case "find": {
let cursor = coll.find(parsed.filter ?? {})
if (parsed.projection) cursor = cursor.project(parsed.projection)
if (parsed.sort) cursor = cursor.sort(parsed.sort)
if (parsed.skip) cursor = cursor.skip(parsed.skip)
// Cap user-specified limit against effectiveLimit to prevent OOM
const queryLimit = parsed.limit ? Math.min(parsed.limit, effectiveLimit) : effectiveLimit
cursor = cursor.limit(queryLimit + 1)
const docs = await cursor.toArray()
const truncated = docs.length > queryLimit
const limited = truncated ? docs.slice(0, queryLimit) : docs
if (limited.length === 0) {
return { columns: [], rows: [], row_count: 0, truncated: false }
}
// Build column list from all documents (documents may have different fields)
const colSet = new Set<string>()
for (const doc of limited) {
for (const key of Object.keys(doc)) {
colSet.add(key)
}
}
const columns = Array.from(colSet)
const rows = limited.map((doc: any) => columns.map((col) => serializeValue(doc[col])))
return { columns, rows, row_count: limited.length, truncated }
}
case "aggregate": {
if (!parsed.pipeline || !Array.isArray(parsed.pipeline)) {
throw new Error("aggregate requires a 'pipeline' array")
}
// Block dangerous stages/operators:
// - $out/$merge: write operations (top-level stage keys)
// - $function/$accumulator: arbitrary JS execution (can be nested in expressions)
const pipeline = [...parsed.pipeline]
const blockedWriteStages = ["$out", "$merge"]
const hasBlockedWrite = pipeline.some((stage) =>
blockedWriteStages.some((s) => s in stage),
)
if (hasBlockedWrite) {
throw new Error(
`Pipeline contains a blocked write stage (${blockedWriteStages.join(", ")}). Write operations are not allowed.`,
)
}
// $function/$accumulator can appear nested inside $project, $addFields, $group, etc.
// Stringify and scan to catch them at any depth.
const pipelineStr = JSON.stringify(pipeline)
if (pipelineStr.includes('"$function"') || pipelineStr.includes('"$accumulator"')) {
throw new Error(
"Pipeline contains a blocked operator ($function, $accumulator). Executing arbitrary JavaScript is not allowed.",
)
}
// Cap or append $limit to prevent OOM (write stages already blocked above).
const limitIdx = pipeline.findIndex((stage) => "$limit" in stage)
if (limitIdx >= 0) {
// Cap user-specified $limit against effectiveLimit
const userLimit = (pipeline[limitIdx] as any).$limit
if (typeof userLimit === "number" && userLimit > effectiveLimit) {
pipeline[limitIdx] = { $limit: effectiveLimit + 1 }
}
} else {
pipeline.push({ $limit: effectiveLimit + 1 })
}
const docs = await coll.aggregate(pipeline).toArray()
const truncated = docs.length > effectiveLimit
const limited = truncated ? docs.slice(0, effectiveLimit) : docs
if (limited.length === 0) {
return { columns: [], rows: [], row_count: 0, truncated: false }
}
const colSet = new Set<string>()
for (const doc of limited) {
for (const key of Object.keys(doc)) {
colSet.add(key)
}
}
const columns = Array.from(colSet)
const rows = limited.map((doc: any) => columns.map((col) => serializeValue(doc[col])))
return { columns, rows, row_count: limited.length, truncated }
}
case "countDocuments": {
const count = await coll.countDocuments(parsed.filter ?? {})
return {
columns: ["count"],
rows: [[count]],
row_count: 1,
truncated: false,
}
}
case "distinct": {
if (!parsed.field) {
throw new Error("distinct requires a 'field' string")
}
const values = await coll.distinct(parsed.field, parsed.filter ?? {})
const truncated = values.length > effectiveLimit
const limited = truncated ? values.slice(0, effectiveLimit) : values
return {
columns: [parsed.field],
rows: limited.map((v: unknown) => [serializeValue(v)]),
row_count: limited.length,
truncated,
}
}
case "insertOne": {
if (!parsed.document) {
throw new Error("insertOne requires a 'document' object")
}
const result = await coll.insertOne(parsed.document)
return {
columns: ["insertedId"],
rows: [[result.insertedId.toString()]],
row_count: 1,
truncated: false,
}
}
case "insertMany": {
if (!parsed.documents || !Array.isArray(parsed.documents)) {
throw new Error("insertMany requires a 'documents' array")
}
const result = await coll.insertMany(parsed.documents)
return {
columns: ["insertedCount"],
rows: [[result.insertedCount]],
row_count: 1,
truncated: false,
}
}
case "updateOne": {
if (!parsed.update) {
throw new Error("updateOne requires an 'update' object")
}
const result = await coll.updateOne(parsed.filter ?? {}, parsed.update)
return {
columns: ["matchedCount", "modifiedCount"],
rows: [[result.matchedCount, result.modifiedCount]],
row_count: 1,
truncated: false,
}
}
case "updateMany": {
if (!parsed.update) {
throw new Error("updateMany requires an 'update' object")
}
const result = await coll.updateMany(parsed.filter ?? {}, parsed.update)
return {
columns: ["matchedCount", "modifiedCount"],
rows: [[result.matchedCount, result.modifiedCount]],
row_count: 1,
truncated: false,
}
}
case "deleteOne": {
const result = await coll.deleteOne(parsed.filter ?? {})
return {
columns: ["deletedCount"],
rows: [[result.deletedCount]],
row_count: 1,
truncated: false,
}
}
case "deleteMany": {
const result = await coll.deleteMany(parsed.filter ?? {})
return {
columns: ["deletedCount"],
rows: [[result.deletedCount]],
row_count: 1,
truncated: false,
}
}
case "createIndex": {
if (!parsed.keys) {
throw new Error("createIndex requires a 'keys' object")
}
const indexName = await coll.createIndex(parsed.keys, parsed.options ?? {})
return {
columns: ["indexName"],
rows: [[indexName]],
row_count: 1,
truncated: false,
}
}
case "listIndexes": {
const indexes = await coll.listIndexes().toArray()
if (indexes.length === 0) {
return { columns: [], rows: [], row_count: 0, truncated: false }
}
const columns = ["name", "key", "unique"]
const rows = indexes.map((idx: any) => [idx.name, JSON.stringify(idx.key), idx.unique ?? false])
return { columns, rows, row_count: rows.length, truncated: false }
}
default:
throw new Error(`Unsupported MQL command: ${cmd}`)
}
},
async listSchemas(): Promise<string[]> {
try {
const admin = client.db().admin()
const result = await admin.listDatabases({ nameOnly: true, authorizedDatabases: true })
return result.databases
.map((db: any) => db.name as string)
.filter((name: string) => name !== "local" && name !== "config")
.sort()
} catch {
// Fallback for users without listDatabases privilege: return the configured/default database
const db = resolveDb()
return [db.databaseName]
}
},
async listTables(schema: string): Promise<Array<{ name: string; type: string }>> {
const db = client.db(schema)
const collections = await db.listCollections().toArray()
return collections
.map((c: any) => ({
name: c.name as string,
type: c.type === "view" ? "view" : "collection",
}))
.sort((a: { name: string }, b: { name: string }) => a.name.localeCompare(b.name))
},
async describeTable(schema: string, table: string): Promise<SchemaColumn[]> {
const db = client.db(schema)
const coll = db.collection(table)
// Sample up to 100 documents to infer schema
const docs = await coll.find({}).limit(100).toArray()
if (docs.length === 0) {
return []
}
const fieldTypes = extractFields(docs)
// Track which fields are missing from some documents (nullable by absence)
const fieldPresence = new Map<string, number>()
for (const doc of docs) {
for (const key of Object.keys(doc)) {
fieldPresence.set(key, (fieldPresence.get(key) ?? 0) + 1)
}
}
const columns: SchemaColumn[] = []
for (const [name, types] of fieldTypes) {
const typeArr = Array.from(types)
const hasNull = typeArr.includes("null")
const nonNullTypes = typeArr.filter((t) => t !== "null")
const dataType =
nonNullTypes.length === 0 ? "null" : nonNullTypes.length === 1 ? nonNullTypes[0] : nonNullTypes.join(" | ")
// Field is nullable if it has null values OR is missing from some documents
const presentIn = fieldPresence.get(name) ?? 0
const missingFromSome = presentIn < docs.length
columns.push({
name,
data_type: dataType,
nullable: hasNull || missingFromSome,
})
}
// Sort: _id first, then alphabetical
columns.sort((a, b) => {
if (a.name === "_id") return -1
if (b.name === "_id") return 1
return a.name.localeCompare(b.name)
})
return columns
},
async close() {
if (client) {
await client.close()
client = null
}
},
}
}