import 'package:EasyDartModule/base/database/DataBase.dart'; import 'package:mongo_dart/mongo_dart.dart'; class MongoDb implements DataBase { final DataBaseConfig config; final Db db; MongoDb(this.config) : db = Db( "mongodb://${config.userName}:${config.password}@${config.host}/${config.dataBase}?authSource=admin") { Future.delayed(Duration(seconds: 1), () async { do { try { await db.open(); print('Connected successfully!'); } catch (e) { print('Connection error: $e'); await Future.delayed(Duration(seconds: 1)); } } while (!db.isConnected); }); } DbCollection getCollection(String name) { return db.collection(name); } @override Future delete(String table, dynamic condition) async { await getCollection(table).deleteMany(condition); } @override Future insert(String table, Map data) async { await getCollection(table).insert(data); } @override Future>> query(String table, {dynamic condition}) async { if (condition == null) { return await getCollection(table).find().toList(); } return await getCollection(table).find(condition).toList(); } @override Future update( String table, Map data, dynamic condition) async { await getCollection(table).update(condition, data); } }