Skip to content

Commit

Permalink
BREAKING CHANGE: Replace SwiftyJSON with Codable (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Lees11 authored Jan 28, 2019
1 parent 5b66919 commit fddf294
Show file tree
Hide file tree
Showing 24 changed files with 1,289 additions and 1,335 deletions.
5 changes: 2 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/IBM-Swift/HeliumLogger.git", from: "1.7.0"),
.package(url: "https://github.com/IBM-Swift/LoggerAPI.git", from: "1.7.0"),
kituraNetPackage,
.package(url: "https://github.com/IBM-Swift/SwiftyJSON.git", from: "17.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target defines a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "CouchDB",
dependencies: ["HeliumLogger", "KituraNet", "SwiftyJSON"]),
dependencies: ["LoggerAPI", "KituraNet"]),
.target(
name: "CouchDBSample",
dependencies: ["CouchDB"]),
Expand Down
121 changes: 111 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

# Kitura-CouchDB

Kitura-CouchDB is a pure Swift client which allows Kitura applications to interact with a CouchDB database.
Kitura-CouchDB is a pure Swift client which allows Kitura applications to interact with a CouchDB or Cloudant database.

## Usage

Expand All @@ -46,11 +46,11 @@ Add `CouchDB` to your target's dependencies:
import CouchDB
```

## Use Kitura-CouchDB locally
## Run Kitura-CouchDB Sample

The CouchDBSample executable demonstrates how to create, read, update and delete documents within a CouchDB database in Swift.
To run the CouchDB Sample, you must set up and connect to a local CouchDB database by following the steps below:

1. [Download CouchDB](http://couchdb.apache.org/#download) and install.
1. [Download and install CouchDB.](http://couchdb.apache.org/#download)

2. Set up an admin username and password in CouchDB.

Expand All @@ -66,8 +66,8 @@ The CouchDBSample executable demonstrates how to create, read, update and delete

```swift
let connProperties = ConnectionProperties(
host: host, // httpd address
port: port, // httpd port
host: host, // http address
port: port, // http port
secured: secured, // https or http
username: nil, // admin username
password: nil // admin password
Expand All @@ -86,13 +86,114 @@ The CouchDBSample executable demonstrates how to create, read, update and delete
.build/debug/CouchDBSample
```

You will see informational messages such as "Successfully created the following JSON document in CouchDB:" for each of the operations (create, read, update and delete) performed on the `kitura_test_db` database.
You should see informational messages such as "Successfully created the following JSON document in CouchDB:" for each of the operations (create, read, update and delete) performed on the `kitura_test_db` database.

## Example
## API Documentation

For a more comprehensive example, you can follow the Kitura tutorial [Getting Started with Server-side Swift on raywenderlich.com](https://www.raywenderlich.com/180721/kitura-tutorial-getting-started-with-server-side-swift) that shows you how to create a backend API and then link this to a CouchDB instance running on your local machine.
#### Document

CouchDB is a NoSQL database for storing documents. A `Document` is any structure that can be represented as JSON and contains `_id` and `_rev` fields.
- The `_id` field is the unique identifier for the document. If it is not set, a random UUID will be assigned for the document.
- The `_rev` field is the revision of the document. It is returned when you make requests and is used to prevent conflicts from multiple users updating the same document.

To define a CouchDB document, create a Swift object and make it conform to the `Document` protocol:
```swift
struct MyDocument: Document {
let _id: String?
var _rev: String?
var value: String
}
```

#### CouchDBClient

The `CouchDBClient` represents a connection to a CouchDB server. It is initialized with your `ConnectionProperties` and handles the creation, retrieval and deletion of CouchDB databases.

```swift
// Define ConnectionProperties
let conProperties = ConnectionProperties(
host: "127.0.0.1", // http address
port: 5984, // http port
secured: false, // https or http
username: "<CouchDB-username>", // admin username
password: "<CouchDB-password>" // admin password
)
// Initialize CouchDBClient
let couchDBClient = CouchDBClient(connectionProperties: conProperties)
```
- Create a new database
```swift
couchDBClient.createDB("NewDB") { (database, error) in
if let database = database {
// Use database
}
}
```
- Get an existing database
```swift
couchDBClient.retrieveDB("ExistingDB") { (database, error) in
if let database = database {
// Use database
}
}
```
- Delete a database
```swift
couchDBClient.deleteDB("ExistingDB") { (error) in
if let error = error {
// Handle the error
}
}
```
#### 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 `Document` attachment
The following code demonstrates the CRUD operations for a single `Document`:
```swift
var myDocument = MyDocument(_id: "Kitura", _rev: nil, value: "Hello World")
```
- Create Document
```swift
database.create(myDocument) { (response, error) in
if let response = response {
print("Document: \(response.id), created with rev: \(response.rev)")
}
}
```
- Retrieve Document
```swift
database.retrieve("Kitura") { (document: MyDocument?, error: CouchDBError?) in
if let document = document {
print("Retrieved document with value: \(document.value)")
}
}
```
- Update Document
```swift
myDocument.value = "New Value"
database.update("Kitura", rev: "<latest_rev>", document: myDocument) { (response, error) in
if let response = response {
print("Document: \(response.id), updated")
}
}
```
- Delete a Document
```swift
database.delete("Kitura", rev: "<latest_rev>") { (error) in
if error == nil {
print("Document successfully deleted")
}
}
```
## API Documentation
For more information visit our [API reference](https://ibm-swift.github.io/Kitura-CouchDB/index.html).
## Community
Expand Down
10 changes: 2 additions & 8 deletions Sources/CouchDB/ConnectionProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ public struct ConnectionProperties {
public let host: String

/// Port number where CouchDB server is listening for incoming connections
public let port: Int16
public let port: UInt16

/// Whether or not to use a secured connection
public let secured: Bool

// MARK: Authentication credentials to access CouchDB

/// CouchDB admin username
let username: String?

Expand All @@ -47,7 +45,7 @@ public struct ConnectionProperties {
/// - secured: Whether or not to use a secured connection.
/// - username: CouchDB admin username. Defaults to `nil`.
/// - password: CouchDB admin password. Defaults to `nil`.
public init(host: String, port: Int16, secured: Bool, username: String?=nil, password: String?=nil) {
public init(host: String, port: UInt16, secured: Bool, username: String?=nil, password: String?=nil) {
self.host = host
self.port = port
self.secured = secured
Expand All @@ -58,8 +56,6 @@ public struct ConnectionProperties {
}
}

// MARK: Computed properties

/// Use https or http.
var HTTPProtocol: String {
return secured ? "https" : "http"
Expand All @@ -75,8 +71,6 @@ public struct ConnectionProperties {
}
}

// MARK: Extension for <CustomStringConvertible>

extension ConnectionProperties: CustomStringConvertible {
/// String description for a `ConnectionProperties`.
public var description: String {
Expand Down
Loading

0 comments on commit fddf294

Please sign in to comment.