jaguar_postgres 2.4.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 40

jaguar_postgres #

A Postgres interceptor for Jaguar.

Usage #

Creating the pool #

Create an instance of PostgresPool. Supply essential database configuration. By default, PostgresPool builds on a SharedPool. Supply minPoolSize and maxPoolSize to control pool size.

final postgresPool = PostgresPool('jaguar_learn',
    password: 'dart_jaguar', minPoolSize: 5, maxPoolSize: 10);

Getting and using connection #

Use injectInterceptor method of PostgresPool to get a connection to Postgres. injectInterceptor automatically releases the connection after the request has been serviced or if an exception occurs.

  @GetJson()
  Future<List<Map>> readAll(Context ctx) async {
    pg.PostgreSQLConnection db = await postgresPool.injectInterceptor(ctx);
    List<Map<String, Map<String, dynamic>>> values =
        await db.mappedResultsQuery("SELECT * FROM contacts;");
    return values.map((m) => m.values.first).toList();
  }

Changelog #

2.1.5 #

  • Simplified the example

2.1.4 #

  • Removed _releaseConn from PostgresPool

2.1.2 #

  • Updated homepage

2.1.1 #

  • Architecture
  • Introducing PostgresPool

example/main.dart

/// File: main.dart
library jaguar.example.silly;

import 'dart:async';
import 'package:jaguar_reflect/jaguar_reflect.dart';
import 'package:jaguar/jaguar.dart';
import 'package:postgres/postgres.dart' as pg;

import 'package:conn_pool/conn_pool.dart';
import 'package:jaguar_postgres/jaguar_postgres.dart';

final postgresPool = PostgresPool('jaguar_learn',
    password: 'dart_jaguar', minPoolSize: 5, maxPoolSize: 10);

@GenController(path: '/contact')
class ContactsApi extends Controller {
  @GetJson()
  Future<List<Map>> readAll(Context ctx) async {
    pg.PostgreSQLConnection db = await postgresPool(ctx);
    List<Map<String, Map<String, dynamic>>> values =
        await db.mappedResultsQuery("SELECT * FROM contacts;");
    return values.map((m) => m.values.first).toList();
  }

  @PostJson()
  Future<List<Map>> create(Context ctx) async {
    Map body = await ctx.bodyAsJsonMap();
    pg.PostgreSQLConnection db = await postgresPool(ctx);
    List<List<dynamic>> id = await db.query(
        "INSERT INTO contacts (name, age) VALUES ('${body['name']}', ${body['age']}) RETURNING id;");
    if (id.isEmpty || id.first.isEmpty) Response.json(null);
    List<Map<String, Map<String, dynamic>>> values =
        await db.mappedResultsQuery("SELECT * FROM contacts;");
    return values.map((m) => m.values.first).toList();
  }
}

Future<void> setup() async {
  // TODO handle open error
  Connection<pg.PostgreSQLConnection> conn = await postgresPool.pool.get();
  pg.PostgreSQLConnection db = conn.connection;

  try {
    await db.execute("CREATE DATABSE jaguar_learn;");
  } catch (e) {} finally {}

  try {
    await db.execute("DROP TABLE contacts;");
  } catch (e) {} finally {}

  try {
    await db.execute(
        "CREATE TABLE contacts (id SERIAL PRIMARY KEY, name VARCHAR(255), age INT);");
  } catch (e) {} finally {
    if (conn != null) await conn.release();
  }
}

Future<void> main() async {
  await setup();

  final server = Jaguar(port: 10000);
  server.add(reflect(ContactsApi()));
  server.log.onRecord.listen(print);

  await server.serve(logRequests: true);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  jaguar_postgres: ^2.4.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:jaguar_postgres/jaguar_postgres.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
93
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
61
Overall:
Weighted score of the above. [more]
40
Learn more about scoring.

We analyzed this package on Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Primary library: package:jaguar_postgres/jaguar_postgres.dart with components: io.

Health suggestions

Fix lib/src/interceptor.dart. (-3.93 points)

Analysis of lib/src/interceptor.dart reported 8 hints, including:

line 16 col 19: Use = to separate a named parameter from its default value.

line 17 col 15: Use = to separate a named parameter from its default value.

line 18 col 22: Use = to separate a named parameter from its default value.

line 20 col 18: Use = to separate a named parameter from its default value.

line 21 col 27: Use = to separate a named parameter from its default value.

Fix lib/src/manager.dart. (-2.96 points)

Analysis of lib/src/manager.dart reported 6 hints, including:

line 35 col 17: Use = to separate a named parameter from its default value.

line 36 col 16: Use = to separate a named parameter from its default value.

line 37 col 20: Use = to separate a named parameter from its default value.

line 39 col 18: Use = to separate a named parameter from its default value.

line 40 col 28: Use = to separate a named parameter from its default value.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (postgres).

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Package is getting outdated. (-9.04 points)

The package was last published 56 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
conn_pool ^2.1.8 2.1.9
jaguar ^2.4.4 2.4.36
postgres ^1.0.0 1.0.2 2.0.0-dev1.0
Transitive dependencies
auth_header 2.1.4
charcode 1.1.2
collection 1.14.12
convert 2.1.1
crypto 2.1.3
http_server 0.9.8+3
jaguar_common 2.1.4
jaguar_serializer 2.2.12
logging 0.11.3+2
meta 1.1.7
mime 0.9.6+3
path 1.6.4
path_tree 2.2.2
stack_trace 1.9.3
typed_data 1.1.6
Dev dependencies
jaguar_reflect ^2.4.0