-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.dart
44 lines (37 loc) · 955 Bytes
/
mongo.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#library ('mongo.dart');
#import ('mongo-dart/lib/mongo.dart');
class MongoDb {
int _currentId = 0;
DbCollection _collection(onOpened(DbCollection col)) {
final Db db = new Db('sssg');
db.open().then((bool result) {
Future f = onOpened(db.collection('echo-server'));
if (f != null) {
f.then((e) { db.close(); });
} else {
db.close();
}
});
}
MongoDb() {
_collection((DbCollection col) {
col.remove();
});
}
void dataInsert(String m) {
_collection((DbCollection col) {
print('dataInsert($m)');
return col.insert({'id': '${_currentId++}', 'message': m});
});
}
void dataSelect(onSelected(List<Map> entries)) {
_collection((DbCollection col) {
Future<List<Map>> f = col.find({}).toList();
f.then(onSelected);
f.handleException((e) {
print('Error. - ${e.message}');
});
return f;
});
}
}