Add document deletion

This commit is contained in:
Shadowfacts 2020-02-09 21:13:46 -05:00
parent feeb4e0370
commit e54c2a0570
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 90 additions and 28 deletions

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

@ -102,11 +102,51 @@ class QueryViewController: NSViewController {
mostRecentQuery = queryTextView.string
refresh()
}
rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map {
Node(value: $0)
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() {
@ -118,6 +158,15 @@ 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)
}
}
}

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>