Using MongoDB with Jaguar.dart

Packages used

Manually establishing a connection

  1. It doesn’t use connection pools.
  2. You have to manually make sure the connection is released when there is an exception in the route handling.
  3. Other interceptors and route handlers cannot share the same connection.

Interceptor

  1. Establishes connection to MongoDB on route entry
  2. Uses connection pool to
    - Limit number of simultaneously open connections
    - Reuse open connections
  3. Releases connection to connection pool on route exit
  4. Releases connection to connection pool when there is an exception in route processing

Connection pool

  1. Limit number of simultaneously open connections
  2. Reuse open connections

Connection release on exit and exception

Serialization

Defining the model

Defining Mongo serializer for TodoItem

Generating the _$TodoItemMongoSerializer

JSON serializer for TodoItem

The API

@Api(path: '/api/todos')
@WrapOne(#mongo)
class TodoApi {
MongoDB mongo(Context ctx) => new MongoDb(mongoUrl);
//TODO route handlers go here}

Inserting a document

@Post()
Future<Response<String>> insert(Context ctx) async {
mgo.Db db = ctx.getInput(MongoDB);
Map body = await ctx.req.bodyAsJsonMap();
TodoItem todo = jsonSerializer.fromMap(body);
final String id = new mgo.ObjectId().toHexString();
todo.id = id;
final coll = db.collection(todoColl);
await coll.insert(mongoSerializer.toMap(todo));

Map res = await coll.findOne(mgo.where.id(mgo.ObjectId.parse(id)));
return Response.json(mgoToJson(res));
}
TodoItem todo = jsonSerializer.fromMap(body);
final String id = new mgo.ObjectId().toHexString();
await coll.insert(mongoSerializer.toMap(todo));

Finding a document by Id

@Get(path: '/:id')  
Future<Response<String>> getById(Context ctx) async {
String id = ctx.pathParams['id'];
mgo.Db db = ctx.getInput<mgo.Db>(MongoDb);
final coll = db.collection(todoColl);
Map res = await coll.findOne(mgo.where.id(mgo.ObjectId.parse(id)));
return Response.json(_mgoToJson(res));
}
Map res = await coll.findOne(mgo.where.id(mgo.ObjectId.parse(id)));
Map _mgoToJson(Map map) => jsonSerializer.toMap(mongoSerializer.fromMap(map));

Getting all documents

@Get()  
Future<Response<String>> getAll(Context ctx) async {
mgo.Db db = ctx.getInput<mgo.Db>(MongoDb);
final coll = db.collection(todoColl);
List<Map> res = await (await coll.find()).map(_mgoToJson).toList();
return Response.json(res);
}
await coll.find()

Updating a document

@Put()  
Future<Response<String>> update(Context ctx) async {
Map body = await ctx.req.bodyAsJsonMap();
mgo.Db db = ctx.getInput<mgo.Db>(MongoDb);
TodoItem todo = jsonSerializer.fromMap(body);
final String id = todo.id;
final coll = db.collection(todoColl);
await coll.update(mgo.where.id(mgo.ObjectId.parse(id)), _mgoEnc(todo));
Map res = await coll.findOne(mgo.where.id(mgo.ObjectId.parse(id)));
return Response.json(_mgoToJson(res));
}
await coll.update(mgo.where.id(mgo.ObjectId.parse(id)), _mgoEnc(todo));

Deleting a document

@Delete(path: '/:id')  
Future deleteById(Context ctx) async {
String id = ctx.pathParams['id'];
mgo.Db db = ctx.getInput<mgo.Db>(MongoDb);
final coll = db.collection(todoColl);
await coll.remove(mgo.where.id(mgo.ObjectId.parse(id)));
}
await coll.remove(mgo.where.id(mgo.ObjectId.parse(id)));

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Reducing your Google Pub/Sub costs over 95% by micro-batching with Google Cloud Storage

Why So Negative? : A Positive Guide Towards Negative Testing

Negative testing

12 features that make streaming easy with mimic.

Android Jetpack:Empower your UI with Android Data Binding-Part 2

Happy Birthday vcluster!

vcluster logo

Playing with Key-Value Observing (KVO) (Swift3)

Monkey Run

Versioning Independent Components

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ravi Teja Gudapati

Ravi Teja Gudapati

More from Medium

Hosting a local API Online

What is Version Controlling and NoSQL

How to Setup a HarperDB Instance in a Node.js REST API

Pagination — Offset vs Cursor in MySQL