// use default params
let myClient = CouchDBClient()
// provide your own params
let couchDBClient = CouchDBClient(
couchProtocol: .http,
couchHost: "127.0.0.1",
couchPort: 5984,
userName: "admin",
userPassword: "myPassword"
)
If you don’t want to have your password in the code you can pass COUCHDB_PASS param in you command line. For example you can run your Server Side Swift project:
// Example struct
struct ExpectedDoc: CouchDBRepresentable, Codable {
var name: String
var _id: String?
var _rev: String?
}
Insert data
var testDoc = ExpectedDoc(name: "My name")
try await couchDBClient.insert(
dbName: "databaseName",
doc: &testDoc
)
print(testDoc) // testDoc has _id and _rev values now
Update data
// get data from DB by document ID
var doc: ExpectedDoc = try await couchDBClient.get(dbName: "databaseName", uri: "documentId")
print(doc)
// Update value
doc.name = "Updated name"
try await couchDBClient.update(
dbName: testsDB,
doc: &doc
)
print(doc) // doc will have updated name and _rev values now
Delete data example:
let response = try await couchDBClient.delete(fromDb: "databaseName", doc: doc)
// or by uri
let response = try await couchDBClient.delete(fromDb: "databaseName", uri: doc._id,rev: doc._rev)
CouchDB Client
This is simple lib to work with CouchDB in Swift.
The only depndency for this lib is async-http-client
Documentaion
You can find docs, examples and even tutorials here.
Installation
Swift Package Manager
Add to the
dependencies
value of yourPackage.swift
.Initialization
If you don’t want to have your password in the code you can pass COUCHDB_PASS param in you command line. For example you can run your Server Side Swift project:
Just use initializer without userPassword param:
Usage examples
Define your document model:
Insert data
Update data
Delete data example:
Get all DBs example:
Using with Vapor
Here’s a simple tutorial for Vapor.