Skip to content

Commit

Permalink
Starting to implement the Database type
Browse files Browse the repository at this point in the history
Instead of letting Session cares with database specific stuff, a new
object, called Database was introduced. The next step is to create a
data type called Document, that will be added to the database.
  • Loading branch information
clarete committed Sep 8, 2010
1 parent 89b8926 commit 9c8a899
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions couchdb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ local socket = require("socket")
local http = require("socket.http")
local ltn12 = require("ltn12")

Database = {session=nil, name=nil}

Session = {uri="http:https://localhost:5984"}

function Session:new(uri)
Expand All @@ -31,6 +33,15 @@ function Session:new(uri)
return o
end

function Database:new(session, name)
local o = {}
setmetatable(o, self)
self.__index = self
o.session = session
o.name = name
return o
end

local function _do_request(url, method)
local t = {}
local _, code, headers, human_readable_error = http.request{
Expand All @@ -51,15 +62,19 @@ local function _do_request(url, method)
end

function Session:all_dbs()
return _do_request(self.uri .. "/_all_dbs", "GET")
local result = {}
for _, v in pairs(_do_request(self.uri .. "/_all_dbs", "GET")) do
table.insert(result, Database:new(self, v))
end
return result
end

function Session:create_database(name)
return _do_request(self.uri .. "/" .. name, "PUT")
function Database:create()
_do_request(self.session.uri .. "/" .. self.name, "PUT")
end

function Session:delete_database(name)
return _do_request(self.uri .. "/" .. name, "DELETE")
function Database:delete()
return _do_request(self.session.uri .. "/" .. self.name, "DELETE")
end

return _M

0 comments on commit 9c8a899

Please sign in to comment.