Dart Documentationclean_sync.serverMongoDatabase

MongoDatabase class

class MongoDatabase {
 Db _db;
 Future _conn;
 List<Future> init = [];
 DbCollection _lock;
 Cache cache;

 MongoDatabase(String url, {Cache this.cache: dummyCache} ) {
   _db = new Db(url);
   _conn = _db.open(); // open connection to database
   init.add(_conn);
   init.add(_conn.then((_) {
     _lock = _db.collection(LOCK_COLLECTION_NAME);
     return true;
     }));
 }

 void close() {
   Future.wait(init).then((_) => _db.close());
 }

 void create_collection(String collectionName) {
   init.add(_conn.then((_) =>
       _db.createIndex(historyCollectionName(collectionName), key: 'version',
       unique: true)));
 }

 /**
  * Creates index on chosen collection and corresponding indexes on collection
  * history. keys is a map in form {field_name: 1 or -1} with 1/-1 specifying
  * ascending/descending order (same as the map passed to mongo function
  * ensureIndex).
  */
 void createIndex(String collectionName, Map keys, {unique: false}) {
   Map beforeKeys = {};
   Map afterKeys = {};
   keys.forEach((key, val) {
     beforeKeys['before.$key'] = val;
     afterKeys['after.$key'] = val;
   });
   beforeKeys['version'] = 1;
   afterKeys['version'] = 1;
   init.add(_conn.then((_) =>
       _db.createIndex(historyCollectionName(collectionName),
           keys: beforeKeys)));
   init.add(_conn.then((_) =>
       _db.createIndex(historyCollectionName(collectionName),
           keys: afterKeys)));
   if (keys.isNotEmpty) {
     init.add(_conn.then((_) =>
         _db.createIndex(collectionName, keys: keys, unique: unique)));
   }
 }

 MongoProvider collection(String collectionName) {
   DbCollection collection = _db.collection(collectionName);
   DbCollection collectionHistory =
       _db.collection(historyCollectionName(collectionName));
   return new MongoProvider(collection, collectionHistory, _lock, cache);
 }

 Future dropCollection(String collectionName) =>
   _conn.then((_) => Future.wait([
     _db.collection(collectionName).drop(),
     _db.collection(historyCollectionName(collectionName)).drop()
   ]));

 Future removeLocks() => _lock.drop();
}

Constructors

new MongoDatabase(String url, {Cache cache: dummyCache}) #

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

docs inherited from Object
MongoDatabase(String url, {Cache this.cache: dummyCache} ) {
 _db = new Db(url);
 _conn = _db.open(); // open connection to database
 init.add(_conn);
 init.add(_conn.then((_) {
   _lock = _db.collection(LOCK_COLLECTION_NAME);
   return true;
   }));
}

Properties

Cache cache #

Cache cache

List<Future> init #

List<Future> init = []

Methods

void close() #

void close() {
 Future.wait(init).then((_) => _db.close());
}

MongoProvider collection(String collectionName) #

MongoProvider collection(String collectionName) {
 DbCollection collection = _db.collection(collectionName);
 DbCollection collectionHistory =
     _db.collection(historyCollectionName(collectionName));
 return new MongoProvider(collection, collectionHistory, _lock, cache);
}

void create_collection(String collectionName) #

void create_collection(String collectionName) {
 init.add(_conn.then((_) =>
     _db.createIndex(historyCollectionName(collectionName), key: 'version',
     unique: true)));
}

void createIndex(String collectionName, Map keys, {unique: false}) #

Creates index on chosen collection and corresponding indexes on collection history. keys is a map in form {field_name: 1 or -1} with 1/-1 specifying ascending/descending order (same as the map passed to mongo function ensureIndex).

void createIndex(String collectionName, Map keys, {unique: false}) {
 Map beforeKeys = {};
 Map afterKeys = {};
 keys.forEach((key, val) {
   beforeKeys['before.$key'] = val;
   afterKeys['after.$key'] = val;
 });
 beforeKeys['version'] = 1;
 afterKeys['version'] = 1;
 init.add(_conn.then((_) =>
     _db.createIndex(historyCollectionName(collectionName),
         keys: beforeKeys)));
 init.add(_conn.then((_) =>
     _db.createIndex(historyCollectionName(collectionName),
         keys: afterKeys)));
 if (keys.isNotEmpty) {
   init.add(_conn.then((_) =>
       _db.createIndex(collectionName, keys: keys, unique: unique)));
 }
}

Future dropCollection(String collectionName) #

Future dropCollection(String collectionName) =>
 _conn.then((_) => Future.wait([
   _db.collection(collectionName).drop(),
   _db.collection(historyCollectionName(collectionName)).drop()
 ]));

Future removeLocks() #

Future removeLocks() => _lock.drop();