Compare commits

...

3 Commits

Author SHA1 Message Date
Shadowfacts e54c2a0570
Add document deletion 2020-02-09 21:13:46 -05:00
Shadowfacts feeb4e0370
Add refreshing 2020-02-09 21:13:22 -05:00
Shadowfacts 3c4d07093d
Use correct Node initializer 2020-02-09 20:37:43 -05:00
6 changed files with 150 additions and 45 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="15702" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16085" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15702"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16085"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
@ -107,7 +107,8 @@
<action selector="saveDocumentAs:" target="-1" id="mDf-zr-I0C"/>
</connections>
</menuItem>
<menuItem title="Revert to Saved" keyEquivalent="r" id="KaW-ft-85H">
<menuItem title="Revert to Saved" id="KaW-ft-85H">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="revertDocumentToSaved:" target="-1" id="iJ3-Pv-kwq"/>
</connections>
@ -362,6 +363,11 @@
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Database" id="nD3-Xo-LUZ">
<items>
<menuItem title="Refresh" keyEquivalent="r" id="kpI-5J-eaC">
<connections>
<action selector="refresh:" target="-1" id="S5L-sX-Z2j"/>
</connections>
</menuItem>
<menuItem title="Run Query" id="RSP-nH-a1I">
<string key="keyEquivalent" base64-UTF8="YES">
DQ

View File

@ -33,5 +33,13 @@ class MongoController {
client = try! MongoClient(connectionString, using: group)
}
func db(for collection: DatabaseCollection) -> MongoDatabase {
return client.db(collection.database)
}
func collection(_ collection: DatabaseCollection) -> MongoCollection<Document> {
return db(for: collection).collection(collection.name)
}
}

View File

