The smallest useful StreetJS service: a Todo list with PostgreSQL persistence in
three files. If you have read PostgreSQL Integration,
this assembles those pieces into a runnable app.
1. Scaffold + database
1
2
3
street create todo-api
cd todo-api && npm install
docker compose up -d postgres
// src/repositories/todo.repository.tsimport{Injectable,container,PgPool}from'streetjs';importtype{PgRow}from'streetjs';exportinterfaceTodo{id:string;title:string;done:boolean;createdAt:Date;}consttoTodo=(r:PgRow):Todo=>({id:String(r['id']),title:String(r['title']),done:Boolean(r['done']),createdAt:newDate(String(r['created_at'])),});@Injectable()exportclassTodoRepository{privatereadonlypool=container.resolve(PgPool);asynclist(){return (awaitthis.pool.query('SELECT * FROM todos ORDER BY created_at DESC')).rows.map(toTodo);}asynccreate(title:string){returntoTodo((awaitthis.pool.query('INSERT INTO todos (title) VALUES ($1) RETURNING *',[title])).rows[0]as PgRow);}asynctoggle(id:string){awaitthis.pool.query('UPDATE todos SET done = NOT done WHERE id = $1',[id]);}asyncremove(id:string){awaitthis.pool.query('DELETE FROM todos WHERE id = $1',[id]);}}