Lua resty minimal couchdb client
#luarocks install lua-resty-couchdb
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()
Please refer to the CouchDB API documentation at docs.couchdb.org for available REST API.
This api should be called first to set the correct database parameter before calling any database action method.
- database name eg: booking
Get database value
- id document id
- return lua table
Insert data to database
- id document id
- data (table) data to save
Insert data to database
- id document id
- data (table) data to save
Delete data from database
- id document id
Update existing data. This api will automatically get the latest rev to use for updating the data.
- id document id
- data (table) to save
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
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
Update data using bulk api
- opts (table) options parameter as documented here.
Create new database name
Delete database