Slides from my talk on #ruby-mg meeting.
Intro about how we in catars.me are using postgREST to create fast and simple API that can be represented with various mithril.js components.
10. PostgREST serves a fully RESTful API from any
existing PostgreSQL database. It provides a
cleaner, more standards-compliant, faster API
than you are likely to write from scratch.
begriffs
github.com/begriffs/postgrest
18. Auth
TABLE
CREATE TABLE postgrest.auth (
id character varying NOT NULL,
rolname name NOT NULL,
pass character(60) NOT NULL,
CONSTRAINT auth_pkey PRIMARY KEY (id)
) WITH ( OIDS=FALSE );
20. PostgREST exposes the current
user id as user_vars.user_id,
which is a variable we can use
inside triggers and functions.
SELECT current_setting('user_vars.user_id');
begriffs
github.com/begriffs/postgrest
25. Serializing JSON
VIEW “1”.team_totals
CREATE OR REPLACE VIEW "1".team_totals as (
select
count(DISTINCT u.id) as member_count,
array_to_json(array_agg(DISTINCT country.name)) as countries,
count(DISTINCT c.project_id)
FILTER (WHERE c.was_confirmed)
as total_contributed_projects,
count(DISTINCT lower(unaccent(u.address_city))) as total_cities,
sum(c.value)
FILTER (WHERE c.was_confirmed)
as total_amount
from users u
left join contributions c on c.user_id = u.id
left join countries country on country.id = u.country_id
where u.admin
);
29. Mithril is a client-side MVC framework - a tool
to organize code in a way that is easy to think
about and to maintain.
30. • Only 12kb gzipped, no dependencies
• Virtual DOM diffing and compilable templates
• Intelligent auto-redrawing system
• Hierarchical MVC via components
• Small API, small learning curve
38. mithril.postgrest
• Help you authenticating in a PostgREST server.
• Provide wrappers arround the mithril request
function to use JWT.
• Provide a constructor for objects that will
interact with PostgREST endpoints
• Provide some helpers to build some useful View-
Model objects.
39. mithril.postgrest
• Help you authenticating in a PostgREST server.
• Provide wrappers arround the mithril request
function to use JWT.
• Provide a constructor for objects that will
interact with PostgREST endpoints
• Provide some helpers to build some useful View-
Model objects.
42. pagination view model
mithril.postgrest
var userPages = m.postgrest.paginationVM(users.getPageWithToken);
// The firstPage function returns a mithril promise
userPages.firstPage(filters.parameters()).then(() => {
// Results are in collection
console.log(userPages.collection());
},
() => {
alert('Error loading users');
});
* paginationVM(loadPageFunction) - Generate a pagination View-Model that loads pages using
the loadPageFunction (should be a mithril request)
43. pagination view model
mithril.postgrest
// The nextPage function returns a mithril promise
userPages.nextPage().then(() => {
// Results are appended to collection
console.log(userPages.collection());
},
() => {
alert('Error loading next page');
});
51. step 2 - view
project reminder view
postgREST default schema = “1”
check if transaction user is a owner or admin
52. step 2 - view
project reminder grants
since we are using postgres user role system we need to grant privileges to
roles that you want to given access.
53. step 2 - view
project reminder functions / triggers
since postgREST implements authentication endpoints and JWT tokens, we need
to prevent that admin or web_user delete all your reminders and create
reminders for other users.