-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wip] feat(dao) first db-agnostic insert
- Loading branch information
1 parent
44d9765
commit eba2d90
Showing
17 changed files
with
331 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
local inspect = require "inspect" | ||
|
||
local uuid = require "lua_uuid" | ||
local utils = require "kong.tools.utils" | ||
local Object = require "classic" | ||
local Errors = require "kong.dao.errors" | ||
|
||
--- DAO | ||
-- this just avoids having to deal with instanciating models | ||
|
||
local DAO = Object:extend() | ||
|
||
function DAO:new(db, model_mt) | ||
self.db = db | ||
self.model_mt = model_mt | ||
end | ||
|
||
function DAO:insert(tbl) | ||
local model = self.model_mt(tbl) | ||
local ok, err = model:validate() | ||
if not ok then | ||
return nil, err | ||
end | ||
|
||
for col, field in pairs(model.__schema.fields) do | ||
if field.dao_insert_value and model[col] == nil then | ||
local f = self.db.dao_insert_values[field.type] | ||
if f then | ||
model[col] = f() | ||
end | ||
end | ||
end | ||
|
||
local res, err = self.db:insert(model) | ||
if err ~= nil then | ||
return nil, Errors.db(err) | ||
end | ||
|
||
return res | ||
end | ||
|
||
return DAO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
local constants = require "kong.constants" | ||
local printable_mt = require "kong.tools.printable" | ||
local setmetatable = setmetatable | ||
local getmetatable = getmetatable | ||
local tostring = tostring | ||
local type = type | ||
|
||
local error_mt = {} | ||
error_mt.__index = error_mt | ||
|
||
function error_mt:__tostring() | ||
return tostring(self.message) | ||
end | ||
|
||
function error_mt.__concat(a, b) | ||
if getmetatable(a) == error_mt then | ||
return tostring(a)..b | ||
else | ||
return a..tostring(b) | ||
end | ||
end | ||
|
||
local function build_error(err_type) | ||
return function(err) | ||
local err_tbl -- in case arg1 is a table | ||
|
||
if err == nil then | ||
return nil | ||
elseif getmetatable(err) == error_mt then | ||
return err | ||
elseif type(err) == "table" then | ||
err_tbl = err | ||
setmetatable(err, printable_mt) | ||
err = tostring(err) -- convert to string | ||
end | ||
|
||
local err_obj = { | ||
message = err, | ||
err_tbl = err_tbl, | ||
[err_type] = true | ||
} | ||
|
||
return setmetatable(err_obj, error_mt) | ||
end | ||
end | ||
|
||
local Errors = {} | ||
|
||
for _, v in pairs(constants.DB_ERROR_TYPES) do | ||
Errors[v] = build_error(v) | ||
end | ||
|
||
return Errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
local Errors = require "kong.dao.errors" | ||
local schemas_validation = require "kong.dao.schemas_validation" | ||
local validate = schemas_validation.validate_entity | ||
|
||
return setmetatable({}, { | ||
__call = function(_, schema) | ||
local Model_mt = {} | ||
Model_mt.__meta = { | ||
__schema = schema, | ||
__name = schema.name, | ||
__table = schema.table | ||
} | ||
|
||
function Model_mt.__index(self, key) | ||
if string.find(key, "__") == 1 then | ||
local meta_field = Model_mt.__meta[key] | ||
if meta_field then | ||
return meta_field | ||
end | ||
end | ||
|
||
return Model_mt[key] | ||
end | ||
|
||
function Model_mt:validate() | ||
local ok, errors, self_check_err = validate(self, self.__schema) | ||
if errors ~= nil then | ||
return nil, Errors.schema(errors) | ||
elseif self_check_err ~= nil then | ||
-- TODO: merge errors and self_check_err now that our errors handle this | ||
return nil, Errors.schema(self_check_err) | ||
end | ||
return ok | ||
end | ||
|
||
return setmetatable({}, { | ||
__call = function(_, tbl) | ||
local m = {} | ||
for k,v in pairs(tbl) do | ||
m[k] = v | ||
end | ||
return setmetatable(m, Model_mt) | ||
end | ||
}) | ||
end | ||
}) |
Oops, something went wrong.