viewdb 0.9.0 → 0.11.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/test/observe.js CHANGED
@@ -1,165 +1,165 @@
1
- var ViewDb = require('..');
2
- var _ = require('lodash');
3
-
4
- describe('Observe', () => {
5
- it('#observe with insert', (done) => {
6
- var store = new ViewDb();
7
- store.open().then(function () {
8
- var cursor = store.collection('dollhouse').find({});
9
- var handle = cursor.observe({
10
- added: function (x) {
11
- expect(x._id).toBe('echo');
12
- handle.stop();
13
- done();
14
- }
15
- });
16
- store.collection('dollhouse').insert({ _id: 'echo' });
17
- });
18
- });
19
- it('#observe with query and insert', (done) => {
20
- var store = new ViewDb();
21
- store.open().then(function () {
22
- store.collection('dollhouse').insert({ _id: 'echo' });
23
- var cursor = store.collection('dollhouse').find({ _id: 'echo2' });
24
- cursor.observe({
25
- added: function (x) {
26
- expect(x._id).toBe('echo2');
27
- done();
28
- }
29
- });
30
- store.collection('dollhouse').insert({ _id: 'echo2' });
31
- });
32
- });
33
- it('#observe with query and update', (done) => {
34
- var store = new ViewDb();
35
- store.open().then(function () {
36
- var cursor = store.collection('dollhouse').find({ _id: 'echo' });
37
- var handle = cursor.observe({
38
- added: function (x) {
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);
45
- handle.stop();
46
- done();
47
- }
48
- });
49
-
50
- store.collection('dollhouse').insert({ _id: 'echo', age: 10 }, function () {
51
- store.collection('dollhouse').save({ _id: 'echo', age: 100 });
52
- });
53
- });
54
- });
55
- it('#observe with query and skip', (done) => {
56
- var store = new ViewDb();
57
- store.open().then(function () {
58
- store.collection('dollhouse').insert({ _id: 'echo' });
59
- store.collection('dollhouse').insert({ _id: 'echo2' });
60
- store.collection('dollhouse').insert({ _id: 'echo3' });
61
- var cursor = store.collection('dollhouse').find({});
62
- var skip = 0;
63
- cursor.limit(1);
64
- var realDone = _.after(3, function () {
65
- cursor.toArray(function (err, res) {
66
- expect(res.length).toBe(0);
67
- handle.stop();
68
- done();
69
- });
70
- });
71
- var handle = cursor.observe({
72
- added: function () {
73
- cursor.skip(++skip);
74
- realDone();
75
- }
76
- });
77
- });
78
- });
79
- it('#observe with no results', (done) => {
80
- var store = new ViewDb();
81
- store.open().then(function () {
82
- var cursor = store.collection('dollhouse').find({});
83
- var handle = cursor.observe({
84
- init: function (coll) {
85
- expect(coll.length).toBe(0);
86
- handle.stop();
87
- done();
88
- }
89
- });
90
- });
91
- });
92
- it('#observe with init after one insert', (done) => {
93
- var store = new ViewDb();
94
- store.collection('dollhouse').insert({ _id: 'echo' }, function () {
95
- store.open().then(function () {
96
- var cursor = store.collection('dollhouse').find({});
97
- var handle = cursor.observe({
98
- init: function (coll) {
99
- expect(coll.length).toBe(1);
100
- handle.stop();
101
- done();
102
- }
103
- });
104
- });
105
- });
106
- });
107
- it('#observe with one insert after init', (done) => {
108
- var store = new ViewDb();
109
- store.open().then(function () {
110
- var cursor = store.collection('dollhouse').find({});
111
- var handle = cursor.observe({
112
- init: function (coll) {
113
- expect(coll.length).toBe(0);
114
- },
115
- added: function (a) {
116
- expect(a._id).toBe('echo');
117
- handle.stop();
118
- done();
119
- }
120
- });
121
- });
122
- setTimeout(function () {
123
- store.collection('dollhouse').insert({ _id: 'echo' });
124
- }, 5);
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
- });
165
- });
1
+ var ViewDb = require('..');
2
+ var _ = require('lodash');
3
+
4
+ describe('Observe', () => {
5
+ it('#observe with insert', (done) => {
6
+ var store = new ViewDb();
7
+ store.open().then(function () {
8
+ var cursor = store.collection('dollhouse').find({});
9
+ var handle = cursor.observe({
10
+ added: function (x) {
11
+ expect(x._id).toBe('echo');
12
+ handle.stop();
13
+ done();
14
+ }
15
+ });
16
+ store.collection('dollhouse').insert({ _id: 'echo' });
17
+ });
18
+ });
19
+ it('#observe with query and insert', (done) => {
20
+ var store = new ViewDb();
21
+ store.open().then(function () {
22
+ store.collection('dollhouse').insert({ _id: 'echo' });
23
+ var cursor = store.collection('dollhouse').find({ _id: 'echo2' });
24
+ cursor.observe({
25
+ added: function (x) {
26
+ expect(x._id).toBe('echo2');
27
+ done();
28
+ }
29
+ });
30
+ store.collection('dollhouse').insert({ _id: 'echo2' });
31
+ });
32
+ });
33
+ it('#observe with query and update', (done) => {
34
+ var store = new ViewDb();
35
+ store.open().then(function () {
36
+ var cursor = store.collection('dollhouse').find({ _id: 'echo' });
37
+ var handle = cursor.observe({
38
+ added: function (x) {
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);
45
+ handle.stop();
46
+ done();
47
+ }
48
+ });
49
+
50
+ store.collection('dollhouse').insert({ _id: 'echo', age: 10 }, function () {
51
+ store.collection('dollhouse').save({ _id: 'echo', age: 100 });
52
+ });
53
+ });
54
+ });
55
+ it('#observe with query and skip', (done) => {
56
+ var store = new ViewDb();
57
+ store.open().then(function () {
58
+ store.collection('dollhouse').insert({ _id: 'echo' });
59
+ store.collection('dollhouse').insert({ _id: 'echo2' });
60
+ store.collection('dollhouse').insert({ _id: 'echo3' });
61
+ var cursor = store.collection('dollhouse').find({});
62
+ var skip = 0;
63
+ cursor.limit(1);
64
+ var realDone = _.after(3, function () {
65
+ cursor.toArray(function (err, res) {
66
+ expect(res.length).toBe(0);
67
+ handle.stop();
68
+ done();
69
+ });
70
+ });
71
+ var handle = cursor.observe({
72
+ added: function () {
73
+ cursor.skip(++skip);
74
+ realDone();
75
+ }
76
+ });
77
+ });
78
+ });
79
+ it('#observe with no results', (done) => {
80
+ var store = new ViewDb();
81
+ store.open().then(function () {
82
+ var cursor = store.collection('dollhouse').find({});
83
+ var handle = cursor.observe({
84
+ init: function (coll) {
85
+ expect(coll.length).toBe(0);
86
+ handle.stop();
87
+ done();
88
+ }
89
+ });
90
+ });
91
+ });
92
+ it('#observe with init after one insert', (done) => {
93
+ var store = new ViewDb();
94
+ store.collection('dollhouse').insert({ _id: 'echo' }, function () {
95
+ store.open().then(function () {
96
+ var cursor = store.collection('dollhouse').find({});
97
+ var handle = cursor.observe({
98
+ init: function (coll) {
99
+ expect(coll.length).toBe(1);
100
+ handle.stop();
101
+ done();
102
+ }
103
+ });
104
+ });
105
+ });
106
+ });
107
+ it('#observe with one insert after init', (done) => {
108
+ var store = new ViewDb();
109
+ store.open().then(function () {
110
+ var cursor = store.collection('dollhouse').find({});
111
+ var handle = cursor.observe({
112
+ init: function (coll) {
113
+ expect(coll.length).toBe(0);
114
+ },
115
+ added: function (a) {
116
+ expect(a._id).toBe('echo');
117
+ handle.stop();
118
+ done();
119
+ }
120
+ });
121
+ });
122
+ setTimeout(function () {
123
+ store.collection('dollhouse').insert({ _id: 'echo' });
124
+ }, 5);
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
+ });
165
+ });
@@ -1,159 +1,159 @@
1
- var ViewDb = require('../..');
2
- var ViewDbTimestampPlugin = require('../..').plugins.TimestampPlugin;
3
- var ViewDBVersioningPlugin = require('../..').plugins.VersioningPlugin;
4
-
5
- describe('Viewdb timestamp plugin', () => {
6
- it('should add changeDateTime and createDateTime timestamp on insert', (done) => {
7
- var viewDb = new ViewDb();
8
- new ViewDbTimestampPlugin(viewDb);
9
- new ViewDBVersioningPlugin(viewDb);
10
- var obj = { id: '123' };
11
- var collection = viewDb.collection('test');
12
- var currentTime = new Date().valueOf();
13
-
14
- // wait 1ms until update operation to check for lastModified updated
15
- setTimeout(function () {
16
- collection.insert(obj, function () {
17
- collection.find({ id: '123' }).toArray(function (err, objects) {
18
- var object = objects[0];
19
- expect(object.createDateTime).exists;
20
- if (currentTime < object.createDateTime) {
21
- done();
22
- } else {
23
- done(new Error('Timestamp was not renewed'));
24
- }
25
- });
26
- });
27
- }, 5);
28
- });
29
- it('should add changeDateTime and createDateTime timestamp on bulk insert', (done) => {
30
- var viewDb = new ViewDb();
31
- new ViewDbTimestampPlugin(viewDb);
32
- new ViewDBVersioningPlugin(viewDb);
33
- var collection = viewDb.collection('test');
34
- var currentTime = new Date().valueOf();
35
-
36
- // wait 1ms until update operation to check for lastModified updated
37
- setTimeout(function () {
38
- collection.insert([{ _id: '123' }, { _id: '999' }], function () {
39
- collection.find({}).toArray(function (err, objects) {
40
- var hasError = false;
41
- objects.forEach(function (object) {
42
- expect(object.createDateTime).exists;
43
- if (currentTime >= object.createDateTime) {
44
- hasError = true;
45
- }
46
- });
47
- (hasError && done(new Error('Timestamp was not renewed'))) || done();
48
- });
49
- });
50
- });
51
- });
52
- it('should update changeDateTime on builk save', (done) => {
53
- var viewDb = new ViewDb();
54
- new ViewDbTimestampPlugin(viewDb);
55
- new ViewDBVersioningPlugin(viewDb);
56
- var collection = viewDb.collection('test');
57
-
58
- collection.insert([{ _id: '123' }, { _id: '999' }]);
59
- collection.find({}).toArray(function (err, objects) {
60
- var insertTime = objects[0].createDateTime;
61
- var updateTime = objects[0].changeDateTime;
62
- expect(insertTime).toBeDefined();
63
- expect(insertTime).toBe(updateTime);
64
- setTimeout(function () {
65
- collection.save(
66
- [
67
- { _id: '123', name: 'Pelle', createDateTime: insertTime, changeDateTime: insertTime },
68
- { _id: '999', name: 'Kalle', createDateTime: insertTime, changeDateTime: insertTime }
69
- ],
70
- function () {
71
- collection.find({}).toArray(function (err, objects) {
72
- objects.forEach(function (object) {
73
- expect(object.createDateTime).toBe(insertTime);
74
- expect(object.changeDateTime).toBeGreaterThan(insertTime);
75
- });
76
- done();
77
- });
78
- }
79
- );
80
- }, 100);
81
- });
82
-
83
- // wait 1ms until update operation to check for lastModified updated
84
- });
85
- it('should update changeDateTime on save', (done) => {
86
- var viewDb = new ViewDb();
87
- new ViewDbTimestampPlugin(viewDb);
88
- new ViewDBVersioningPlugin(viewDb);
89
- var obj = { id: '123' };
90
- var insertTime;
91
- var collection = viewDb.collection('test');
92
-
93
- collection.insert(obj);
94
- collection.find({ id: '123' }).toArray(function (err, objects) {
95
- var object = objects[0];
96
- insertTime = object.createDateTime;
97
- });
98
-
99
- // wait 1ms until update operation to check for changeDateTime updated
100
- setTimeout(function () {
101
- obj.name = 'Pelle';
102
- collection.save(obj);
103
- collection.find({ id: '123' }).toArray(function (err, objects) {
104
- var object = objects[0];
105
- expect(object.createDateTime).toBe(insertTime);
106
- expect(object.changeDateTime).toBeGreaterThan(insertTime);
107
- done();
108
- });
109
- }, 1);
110
- });
111
-
112
- it('should skip changing timestamp with skipTimestamp option on save', (done) => {
113
- var viewDb = new ViewDb();
114
- new ViewDbTimestampPlugin(viewDb);
115
- new ViewDBVersioningPlugin(viewDb);
116
- var obj = { id: '123' };
117
- var insertTime;
118
- var collection = viewDb.collection('test');
119
-
120
- collection.insert(obj);
121
- collection.find({ id: '123' }).toArray(function (err, objects) {
122
- var object = objects[0];
123
- insertTime = object.createDateTime;
124
- });
125
-
126
- // wait 1ms until update operation to check for changeDateTime updated
127
- setTimeout(function () {
128
- obj.name = 'Pelle';
129
- collection.save(obj, { skipTimestamp: true }, function () {
130
- collection.find({ id: '123' }).toArray(function (err, objects) {
131
- var object = objects[0];
132
- expect(object.createDateTime).toBe(insertTime);
133
- expect(object.changeDateTime).toBe(insertTime);
134
- done();
135
- });
136
- });
137
- }, 1);
138
- });
139
-
140
- it('should work together with version plugin', (done) => {
141
- var viewDb = new ViewDb();
142
- new ViewDbTimestampPlugin(viewDb);
143
- new ViewDBVersioningPlugin(viewDb);
144
- var obj = { id: '123' };
145
- var collection = viewDb.collection('test');
146
- collection.insert(obj);
147
- obj.name = 'Pelle';
148
- obj.version = undefined;
149
- collection.save(obj);
150
- collection.find({ id: '123' }).toArray(function (err, objects) {
151
- var object = objects[0];
152
- expect(object.version).toBe(0);
153
- expect(object.name).toBe('Pelle');
154
- expect(object.createDateTime).exists;
155
- expect(object.changeDateTime).exists;
156
- done();
157
- });
158
- });
159
- });
1
+ var ViewDb = require('../..');
2
+ var ViewDbTimestampPlugin = require('../..').plugins.TimestampPlugin;
3
+ var ViewDBVersioningPlugin = require('../..').plugins.VersioningPlugin;
4
+
5
+ describe('Viewdb timestamp plugin', () => {
6
+ it('should add changeDateTime and createDateTime timestamp on insert', (done) => {
7
+ var viewDb = new ViewDb();
8
+ new ViewDbTimestampPlugin(viewDb);
9
+ new ViewDBVersioningPlugin(viewDb);
10
+ var obj = { id: '123' };
11
+ var collection = viewDb.collection('test');
12
+ var currentTime = new Date().valueOf();
13
+
14
+ // wait 1ms until update operation to check for lastModified updated
15
+ setTimeout(function () {
16
+ collection.insert(obj, function () {
17
+ collection.find({ id: '123' }).toArray(function (err, objects) {
18
+ var object = objects[0];
19
+ expect(object.createDateTime).exists;
20
+ if (currentTime < object.createDateTime) {
21
+ done();
22
+ } else {
23
+ done(new Error('Timestamp was not renewed'));
24
+ }
25
+ });
26
+ });
27
+ }, 5);
28
+ });
29
+ it('should add changeDateTime and createDateTime timestamp on bulk insert', (done) => {
30
+ var viewDb = new ViewDb();
31
+ new ViewDbTimestampPlugin(viewDb);
32
+ new ViewDBVersioningPlugin(viewDb);
33
+ var collection = viewDb.collection('test');
34
+ var currentTime = new Date().valueOf();
35
+
36
+ // wait 1ms until update operation to check for lastModified updated
37
+ setTimeout(function () {
38
+ collection.insert([{ _id: '123' }, { _id: '999' }], function () {
39
+ collection.find({}).toArray(function (err, objects) {
40
+ var hasError = false;
41
+ objects.forEach(function (object) {
42
+ expect(object.createDateTime).exists;
43
+ if (currentTime >= object.createDateTime) {
44
+ hasError = true;
45
+ }
46
+ });
47
+ (hasError && done(new Error('Timestamp was not renewed'))) || done();
48
+ });
49
+ });
50
+ });
51
+ });
52
+ it('should update changeDateTime on builk save', (done) => {
53
+ var viewDb = new ViewDb();
54
+ new ViewDbTimestampPlugin(viewDb);
55
+ new ViewDBVersioningPlugin(viewDb);
56
+ var collection = viewDb.collection('test');
57
+
58
+ collection.insert([{ _id: '123' }, { _id: '999' }]);
59
+ collection.find({}).toArray(function (err, objects) {
60
+ var insertTime = objects[0].createDateTime;
61
+ var updateTime = objects[0].changeDateTime;
62
+ expect(insertTime).toBeDefined();
63
+ expect(insertTime).toBe(updateTime);
64
+ setTimeout(function () {
65
+ collection.save(
66
+ [
67
+ { _id: '123', name: 'Pelle', createDateTime: insertTime, changeDateTime: insertTime },
68
+ { _id: '999', name: 'Kalle', createDateTime: insertTime, changeDateTime: insertTime }
69
+ ],
70
+ function () {
71
+ collection.find({}).toArray(function (err, objects) {
72
+ objects.forEach(function (object) {
73
+ expect(object.createDateTime).toBe(insertTime);
74
+ expect(object.changeDateTime).toBeGreaterThan(insertTime);
75
+ });
76
+ done();
77
+ });
78
+ }
79
+ );
80
+ }, 100);
81
+ });
82
+
83
+ // wait 1ms until update operation to check for lastModified updated
84
+ });
85
+ it('should update changeDateTime on save', (done) => {
86
+ var viewDb = new ViewDb();
87
+ new ViewDbTimestampPlugin(viewDb);
88
+ new ViewDBVersioningPlugin(viewDb);
89
+ var obj = { id: '123' };
90
+ var insertTime;
91
+ var collection = viewDb.collection('test');
92
+
93
+ collection.insert(obj);
94
+ collection.find({ id: '123' }).toArray(function (err, objects) {
95
+ var object = objects[0];
96
+ insertTime = object.createDateTime;
97
+ });
98
+
99
+ // wait 1ms until update operation to check for changeDateTime updated
100
+ setTimeout(function () {
101
+ obj.name = 'Pelle';
102
+ collection.save(obj);
103
+ collection.find({ id: '123' }).toArray(function (err, objects) {
104
+ var object = objects[0];
105
+ expect(object.createDateTime).toBe(insertTime);
106
+ expect(object.changeDateTime).toBeGreaterThan(insertTime);
107
+ done();
108
+ });
109
+ }, 1);
110
+ });
111
+
112
+ it('should skip changing timestamp with skipTimestamp option on save', (done) => {
113
+ var viewDb = new ViewDb();
114
+ new ViewDbTimestampPlugin(viewDb);
115
+ new ViewDBVersioningPlugin(viewDb);
116
+ var obj = { id: '123' };
117
+ var insertTime;
118
+ var collection = viewDb.collection('test');
119
+
120
+ collection.insert(obj);
121
+ collection.find({ id: '123' }).toArray(function (err, objects) {
122
+ var object = objects[0];
123
+ insertTime = object.createDateTime;
124
+ });
125
+
126
+ // wait 1ms until update operation to check for changeDateTime updated
127
+ setTimeout(function () {
128
+ obj.name = 'Pelle';
129
+ collection.save(obj, { skipTimestamp: true }, function () {
130
+ collection.find({ id: '123' }).toArray(function (err, objects) {
131
+ var object = objects[0];
132
+ expect(object.createDateTime).toBe(insertTime);
133
+ expect(object.changeDateTime).toBe(insertTime);
134
+ done();
135
+ });
136
+ });
137
+ }, 1);
138
+ });
139
+
140
+ it('should work together with version plugin', (done) => {
141
+ var viewDb = new ViewDb();
142
+ new ViewDbTimestampPlugin(viewDb);
143
+ new ViewDBVersioningPlugin(viewDb);
144
+ var obj = { id: '123' };
145
+ var collection = viewDb.collection('test');
146
+ collection.insert(obj);
147
+ obj.name = 'Pelle';
148
+ obj.version = undefined;
149
+ collection.save(obj);
150
+ collection.find({ id: '123' }).toArray(function (err, objects) {
151
+ var object = objects[0];
152
+ expect(object.version).toBe(0);
153
+ expect(object.name).toBe('Pelle');
154
+ expect(object.createDateTime).exists;
155
+ expect(object.changeDateTime).exists;
156
+ done();
157
+ });
158
+ });
159
+ });