jaguar_mongo 1.1.5 jaguar_mongo: ^1.1.5 copied to clipboard
Mongo interceptor for jaguar
jaguar_mongo #
Mongo interceptor for Jaguar
MongoDb
interceptor shall be wrapped around routes to automatically connect and release connection to a mongodb server
every request.
Note: Fret not! MongoDb 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.
@Api(path: '/api')
class TodoApi {
// Declare Mongo interceptor
MongoDb mongoDb(Context ctx) => new MongoDb('mongodb://localhost:27017/test');
@Get()
@WrapOne(#mongoDb) // Wrap the mongo interceptor around a route
Future<String> fetchAll(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()
@WrapOne(#mongoDb) // Wrap the mongo interceptor around a route
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 #
@Api(path: '/api/todo')
class TodoApi {
// Mongo interceptor
MongoDb mongoDb(Context ctx) => new MongoDb('mongodb://localhost:27017/test');
@Get()
@WrapOne(#mongoDb) // Wrap the mongo interceptor around a route
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()
@WrapOne(#mongoDb)
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());
}