Dart Documentationobjectory_direct_connectionObjectoryDirectConnectionImpl

ObjectoryDirectConnectionImpl class

class ObjectoryDirectConnectionImpl extends Objectory{
 Db db;
 ObjectoryDirectConnectionImpl(String uri,Function registerClassesCallback,bool dropCollectionsOnStartup):
   super(uri, registerClassesCallback, dropCollectionsOnStartup);
 Future open(){
   if (db != null){
     db.close();
   }
   db = new Db(uri);
   return db.open();
 }
 Future insert(PersistentObject persistentObject) =>
     db.collection(persistentObject.collectionName).insert(persistentObject.map);

 Future doUpdate(String collection,ObjectId id, Map toUpdate) =>
       db.collection(collection).update({"_id": id},toUpdate);

 Future remove(PersistentObject persistentObject) =>
     db.collection(persistentObject.collectionName).remove({"_id":persistentObject.id});
 
 ObjectoryCollection constructCollection() => new ObjectoryCollectionDirectConnectionImpl(this);

 Future<Map> dropDb(){
   return db.drop();
 }

 Future<Map> wait(){
   return db.wait();
 }


 void close(){
   db.close();
 }
 Future dropCollections() {
   return Future.wait(getCollections().map(
       (collection) => db.collection(collection).drop()));
 }
}

Extends

Objectory > ObjectoryDirectConnectionImpl

Constructors

new ObjectoryDirectConnectionImpl(String uri, Function registerClassesCallback, bool dropCollectionsOnStartup) #

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
ObjectoryDirectConnectionImpl(String uri,Function registerClassesCallback,bool dropCollectionsOnStartup):
 super(uri, registerClassesCallback, dropCollectionsOnStartup);

Properties

final Map<String, BasePersistentObject> cache #

inherited from Objectory
final Map<String,BasePersistentObject> cache = new  Map<String,BasePersistentObject>()

DataListDecorator dataListDecorator #

inherited from Objectory
DataListDecorator dataListDecorator = (List list) => list

DataMapDecorator dataMapDecorator #

inherited from Objectory
DataMapDecorator dataMapDecorator = (Map map) => map

Db db #

Db db

bool dropCollectionsOnStartup #

inherited from Objectory
bool dropCollectionsOnStartup

Function registerClassesCallback #

inherited from Objectory
Function registerClassesCallback

String uri #

inherited from Objectory
String uri

bool useFieldLevelUpdate #

inherited from Objectory
bool useFieldLevelUpdate = true

Operators

ObjectoryCollection operator [](Type classType) #

inherited from Objectory
ObjectoryCollection operator[](Type classType) => _collections[classType];

Methods

void close() #

void close(){
 db.close();
}

dynamic completeFindOne(Map map, Completer completer, ObjectoryQueryBuilder selector, Type classType) #

inherited from Objectory
completeFindOne(Map map,Completer completer,ObjectoryQueryBuilder selector,Type classType) {
 var obj;
 if (map == null) {
   completer.complete(null);
 }
 else {
   obj = objectory.map2Object(classType,map);
   if ((selector == null) ||  !selector.paramFetchLinks) {
     completer.complete(obj);
   } else {
     obj.fetchLinks().then((_) {
       completer.complete(obj);
     });  
   }
 }
}

ObjectoryCollection constructCollection() #

ObjectoryCollection constructCollection() => new ObjectoryCollectionDirectConnectionImpl(this);

List createTypedList(Type classType) #

inherited from Objectory
List createTypedList(Type classType) {
 return _listFactories[classType]();
}

PersistentObject dbRef2Object(DbRef dbRef) #

inherited from Objectory
PersistentObject dbRef2Object(DbRef dbRef) {
 return findInCacheOrGetProxy(dbRef.id, objectory.getClassTypeByCollection(dbRef.collection));
}

Future doUpdate(String collection, ObjectId id, Map toUpdate) #

Future doUpdate(String collection,ObjectId id, Map toUpdate) =>
     db.collection(collection).update({"_id": id},toUpdate);

Future dropCollections() #

