@@ -2,11 +2,6 @@ var expect = require('expect.js');
2
2
var DB = require ( '../lib/db' ) ;
3
3
var MemoryDB = require ( '../lib/db/memory' ) ;
4
4
5
- // Extend from MemoryDB as defined in this package, not the one that
6
- // sharedb-mingo-memory depends on.
7
- var ShareDbMingo = require ( 'sharedb-mingo-memory' ) . extendMemoryDB ( MemoryDB ) ;
8
- var getQuery = require ( 'sharedb-mingo-memory/get-query' ) ;
9
-
10
5
describe ( 'DB base class' , function ( ) {
11
6
it ( 'can call db.close() without callback' , function ( ) {
12
7
var db = new DB ( ) ;
@@ -59,10 +54,76 @@ describe('DB base class', function() {
59
54
} ) ;
60
55
} ) ;
61
56
57
+
58
+ // Extension of MemoryDB that supports query filters and sorts on simple
59
+ // top-level properties, which is enough for the core ShareDB tests on
60
+ // query subscription updating.
61
+ function BasicQueryableMemoryDB ( ) {
62
+ MemoryDB . apply ( this , arguments ) ;
63
+ }
64
+ BasicQueryableMemoryDB . prototype = Object . create ( MemoryDB . prototype ) ;
65
+ BasicQueryableMemoryDB . prototype . constructor = BasicQueryableMemoryDB ;
66
+
67
+ BasicQueryableMemoryDB . prototype . _querySync = function ( snapshots , query , options ) {
68
+ if ( query . filter ) {
69
+ snapshots = snapshots . filter ( function ( snapshot ) {
70
+ for ( var queryKey in query . filter ) {
71
+ // This fake only supports simple property equality filters, so
72
+ // throw an error on Mongo-like filter properties with dots.
73
+ if ( queryKey . includes ( '.' ) ) {
74
+ throw new Error ( 'Only simple property filters are supported, got:' , queryKey ) ;
75
+ }
76
+ if ( snapshot . data [ queryKey ] !== query . filter [ queryKey ] ) {
77
+ return false ;
78
+ }
79
+ }
80
+ return true ;
81
+ } ) ;
82
+ }
83
+
84
+ if ( query . sort ) {
85
+ if ( ! Array . isArray ( query . sort ) ) {
86
+ throw new Error ( 'query.sort must be an array' ) ;
87
+ }
88
+ if ( query . sort . length ) {
89
+ snapshots . sort ( snapshotComparator ( query . sort ) ) ;
90
+ }
91
+ }
92
+
93
+ return { snapshots : snapshots } ;
94
+ } ;
95
+
96
+ // sortProperties is an array whose items are each [propertyName, direction].
97
+ function snapshotComparator ( sortProperties ) {
98
+ return function ( snapshotA , snapshotB ) {
99
+ for ( var i = 0 ; i < sortProperties . length ; i ++ ) {
100
+ var sortProperty = sortProperties [ i ] ;
101
+ var sortKey = sortProperty [ 0 ] ;
102
+ var sortDirection = sortProperty [ 1 ] ;
103
+
104
+ var aPropVal = snapshotA . data [ sortKey ] ;
105
+ var bPropVal = snapshotB . data [ sortKey ] ;
106
+ if ( aPropVal < bPropVal ) {
107
+ return - 1 * sortDirection ;
108
+ } else if ( aPropVal > bPropVal ) {
109
+ return sortDirection ;
110
+ } else if ( aPropVal === bPropVal ) {
111
+ continue ;
112
+ } else {
113
+ throw new Error ( 'Could not compare ' + aPropVal + ' and ' + bPropVal ) ;
114
+ }
115
+ }
116
+ return 0 ;
117
+ } ;
118
+ }
119
+
120
+ // Run all the DB-based tests against the BasicQueryableMemoryDB.
62
121
require ( './db' ) ( {
63
122
create : function ( callback ) {
64
- var db = new ShareDbMingo ( ) ;
123
+ var db = new BasicQueryableMemoryDB ( ) ;
65
124
callback ( null , db ) ;
66
125
} ,
67
- getQuery : getQuery
126
+ getQuery : function ( options ) {
127
+ return { filter : options . query , sort : options . sort } ;
128
+ }
68
129
} ) ;
0 commit comments