jaguar_mongo 1.3.1

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

jaguar_mongo

Mongo interceptor for Jaguar

MongoDb interceptor can be wrapped around routes to automatically connect and release connection to a mongodb server every request.

Note: MongoDb interceptor does not create and destroy new connection every request. It just uses one from a pool of connections. This way, you wont be over-loading mongodb server with infinite connections.

/// Mongo interceptor
MongoDb mongoDb(Context ctx) => new MongoDb('mongodb://localhost:27017/test');

@Api(path: '/api')
  @WrapOne(mongoDb)  // Wrap the mongo interceptor around a route
class TodoApi {
  @Get()
  Future<String> fetchAll(Context ctx) async {
    // ...
  }
  
  @Post()
  Future<String> add(Context ctx) async {
    // ...
  }
}

MongoDb interceptor injects the per-request Db connection into the interceptor inputs of Context.

The per-request Db connection can be obtained using getInterceptorResult method of the Context.

  @Get()
  Future<String> fetchAll(Context ctx) async {
    // Get the Db instance from the interceptor
    final Db db = ctx.getInterceptorResult(MongoDb);
    // Use Db to fetch Todo items
    final res = await (await db.collection('todo').find()).toList();
    return await JSON.encode(res);
  }

A complete example

Server

/// Mongo interceptor
MongoDb mongoDb(Context ctx) => new MongoDb('mongodb://localhost:27017/test');

@Api(path: '/api/todo')
@WrapOne(mongoDb) // Wrap the mongo interceptor around a route
class TodoApi {
  @Get()
  Future<String> fetchAll(Context ctx) async {
    // Get the Db instance from the interceptor
    final Db db = ctx.getInterceptorResult(MongoDb);
    // Use Db to fetch Todo items
    final res = await (await db.collection('todo').find()).toList();
    return await JSON.encode(res);
  }

  @Post()
  Future<String> add(Context ctx) async {
    final Map body = await ctx.req.bodyAsJsonMap();
    final Db db = ctx.getInterceptorResult(MongoDb);
    await db.collection('todo').insert(body);
    // Use Db to fetch Todo items
    final res = await (await db.collection('todo').find()).toList();
    return await JSON.encode(res);
  }
}

Future server() async {
  final server = new Jaguar();
  server.addApi(reflect(new TodoApi()));
  await server.serve();
}

Client

Future doClient() async {
  final url = 'http://localhost:8080/api/todo';
  final jClient = new JsonClient(client);

  final res = (await jClient.get(url)).deserialize();
  print(res);
  print((await jClient.post(url, body: {'text': 'Laundry', 'time': 'Today'}))
      .deserialize());
  print((await jClient.get(url)).deserialize());
}

Changelog

1.3.1

  • Jaguar 1.3.x

1.2.14

  • Jaguar 1.2.14 style Interceptor

0.6.0

  • mongo_Dart 0.3.x
  • Documentation

example/main.dart

library jaguar_mongo.example;

import 'dart:io';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart';
import 'package:jaguar_client/jaguar_client.dart';
import 'package:jaguar/jaguar.dart';
import 'package:jaguar_reflect/jaguar_reflect.dart';
import 'package:mongo_dart/mongo_dart.dart';

import 'package:jaguar_mongo/jaguar_mongo.dart';

final client = new Client();

/// Mongo interceptor
MongoDb mongoDb(Context ctx) => new MongoDb('mongodb://localhost:27017/test');

@Api(path: '/api/todo')
@WrapOne(mongoDb) // Wrap the mongo interceptor around a route
class TodoApi {
  @Get()
  Future<String> fetchAll(Context ctx) async {
    // Get the Db instance from the interceptor
    final Db db = ctx.getInterceptorResult(MongoDb);
    // Use Db to fetch Todo items
    final res = await (await db.collection('todo').find()).toList();
    return await JSON.encode(res);
  }

  @Post()
  Future<String> add(Context ctx) async {
    final Map body = await ctx.req.bodyAsJsonMap();
    final Db db = ctx.getInterceptorResult(MongoDb);
    await db.collection('todo').insert(body);
    // Use Db to fetch Todo items
    final res = await (await db.collection('todo').find()).toList();
    return await JSON.encode(res);
  }
}

Future server() async {
  final server = new Jaguar(port: 10000);
  server.addApi(reflect(new TodoApi()));
  await server.serve();
}

Future doClient() async {
  final url = 'http://localhost:10000/api/todo';
  final jClient = new JsonClient(client);

  final res = (await jClient.get(url)).deserialize();
  print(res);
  print((await jClient.post(url, body: {'text': 'Laundry', 'time': 'Today'}))
      .deserialize());
  print((await jClient.get(url)).deserialize());
}

main(List<String> args) async {
  await Future.wait([server(), doClient()]);
  exit(0);
}

1. Depend on it

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


dependencies:
  jaguar_mongo: "^1.3.1"

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 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_mongo/jaguar_mongo.dart';
        
Version Uploaded Documentation Archive
1.3.1 Feb 2, 2018 Go to the documentation of jaguar_mongo 1.3.1 Download jaguar_mongo 1.3.1 archive
1.2.14 Feb 1, 2018 Go to the documentation of jaguar_mongo 1.2.14 Download jaguar_mongo 1.2.14 archive
1.2.5 Jan 13, 2018 Go to the documentation of jaguar_mongo 1.2.5 Download jaguar_mongo 1.2.5 archive
1.1.5 Jan 11, 2018 Go to the documentation of jaguar_mongo 1.1.5 Download jaguar_mongo 1.1.5 archive
0.6.0 Nov 4, 2017 Go to the documentation of jaguar_mongo 0.6.0 Download jaguar_mongo 0.6.0 archive
0.5.1 Jul 9, 2017 Go to the documentation of jaguar_mongo 0.5.1 Download jaguar_mongo 0.5.1 archive
0.5.0 Jun 4, 2017 Go to the documentation of jaguar_mongo 0.5.0 Download jaguar_mongo 0.5.0 archive
0.4.0 Feb 19, 2017 Go to the documentation of jaguar_mongo 0.4.0 Download jaguar_mongo 0.4.0 archive
0.3.1 Feb 6, 2017 Go to the documentation of jaguar_mongo 0.3.1 Download jaguar_mongo 0.3.1 archive
0.3.0 Feb 6, 2017 Go to the documentation of jaguar_mongo 0.3.0 Download jaguar_mongo 0.3.0 archive

All 17 versions...

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Feb 3, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

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

Platforms

Detected platforms: Flutter, other

Primary library: package:jaguar_mongo/jaguar_mongo.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.

  • Use analysis_options.yaml.

    Rename old .analysis_options file to analysis_options.yaml.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.0 <2.0.0
connection_pool >=0.1.2 <0.2.0 0.1.2
jaguar ^1.3.4 1.3.4
mongo_dart ^0.3.0 0.3.1
Transitive dependencies
args 1.2.0 1.3.0
async 2.0.3
bson 0.3.0
charcode 1.1.1
collection 1.14.5
convert 2.0.1
crypto 2.0.2+1
fixnum 0.10.6
http_server 0.9.7
intl 0.15.2
logging 0.11.3+1
meta 1.1.2
mime 0.9.6
mongo_dart_query 0.3.0
more 1.8.1
path 1.5.1
pool 1.3.4
quiver_hashcode 1.0.0
source_span 1.4.0
stack_trace 1.9.1
string_scanner 1.0.2
typed_data 1.1.5
uuid 0.5.3
yaml 2.1.13
Dev dependencies
http
jaguar_client ^0.2.0
jaguar_reflect