Skip to content

PostgreSQL: Establish connection

Ravi Teja Gudapati edited this page Dec 27, 2017 · 1 revision

Without using interceptor

Packages used

  • postgresql : PostgreSQL client for Dart

Establish connection

connect global method in postgresql package is used to establish connection to PostgreSQL database. close method on Connection object shall be used to release the connection.

const String postgreUrl = "postgres://postgres:dart_jaguar@localhost/todos";

@Api(path: '/api/todos')
class TodoApi {
  @Get(path: '/:id')
  Future<Response<String>> getById(Context ctx) async {
    String id = ctx.pathParams.id;
    final pg.Connection db = pg.connect(postgreUrl);
    ...
    db.close();
    ...
  }
}

Using interceptor

Packages Used

  • postgresql : PostgreSQL client for Dart.
  • jaguar_postgresql : PostgreSQL interceptor for Jaguar.

Establish connection

jaguar_postgresql package provides PostgresDb interceptor to establish connection to PostgreSQL database before the execution of route handler and release the connection after the execution of route handler. PostgresDb interceptor accepts URL of the PostgreSQL database as argument.

const String postgreUrl = "postgres://postgres:dart_jaguar@localhost/todos";

@Api(path: '/api/todos')
@Wrap(const [#postgres])
class TodoApi {
  PostgresDb postgres(Context ctx) => new PostgresDb(postgreUrl);

  @Post()
  Future<Response<String>> insert(Context ctx) async {
    final pg.Connection db = ctx.getInput<pg.Connection>(PostgresDb);
    ...
  }
}

Basics

Serialization

Forms

Sessions

Authentication

  • Basic authentication
  • Form authentication
  • JSON authentication
  • Authorization
  • OAuth

Database

Security

Real time

  • Server sent events (SSE)
  • Websockets

Deployment

  • systemd
  • Docker
  • AppEngine

API Documentation

Clone this wiki locally