KONG DB抽象

function DB.new(kong_config, strategy)
if not kong_config then
error("missing kong_config", 2)
end

if strategy ~= nil and type(strategy) ~= "string" then
error("strategy must be a string", 2)
end

strategy = strategy or kong_config.database

-- load errors

local errors = Errors.new(strategy)

local schemas = {}

do
-- load schemas
-- core entities are for now the only source of schemas.
-- TODO: support schemas from plugins entities as well.

for _, entity_name in ipairs(constants.CORE_ENTITIES) do
local entity_schema = require("kong.db.schema.entities." .. entity_name)

-- validate core entities schema via metaschema
local ok, err_t = MetaSchema:validate(entity_schema)
if not ok then
return nil, fmt("schema of entity '%s' is invalid: %s", entity_name,
tostring(errors:schema_violation(err_t)))
end
local entity, err = Entity.new(entity_schema)
if not entity then
return nil, fmt("schema of entity '%s' is invalid: %s", entity_name,
err)
end
schemas[entity_name] = entity
-- load core entities subschemas
local subschemas
ok, subschemas = utils.load_module_if_exists("kong.db.schema.entities." .. entity_name .. "_subschemas")
if ok then
for name, subschema in pairs(subschemas) do
local ok, err = entity:new_subschema(name, subschema)
if not ok then
return nil, ("error initializing schema for %s: %s"):format(entity_name, err)
end
end
end
end
end

-- load strategy
//按照配置,构造返回特定类型数据库的连接器,针对每个实体对象的底层访问对像的字典 strategies
local connector, strategies, err = Strategies.new(kong_config, strategy,
schemas, errors)
if err then
return nil, err
end

local daos = {}

local self = {
daos = daos, -- each of those has the connector singleton
strategies = strategies,针对每个实体对象的底层访问对像的字典strategies
connector = connector,
strategy = strategy,
errors = errors,
infos = connector:infos(),
kong_config = kong_config,
}

do
-- load DAOs
for _, schema in pairs(schemas) do
local strategy = strategies[schema.name]
if not strategy then
return nil, fmt("no strategy found for schema '%s'", schema.name)
end
      //DB中定义了daos的数据访问的抽象层字典,每个DAO封装了针对特定实体的底层数据访问对象strategy
daos[schema.name] = DAO.new(self, schema, strategy, errors)
     
end
end

-- we are 200 OK


return setmetatable(self, DB)
end

猜你喜欢

转载自www.cnblogs.com/justart/p/12443197.html
今日推荐