The daos.lua
file is used to define schemas for custom entities.
A schema is a Lua table which describes entities. There’s structural information such as the names and types of different fields, which is similar to the fields describing your plugin configuration. However, custom entity schemas require additional metadata. For example, you need to specify which field, or fields, constitute the entity’s primary key.
The daos.lua
file should return a table containing one or more schemas. For example, the following configuration defines a keyauth_credentials
:
local typedefs = require "kong.db.schema.typedefs"
return {
{
name = "keyauth_credentials", -- the name of table in the database
endpoint_key = "key",
primary_key = { "id" },
cache_key = { "key" },
generate_admin_api = true,
admin_api_name = "key-auths",
admin_api_nested_name = "key-auth",
fields = {
{
-- a value to be generated by the DAO itself
id = typedefs.uuid,
},
{
-- also inserted by the DAO itself
created_at = typedefs.auto_timestamp_s,
},
{
-- a foreign key to a Consumer's id
consumer = {
type = "foreign",
reference = "consumers",
default = ngx.null,
on_delete = "cascade",
},
},
{
-- a unique API key
key = {
type = "string",
required = false,
unique = true,
auto = true,
},
},
},
},
}
Here is a description of some top-level properties:
Name |
Type |
Required |
Description |
---|---|---|---|
name
|
string
|
The name of database table, which is also used in the DAO name: kong.db.{name} .
|
|
primary_key
|
table
|
Field names forming the entity’s primary key.
Schemas support composite keys, even if most Kong Gateway core entities currently use an UUID named id .
|
|
endpoint_key
|
string
|
The name of the field used as an alternative identifier on the Admin API.
In the example above, key is the endpoint_key.
This means that a credential with id = 123 and key = "foo" could be referenced as both /keyauth_credentials/123 and /keyauth_credentials/foo .
|
|
cache_key
|
table
|
Contains the name of the fields used for generating the cache_key , a string which must unequivocally identify the entity inside Kong Gateway’s cache.
A unique field, like key in the example above, is usually good candidate.
In other cases a combination of several fields is preferable.
|
|
generate_admin_api
|
boolean
|
Whether to auto-generate an Admin API for the entity or not. By default the Admin API is generated for all DAOs, including custom ones.
If you want to create a fully customized Admin API for the DAO or want to disable auto-generation for the DAO altogether, set this option to false .
|
|
admin_api_name
|
string
|
When generate_admin_api is enabled, the Admin API auto-generator uses the name to derive the URLs for the auto-generated Admin API.
Sometimes you may want to name the URLs differently from the name .
In the example above, the keyauth_credentials endpoints will use the name key-auths .
|
|
admin_api_nested_name
|
string
|
Similar to admin_api_name , the admin_api_nested_name parameter specifies the name for a DAO that the Admin API auto-generator creates in nested contexts.
You only need to use this parameter if you don’t want to use name or admin_api_name .
For example, Kong Gateway for legacy reasons has urls such as consumers/john/key-auth where key-auth does not follow plural form of /key-auths . admin_api_nested_name enables you to specify a different name in those cases.
|
|
fields
|
table
|
Each field definition is a table with a single key, which is the field’s name. The table value is a sub-table containing the field’s attributes, some of which will be described in the table below. |
Many field attributes encode validation rules. When attempting to insert or update entities using the DAO, these validations will be checked, and an error returned if the provided input doesn’t conform to them.
The typedefs
variable (obtained by requiring kong.db.schema.typedefs
) is a table containing a lot of useful type definitions and aliases, including typedefs.uuid
, the most usual type for the primary key, and typedefs.auto_timestamp_s
, for created_at
fields.
It is used extensively when defining fields.
Here’s a non-exhaustive list of available field attributes:
Name |
Type |
Description |
---|---|---|
type
|
string
|
The attribute type. The following values are supported:
In addition to these values, the Each field needs to be backed by a database field of a similar type, created via migrations.
|
default
|
any (matching with the value to type )
|
Specifies the value the field will have when attempting to insert it, if no value was provided. Default values are always set via Lua, never by the underlying database. Thus it’s not recommended to set any default values on fields in migrations. |
required
|
boolean
|
When set to true on a field, an error will be thrown when attempting to insert an entity without a value for this field, unless the field in question has a default value.
|
unique
|
boolean
|
When set to true on a field, an error will be thrown when attempting to insert an entity in the database if another entity already has the same value for this field.
This attribute must be backed up by declaring the field as |
auto
|
boolean
|
When set to true on a field, a value will be autogenerated when attempting to insert an entity without a value for this field. The value will be generated as follows:
|
reference
|
string
|
Reference to another schema.
This is required for fields of type foreign .
The given string must be the name of an existing schema, to which the foreign key will point to.
This means that if a schema B has a foreign key pointing to schema A, then A needs to be loaded before B.
|
on_delete
|
string
|
Optional and exclusive for fields of type foreign .
It dictates what must happen with entities linked by a foreign key when the entity being referenced is deleted.
It can have three possible values:
|
To learn more about schemas, see:
- The source code of typedefs.lua to get an idea of what’s provided there by default.
- The core schemas to see examples of some other field attributes not documented here.
- The
daos.lua
files for existing plugins, such as the key-auth one.