Skip to content

Latest commit

 

History

History

store

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

roc-pg store example

This example is a simple REST API for a store built using the basic-webserver platform and roc-pg's query builder.

Setup

To run it, you first need a PostgreSQL server running and database called roc_pg_example:

# Create a new database
$ createdb roc_pg_example

# Restore dump
$ psql -U postgres -d roc_pg_example < examples/store/db.sql

Now you can generate the schema module from our new database:

# Build the CLI
$ roc build sql-cli/src/roc-sql.roc

# Generate schema for query builder
$ ./sql-cli/src/roc-sql -h localhost -p 5432 -U postgres -d roc_pg_example --schema public > examples/store/Public.roc

You can tweak the connection parameters at example/store/server.roc.

Running

Run the server:

$ roc dev examples/store/server.roc

...and try hitting some of the routes!

See the output of the server for the generated SQL:

GET /products
SQL: select p.id, p.name from public.products as p


GET /orders/1/products
SQL: select p.id, p.name from public.products as p join public.order_products as op on op.product_id = p.id where op.order_id = $1
$1 = 1