couchdb-vapor

1.5.0

CouchDB client in Swift
makoni/couchdb-vapor

What's New

1.5.0

2024-04-08T14:56:50Z
  • Bumped async-http-client minimum version to the new 1.21.0. If you can't use it in your project you can stay on 1.4.0.
  • The library will use HTTPClient.shared (new in async-http-client 1.21.0) internally for requests if no EventLoopGroup is provided.
  • No more internal calls for httpClient.syncShutdown() in case of HTTPClient.shared usage.

Full Changelog: 1.4.0...1.5.0

CouchDB Client

Vapor 4

Build on macOS Build on Ubuntu Test on Ubuntu

This is a simple lib to work with CouchDB in Swift.

  • Latest version is based on async/await and requires Swift 5.8 or newer. Works with Vapor 4.50 and newer.
  • Version 1.0.0 can be used with Vapor 4 without async/await. Swift 5.3 is required
  • You can use the old version for Vapor 3 from vapor3 branch or using version < 1.0.0.

The only dependency for this lib is async-http-client

Documentation

You can find docs, examples and even tutorials here.

Installation

Swift Package Manager

Add to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/makoni/couchdb-vapor.git", from: "1.2.0"),
]

Initialization

// 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 your command line. For example you can run your Server Side Swift project:

COUCHDB_PASS=myPassword /path/.build/x86_64-unknown-linux-gnu/release/Run

Just use initializer without userPassword param:

let couchDBClient = CouchDBClient(
    couchProtocol: .http,
    couchHost: "127.0.0.1",
    couchPort: 5984,
    userName: "admin"
)

Usage examples

Define your document model:

// Example struct
struct ExpectedDoc: CouchDBRepresentable {
    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 a database 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:

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)

Get all databases example:

let dbs = try await couchDBClient.getAllDBs()
print(dbs)
// prints: ["_global_changes", "_replicator", "_users", "yourDBname"]

Find documents in a database by selector:

let selector = ["selector": ["name": "Sam"]]
let docs: [ExpectedDoc] = try await couchDBClient.find(in: "databaseName", selector: selector)
print(docs)

Using with Vapor

Here's a simple tutorial for Vapor.

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

Last updated: Mon Apr 22 2024 04:35:16 GMT-0900 (Hawaii-Aleutian Daylight Time)