Yancy::Guides::Schema - How to connect to and describe your schema
version 1.072
use Mojolicious::Lite; plugin Yancy => { backend => 'pg://localhost/myapp', read_schema => 1, schema => { users => { title => 'Users', description => 'The authorized user accounts', }, }, };
This document describes how to configure a database connection (Backend) and how to add annotations to your schema.
The backend URL defines what database to use and how to connect to it. Each backend has its own format of URL, and some examples are shown below. See your backend's documentation for more information.
backend
# Backend URL backend => 'pg://user@example.com/mydb', # Backend hash backend => { Pg => { dsn => 'dbi:Pg:dbname', username => 'fry', password => 'b3nd3r1sgr34t', }, }
# Backend URL backend => 'mysql://user@localhost/mydb', # Backend hash backend => { Mysql => { dsn => 'dbi:mysql:mydb', username => 'fry', password => 'b3nd3r1sgr34t', }, }
# Backend URL backend => 'sqlite:filename.db', # Backend hash backend => { Sqlite => { dsn => 'sqlite:data.db', }, }
# Backend URL backend => 'dbic://My::Schema/dbi:SQLite:file.db', # Backend arrayref (passed to Schema->connect() method) backend => { Dbic => [ 'My::Schema', 'dbi:SQLite:mysql.db', undef, undef, { PrintError => 1 }, ], }
The schema data structure defines what data is in the database. Each key in this structure refers to the name of a schema, and the value describe the fields for items inside the schema.
schema
Each backend may define a schema differently. For a relational database like Postgres or MySQL, a schema is a table, and the fields are columns. For an ORM like DBIx::Class, the schemas are ResultSet objects. For a document store like MongoDB, the schemas are collections. See your backend's documentation for more information.
Schemas are configured using JSON Schema. The JSON Schema defines what fields (properties) an item has, and what type of data those field have. The JSON Schema also can define constraints like required fields or validate strings with regular expressions. The schema can also contain metadata like a title, description, and even an example value. For more information on what can be defined, see the docs on JSON Schema.
title
description
example
For a schema named people that has 3 fields (an integer id and two strings, name and email), a minimal JSON schema will look like this:
people
id
name
email
schema => { people => { properties => { id => { type => 'integer', readOnly => 1, }, name => { type => 'string', }, email => { type => 'string', }, }, }, },
By default, Yancy will read your database to fill in as much schema information as it can. This includes the field type, field order (x-order), enumerated values (enum), required fields (required), ID fields (x-id-field), foreign keys (x-foreign-key), and some format (date/time mostly). You can (and should) add your own annotations and corrections while configuring Yancy. The schema configuration will be merged with the information Yancy reads from the database, with the configuration overriding the defaults from the database.
type
x-order
enum
required
x-id-field
x-foreign-key
format
Yancy generates input elements based on the type, and format of the object's properties.
type => "boolean" - A Yes/No field. Boolean fields support input values 0, 1, "true", and "false". They will be stored as 0, and 1 in the database.
type => "boolean"
0
1
"true"
"false"
type => "integer" - A number field (<input type="number" >)
type => "integer"
<input type="number" >
type => "number" - A number field (<input type="number" >)
type => "number"
type => "string", format => "date" - A date field (<input type="date">)
type => "string", format => "date"
<input type="date">
type => "string", format => "date-time" - A date/time field (<input type="datetime-local">) Date/time fields can have a special default value: now. This will be replaced with the current date/time in the database.
type => "string", format => "date-time"
<input type="datetime-local">
default
now
type => "string", format => "email" - A e-mail address (<input type="email">)
type => "string", format => "email"
<input type="email">
type => "string", format => "url" - A URL input (<input type="url">)
type => "string", format => "url"
<input type="url">
type => "string", format => "tel" - A telephone number (<input type="tel">)
type => "string", format => "tel"
<input type="tel">
type => "string", format => "textarea" - A multiline text field (<textarea>)
type => "string", format => "textarea"
<textarea>
type => "string", format => "markdown" - A Markdown field that shows a live preview of the rendered HTML. The Markdown can be saved as HTML in another field by adding x-html-field => $field_name to that field.
type => "string", format => "markdown"
x-html-field => $field_name
enum => [...], type => "..." - A <select> element. This can be of any type.
enum => [...], type => "..."
<select>
type => "string", format => "filepath" - A file upload field (<input type="file">). See Yancy::Plugin::File for more information.
type => "string", format => "filepath"
<input type="file">
type => "string", format => "binary" - A field containing binary data. This currently does not generate any input field, but it may become another way to upload files in the future.
type => "string", format => "binary"
JSON schemas allow specifying multiple types for a field using an array. If a field has multiple types, the generated form will use the first type to decide what kind of field to display.
Other schema attributes will be translated as necessary to the HTML input fields:
title will be used to label the input field
description will be placed near the input field to explain it
readOnly will set the input field as read-only
readOnly
pattern for string fields, a string that can be used as a regex, like pattern => '^foo-\d+$'.
pattern
pattern => '^foo-\d+$'
minimum for numeric fields, the minimum value
minimum
maximum for numeric fields, the maximum value
maximum
minLength for string fields, the minimum length
minLength
maxLength for string fields, the maximum length
maxLength
JSON Schema allows marking properties as required using the required property, which must be an array of property names.
schema => { people => { required => [ 'name', 'email' ], properties => { id => { type => 'integer', readOnly => 1, }, name => { type => 'string', }, email => { type => 'string', }, }, }, },
Required values will be marked as such in the HTML.
If a value can be null (undef in Perl terms) in addition to its declared type (string, integer, etc...), you must add it to the type field by using an array of types:
null
undef
string
integer
schema => { people => { required => [ 'name' ], properties => { id => { type => 'integer', readOnly => 1, }, name => { type => 'string', # Required and must be a string }, email => { type => [ 'string', 'null' ], # Can be null }, }, }, },
If you don't do this, and still include the field in an object, you will get an error: Expected string - Got null.. The correct way to fix this error is to add null as an option for the field's type.
Expected string - Got null.
There are some extended fields you can add to your schema definition to control how it is treated by Yancy.
A friendly title for the schema
A description of the schema. Markdown will be parsed into HTML.
You can use the trim and unindent functions from Mojo::Util to allow indenting your schema description:
trim
unindent
use Mojolicious::Lite; use Mojo::Util qw( unindent trim ); plugin Yancy => { schema => { employees => { description => unindent( trim q{ The employees of Planet Express. * [View the employee health plan](/decapod-life) * [Latest Good News](/news) } ), }, }, };
If this is true, the schema will be hidden from the list in the Yancy web app. This does not prevent using the API to edit this data.
Ignore this schema: Do not add it to the API, do not show it in the rich editing form. This is for schema that should not be edited from the Yancy form or the Yancy API.
This allows for removing schema when using "read_schema".
This key sets the name of the schema's ID field to use to uniquely identify individual items. By default, Yancy assumes the ID field is named id. If your schema uses some other identifier (e-mail address or username for example), you should set this configuration key.
people => { 'x-id-field' => 'email', properties => { ... }, },
This field can be any unique identifier, but it will be the ID that Yancy uses for all of its operations.
This key should be an array of columns to display on the list view, in order. This helps put useful information on the list page.
people => { 'x-list-columns' => [ 'name', 'email' ], properties => { ... }, },
Instead of field names, columns can also be made out of templates using a hash with title and template keys. Inside the template key, use fields from the row with {field}, like so:
template
{field}
people => { 'x-list-columns' => [ { title => "Person", template => '{name} <{email}>' }, ], },
This key is an array of filter names to run when setting or creating an item. Filters can allow for hashing passwords, for example. Filters are added by plugins or during configuration of Mojolicious::Plugin::Yancy. See "yancy.filter.add" in Mojolicious::Plugin::Yancy for how to create a filter in your app.
Instead of a filter name, you can provide an array. The first member will be the name, and any further members will be passed to the filter code-ref as parameters after the mandatory three.
A URL to view the schema in the application. Will be shown as a button in the editor.
A URL to view the items in the schema. Will be shown as an icon next to the item row. Add data from the row in the url using {field}, like so:
# /people/1 /people/{id} # /user/preaction /user/{username}
There are some extended fields you can add to a field configuration to control how it is treated by Yancy.
A friendly title for the field
A description of the field. Markdown will be parsed into HTML.
The default value for the field, if the field is missing or undef (null in JavaScript).
If provided, this field is a foreign key linking to the given schema. This field must link to the ID field of the other schema.
schema => { user => { 'x-id-field' => 'username', properties => { username => { type => 'string', }, }, }, comment => { properties => { username => { type => 'string', 'x-foreign-key' => 'user', }, }, }, },
By default, the target schema's first list column (if x-list-columns is defined) or the schema's ID field is used to show the current value of the relationship. This can be changed by setting x-display-field to the field in the target schema you want to use.
x-list-columns
x-display-field
By default, the target schema's ID field (x-id-field or id) will be used as the value for the foreign key. This can be changed by setting x-value-field to the field in the target schema you want to use.
x-value-field
NOTE: This support is experimental and will need further development to support more possibilities of foreign key linkages. Patches appreciated!
If true, thie field will be hidden from the rich editing form. This is for schema that you want to use from the API but do not want to edit from the Yancy application.
Set the order of the fields in the edit form by assigning a number to the x-order property. Fields in the form are be sorted by their x-order, and then by their name (alphabetically). Fields that do not have x-order set will be sorted after fields that do.
This key is an array of filter names to run on the field when setting or creating an item. Filters can allow for hashing passwords, for example. Filters are added by plugins or during configuration of Mojolicious::Plugin::Yancy. See "yancy.filter.add" in Mojolicious::Plugin::Yancy for how to create a filter in your app.
# to get a data-light "view" of users when listing comments end of blogpost usermini => { type => 'object', 'x-view' => { schema => 'user' }, properties => { id => { 'x-order' => 1, type => 'integer' }, username => { 'x-order' => 2, type => 'string' }, }, },
This key means the schema is not a real one that exists in the backend, but a strict subset of a real one. It is an object with keys:
Mandatory. Names the "real" schema. NB This is the schema's text name, not a JSON pointer.
All the properties' types will need to be the same as on the "real" one since the datasource will be the real one. If no properties are given, the "real" schema's ones will be used.
The generated OpenAPI spec will only have read functionality for the "view" schema, not mutations.
Yancy, Mojolicious::Plugin::Yancy
Doug Bell <preaction@cpan.org>
This software is copyright (c) 2021 by Doug Bell.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Yancy, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Yancy
CPAN shell
perl -MCPAN -e shell install Yancy
For more information on module installation, please visit the detailed CPAN module installation guide.