jaguar_postgres 2.2.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 75

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);

@Controller(path: '/contact')
class ContactsApi {
  @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();
  }

  @PostJson()
  Future<List<Map>> create(Context ctx) async {
    Map body = await ctx.bodyAsJsonMap();
    pg.PostgreSQLConnection db = await postgresPool.injectInterceptor(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 = new 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.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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';
  
Version Uploaded Documentation Archive
2.2.0 Aug 15, 2018 Go to the documentation of jaguar_postgres 2.2.0 Download jaguar_postgres 2.2.0 archive
2.1.8 Aug 8, 2018 Go to the documentation of jaguar_postgres 2.1.8 Download jaguar_postgres 2.1.8 archive
2.1.6 Jul 8, 2018 Go to the documentation of jaguar_postgres 2.1.6 Download jaguar_postgres 2.1.6 archive
2.1.5 Jul 8, 2018 Go to the documentation of jaguar_postgres 2.1.5 Download jaguar_postgres 2.1.5 archive
2.1.4 Jul 8, 2018 Go to the documentation of jaguar_postgres 2.1.4 Download jaguar_postgres 2.1.4 archive
2.1.1 Jul 7, 2018 Go to the documentation of jaguar_postgres 2.1.1 Download jaguar_postgres 2.1.1 archive
0.5.0 Jun 7, 2017 Go to the documentation of jaguar_postgres 0.5.0 Download jaguar_postgres 0.5.0 archive
0.4.0 Feb 19, 2017 Go to the documentation of jaguar_postgres 0.4.0 Download jaguar_postgres 0.4.0 archive
0.3.0 Feb 6, 2017 Go to the documentation of jaguar_postgres 0.3.0 Download jaguar_postgres 0.3.0 archive
0.2.0 Jan 21, 2017 Go to the documentation of jaguar_postgres 0.2.0 Download jaguar_postgres 0.2.0 archive

All 11 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
49
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

We analyzed this package on Aug 15, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.11.8

Platforms

Detected platforms: Flutter, other

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

Suggestions

The description is too short.

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
conn_pool ^2.1.6 2.1.6
jaguar ^2.2.0 2.2.6
postgres ^1.0.0 1.0.0
Transitive dependencies
auth_header 2.1.2
charcode 1.1.2
collection 1.14.11
convert 2.0.2
crypto 2.0.6
http_server 0.9.8
jaguar_common 2.1.4
logging 0.11.3+2
mime 0.9.6+2
path 1.6.2
path_tree 2.2.2
stack_trace 1.9.3
typed_data 1.1.6
Dev dependencies
jaguar_reflect ^2.2.0