viewdb 0.5.12 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc +1 -0
- package/README.md +416 -1
- package/jest.config.js +11 -0
- package/lib/cursor.js +13 -9
- package/lib/index.js +1 -1
- package/lib/inmemory/collection.js +8 -10
- package/lib/inmemory/store.js +12 -12
- package/lib/merger.js +55 -56
- package/lib/observe.js +41 -41
- package/lib/plugins/viewdb_timestamp_plugin.js +2 -2
- package/lib/plugins/viewdb_versioning_plugin.js +7 -9
- package/lib/viewdb.js +12 -14
- package/package.json +12 -11
- package/test/cursor.js +50 -35
- package/test/merger.js +193 -160
- package/test/observe.js +63 -25
- package/test/plugins/viewdb_timestamp_plugin.js +39 -35
- package/test/plugins/viewdb_versioning_plugin.js +28 -28
- package/test/viewdb.js +88 -77
package/test/merger.js
CHANGED
|
@@ -1,163 +1,196 @@
|
|
|
1
|
-
var should = require('should');
|
|
2
1
|
var _ = require('lodash');
|
|
3
2
|
var merge = require('../lib/merger');
|
|
4
3
|
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
4
|
+
describe('Merger', () => {
|
|
5
|
+
it('#merge with remove element', (done) => {
|
|
6
|
+
var l1 = [{ a: 1 }, 'b', 'd'];
|
|
7
|
+
var l2 = [{ a: 1 }, 'b'];
|
|
8
|
+
|
|
9
|
+
var res = merge(l1, l2, {
|
|
10
|
+
removed: function (e) {
|
|
11
|
+
expect(e).toBe('d');
|
|
12
|
+
done();
|
|
13
|
+
},
|
|
14
|
+
comparatorId: _.isEqual,
|
|
15
|
+
comparator: _.isEqual
|
|
16
|
+
});
|
|
17
|
+
expect(_.isEqual(l2, res)).toBe(true);
|
|
18
|
+
|
|
19
|
+
// console.log(_.difference(l1,l2));
|
|
20
|
+
});
|
|
21
|
+
it('#merge with remove complex element', (done) => {
|
|
22
|
+
var l1 = [{ a: 1 }, 'b', 'd', { e: 1 }];
|
|
23
|
+
var l2 = [{ a: 1 }, 'b'];
|
|
24
|
+
var removed = [];
|
|
25
|
+
var res = merge(l1, l2, {
|
|
26
|
+
removed: function (e) {
|
|
27
|
+
removed.push(e);
|
|
28
|
+
},
|
|
29
|
+
comparatorId: _.isEqual,
|
|
30
|
+
comparator: _.isEqual
|
|
31
|
+
});
|
|
32
|
+
expect(removed.length).toBe(2);
|
|
33
|
+
expect(_.isEqual(l2, res)).toBe(true);
|
|
34
|
+
done();
|
|
35
|
+
|
|
36
|
+
// console.log(_.difference(l1,l2));
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('#merge with objects instead of arrays', (done) => {
|
|
40
|
+
var l1 = { 0: { a: 1 }, 1: 'b', 2: 'd', 3: { e: 1 } };
|
|
41
|
+
var l2 = [{ a: 1 }, 'b'];
|
|
42
|
+
var removed = [];
|
|
43
|
+
var res = merge(l1, l2, {
|
|
44
|
+
removed: function (e) {
|
|
45
|
+
removed.push(e);
|
|
46
|
+
},
|
|
47
|
+
comparatorId: _.isEqual,
|
|
48
|
+
comparator: _.isEqual
|
|
49
|
+
});
|
|
50
|
+
expect(removed.length).toBe(2);
|
|
51
|
+
expect(_.isEqual(l2, res)).toBe(true);
|
|
52
|
+
done();
|
|
53
|
+
|
|
54
|
+
// console.log(_.difference(l1,l2));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('#merge with one add element', (done) => {
|
|
58
|
+
var l1 = [{ a: 1 }, 'b'];
|
|
59
|
+
var l2 = [{ a: 1 }, 'b', 'c'];
|
|
60
|
+
|
|
61
|
+
var res = merge(l1, l2, {
|
|
62
|
+
added: function (e) {
|
|
63
|
+
expect(e).toBe('c');
|
|
64
|
+
done();
|
|
65
|
+
},
|
|
66
|
+
removed: function () {
|
|
67
|
+
done(new Error('should not be called'));
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
expect(_.isEqual(l2, res)).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
it('#merge with one complex add element', (done) => {
|
|
73
|
+
var l1 = [{ a: 1 }, 'b'];
|
|
74
|
+
var l2 = [{ a: 1 }, 'b', { c: 1 }];
|
|
75
|
+
|
|
76
|
+
var res = merge(l1, l2, {
|
|
77
|
+
added: function (e) {
|
|
78
|
+
expect(_.isEqual(e, { c: 1 })).toBe(true);
|
|
79
|
+
done();
|
|
80
|
+
},
|
|
81
|
+
removed: function () {
|
|
82
|
+
done(new Error('should not be called'));
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
expect(_.isEqual(l2, res)).toBe(true);
|
|
86
|
+
});
|
|
87
|
+
it('#merge with one move element', (done) => {
|
|
88
|
+
var l1 = [{ a: 1 }, 'b', { c: 1 }];
|
|
89
|
+
var l2 = [{ a: 1 }, { c: 1 }, 'b'];
|
|
90
|
+
var moved = [];
|
|
91
|
+
var res = merge(l1, l2, {
|
|
92
|
+
added: function () {
|
|
93
|
+
done(new Error('should not be called'));
|
|
94
|
+
},
|
|
95
|
+
removed: function () {
|
|
96
|
+
done(new Error('should not be called'));
|
|
97
|
+
},
|
|
98
|
+
moved: function (e, oldIndex, newIndex) {
|
|
99
|
+
moved.push(arguments);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
expect(moved.length).toBe(1);
|
|
103
|
+
|
|
104
|
+
expect(_.isEqual(l2, res)).toBe(true);
|
|
105
|
+
|
|
106
|
+
done();
|
|
107
|
+
});
|
|
108
|
+
it('#merge with one changing elements', (done) => {
|
|
109
|
+
var l1 = [{ _id: 1, a: 'Hello' }];
|
|
110
|
+
var l2 = [{ _id: 1, a: 'Hej' }];
|
|
111
|
+
var res = merge(l1, l2, {
|
|
112
|
+
added: function () {
|
|
113
|
+
done(new Error('should not be called'));
|
|
114
|
+
},
|
|
115
|
+
removed: function () {
|
|
116
|
+
done(new Error('should not be called'));
|
|
117
|
+
},
|
|
118
|
+
moved: function () {
|
|
119
|
+
done(new Error('should not be called'));
|
|
120
|
+
},
|
|
121
|
+
changed: function (o, n, index) {},
|
|
122
|
+
comparatorId: function (a, b) {
|
|
123
|
+
return a._id === b._id;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
expect(_.isEqual(l2, res)).toBe(true);
|
|
127
|
+
done();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('#merge true and false array', (done) => {
|
|
131
|
+
var l1 = [true, false];
|
|
132
|
+
var l2 = [false, true];
|
|
133
|
+
var res = merge(l1, l2);
|
|
134
|
+
expect(_.isEqual(l2, res)).toBe(true);
|
|
135
|
+
done();
|
|
136
|
+
});
|
|
137
|
+
it('#merge complex moves', (done) => {
|
|
138
|
+
var l1 = [
|
|
139
|
+
{ _id: 1, a: 'Hello1' },
|
|
140
|
+
{ _id: 2, a: 'Hello2' },
|
|
141
|
+
{ _id: 3, a: 'Hello3' },
|
|
142
|
+
{ _id: 4, a: 'Hello4' }
|
|
143
|
+
];
|
|
144
|
+
var l2 = [
|
|
145
|
+
{ _id: 4, a: 'Hej4' },
|
|
146
|
+
{ _id: 3, a: 'Hej3' },
|
|
147
|
+
{ _id: 2, a: 'Hej2' },
|
|
148
|
+
{ _id: 1, a: 'Hej1' }
|
|
149
|
+
];
|
|
150
|
+
|
|
151
|
+
var res = merge(
|
|
152
|
+
l1,
|
|
153
|
+
l2,
|
|
154
|
+
_.defaults(
|
|
155
|
+
{
|
|
156
|
+
comparatorId: function (a, b) {
|
|
157
|
+
return a._id === b._id;
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
{}
|
|
161
|
+
)
|
|
162
|
+
);
|
|
163
|
+
expect(_.isEqual(l2, res)).toBe(true);
|
|
164
|
+
done();
|
|
165
|
+
});
|
|
166
|
+
it('#merge complex moves and add and remove', (done) => {
|
|
167
|
+
var l1 = [
|
|
168
|
+
{ _id: 1, a: 'Hello1' },
|
|
169
|
+
{ _id: 2, a: 'Hello2' },
|
|
170
|
+
{ _id: 3, a: 'Hello3' },
|
|
171
|
+
{ _id: 4, a: 'Hello4' }
|
|
172
|
+
];
|
|
173
|
+
var l2 = [
|
|
174
|
+
{ _id: 4, a: 'Hej4' },
|
|
175
|
+
{ _id: 99, a: 'Hej99' },
|
|
176
|
+
{ _id: 2, a: 'Hej2' },
|
|
177
|
+
{ _id: 1, a: 'Hej1' },
|
|
178
|
+
{ _id: 100, a: 'Hej100' }
|
|
179
|
+
];
|
|
180
|
+
|
|
181
|
+
var res = merge(
|
|
182
|
+
l1,
|
|
183
|
+
l2,
|
|
184
|
+
_.defaults(
|
|
185
|
+
{
|
|
186
|
+
comparatorId: function (a, b) {
|
|
187
|
+
return a._id === b._id;
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
{}
|
|
191
|
+
)
|
|
192
|
+
);
|
|
193
|
+
expect(_.isEqual(l2, res)).toBe(true);
|
|
194
|
+
done();
|
|
195
|
+
});
|
|
196
|
+
});
|
package/test/observe.js
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
var should = require('should');
|
|
2
|
-
|
|
3
1
|
var ViewDb = require('..');
|
|
4
2
|
var _ = require('lodash');
|
|
5
3
|
|
|
6
|
-
describe('Observe',
|
|
7
|
-
it('#observe with insert',
|
|
4
|
+
describe('Observe', () => {
|
|
5
|
+
it('#observe with insert', (done) => {
|
|
8
6
|
var store = new ViewDb();
|
|
9
7
|
store.open().then(function () {
|
|
10
8
|
var cursor = store.collection('dollhouse').find({});
|
|
11
9
|
var handle = cursor.observe({
|
|
12
10
|
added: function (x) {
|
|
13
|
-
x._id.
|
|
11
|
+
expect(x._id).toBe('echo');
|
|
14
12
|
handle.stop();
|
|
15
13
|
done();
|
|
16
14
|
}
|
|
@@ -18,31 +16,32 @@ describe('Observe', function () {
|
|
|
18
16
|
store.collection('dollhouse').insert({ _id: 'echo' });
|
|
19
17
|
});
|
|
20
18
|
});
|
|
21
|
-
it('#observe with query and insert',
|
|
19
|
+
it('#observe with query and insert', (done) => {
|
|
22
20
|
var store = new ViewDb();
|
|
23
21
|
store.open().then(function () {
|
|
24
22
|
store.collection('dollhouse').insert({ _id: 'echo' });
|
|
25
23
|
var cursor = store.collection('dollhouse').find({ _id: 'echo2' });
|
|
26
|
-
|
|
24
|
+
cursor.observe({
|
|
27
25
|
added: function (x) {
|
|
28
|
-
x._id.
|
|
26
|
+
expect(x._id).toBe('echo2');
|
|
29
27
|
done();
|
|
30
28
|
}
|
|
31
29
|
});
|
|
32
30
|
store.collection('dollhouse').insert({ _id: 'echo2' });
|
|
33
31
|
});
|
|
34
32
|
});
|
|
35
|
-
it('#observe with query and update',
|
|
33
|
+
it('#observe with query and update', (done) => {
|
|
36
34
|
var store = new ViewDb();
|
|
37
35
|
store.open().then(function () {
|
|
38
36
|
var cursor = store.collection('dollhouse').find({ _id: 'echo' });
|
|
39
37
|
var handle = cursor.observe({
|
|
40
38
|
added: function (x) {
|
|
41
|
-
x.age.
|
|
42
|
-
x._id.
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
expect(x.age).toBe(10);
|
|
40
|
+
expect(x._id).toBe('echo');
|
|
41
|
+
},
|
|
42
|
+
changed: function (o, n) {
|
|
43
|
+
expect(o.age).toBe(10);
|
|
44
|
+
expect(n.age).toBe(100);
|
|
46
45
|
handle.stop();
|
|
47
46
|
done();
|
|
48
47
|
}
|
|
@@ -53,7 +52,7 @@ describe('Observe', function () {
|
|
|
53
52
|
});
|
|
54
53
|
});
|
|
55
54
|
});
|
|
56
|
-
it('#observe with query and skip',
|
|
55
|
+
it('#observe with query and skip', (done) => {
|
|
57
56
|
var store = new ViewDb();
|
|
58
57
|
store.open().then(function () {
|
|
59
58
|
store.collection('dollhouse').insert({ _id: 'echo' });
|
|
@@ -64,40 +63,40 @@ describe('Observe', function () {
|
|
|
64
63
|
cursor.limit(1);
|
|
65
64
|
var realDone = _.after(3, function () {
|
|
66
65
|
cursor.toArray(function (err, res) {
|
|
67
|
-
res.length.
|
|
66
|
+
expect(res.length).toBe(0);
|
|
68
67
|
handle.stop();
|
|
69
68
|
done();
|
|
70
|
-
})
|
|
69
|
+
});
|
|
71
70
|
});
|
|
72
71
|
var handle = cursor.observe({
|
|
73
|
-
added: function (
|
|
72
|
+
added: function () {
|
|
74
73
|
cursor.skip(++skip);
|
|
75
74
|
realDone();
|
|
76
75
|
}
|
|
77
76
|
});
|
|
78
77
|
});
|
|
79
78
|
});
|
|
80
|
-
it('#observe with no results',
|
|
79
|
+
it('#observe with no results', (done) => {
|
|
81
80
|
var store = new ViewDb();
|
|
82
81
|
store.open().then(function () {
|
|
83
82
|
var cursor = store.collection('dollhouse').find({});
|
|
84
83
|
var handle = cursor.observe({
|
|
85
84
|
init: function (coll) {
|
|
86
|
-
coll.length.
|
|
85
|
+
expect(coll.length).toBe(0);
|
|
87
86
|
handle.stop();
|
|
88
87
|
done();
|
|
89
88
|
}
|
|
90
89
|
});
|
|
91
90
|
});
|
|
92
91
|
});
|
|
93
|
-
it('#observe with init after one insert',
|
|
92
|
+
it('#observe with init after one insert', (done) => {
|
|
94
93
|
var store = new ViewDb();
|
|
95
94
|
store.collection('dollhouse').insert({ _id: 'echo' }, function () {
|
|
96
95
|
store.open().then(function () {
|
|
97
96
|
var cursor = store.collection('dollhouse').find({});
|
|
98
97
|
var handle = cursor.observe({
|
|
99
98
|
init: function (coll) {
|
|
100
|
-
coll.length.
|
|
99
|
+
expect(coll.length).toBe(1);
|
|
101
100
|
handle.stop();
|
|
102
101
|
done();
|
|
103
102
|
}
|
|
@@ -105,16 +104,16 @@ describe('Observe', function () {
|
|
|
105
104
|
});
|
|
106
105
|
});
|
|
107
106
|
});
|
|
108
|
-
it('#observe with one insert after init',
|
|
107
|
+
it('#observe with one insert after init', (done) => {
|
|
109
108
|
var store = new ViewDb();
|
|
110
109
|
store.open().then(function () {
|
|
111
110
|
var cursor = store.collection('dollhouse').find({});
|
|
112
111
|
var handle = cursor.observe({
|
|
113
112
|
init: function (coll) {
|
|
114
|
-
coll.length.
|
|
113
|
+
expect(coll.length).toBe(0);
|
|
115
114
|
},
|
|
116
115
|
added: function (a) {
|
|
117
|
-
a._id.
|
|
116
|
+
expect(a._id).toBe('echo');
|
|
118
117
|
handle.stop();
|
|
119
118
|
done();
|
|
120
119
|
}
|
|
@@ -124,4 +123,43 @@ describe('Observe', function () {
|
|
|
124
123
|
store.collection('dollhouse').insert({ _id: 'echo' });
|
|
125
124
|
}, 5);
|
|
126
125
|
});
|
|
126
|
+
it('#observe with query update', (done) => {
|
|
127
|
+
const store = new ViewDb();
|
|
128
|
+
store.open().then(() => {
|
|
129
|
+
const cursor = store.collection('dollhouse').find({});
|
|
130
|
+
const handle = cursor.observe({
|
|
131
|
+
init: (docs) => {
|
|
132
|
+
expect(docs.length).toBe(0);
|
|
133
|
+
},
|
|
134
|
+
added: (doc) => {
|
|
135
|
+
expect(doc._id).toMatch(/^echo/);
|
|
136
|
+
},
|
|
137
|
+
changed: (found, e) => {
|
|
138
|
+
expect(found).toEqual({ _id: 'echo1', name: 'marco' });
|
|
139
|
+
expect(e).toEqual({ _id: 'echo1', name: 'marco', data: 'changed' });
|
|
140
|
+
|
|
141
|
+
handle.stop();
|
|
142
|
+
done();
|
|
143
|
+
},
|
|
144
|
+
removed: (doc) => {
|
|
145
|
+
expect(doc).toEqual({ _id: 'echo3', name: 'polo' });
|
|
146
|
+
|
|
147
|
+
store.collection('dollhouse').save([{ _id: 'echo3', name: 'polo', data: 'changed' }], () => {
|
|
148
|
+
store.collection('dollhouse').save([{ _id: 'echo1', name: 'marco', data: 'changed' }]);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
store.collection('dollhouse').insert(
|
|
154
|
+
[
|
|
155
|
+
{ _id: 'echo1', name: 'marco' },
|
|
156
|
+
{ _id: 'echo2', name: 'marco' },
|
|
157
|
+
{ _id: 'echo3', name: 'polo' }
|
|
158
|
+
],
|
|
159
|
+
() => {
|
|
160
|
+
cursor.updateQuery({ name: 'marco' });
|
|
161
|
+
}
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
127
165
|
});
|