@@ -17,6 +17,34 @@ function extendMemoryDB(MemoryDB) {
17
17
18
18
ShareDBMingo . prototype = Object . create ( MemoryDB . prototype ) ;
19
19
20
+ ShareDBMingo . prototype . query = function ( collection , query , fields , options , callback ) {
21
+ var includeMetadata = options && options . metadata ;
22
+ var db = this ;
23
+ if ( typeof callback !== 'function' ) throw new Error ( 'Callback required' ) ;
24
+ process . nextTick ( function ( ) {
25
+ var collectionDocs = db . docs [ collection ] ;
26
+ var snapshots = [ ] ;
27
+ // Include metadata for the snapshots we are about to query, so the metadata
28
+ // can be used to filter the results
29
+ var includeMetadataForQuery = true ;
30
+ for ( var id in collectionDocs || { } ) {
31
+ var snapshot = db . _getSnapshotSync ( collection , id , includeMetadataForQuery ) ;
32
+ snapshots . push ( snapshot ) ;
33
+ }
34
+ try {
35
+ var result = db . _querySync ( snapshots , query , options ) ;
36
+ // If metadata was not explicitly defined in the original options, we want
37
+ // to remove the metadata from the snapshots to match ShareDB's behavior
38
+ if ( result . snapshots && ! includeMetadata ) {
39
+ result . snapshots . forEach ( function ( snapshot ) { snapshot . m = null ; } ) ;
40
+ }
41
+ callback ( null , result . snapshots , result . extra ) ;
42
+ } catch ( err ) {
43
+ callback ( err ) ;
44
+ }
45
+ } ) ;
46
+ } ;
47
+
20
48
ShareDBMingo . prototype . _querySync = function ( snapshots , query , options ) {
21
49
var parsed = parseQuery ( query ) ;
22
50
var mingoQuery = new Mingo . Query ( castToSnapshotQuery ( parsed . query ) ) ;
@@ -85,13 +113,16 @@ function extendMemoryDB(MemoryDB) {
85
113
86
114
// Build a query object that mimics how the query would be executed if it were
87
115
// made against snapshots persisted with `sharedb-mongo`
88
- // FIXME: This doesn't handle nested doc properties with dots like: {'_m.mtime': 12300}
89
116
function castToSnapshotQuery ( query ) {
90
117
var snapshotQuery = { } ;
118
+ var propertySegments ;
91
119
for ( var property in query ) {
120
+ propertySegments = property . split ( '.' ) ;
121
+
92
122
// Mongo doc property
93
- if ( MONGO_DOC_PROPERTIES [ property ] ) {
94
- snapshotQuery [ MONGO_DOC_PROPERTIES [ property ] ] = query [ property ] ;
123
+ if ( MONGO_DOC_PROPERTIES [ propertySegments [ 0 ] ] ) {
124
+ propertySegments [ 0 ] = MONGO_DOC_PROPERTIES [ propertySegments [ 0 ] ] ;
125
+ snapshotQuery [ propertySegments . join ( '.' ) ] = query [ property ] ;
95
126
96
127
// top-level boolean operator
97
128
} else if ( property [ 0 ] === '$' && Array . isArray ( query [ property ] ) ) {
0 commit comments