in kong/kong/db/init.lua [32:120]
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
local errors = Errors.new(strategy)
local schemas = {}
do
for _, entity_name in ipairs(constants.CORE_ENTITIES) do
local entity_schema = require("kong.db.schema.entities." .. entity_name)
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
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
local connector, strategies, err = Strategies.new(kong_config, strategy,
schemas, errors)
if err then
return nil, err
end
local daos = {}
local self = {
daos = daos,
strategies = strategies,
connector = connector,
strategy = strategy,
errors = errors,
infos = connector:infos(),
kong_config = kong_config,
}
do
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
daos[schema.name] = DAO.new(self, schema, strategy, errors)
end
end
return setmetatable(self, DB)
end