Skip to content

Commit

Permalink
Avoiding storing the 'id' key as a normal document attribute.
Browse files Browse the repository at this point in the history
duplicates of the id were being stored (id and _id). Using select() to filter the schema
  • Loading branch information
Thiago Silva committed Sep 9, 2010
1 parent 9f6c2b6 commit f937586
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions couchdb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ local function delegate(parent)
return obj
end


-- util

function select(tb, fun)
local ret = {}
for k,v in pairs(tb) do
if (fun(v,k)) then
ret[k] = v
end
end
return ret
end

-- Local functions

local function _do_request(url, method, content)
Expand Down Expand Up @@ -89,8 +102,12 @@ end

function create_document(schema)
local obj = delegate(Document)
obj.schema = schema
obj.id = schema.id or obj.id
if schema.id then
obj.id = schema.id
obj.schema = select(obj, function(v,k) return k ~= 'id' end)
else
obj.schema = schema
end
return obj
end

Expand Down

0 comments on commit f937586

Please sign in to comment.