Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

redstone_mapper_mongo

cgarciae edited this page Jan 4, 2015 · 5 revisions

redstone_mapper_mongo is a MongoDB extension for redstone_mapper.

This package is a wrapper for the mongo_dart driver.

Usage:

Create a MongoDbManager to manage connections with the database:

var dbManager = new MongoDbManager("mongodb://localhost/dbname", poolSize: 3);

If you are using redstone_mapper as a Redstone.dart plugin, you can pass a MongoDbManager to getMapperPlugin(), so a database connection will be available for every request:

import 'package:redstone/server.dart' as app;
import 'package:redstone_mapper/plugin.dart';
import 'package:redstone_mapper_mongo/manager.dart';

main() {
  
  var dbManager = new MongoDbManager("mongodb://localhost/dbname", poolSize: 3);
  
  app.addPlugin(getMapperPlugin(dbManager));
  app.setupConsoleLog();
  app.start();
  
}

//redstone_mapper will create a "dbConn" attribute
//for every request.
@app.Route("/services/users/list")
listUsers(@app.Attr() MongoDb dbConn) =>
   dbConn.collection("users").find().toList();
   
//If you prefer, you can also create a getter to access the
//database connection of the current request, so
//you don't need to add an extra parameter for every route.
MongoDb get mongoDb => app.request.attributes.dbConn;

The MongoDb object is a wrapper that provides helper functions for encoding and decoding objects:

import 'dart:async';
import 'package:redstone/server.dart' as app;
import 'package:redstone_mapper/mapper.dart';
import 'package:redstone_mapper/plugin.dart';
import 'package:redstone_mapper_mongo/manager.dart';
import 'package:redstone_mapper_mongo/metadata.dart';

class User {
  
  //@Id is a special annotation to handle the "_id" document field, 
  //it instructs redstone_mapper to convert ObjectId values to String, 
  //and vice versa.
  @Id()
  String id;

  @Field()
  String username;

  @Field()
  String password;

  @ReferenceId()
  String bestFriend;

  //the field can also be a list
  @ReferenceId()
  List<String> friends;
  
}

MongoDb get mongoDb => app.request.attributes.dbConn;

@app.Route("/services/users/list")
@Encode()
Future<List<User>> listUsers() => 
  //query documents from the "users" collection, and decode
  //the result to List<User>.
  mongoDb.find("users", User); 

@app.Route("/services/users/add", methods: const[app.POST])
Future addUser(@Decode() User user) => 
  //encode user, and insert it in the "users" collection.
  mongoDb.insert("users", user);

However, the MongoDb class doesn't hide the mongo_dart API. You can access a DbCollection with the MongoDb.collection() method. Also, you can access the original connection object with the MongoDb.innerConn property.

Moreover, you can use a MongoDbService to handle operations that concerns the same document type:

MongoDbService<User> userService = new MongoDbService<User>("users");

@app.Route("/services/users/list")
@Encode()
Future<List<User>> listUsers() => userService.find(); 

@app.Route("/services/users/add", methods: const[app.POST])
Future addUser(@Decode() User user) => userService.insert(user);

You can use the reference ids to query for the real objects

@app.Route("/get/friends/:id")
@Encode()
getFriends(@app.Attr() MongoDb dbConn, String id) 
{
    return dbConn.findOne("users", User, where.id(StringToId(id))).then((User user){
        var friendIds = user.friendIds.map (StringToId).toList();
        return dbConn.find("users", User, where.oneFrom("_id", friendIds));
    });
}

//Useful shortcut
StringToId (String id) => new ObjectId.fromHexString(id);

It's also possible to inherit from MongoDbService:

@app.Group("/services/users")
Class UserService extends MongoDbService<User> {

  UserService() : super("users");

  @app.Route("/list")
  @Encode()
  Future<List<User>> list() => find();

  @app.Route("/add")
  Future add(@Decode() User user) => insert(user);

}

MongoDbService will by default use the database connection associated with the current request. If you are not using Redstone.dart, be sure to use the MongoDbService.fromConnection() constructor to create a new service.