Future dropCollections() {
 return Future.wait(getCollections().map(
     (collection) => db.collection(collection).drop()));
}

Future<Map> dropDb() #

Future<Map> dropDb(){
 return db.drop();
}

PersistentObject findInCacheOrGetProxy(id, Type classType) #

inherited from Objectory
PersistentObject findInCacheOrGetProxy(var id, Type classType) {
 if (id == null) {
   return null;
 }
 PersistentObject result = _findInCache(id);
 if (result == null) {
   result = objectory.newInstance(classType);
   result.id = id;
 }
 return result;
}

ObjectId generateId() #

inherited from Objectory
ObjectId generateId() => new ObjectId();

Type getClassTypeByCollection(String collectionName) #

inherited from Objectory
Type getClassTypeByCollection(String collectionName) => _collectionNameToTypeMap[collectionName];

List<String> getCollections() #

inherited from Objectory
List<String> getCollections() => _collections.values.map((ObjectoryCollection oc) => oc.collectionName).toList();

Future<bool> initDomainModel() #

inherited from Objectory
Future<bool> initDomainModel() {
 registerClassesCallback();
 return open().then((_){
   if (dropCollectionsOnStartup) {
     return objectory.dropCollections();
   }
 });
}

Future insert(PersistentObject persistentObject) #

Future insert(PersistentObject persistentObject) =>
   db.collection(persistentObject.collectionName).insert(persistentObject.map);

BasePersistentObject map2Object(Type classType, Map map) #

inherited from Objectory
BasePersistentObject map2Object(Type classType, Map map){
 if (map == null) {
   map = new LinkedHashMap();
 }
 var result = newInstance(classType);
 result.map = map;
 if (result is PersistentObject){
   result.id = map["_id"];
 }
 if (result is PersistentObject) {
   if (result.id != null) {
     objectory._addToCache(result);
   }
 }
 return result;
}

BasePersistentObject newInstance(Type classType) #

inherited from Objectory
BasePersistentObject newInstance(Type classType){
 if (_factories.containsKey(classType)){
   return _factories[classType]();
 }
 throw new Exception('Class $classType have not been registered in Objectory');
}

Future open() #

Future open(){
 if (db != null){
   db.close();
 }
 db = new Db(uri);
 return db.open();
}

void registerClass(Type classType, FactoryMethod factory, [FactoryMethod listFactory]) #

inherited from Objectory
void registerClass(Type classType,FactoryMethod factory,[FactoryMethod listFactory]){
 _factories[classType] = factory;
 _listFactories[classType] = (listFactory==null ? ()=>new List<PersistentObject>() : listFactory);
 BasePersistentObject obj = factory();
 if (obj is PersistentObject) {
   var collectionName = obj.collectionName;
   _collectionNameToTypeMap[collectionName] = classType;
   _collections[classType] = _createObjectoryCollection(classType,collectionName);
 }
}

Future remove(PersistentObject persistentObject) #

Future remove(PersistentObject persistentObject) =>
   db.collection(persistentObject.collectionName).remove({"_id":persistentObject.id});

Future save(PersistentObject persistentObject) #

inherited from Objectory
Future save(PersistentObject persistentObject){
 Future res;
 if (persistentObject.id != null){
   res = update(persistentObject);
 }
 else{
   persistentObject.id = generateId();
   persistentObject.map["_id"] = persistentObject.id;
   objectory._addToCache(persistentObject);
   res =  insert(persistentObject);
 }
 persistentObject.dirtyFields.clear();
 return res;
}

Future update(PersistentObject persistentObject) #

inherited from Objectory
Future update(PersistentObject persistentObject) {
 var id = persistentObject.id;
 if (id == null) {
   return new Future.error(new Exception('Update operation on object with null id'));
 }
 Map toUpdate = _getMapForUpdateCommand(persistentObject);
 if (toUpdate.isEmpty) {
   return new Future.value({'ok': 1.0, 'warn': 'Update operation called without actual changes'});
 }
 return doUpdate(persistentObject.collectionName,id,toUpdate);
}

Future<Map> wait() #

Future<Map> wait(){
 return db.wait();
}