chadbean / Kitura-CouchDB

CouchDB extension for Kitura

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kitura-CouchDB

Build Status Build Status

CouchDB library for Kitura

This library allows Kitura applications to interact with a CouchDB database.

Depends on Kitura-router.

Build CouchDBSample:

  1. Download CouchDB and install.

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

  3. Create a database with the name kitura_test_db.

  4. 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
    )
  5. Open a Terminal window to the Kitura-CouchDB folder and run make:

    make
  6. Run the CouchDBSample executable:

    .build/debug/CouchDBSample

Usage:

(Todo)

Get the "_users" database from the client

let userDatabase = databaseClient.usersDatabase()

Add user to the "_users" database:

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()
})

Get a session cookie for the user:

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()
})

About

CouchDB extension for Kitura

License:Apache License 2.0


Languages

Language:Swift 98.8%Language:Makefile 1.2%