Compare commits
No commits in common. "624befaec26d242cc797152414ca1d3b4e0a9055" and "954b0bc8a6c569494c073027895082198d70b932" have entirely different histories.
624befaec2
...
954b0bc8a6
|
@ -7,37 +7,63 @@
|
|||
//
|
||||
|
||||
import Foundation
|
||||
import JavaScriptCore
|
||||
import MongoSwift
|
||||
|
||||
struct MongoEvaluator {
|
||||
|
||||
static let decoder = BSONDecoder()
|
||||
static let context: JSContext = {
|
||||
let context = JSContext()!
|
||||
let date: @convention(block) (JSValue) -> Dictionary<String, Any> = { (input) in
|
||||
["$date": input]
|
||||
}
|
||||
let functions: [NSString: @convention(block) (JSValue) -> Dictionary<String, Any>] = [
|
||||
"ObjectId": { (input) in
|
||||
["$oid": input]
|
||||
},
|
||||
"NumberLong": { (input) in
|
||||
["$numberLong": input]
|
||||
},
|
||||
"NumberInt": { (input) in
|
||||
["$numberInt": input]
|
||||
},
|
||||
"NumberDecimal": { (input) in
|
||||
["$numberDecimal": input]
|
||||
},
|
||||
"Date": date,
|
||||
"ISODate": date,
|
||||
]
|
||||
functions.forEach { (key, value) in
|
||||
context.setObject(value, forKeyedSubscript: key)
|
||||
}
|
||||
let dbRef: @convention(block) (String, String) -> Dictionary<String, Any> = { (ref, id) in
|
||||
["$ref": ref, "$id": ["$oid": id]]
|
||||
}
|
||||
context.setObject(dbRef, forKeyedSubscript: "DBRef" as NSString)
|
||||
return context
|
||||
}()
|
||||
|
||||
static func parse(extendedJSON: String) -> BSON? {
|
||||
guard !extendedJSON.isEmpty else {
|
||||
static func parse(shellJSON: String) -> Document? {
|
||||
guard !shellJSON.isEmpty,
|
||||
let result = context.evaluateScript("JSON.stringify(\(shellJSON))") else {
|
||||
return nil
|
||||
}
|
||||
do {
|
||||
return try decoder.decode(BSON.self, from: extendedJSON)
|
||||
return try Document(fromJSON: result.toString())
|
||||
} catch {
|
||||
print("Unable to parse BSON: \(error)")
|
||||
print("Unable to parse Document: \(error)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
static func eval(command: String, connectingTo connStr: String) -> [BSON] {
|
||||
let realCommand = """
|
||||
Object.prototype.printExtJSON = function() { print(JSON.stringify(this)); };
|
||||
Array.prototype.printExtJSON = function() { this.map(JSON.stringify).forEach(it => print(it)); };
|
||||
\(command).printExtJSON()
|
||||
"""
|
||||
static func eval(command: String, connectingTo connStr: String) -> [Document] {
|
||||
print("Running command \"\(command)\" against \(connStr)")
|
||||
|
||||
let mongoOutputPipe = Pipe()
|
||||
|
||||
let mongoProcess = Process()
|
||||
mongoProcess.launchPath = "/usr/local/bin/mongo"
|
||||
mongoProcess.arguments = [connStr, "--quiet", "--norc", "--eval", realCommand]
|
||||
mongoProcess.arguments = [connStr, "--quiet", "--norc", "--eval", command]
|
||||
mongoProcess.standardOutput = mongoOutputPipe
|
||||
mongoProcess.launch()
|
||||
|
||||
|
@ -52,7 +78,7 @@ Array.prototype.printExtJSON = function() { this.map(JSON.stringify).forEach(it
|
|||
mongoOutputHandle.closeFile()
|
||||
|
||||
let lines = strs.joined(separator: "").components(separatedBy: "\n")
|
||||
return lines.compactMap(parse(extendedJSON:))
|
||||
return lines.compactMap(parse(shellJSON:))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -37,15 +37,8 @@ class Node {
|
|||
}
|
||||
|
||||
init(key: Key? = nil, value: BSON) {
|
||||
self.value = value
|
||||
|
||||
if key == nil,
|
||||
case let .document(doc) = value,
|
||||
case let .objectId(id) = doc["_id"] {
|
||||
self.key = .objectId(id)
|
||||
} else {
|
||||
self.key = key
|
||||
}
|
||||
self.value = value
|
||||
}
|
||||
|
||||
convenience init(document: Document) {
|
||||
|
|
|
@ -47,11 +47,9 @@ class DatabaseViewController: NSViewController {
|
|||
super.viewDidLoad()
|
||||
|
||||
mongoController.client.listDatabaseNames().flatMap { (databaseNames) -> EventLoopFuture<[DatabaseCollections]> in
|
||||
let futures = databaseNames.map { (name: String) -> EventLoopFuture<DatabaseCollections> in
|
||||
let db = self.mongoController.client.db(name)
|
||||
return db.listCollectionNames().map { (collectionNames: [String]) -> DatabaseCollections in
|
||||
let sortedNames = collectionNames.sorted()
|
||||
return DatabaseCollections(database: name, collections: sortedNames)
|
||||
let futures = databaseNames.map { (name) in
|
||||
self.mongoController.client.db(name).listCollectionNames().map { (collectionNames) in
|
||||
DatabaseCollections(database: name, collections: collectionNames)
|
||||
}
|
||||
}
|
||||
return EventLoopFuture.whenAllSucceed(futures, on: futures.first!.eventLoop)
|
||||
|
@ -60,8 +58,7 @@ class DatabaseViewController: NSViewController {
|
|||
|
||||
switch res {
|
||||
case let .success(databaseCollections):
|
||||
let sortedCollections = databaseCollections.sorted(by: { $0.database < $1.database })
|
||||
self.databaseCollections = sortedCollections
|
||||
self.databaseCollections = databaseCollections
|
||||
DispatchQueue.main.async {
|
||||
self.collectionsOutlineView.reloadData()
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ class QueryViewController: NSViewController {
|
|||
let collection: DatabaseCollection
|
||||
|
||||
var defaultQuery: String {
|
||||
"db.getCollection('\(collection.name)').find({}).toArray()"
|
||||
"db.getCollection('\(collection.name)').find({})"
|
||||
}
|
||||
|
||||
var hasQueryChanged: Bool {
|
||||
|
|
|
@ -990,7 +990,7 @@
|
|||
<autoresizingMask key="autoresizingMask"/>
|
||||
</scroller>
|
||||
</scrollView>
|
||||
<scrollView fixedFrame="YES" horizontalLineScroll="19" horizontalPageScroll="10" verticalLineScroll="19" verticalPageScroll="10" usesPredominantAxisScrolling="NO" id="tUs-K7-aZX">
|
||||
<scrollView fixedFrame="YES" autohidesScrollers="YES" horizontalLineScroll="19" horizontalPageScroll="10" verticalLineScroll="19" verticalPageScroll="10" usesPredominantAxisScrolling="NO" id="tUs-K7-aZX">
|
||||
<rect key="frame" x="0.0" y="96" width="741" height="371"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
|
||||
<clipView key="contentView" id="eia-Pq-LIB">
|
||||
|
|
Loading…
Reference in New Issue