Swiftpack.co - makoni/couchdb-vapor as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by makoni.
makoni/couchdb-vapor 1.1.0
CouchDB client for Vapor
⭐️ 9
🕓 41 weeks ago
iOS macOS
.package(url: "https://github.com/makoni/couchdb-vapor.git", from: "1.1.0")

CouchDB Client for Vapor

Platforms Swift Package Manager Swift 5 Vapor 3

This is simple lib to work with CouchDB in Swift.

  • Latest version is based on async/await and requires Swift 5.5 and 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 old version for Vapor 3 from vapor3 branch or using version < 1.0.0.

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

Installation

Swift Package Manager

Add to the dependencies value of your Package.swift.

Swift 5

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

Usage

Get data In Vapor 4 routes:

// using default settings
let couchDBClient = CouchDBClient()
// providing settings
let couchDBClient2 = CouchDBClient(couchProtocol: .http, couchHost: "127.0.0.1", couchPort: 5984, userName: "username", userPassword: "userpass")

// Sample document model
struct ExpectedDoc: Codable {
    var name: String
    var _id: String
    var _rev: String
}

// Sample view data
struct PageData: Content {
    let title: String
}

func routes(_ app: Application) throws {
    app.get(":docId") { req async throws -> View in
        let docId = req.parameters.get("docId")!

        let couchResponse = try await couchDBClient.get(dbName: "yourDBname", uri: docId, worker: req.eventLoop)

        guard let body = response.body, let bytes = body.readBytes(length: body.readableBytes) else { throw Abort(.notFound) }

        let data = Data(bytes)		
        let doc = try JSONDecoder().decode(ExpectedDoc.self, from: data)

        let pageData = PageData(
            title: doc.name
        )
	
        return try await req.view().render("view-name", pageData)
    }
}

Insert data example:

let testData = [name: "some name"]

let data = try JSONEncoder().encode(testData)

let response = try await couchDBClient.insert(dbName: "yourDBname", body: HTTPBody(data: data), worker: req.eventLoop)
print(response)
// prints: CouchDBClient.CouchUpdateResponse(ok: true, id: "0a1eea865fdec7a00afb96685001c7be", rev: "1-e6bde9e60844ba5648cc61b446f9f4b3"))

Update data example:

let updatedData = ExpectedDoc(name: "some new name", _id: "0a1eea865fdec7a00afb96685001c7be", _rev: "1-e6bde9e60844ba5648cc61b446f9f4b3")

let data = try JSONEncoder().encode(testData)

let response = try await couchDBClient.update(dbName: "yourDBname", uri: updatedData._id, body: HTTPBody(data: data), worker: req.eventLoop)
print(response)
// prints: CouchDBClient.CouchUpdateResponse(ok: true, id: "0a1eea865fdec7a00afb96685001c7be", rev: "1-e6bde9e60844ba5648cc61b446f9f4b4"))

Delete data example:

let updatedData = ExpectedDoc(name: "some new name", _id: "0a1eea865fdec7a00afb96685001c7be", _rev: "1-e6bde9e60844ba5648cc61b446f9f4b4")

let data = try JSONEncoder().encode(testData)

let response = try await couchDBClient.delete(fromDb: "yourDBname", uri: updatedData._id, rev: updatedData._rev, worker: req.eventLoop)
print(response)
// prints: CouchDBClient.CouchUpdateResponse(ok: true, id: "0a1eea865fdec7a00afb96685001c7be", rev: "1-e6bde9e60844ba5648cc61b446f9f4b5"))

Get all DBs example:


let dbs = try await couchDBClient.getAllDBs(worker: req.eventLoop)

print(dbs)
// prints: ["_global_changes", "_replicator", "_users", "yourDBname"]

GitHub

link
Stars: 9
Last commit: 26 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

1.1.0
41 weeks ago

CouchDBClient is now using async/await and works with Vapor 4.50 and newer.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics