Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pmb0/mojo-pg-orm
A simple blocking and non-blocking object relational mapper for Mojolicious and PostgreSQL.
https://github.com/pmb0/mojo-pg-orm
Last synced: about 1 month ago
JSON representation
A simple blocking and non-blocking object relational mapper for Mojolicious and PostgreSQL.
- Host: GitHub
- URL: https://github.com/pmb0/mojo-pg-orm
- Owner: pmb0
- Created: 2015-05-02T14:59:07.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-02-25T08:46:25.000Z (almost 9 years ago)
- Last Synced: 2023-03-22T20:16:28.774Z (almost 2 years ago)
- Language: Perl
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Mojo::Pg::ORM [![Build Status](https://travis-ci.org/pmb0/mojo-pg-orm.svg?branch=master)](https://travis-ci.org/pmb0/mojo-pg-orm)
A simple blocking and non-blocking object relational mapper for Mojolicious
and PostgreSQL.*THIS IS EXPERIMENTAL SOFTWARE. USE AT YOUR OWN RISK.*
1. Create a `Mojolicious` application
```perl
# Mojolicious::Lite example app
package main;
use Mojolicious::Lite;
use experimental 'signatures';use Mojo::Pg::ORM;
use My::Model;helper orm => sub { state $orm = My::Model->new($connection) };
# non-blocking
get '/postings/:id' => sub($c) {
$c->orm->model('Posting')->find($c->param('id'), sub($err, $posting) {
$c->render(text => $posting->{title});
});
};# non-blocking
get '/postings' => sub($c) {
$c->orm->model('Posting')->search(undef, sub($err, $posting) {
$c->stash(postings => $postings);
$c->render;
});
};# blocking
get '/postings' => sub($c) {
$c->stash(postings => $c->orm->model('Posting')->all);
$c->render;
};
```2. Create `My::Model` class
```perl
# Base class to be instantiated in main
package My::Model;
use Mojo::Base 'Mojo::Pg::ORM';
```3. Create `Mojo::Pg::ORM::Model` classes
```perl
# Model class for the table "postings"
package My::Model::Posting;
use Mojo::Base 'Mojo::Pg::ORM::Model';
use experimental 'signatures';use Mojo::Date;
hook before_create => sub($self) {
$self->{created} = Mojo::Date->new;
}sub hello($self) {
return 'Hello, ' . $self->{title};
}
```