CouchDB library for Kitura
This library allows Kitura applications to interact with a CouchDB database.
Depends on Kitura-router.
-
Download CouchDB and install.
-
Set up an admin username and password in CouchDB.
-
Create a database with the name
kitura_test_db
. -
Update the following code in
main.swift
with your admin username and password:let connProperties = ConnectionProperties( host: "127.0.0.1", // httpd address port: 5984, // httpd port secured: false, // https or http username: nil, // admin username password: nil // admin password )
-
Open a Terminal window to the
Kitura-CouchDB
folder and runmake
:make
-
Run the CouchDBSample executable:
.build/debug/CouchDBSample
(Todo)
let userDatabase = databaseClient.usersDatabase()
var user = JSONDictionary()
user["type"] = "user"
user["roles"] = []
user["name"] = name
user["password"] = password
user["email"] = email
let document = JSON(user)
userDatabase.signupUser(document) { (id, doc, error) in
if let document = doc, let id = id where error == nil {
response.status(HttpStatusCode.OK).sendJson(document)
}
else {
if let error = error {
response.status(error.code).sendJson(JSON(error.userInfo))
}
else {
response.status(HttpStatusCode.BAD_REQUEST).send("Signup failed")
}
}
next()
})
userDatabase.getSessionCookie(name, password: password, callback: { (cookie, document, error) in
if let error = error {
response.status(error.code).sendJson(JSON(error.userInfo))
}
else {
var document = JSONDictionary()
document["ok"] = true
document["cookie"] = cookie
let json = JSON(document)
response.status(HttpStatusCode.OK).sendJson(json)
}
next()
})