Yesod web framework/Model
Model
[edit | edit source]Using in-memory mutable data (in the foundation datatype)
[edit | edit source]E.g. a visitor count. See ref.[1]
The Database layer
[edit | edit source]- persistent is the name of the database access layer with templates for generating types for entities and keys as well as schema initialization.[2][3][4]
There is first class support for PostgreSQL, SQLite, MongoDB, CouchDB and MySQL, with experimental support for Redis.[2]
The Database layout is described in a template listing the entities, fields and constraints.[5]
- For every entity listed, an integer key column "id" is generated with autoincrement and primary index attributes, with a type alias appending Id to the entity name
- For every entity listed, a record type named as the entity is generated were record fields names are composed prefixing the entity name to the field name like "personName". An EntityField type "PersonName" is also generated for foreign key referencing from other entities.
- There is an automatic database schema migration mechanism for DB schema updates, which, to succeed, requires, when adding columns to existent tables, to specify Default-column-value constraints with sql level notation.[6]
- "At most one" cardinality has a special mechanism around the type Checkmark.[7]
- Weak entities (childs in life constrained owner-child relationships) have no special support for cascade delete triggers, but there are functions to deleteCascade manually in the Database.Persist.Class module.[8]
- automatic table creation, schema update and table migration
- Modifications of the entities template produces an schema update with automatic table creation, and migration for the DBMS's that support "ALTER TABLE" SQL commands in a migrateAll procedure, generated from the template content. See "Migrations" in ref.[2] to look for migration aware DBMS.
share [mkPersist sqlSettings,
mkMigrate "migrateAll" -- generates the migration procedure with the specified name
] [persist|
User -- table name and entity record type
-- implicit autoincrement column "id" as primary key, typed UserId
ident Text -- refers to db. table column "ident";
-- generates a record field prefixing the table name as "userIdent"
password Text Maybe -- Maybe indicates Nullable field
UniqueUser ident -- unique constraint with space sep. field sequence
Email -- table name and entity record type
-- implicit autoincrement column "id" as primary key, typed EmailId
email Text
user UserId -- foreign key by specifying other tables EntityField types
verkey Text Maybe
newlyAddedColumn Text "default='sometext'::character varying" -- sql level Default constraint
UniqueEmail email -- unique constraint
|]
- Esqueleto: is a haskell combinators layer to generate correct relational queries to persistent.[9]
Example for persistent rawSQL and Esqueleto queries.[10]
The following packages are part of the yesod-platform:[11]
- email-validate: Validating an email address.[12]
- mime-mail: Compose and send MIME email messages.[13]
- Useful glue functions between the fb library and Yesod.[14]
Development cycle
[edit | edit source]New Yesod apps are generated from the HaskellStack tool[15] templates, replacing previous command "yesod init"
Stack based app. template names are prefixed by yesod as "yesod-{minimal | postgres | sqlite | mysql | mongo | ...}"
- Since HaskellStack uses the stackage repo by default, extra packages from the hackage repo should be referred in the "stack.yaml" extra-deps section.
- You may customize packages to a local subfolder. They must be referred in the "stack.yaml" packages section.
The "Yesod helper" tool
[edit | edit source]- The yesod helper tool [16]
yesod devel
run from the project site, recompiles and restarts the project at every file tree modification.yesod add-handler
adds a new handler and module to the project, adding an import clause for the handler in the "Application" module.
Deploying with Keter: A web app server monitor and reverse proxy server
[edit | edit source]Keter is a process as a service that handles deployment and restart of Yesod web app servers, and, per web app, database creation for PostgreSQL.
The console command yesod keter
packs the web app. as a keter bundle for uploading to a keter folder named "incoming".
Keter monitors the "incoming" folder and unpacks the app. to a temporary one, then assigns the web app a port to listen to, and starts it.
Initially it worked with Nginx as reverse proxy (keter version 0.1*), adding virtual server entries to its configuration and making Nginx reload it, but now Keter itself provides its own reverse proxy functionality, removing Nginx dependency and acting as the main web server.[20]
Old documentation (Nginx based).[21][22]
Integration with JavaScript generated from functional languages
[edit | edit source]References
[edit | edit source]- ↑ "Book - Initializing data in the foundation datatype". Yesodweb.com. Retrieved 2014-05-26.
- ↑ a b c "book - Persistent". Yesodweb.com. Retrieved 2012-10-23.
- ↑ "Yesod-persistent package". Hackage.haskell.org. Retrieved 2012-10-23.
- ↑ "Yesod-persistent docs". github.com. Retrieved 2018-10-16.
- ↑ "Yesod-persistent entity syntax". github.com. Retrieved 2018-10-16.
- ↑ "Redundant migrations for fields' default values". GitHub.com. Retrieved 2012-12-04.
- ↑ ""At most one" cardinality enforcement in persistent with type Checkmark". Hackage.haskell.org. Retrieved 2018-10-16.
- ↑ "How can I create a foreign key constraint using Yesod/Persistent?". stackoverflow.com. Retrieved 2018-10-16.
- ↑ "esqueleto package". Hackage.haskell.org. Retrieved 2012-10-23.
- ↑ "Query example at". Stackoverflow.com. 2012-09-19. Retrieved 2012-10-23.
- ↑ "The yesod package". Hackage.haskell.org. Retrieved 2019-06-26.
- ↑ "The email-validate package". Hackage.haskell.org. Retrieved 2012-10-26.
- ↑ "The mime-mail package". Hackage.haskell.org. Retrieved 2012-10-26.
- ↑ "The yesod-fb package". Hackage.haskell.org. Retrieved 2012-10-26.
- ↑ Haskell Stack - How to install
- ↑ The yesod-bin pkg with the helper tool (with instructions for use with the stack tool)
- ↑ "book - Deploying your Webapp". Yesodweb.com. Retrieved 2012-10-23.
- ↑ Readme.Md. "Yesod keter readme". GitHub. Retrieved 2012-10-23.
- ↑ "The keter package". Hackage.haskell.org. Retrieved 2012-10-23.
- ↑ "Keter updates". Yesodweb.com. 2012-10-25. Retrieved 2012-10-25.
- ↑ "Keter: Web App Deployment". Yesodweb.com. 2012-05-11. Retrieved 2012-10-23.
- ↑ "Keter: It's Alive!". Yesodweb.com. 2012-05-17. Retrieved 2012-10-23.
- ↑ "Javascript Options". github.com. Retrieved 2014-03-12.
- ↑ "Yesod, AngularJS and Fay". yesodweb.com. 2012-10-30. Retrieved 2014-03-12.
- ↑ "HaskellWiki - The JavaScript Problem". haskell.org. Retrieved 2014-04-12.