Database
public class Database
The Database class is used to make HTTP requests to the corresponding CouchDB database. This class can make CRUD (Create, Retrieve, Update, Delete) requests for:
- A single CouchDB
Document - An array of CouchDB documents
- A CouchDB
DesignDocument - A
Documentattachment
-
Indicates when to update.
See moreDeclaration
Swift
public enum StaleOptions -
Query parameters for view functions from design documents.
See moreDeclaration
Swift
public enum QueryParameters -
Name for the Database.
Declaration
Swift
public let name: String -
Escaped name for the Database.
Declaration
Swift
public let escapedName: String -
ConnectionPropertiesthe Database will use for its actions.Declaration
Swift
public let connProperties: ConnectionProperties
-
Create a new document.
Usage Example:
struct MyDocument: Document { let _id: String? var _rev: String? var value: String } var myDocument = MyDocument(_id: "Kitura", _rev: nil, value: "Hello World") database.create(myDocument) { (response, error) in if let response = response { print("Document: \(response.id), created with rev: \(response.rev)") } }Declaration
Swift
public func create<D: Document>(_ document: D, callback: @escaping (DocumentResponse?, CouchDBError?) -> ()) -
Retrieve a document from the database.
Usage Example:
struct MyDocument: Document { let _id: String? var _rev: String? var value: String } database.retrieve("Kitura") { (document: MyDocument?, error: CouchDBError?) in if var document = document { print("Retrieved document with value: \(document.value)") } }Declaration
Swift
public func retrieve<D: Document>(_ id: String, callback: @escaping (D?, CouchDBError?) -> ()) -
Update a document in the database. If no document exists for the provided id, a new document is created.
Usage Example:
struct MyDocument: Document { let _id: String? var _rev: String? var value: String } var myDocument = MyDocument(_id: "Kitura", _rev: nil, value: "New Value") database.update("<document_id>", rev: "<latest_rev>", document: myDocument) { (response, error) in if let response = response { print("Document: \(response.id), updated") } }Declaration
Swift
public func update<D: Document>(_ id: String, rev: String, document: D, callback: @escaping (DocumentResponse?, CouchDBError?) -> ()) -
Delete a document.
Usage Example:
database.delete("<document_id>", rev: "<latest_rev>") { (error) in if let response = response { print("Document: \(response.id), deleted") } }Declaration
Swift
public func delete(_ id: String, rev: String, callback: @escaping (CouchDBError?) -> ())
-
Bulk update or insert documents into the database.
Note
Note:
- CouchDB will return the results in the same order as supplied in the array. The
idand revision will be added for every document passed as content to a bulk insert, even for those that were just deleted. - If you omit the per-document
_idspecification, CouchDB will generate unique IDs for you, as it does for regularcreate(_:callback:)function. - Updating existing documents requires setting the
_revmember to the revision being updated. To delete a document set the_deletedmember totrue.[ {"_id": "0", "_rev": "1-62657917", "_deleted": true}, {"_id": "1", "_rev": "1-2089673485", "integer": 2, "string": "2"}, {"_id": "2", "_rev": "1-2063452834", "integer": 3, "string": "3"} ] - If the
_revdoes not match the current version of the document, then that particular document will not be saved and will be reported as a conflict, but this does not prevent other documents in the batch from being saved.[ {"id": "0", "error": "conflict", "reason": "Document update conflict."}, {"id": "1", "rev": "2-1579510027"}, {"id": "2", "rev": "2-3978456339"} ]
Declaration
Swift
public func bulk(documents: BulkDocuments, callback: @escaping ([BulkResponse]?, CouchDBError?) -> ())Parameters
documentsAn
BulkDocumentsstruct containing an array of JSON documents to be updated or inserted.callbackcallback containing either a
BulkResponsearray or an error. - CouchDB will return the results in the same order as supplied in the array. The
-
Executes the specified view function from the specified design document.
Declaration
Swift
public func queryByView(_ view: String, ofDesign design: String, usingParameters params: [Database.QueryParameters], callback: @escaping (AllDatabaseDocuments?, CouchDBError?) -> ())Parameters
viewView function name String.
designDesign document name.
paramsQuery parameters for the function.
callbackCallback containing either the
AllDatabaseDocumentsor aCouchDBError. -
Retrieve all documents in the database using the CouchDB
_all_docsview. IfincludeDocumentsis false, each returnedAllDatabaseDocumentsrow will be structured as follows:[ "id": "<_id>", "key": "<_id>", "value": [ "rev": "<_rev>" ] ]If
includeDocumentsis true, each row will have an additionaldoc
field containing the JSON document. These documents can then be decoded to a given swift type usingdecodeDocuments(ofType:). https://docs.couchdb.org/en/stable/api/database/bulk-api.htmlUsage Example:
struct MyDocument: Document { let _id: String? var _rev: String? var value: String } database.retrieveAll(includeDocuments: true) { (allDocs, error) in if let allDocs = allDocs, let decodedDocs = allDocs.decodeDocuments(ofType: MyDocument) { for doc in decodedDocs { print("Retrieved MyDocument with value: \(doc.value)") } } }Declaration
Swift
public func retrieveAll(includeDocuments: Bool = false, callback: @escaping (AllDatabaseDocuments?, CouchDBError?) -> ())
-
Create a design document. If a design document already exists with the same name it will be replaced.
Declaration
Swift
public func createDesign(_ designName: String, document: DesignDocument, callback: @escaping (DocumentResponse?, CouchDBError?) -> ())Parameters
designNameName String for the design document.
documentThe JSON data of the new design document.
callbackCallback containing the
DocumentResponseor aCouchDBError. -
Delete a design document.
Declaration
Swift
public func deleteDesign(_ designName: String, revision: String, failOnNotFound: Bool = false, callback: @escaping (CouchDBError?) -> ())Parameters
designNameName String of the design document to delete.
revisionThe latest revision String of the design document to delete.
failOnNotFoundBool indicating whether to return an error if the design document was not found.
callbackCallback containing the
DocumentResponseor aCouchDBError.
-
Attach the provided
DatatheDocumentwith the provided ID with the given attachmentName. If an attachment exists with the same name it will be replaced with the new attachment.Declaration
Swift
public func createAttachment(_ docId: String, docRevison: String, attachmentName: String, attachmentData: Data, contentType: String, callback: @escaping (DocumentResponse?, CouchDBError?) -> ())Parameters
docIdDocument ID String that the attachment is associated with.
docRevisionDocument revision String.
attachmentNameAttachment name String.
attachmentDataThe attachment Data.
contentTypeAttachment MIME type String.
callbackCallback containing the
DocumentResponseor aCouchDBError. -
Get an attachment associated with a specified document.
Declaration
Swift
public func retrieveAttachment(_ docId: String, attachmentName: String, callback: @escaping (Data?, String?, CouchDBError?) -> ())Parameters
docIdDocument ID String that the attachment is associated with.
attachmentNameName String for the desired attachment.
callbackCallback containing either the retrieved attachment data and the content type of the attachment or a
CouchDBError. -
Delete an attachment associated with a specified document.
Declaration
Swift
public func deleteAttachment(_ docId: String, docRevison: String, attachmentName: String, callback: @escaping (CouchDBError?) -> ())Parameters
docIdDocument ID String that the attachment is associated with.
docRevisionLatest revision String of the document.
attachmentNameName String of the attachment to be deleted.
callbackCallback containing either the
DocumentResponseor aCouchDBError.
View on GitHub
Database Class Reference