|
1 | 1 | # couchdb-vapor
|
2 | 2 |
|
3 |
| -A description of this package. |
| 3 | +This is simple lib to work with CouchDB with Vapor Framework. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +### Swift Package Manager |
| 8 | + |
| 9 | +Add to the `dependencies` value of your `Package.swift`. |
| 10 | + |
| 11 | +#### Swift 5 |
| 12 | + |
| 13 | +```swift |
| 14 | +dependencies: [ |
| 15 | + .package(url: "https://github.com/makoni/couchdb-vapor.git", from: "0.0.4"), |
| 16 | +] |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +Get data In Vapor routes: |
| 22 | + |
| 23 | +```swift |
| 24 | +let couchDBClient = CouchDBClient() |
| 25 | + |
| 26 | +// Sample document model |
| 27 | +struct ExpectedDoc: Codable { |
| 28 | + var name: String |
| 29 | + var _id: String |
| 30 | + var _rev: String |
| 31 | +} |
| 32 | + |
| 33 | +// Sample view data |
| 34 | +struct PageData: Content { |
| 35 | + let title: String |
| 36 | +} |
| 37 | + |
| 38 | +/// Register your application's routes here. |
| 39 | +public func routes(_ router: Router) throws { |
| 40 | + router.get(String.parameter) { req -> Future<View> in |
| 41 | + let docId = req.parameters.next(String.self) |
| 42 | + |
| 43 | + let response = try couchDBClient.get(dbName: "yourDBname", uri: docId, worker: req)?.wait() |
| 44 | + |
| 45 | + // response data is in response!.body.data! |
| 46 | + guard let data = response?.body.data else { |
| 47 | + throw Abort(.notFound) |
| 48 | + } |
| 49 | + |
| 50 | + // Decode data |
| 51 | + let decoder = JSONDecoder() |
| 52 | + let doc = try decoder.decode(ExpectedDoc.self, from: data) |
| 53 | + |
| 54 | + let pageData = PageData( |
| 55 | + title: doc.name |
| 56 | + ) |
| 57 | + |
| 58 | + return try req.view().render("view-name", pageData) |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Insert data example: |
| 64 | + |
| 65 | +```swift |
| 66 | +let testData = [name: "some name"] |
| 67 | + |
| 68 | +let encoder = JSONEncoder() |
| 69 | +let data = try encoder.encode(testData) |
| 70 | + |
| 71 | +let response = try couchDBClient.insert(dbName: "yourDBname", body: HTTPBody(data: data), worker: req)?.wait() |
| 72 | +print(response) |
| 73 | +// prints: CouchDBClient.CouchUpdateResponse(ok: true, id: "0a1eea865fdec7a00afb96685001c7be", rev: "1-e6bde9e60844ba5648cc61b446f9f4b3")) |
| 74 | +``` |
| 75 | + |
| 76 | +Update data example: |
| 77 | + |
| 78 | +```swift |
| 79 | +let updatedData = ExpectedDoc(name: "some new name", _id: "0a1eea865fdec7a00afb96685001c7be", _rev: "1-e6bde9e60844ba5648cc61b446f9f4b3") |
| 80 | + |
| 81 | +let encoder = JSONEncoder() |
| 82 | +let data = try encoder.encode(testData) |
| 83 | + |
| 84 | +let response = try couchDBClient.update(dbName: "yourDBname", uri: updatedData._id, body: HTTPBody(data: data), worker: req)?.wait() |
| 85 | +print(response) |
| 86 | +// prints: CouchDBClient.CouchUpdateResponse(ok: true, id: "0a1eea865fdec7a00afb96685001c7be", rev: "1-e6bde9e60844ba5648cc61b446f9f4b4")) |
| 87 | +``` |
| 88 | + |
| 89 | +Delete data example: |
| 90 | + |
| 91 | +```swift |
| 92 | +let updatedData = ExpectedDoc(name: "some new name", _id: "0a1eea865fdec7a00afb96685001c7be", _rev: "1-e6bde9e60844ba5648cc61b446f9f4b4") |
| 93 | + |
| 94 | +let encoder = JSONEncoder() |
| 95 | +let data = try encoder.encode(testData) |
| 96 | + |
| 97 | +let response = try couchDBClient.delete(fromDb: "yourDBname", uri: updatedData._id, rev: updatedData._rev, worker: req)?.wait() |
| 98 | +print(response) |
| 99 | +// prints: CouchDBClient.CouchUpdateResponse(ok: true, id: "0a1eea865fdec7a00afb96685001c7be", rev: "1-e6bde9e60844ba5648cc61b446f9f4b5")) |
| 100 | +``` |
| 101 | + |
| 102 | +Get all DBs example: |
| 103 | + |
| 104 | +```swift |
| 105 | + |
| 106 | +let response = try couchDBClient.getAllDBs(worker: req).wait() |
| 107 | +guard let dbs = response else { |
| 108 | + throw Abort(.notFound) |
| 109 | +} |
| 110 | + |
| 111 | +print(dbs) |
| 112 | +// prints: ["_global_changes", "_replicator", "_users", "yourDBname"] |
| 113 | +``` |
0 commit comments