@@ -52,13 +52,12 @@ fun <T: DocumentModel> Collection.getDocumentAs(id: String, deserializer: Deseri
5252 modelFromC4Doc(this , id, getC4Document(id), deserializer)
5353
5454
55- /* *
56- * Saves a [DocumentModel] instance as a document in the collection, with a specified conflict handler.
57- * If the model's [DocumentModel.documentMeta] property is null, it will be saved as a new document with the
58- * given [docID], which must not be null.
59- * Otherwise the [DocumentModel.documentMeta] property determines the document ID and prior revision ID, and the
60- * [docID] parameter should be null.
61- * After a successful save, the [DocumentModel.documentMeta] property is updated to the current state. */
55+ /* * Saves a [DocumentModel] instance as a document in the collection, with a specified conflict handler.
56+ * If the model's [DocumentModel.documentMeta] property is null, it will be saved as a new document with the
57+ * given [docID], which must not be null.
58+ * Otherwise the [DocumentModel.documentMeta] property determines the document ID and prior revision ID, and the
59+ * [docID] parameter should be null.
60+ * After a successful save, the [DocumentModel.documentMeta] property is updated to the current state. */
6261@ExperimentalSerializationApi
6362inline fun <reified T : DocumentModel > Collection.save (model : T ,
6463 docID : String? = null,
@@ -80,7 +79,7 @@ fun <T: DocumentModel> Collection.save(model: T,
8079 doc = MutableDocument (docID)
8180 } else {
8281 require(meta.collection == this || meta.collection == null ) {" saving document to wrong collection" }
83- require(docID == null || docID == meta.id) {" docID parameter does not match meta .id" }
82+ require(docID == null || docID == meta.id) {" docID parameter does not match documentMeta .id" }
8483 doc = getDocument(meta.id)?.toMutable() ? : MutableDocument (meta.id)
8584 }
8685
@@ -131,6 +130,31 @@ fun <T: DocumentModel> Collection.save(model: T,
131130typealias ModelConflictHandler <T > = (T , T ? )-> Boolean
132131
133132
133+ /* * Deletes a model's document from the collection.
134+ * @throws CouchbaseLiteException if the [DocumentModel.documentMeta] property is null. */
135+ fun Collection.delete (model : DocumentModel , concurrencyControl : ConcurrencyControl = ConcurrencyControl .LAST_WRITE_WINS ): Boolean {
136+ val meta = model.documentMeta ? : throw CouchbaseLiteException (" DocumentModel has no document ID" )
137+ require(meta.collection == this || meta.collection == null ) {" deleting document from wrong collection" }
138+ val doc = getDocument(meta.id) ? : return true
139+ if (doc.revisionID != meta.revisionID && concurrencyControl == ConcurrencyControl .FAIL_ON_CONFLICT )
140+ return false
141+ if (! delete(doc, concurrencyControl))
142+ return false
143+ model.documentMeta = null
144+ return true
145+ }
146+
147+
148+ /* * Purges a model's document from the collection.
149+ * @throws CouchbaseLiteException if the [DocumentModel.documentMeta] property is null,
150+ * or the document doesn't exist in the collection. */
151+ fun Collection.purge (model : DocumentModel ) {
152+ val id = model.documentMeta?.id ? : throw CouchbaseLiteException (" DocumentModel has no document ID" )
153+ purge(id)
154+ model.documentMeta = null
155+ }
156+
157+
134158/* * Creates a [DocumentModel] instance from a [C4Document]. */
135159private fun <T : DocumentModel > modelFromC4Doc (collection : Collection ,
136160 docID : String ,
0 commit comments