Skip to content

paragasu/lua-resty-couchdb

Repository files navigation

lua-resty-couchdb

Lua resty minimal couchdb client

Installation

#luarocks install lua-resty-couchdb

Usage

local couch   = require 'resty.couchdb'
local config  = {
  host = 'https://localhost:5984',
  user = 'couchdb-user',
  password = 'couchdb-pass'
}
local couch   = couch.new(config)
local user = couch:db('_users')

-- create db
local res, err = user:create()

-- add rows
local res, err = user:post(data)

-- view
local res, err = user:view('room', 'booked', {
  inclusive_end = tostring(true), -- boolean not supported, must be string
  start_key = '"hello"', -- double quote required by couchdb
  end_key = '"world'
})

-- all docs
local res, err = user:all_docs({
  inclusive_end = tostring(true), -- boolean not supported, must be string
  start_key = '"hello"', -- double quote required by couchdb
  end_key = '"world'
})

-- delete db
local res, err = user:destory()

API

Please refer to the CouchDB API documentation at docs.couchdb.org for available REST API.

configuration

This api should be called first to set the correct database parameter before calling any database action method.

  • database name eg: booking

get(id)

Get database value

  • id document id
  • return lua table

put(data)

Insert data to database

  • id document id
  • data (table) data to save

post(data)

Insert data to database

  • id document id
  • data (table) data to save

delete(id)

Delete data from database

  • id document id

save(data)

Update existing data. This api will automatically get the latest rev to use for updating the data.

  • id document id
  • data (table) to save

view(design_name, view_name, opts)

Query rows of data using views

  • design_name (string) couchdb design name
  • view_name (string) couchdb view name
  • opts (table) options parameter as documented here. Important note: start_key and end_key must always surrounded by double quote and boolean value not supported. For boolean value, it should be converted to string using lua tostring

all_docs(opts)

Query rows of data using bulk api

  • opts (table) options parameter as documented here. Important note: start_key and end_key must always surrounded by double quote and boolean value not supported. For boolean value, it should be converted to string using lua tostring

bulk_docs(opts)

Update data using bulk api

create()

Create new database name

destroy()

Delete database

Reference