import{PgPool}from'streetjs';constpool=newPgPool({host:'localhost',port:5432,user:'app',password:'secret',database:'myapp',});awaitpool.initialize();constresult=awaitpool.query('SELECT * FROM users WHERE id = $1',[userId]);
Repository Pattern
1
2
3
4
5
6
7
8
9
10
11
12
13
import{StreetPostgresRepository}from'streetjs';classUserRepositoryextendsStreetPostgresRepository<User>{constructor(pool:PgPool){super(pool,'users');}asyncfindByEmail(email:string):Promise<User|null>{constresult=awaitthis.pool.query('SELECT * FROM users WHERE email = $1',[email]);returnresult.rows[0]as User??null;}}
Throw StreetJS exceptions from handlers — they’re automatically serialized:
1
2
3
4
5
6
7
import{NotFoundException,BadRequestException}from'streetjs';asyncgetUser(ctx){constuser=awaituserService.findById(ctx.params.id);if (!user)thrownewNotFoundException('User not found');ctx.json(user);}