@ -12,16 +12,17 @@ import MongoSwift
class Node {
let key: Key?
let value: BSON
weak var parent: Node?
lazy private(set) var children: [Node] = {
switch value {
case let .array(array):
return array.enumerated().map { (index, val) in
Node(key: .index(index), value: val)
Node(key: .index(index), value: val, parent: self)
}
case let .document(doc):
return doc.map { (key, val) in
Node(key: .name(key), value: val)
Node(key: .name(key), value: val, parent: self)
}
default:
return []
@ -36,8 +37,9 @@ class Node {
numberOfChildren > 0
}
init(key: Key? = nil, value: BSON) {
init(key: Key? = nil, value: BSON, parent: Node? = nil) {
self.value = value
self.parent = parent
if key == nil,
case let .document(doc) = value,

View File

@ -150,10 +150,11 @@ class DatabaseViewController: NSViewController {
}
@IBAction func runQuery(_ sender: Any) {
guard let queryViewController = queryViewController else {
return
}
queryViewController.runQuery()
queryViewController?.runQuery()
}
@IBAction func refresh(_ sender: Any) {
queryViewController?.refresh()
}
}

View File

@ -27,6 +27,8 @@ class QueryViewController: NSViewController {
return queryTextView.string != defaultQuery
}
var mostRecentQuery: String? = nil
var rootNodes: [Node] = []
init(mongoController: MongoController, collection: DatabaseCollection) {
@ -43,11 +45,8 @@ class QueryViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let db = mongoController.client.db(collection.database)
let collection = db.collection(self.collection.name)
let documents = try! collection.find().all()
rootNodes = documents.map { Node(document: $0) }
refresh()
verticalSplitView.delegate = self
verticalSplitView.setHoldingPriority(.defaultHigh, forSubviewAt: 0)
@ -61,9 +60,6 @@ class QueryViewController: NSViewController {
outlineView.target = self
outlineView.doubleAction = #selector(outlineCellDoubleClicked)
title = "\(self.collection.database).\(self.collection.name)"
documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")"
}
override func viewWillAppear() {
@ -80,14 +76,77 @@ class QueryViewController: NSViewController {
view.window!.makeFirstResponder(outlineView)
}
func runQuery() {
let query = queryTextView.string
let connStr = "\(mongoController.connectionString)/\(collection.database)"
func refresh(reload: Bool = true) {
if let query = mostRecentQuery {
let connStr = "\(mongoController.connectionString)/\(collection.database)"
rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map {
Node(value: $0)
}
title = query
documentCountLabel.stringValue = "\(rootNodes.count) result\(rootNodes.count == 1 ? "" : "s")"
} else {
let documents = try! mongoController.collection(collection).find().all()
rootNodes = documents.map { Node(document: $0) }
rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map {
Node(document: $0)
title = "\(self.collection.database).\(self.collection.name)"
documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")"
}
if reload {
outlineView.reloadData()
}
}
func runQuery() {
mostRecentQuery = queryTextView.string
refresh()
}
func deleteRootNode(_ node: Node) {
guard case let .document(doc) = node.value else { return }
let alert = NSAlert()
alert.alertStyle = .warning
alert.messageText = "Confirm deletion"
alert.informativeText = "Are you sure you want to delete the document"
let id: ObjectId?
if case let .objectId(docId) = doc["_id"] {
id = docId
alert.informativeText += " with id \(docId)"
} else {
id = nil
}
alert.addButton(withTitle: "Delete")
alert.addButton(withTitle: "Cancel")
alert.beginSheetModal(for: view.window!) { (response) in
guard response == .alertFirstButtonReturn else { return }
self.mongoController.collection(self.collection).deleteOne(doc).whenComplete { (result) in
DispatchQueue.main.async {
switch result {
case let .success(result):
guard let result = result, result.deletedCount == 1 else {
let alert = NSAlert()
alert.alertStyle = .critical
alert.messageText = "Error deleting document"
if let id = id {
alert.informativeText = "The document with id \(id) could not be deleted."
} else {
alert.informativeText = "The document could not be deleted."
}
alert.beginSheetModal(for: self.view.window!, completionHandler: nil)
return
}
self.refresh()
case let .failure(error):
let alert = NSAlert(error: error)
alert.beginSheetModal(for: self.view.window!, completionHandler: nil)
}
}
}
}
outlineView.reloadData()
}
@objc func outlineCellDoubleClicked() {
@ -99,9 +158,27 @@ class QueryViewController: NSViewController {
}
}
}
@IBAction func deleteNode(_ sender: Any) {
guard let node = outlineView.item(atRow: outlineView.clickedRow) as? Node else {
return
}
if node.parent == nil {
deleteRootNode(node)
}
}
}
extension QueryViewController: NSMenuItemValidation {
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
if menuItem.action == #selector(deleteNode(_:)) {
return outlineView.clickedRow != -1
}
return true
}
}
extension QueryViewController: NSSplitViewDelegate {
func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
return max(80, min(splitView.bounds.height / 2, proposedPosition))

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="15702" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16085" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15702"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16085"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
@ -994,19 +994,18 @@
<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">
<rect key="frame" x="1" y="0.0" width="739" height="370"/>
<rect key="frame" x="1" y="0.0" width="724" height="355"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<outlineView verticalHuggingPriority="750" allowsExpansionToolTips="YES" alternatingRowBackgroundColors="YES" columnReordering="NO" multipleSelection="NO" autosaveName="QueryOutline" rowSizeStyle="automatic" headerView="jah-QY-QbI" viewBased="YES" indentationPerLevel="16" outlineTableColumn="emg-oX-7XY" id="uu9-9q-MWr">
<rect key="frame" x="0.0" y="0.0" width="739" height="345"/>
<rect key="frame" x="0.0" y="0.0" width="724" height="330"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
<color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
<tableColumns>
<tableColumn identifier="FieldNameCol" width="339" minWidth="40" maxWidth="1000" id="emg-oX-7XY">
<tableColumn identifier="FieldNameCol" width="334" minWidth="40" maxWidth="1000" id="emg-oX-7XY">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="Field Name">
<font key="font" metaFont="controlContent" size="11"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
</tableHeaderCell>
@ -1018,11 +1017,11 @@
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView identifier="FieldNameCell" id="8dc-a6-0k5">
<rect key="frame" x="1" y="1" width="339" height="17"/>
<rect key="frame" x="1" y="1" width="334" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="qEd-h6-M2A">
<rect key="frame" x="0.0" y="0.0" width="339" height="17"/>
<rect key="frame" x="0.0" y="0.0" width="334" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Table View Cell" id="8JQ-A4-jXh">
<font key="font" metaFont="system"/>
@ -1037,9 +1036,8 @@
</tableCellView>
</prototypeCellViews>
</tableColumn>
<tableColumn identifier="FieldValueCol" width="297" minWidth="40" maxWidth="1000" id="ggz-sp-EaO">
<tableColumn identifier="FieldValueCol" width="292" minWidth="40" maxWidth="1000" id="ggz-sp-EaO">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="Field Value">
<font key="font" metaFont="controlContent" size="11"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
</tableHeaderCell>
@ -1051,11 +1049,11 @@
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView identifier="FieldValueCell" id="E6Z-9f-0gg">
<rect key="frame" x="343" y="1" width="297" height="17"/>
<rect key="frame" x="338" y="1" width="292" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Px2-mb-c5m">
<rect key="frame" x="0.0" y="0.0" width="297" height="17"/>
<rect key="frame" x="0.0" y="0.0" width="292" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Table View Cell" id="rED-77-VFk">
<font key="font" metaFont="system"/>
@ -1070,9 +1068,8 @@
</tableCellView>
</prototypeCellViews>
</tableColumn>
<tableColumn identifier="ValueTypeCol" width="94" minWidth="10" maxWidth="3.4028234663852886e+38" id="Tow-Y6-pc7">
<tableColumn identifier="ValueTypeCol" width="89" minWidth="10" maxWidth="3.4028234663852886e+38" id="Tow-Y6-pc7">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" alignment="left" title="Value Type">
<font key="font" metaFont="controlContent" size="11"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</tableHeaderCell>
@ -1084,11 +1081,11 @@
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView identifier="ValueTypeCell" id="tUj-Jd-Rj1">
<rect key="frame" x="643" y="1" width="94" height="17"/>
<rect key="frame" x="633" y="1" width="89" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="mBU-Qs-G4S">
<rect key="frame" x="0.0" y="0.0" width="94" height="17"/>
<rect key="frame" x="0.0" y="0.0" width="89" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Table View Cell" id="tmE-QF-vUZ">
<font key="font" metaFont="system"/>
@ -1107,16 +1104,16 @@
</outlineView>
</subviews>
</clipView>
<scroller key="horizontalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="WBg-uK-bLa">
<rect key="frame" x="1" y="119" width="223" height="15"/>
<scroller key="horizontalScroller" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="WBg-uK-bLa">
<rect key="frame" x="1" y="355" width="724" height="15"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="Sj7-M1-no8">
<rect key="frame" x="224" y="17" width="15" height="102"/>
<scroller key="verticalScroller" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="Sj7-M1-no8">
<rect key="frame" x="725" y="25" width="15" height="330"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<tableHeaderView key="headerView" id="jah-QY-QbI">
<rect key="frame" x="0.0" y="0.0" width="739" height="25"/>
<rect key="frame" x="0.0" y="0.0" width="724" height="25"/>
<autoresizingMask key="autoresizingMask"/>
</tableHeaderView>
</scrollView>
@ -1135,7 +1132,21 @@
<constraint firstAttribute="trailing" secondItem="E3D-CM-hk3" secondAttribute="trailing" id="pjR-Xa-8co"/>
<constraint firstItem="E3D-CM-hk3" firstAttribute="leading" secondItem="Hz6-mo-xeY" secondAttribute="leading" id="sR0-qd-Vco"/>
</constraints>
<connections>
<outlet property="menu" destination="nL5-kg-dty" id="KQ4-CJ-Hz4"/>
</connections>
<point key="canvasLocation" x="271.5" y="262.5"/>
</customView>
<menu id="nL5-kg-dty">
<items>
<menuItem title="Delete" id="nn0-ZF-eDZ">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="deleteNode:" target="-2" id="dgm-SC-hn5"/>
</connections>
</menuItem>
</items>
<point key="canvasLocation" x="887" y="210"/>
</menu>
</objects>
</document>