viewdb 0.11.0 → 0.13.0-beta.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/dist/cursor.d.ts +23 -0
- package/dist/cursor.d.ts.map +1 -0
- package/dist/cursor.js +81 -0
- package/dist/cursor.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/inmemory/collection.d.ts +20 -0
- package/dist/inmemory/collection.d.ts.map +1 -0
- package/dist/inmemory/collection.js +111 -0
- package/dist/inmemory/collection.js.map +1 -0
- package/dist/inmemory/store.d.ts +8 -0
- package/dist/inmemory/store.d.ts.map +1 -0
- package/dist/inmemory/store.js +21 -0
- package/dist/inmemory/store.js.map +1 -0
- package/dist/merger.d.ts +4 -0
- package/dist/merger.d.ts.map +1 -0
- package/dist/merger.js +139 -0
- package/dist/merger.js.map +1 -0
- package/dist/observe.d.ts +18 -0
- package/dist/observe.d.ts.map +1 -0
- package/dist/observe.js +93 -0
- package/dist/observe.js.map +1 -0
- package/dist/plugins/index.d.ts +4 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +8 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/plugins/viewdb_timestamp_plugin.d.ts +3 -0
- package/dist/plugins/viewdb_timestamp_plugin.d.ts.map +1 -0
- package/dist/plugins/viewdb_timestamp_plugin.js +92 -0
- package/dist/plugins/viewdb_timestamp_plugin.js.map +1 -0
- package/dist/plugins/viewdb_versioning_plugin.d.ts +3 -0
- package/dist/plugins/viewdb_versioning_plugin.d.ts.map +1 -0
- package/dist/plugins/viewdb_versioning_plugin.js +83 -0
- package/dist/plugins/viewdb_versioning_plugin.js.map +1 -0
- package/dist/types.d.ts +76 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/viewdb.d.ts +13 -0
- package/dist/viewdb.d.ts.map +1 -0
- package/dist/viewdb.js +24 -0
- package/dist/viewdb.js.map +1 -0
- package/package.json +19 -12
- package/.prettierrc +0 -1
- package/index.js +0 -1
- package/jest.config.js +0 -11
- package/lib/cursor.js +0 -84
- package/lib/index.js +0 -6
- package/lib/inmemory/collection.js +0 -112
- package/lib/inmemory/store.js +0 -19
- package/lib/merger.js +0 -62
- package/lib/observe.js +0 -53
- package/lib/plugins/index.js +0 -2
- package/lib/plugins/viewdb_timestamp_plugin.js +0 -106
- package/lib/plugins/viewdb_versioning_plugin.js +0 -93
- package/lib/viewdb.js +0 -21
- package/test/cursor.js +0 -101
- package/test/merger.js +0 -196
- package/test/observe.js +0 -165
- package/test/plugins/viewdb_timestamp_plugin.js +0 -159
- package/test/plugins/viewdb_versioning_plugin.js +0 -112
- package/test/viewdb.js +0 -210
package/test/observe.js
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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,112 +0,0 @@
|
|
|
1
|
-
var _ = require('lodash');
|
|
2
|
-
|
|
3
|
-
var ViewDb = require('../..');
|
|
4
|
-
var ViewDbVersioningPlugin = require('../..').plugins.VersioningPlugin;
|
|
5
|
-
|
|
6
|
-
describe('Viewdb versioning plugin', () => {
|
|
7
|
-
it('should add version on insert', (done) => {
|
|
8
|
-
var viewDb = new ViewDb();
|
|
9
|
-
new ViewDbVersioningPlugin(viewDb);
|
|
10
|
-
var obj = { id: '123' };
|
|
11
|
-
|
|
12
|
-
var collection = viewDb.collection('test');
|
|
13
|
-
collection.insert(obj);
|
|
14
|
-
|
|
15
|
-
collection.find({ id: '123' }).toArray(function (err, objects) {
|
|
16
|
-
var object = objects[0];
|
|
17
|
-
|
|
18
|
-
expect(object.version).toBe(0);
|
|
19
|
-
done();
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
it('should add version on builk insert', (done) => {
|
|
23
|
-
var viewDb = new ViewDb();
|
|
24
|
-
new ViewDbVersioningPlugin(viewDb);
|
|
25
|
-
|
|
26
|
-
var collection = viewDb.collection('test');
|
|
27
|
-
collection.insert([{ id: '123' }, { id: '999' }]);
|
|
28
|
-
|
|
29
|
-
collection.find({}).toArray(function (err, objects) {
|
|
30
|
-
expect(objects[0].version).toBe(0);
|
|
31
|
-
expect(objects[1].version).toBe(0);
|
|
32
|
-
done();
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
it('should increase version on save', (done) => {
|
|
36
|
-
var viewDb = new ViewDb();
|
|
37
|
-
new ViewDbVersioningPlugin(viewDb);
|
|
38
|
-
var obj = { id: '123' };
|
|
39
|
-
|
|
40
|
-
var collection = viewDb.collection('test');
|
|
41
|
-
collection.insert(obj);
|
|
42
|
-
obj.name = 'Pelle';
|
|
43
|
-
collection.save(obj);
|
|
44
|
-
|
|
45
|
-
collection.find({ id: '123' }).toArray(function (err, objects) {
|
|
46
|
-
var object = objects[0];
|
|
47
|
-
expect(object.version).toBe(1);
|
|
48
|
-
expect(object.name).toBe('Pelle');
|
|
49
|
-
done();
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should increase version on bulk save', (done) => {
|
|
54
|
-
var viewDb = new ViewDb();
|
|
55
|
-
new ViewDbVersioningPlugin(viewDb);
|
|
56
|
-
|
|
57
|
-
var collection = viewDb.collection('test');
|
|
58
|
-
collection.insert([
|
|
59
|
-
{ _id: '123', version: 10 },
|
|
60
|
-
{ _id: '999', version: 101 }
|
|
61
|
-
]);
|
|
62
|
-
collection.find({}).toArray(function (err, objects) {
|
|
63
|
-
_.forEach(objects, function (o, i) {
|
|
64
|
-
o.name = i === 0 ? 'Pelle' : 'Kalle';
|
|
65
|
-
});
|
|
66
|
-
collection.save(objects, function () {
|
|
67
|
-
collection.find({}).toArray(function (err, objects) {
|
|
68
|
-
expect(objects[0].version).toBe(12); // add 1 version for insert and one for save
|
|
69
|
-
expect(objects[0].name).toBe('Pelle');
|
|
70
|
-
expect(objects[1].version).toBe(103);
|
|
71
|
-
expect(objects[1].name).toBe('Kalle');
|
|
72
|
-
done();
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
it('should skip changing version with skipVersioning option on save', (done) => {
|
|
78
|
-
var viewDb = new ViewDb();
|
|
79
|
-
new ViewDbVersioningPlugin(viewDb);
|
|
80
|
-
var obj = { id: '123' };
|
|
81
|
-
|
|
82
|
-
var collection = viewDb.collection('test');
|
|
83
|
-
collection.insert(obj);
|
|
84
|
-
obj.name = 'Pelle';
|
|
85
|
-
collection.save(obj, { skipVersioning: true });
|
|
86
|
-
|
|
87
|
-
collection.find({ id: '123' }).toArray(function (err, objects) {
|
|
88
|
-
var object = objects[0];
|
|
89
|
-
expect(object.version).toBe(0); // still version 0
|
|
90
|
-
expect(object.name).toBe('Pelle');
|
|
91
|
-
done();
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
it('should add version on save', (done) => {
|
|
95
|
-
var viewDb = new ViewDb();
|
|
96
|
-
new ViewDbVersioningPlugin(viewDb);
|
|
97
|
-
var obj = { id: '123' };
|
|
98
|
-
|
|
99
|
-
var collection = viewDb.collection('test');
|
|
100
|
-
collection.insert(obj);
|
|
101
|
-
obj.name = 'Pelle';
|
|
102
|
-
obj.version = undefined;
|
|
103
|
-
collection.save(obj);
|
|
104
|
-
|
|
105
|
-
collection.find({ id: '123' }).toArray(function (err, objects) {
|
|
106
|
-
var object = objects[0];
|
|
107
|
-
expect(object.version).toBe(0);
|
|
108
|
-
expect(object.name).toBe('Pelle');
|
|
109
|
-
done();
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
});
|
package/test/viewdb.js
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
var ViewDB = require('..');
|
|
2
|
-
|
|
3
|
-
describe('ViewDB', () => {
|
|
4
|
-
describe('#count', () => {
|
|
5
|
-
it('should return 0 for empty collection', (done) => {
|
|
6
|
-
var db = new ViewDB();
|
|
7
|
-
var collection = db.collection('documents');
|
|
8
|
-
// Perform a total count command
|
|
9
|
-
collection.count(function (err, count) {
|
|
10
|
-
expect(count).toBe(0);
|
|
11
|
-
done();
|
|
12
|
-
});
|
|
13
|
-
});
|
|
14
|
-
});
|
|
15
|
-
describe('#insert', () => {
|
|
16
|
-
it('should store a document and include it in count', (done) => {
|
|
17
|
-
var db = new ViewDB();
|
|
18
|
-
var collection = db.collection('documents');
|
|
19
|
-
collection.insert({ a: 1 }, function (err) {
|
|
20
|
-
expect(err).toBeFalsy();
|
|
21
|
-
// Perform a total count command
|
|
22
|
-
collection.count(function (err, count) {
|
|
23
|
-
expect(count).toBe(1);
|
|
24
|
-
done();
|
|
25
|
-
//assert.equal(null, err);
|
|
26
|
-
//assert.equal(1, count);
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
it('should add id on insert if missing', (done) => {
|
|
31
|
-
var db = new ViewDB();
|
|
32
|
-
var collection = db.collection('documents');
|
|
33
|
-
collection.insert({ a: 1 }, function () {
|
|
34
|
-
collection.find({ a: 1 }).toArray(function (err, res) {
|
|
35
|
-
expect(res[0]._id).toBeDefined();
|
|
36
|
-
done();
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
it('should fail at storing a previously stored document', (done) => {
|
|
41
|
-
var db = new ViewDB();
|
|
42
|
-
var collection = db.collection('documents');
|
|
43
|
-
collection.insert({ _id: 1, a: 1 });
|
|
44
|
-
collection.insert({ _id: 1, a: 2 }, function (err) {
|
|
45
|
-
expect(err).toBeDefined();
|
|
46
|
-
done();
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('should fail at storing an empty document', (done) => {
|
|
51
|
-
var db = new ViewDB();
|
|
52
|
-
var collection = db.collection('documents');
|
|
53
|
-
collection.insert(1, function (err) {
|
|
54
|
-
expect(err).toBeDefined();
|
|
55
|
-
done();
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
it('#insert bulk should work', (done) => {
|
|
59
|
-
var db = new ViewDB();
|
|
60
|
-
var collection = db.collection('documents');
|
|
61
|
-
collection.insert([{ a: 1 }, { b: 2 }], function () {
|
|
62
|
-
collection.count(function (err, res) {
|
|
63
|
-
expect(res).toBe(2);
|
|
64
|
-
});
|
|
65
|
-
done();
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
describe('#save', () => {
|
|
70
|
-
it('should save multiple', (done) => {
|
|
71
|
-
var db = new ViewDB();
|
|
72
|
-
var collection = db.collection('documents');
|
|
73
|
-
collection.insert(
|
|
74
|
-
[
|
|
75
|
-
{ _id: 1, a: 1 },
|
|
76
|
-
{ _id: 2, b: 2 }
|
|
77
|
-
],
|
|
78
|
-
function () {
|
|
79
|
-
collection.save(
|
|
80
|
-
[
|
|
81
|
-
{ _id: 1, a: 10 },
|
|
82
|
-
{ _id: 2, b: 20 }
|
|
83
|
-
],
|
|
84
|
-
function () {
|
|
85
|
-
collection.find({}).toArray(function (err, res) {
|
|
86
|
-
expect(res.length).toBe(2);
|
|
87
|
-
expect(res[0].a).toBe(10);
|
|
88
|
-
expect(res[1].b).toBe(20);
|
|
89
|
-
done();
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
);
|
|
95
|
-
});
|
|
96
|
-
it('should add id on insert if missing', (done) => {
|
|
97
|
-
var db = new ViewDB();
|
|
98
|
-
var collection = db.collection('documents');
|
|
99
|
-
collection.save({ a: 1 });
|
|
100
|
-
collection.save({ b: 1 });
|
|
101
|
-
collection.count(function (err, result) {
|
|
102
|
-
expect(result).toBe(2);
|
|
103
|
-
done();
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
it('should add document on save', (done) => {
|
|
107
|
-
var db = new ViewDB();
|
|
108
|
-
var collection = db.collection('documents');
|
|
109
|
-
collection.save({ a: 1 }, function () {
|
|
110
|
-
collection.count(function (err, count) {
|
|
111
|
-
expect(count).toBe(1);
|
|
112
|
-
done();
|
|
113
|
-
});
|
|
114
|
-
//should.exist(ids._id);
|
|
115
|
-
//done();
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
it('should merge if id exists', (done) => {
|
|
119
|
-
var db = new ViewDB();
|
|
120
|
-
var collection = db.collection('documents');
|
|
121
|
-
collection.save({ a: 1 }, function (err, docs) {
|
|
122
|
-
docs[0]['b'] = 2;
|
|
123
|
-
collection.save(docs, function () {
|
|
124
|
-
collection.count(function (err, count) {
|
|
125
|
-
expect(count).toBe(1);
|
|
126
|
-
done();
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
describe('#find', () => {
|
|
133
|
-
it('find all documents', (done) => {
|
|
134
|
-
var db = new ViewDB();
|
|
135
|
-
var collection = db.collection('documents');
|
|
136
|
-
collection.insert({ a: 1 }, function () {
|
|
137
|
-
collection.find({}).toArray(function (err, docs) {
|
|
138
|
-
expect(docs.length).toBe(1);
|
|
139
|
-
expect(docs[0].a).toBe(1);
|
|
140
|
-
done();
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
it('find one document', (done) => {
|
|
145
|
-
var db = new ViewDB();
|
|
146
|
-
var collection = db.collection('documents');
|
|
147
|
-
collection.insert({ a: 1 }, function (err, ids) {
|
|
148
|
-
collection.find({ _id: ids[0]._id }).toArray(function (err, docs) {
|
|
149
|
-
expect(docs.length).toBe(1);
|
|
150
|
-
expect(docs[0].a).toBe(1);
|
|
151
|
-
done();
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
it('should return empty collection if query does not match', (done) => {
|
|
156
|
-
var db = new ViewDB();
|
|
157
|
-
var collection = db.collection('documents');
|
|
158
|
-
collection.insert({ a: 1 }, function () {
|
|
159
|
-
collection.find({ _id: 5 }).toArray(function (err, docs) {
|
|
160
|
-
expect(docs.length).toBe(0);
|
|
161
|
-
done();
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
describe('#remove', () => {
|
|
167
|
-
it('should remove one document matching a query', (done) => {
|
|
168
|
-
var db = new ViewDB();
|
|
169
|
-
var collection = db.collection('documents');
|
|
170
|
-
collection.insert({ a: 1, name: 'hello' }, function () {
|
|
171
|
-
collection.remove({ name: 'hello' }, null, function () {
|
|
172
|
-
collection.find({}).toArray(function (err, res) {
|
|
173
|
-
expect(res.length).toBe(0);
|
|
174
|
-
done();
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it('shouldnt do anything when no documents are matched against the query', (done) => {
|
|
181
|
-
var db = new ViewDB();
|
|
182
|
-
var collection = db.collection('documents');
|
|
183
|
-
collection.insert({ a: 1, name: 'hello' }, function () {
|
|
184
|
-
collection.remove({ name: 'world' }, null, function () {
|
|
185
|
-
collection.find({}).toArray(function (err, res) {
|
|
186
|
-
expect(res.length).toBe(1);
|
|
187
|
-
done();
|
|
188
|
-
});
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
describe('#drop', () => {
|
|
194
|
-
it('should remove all documents', (done) => {
|
|
195
|
-
var store = new ViewDB();
|
|
196
|
-
store.open().then(function () {
|
|
197
|
-
store.collection('dollhouse').insert({ _id: 'echo' });
|
|
198
|
-
store.collection('dollhouse').drop();
|
|
199
|
-
|
|
200
|
-
store
|
|
201
|
-
.collection('dollhouse')
|
|
202
|
-
.find({})
|
|
203
|
-
.toArray(function (err, results) {
|
|
204
|
-
expect(results.length).toBe(0);
|
|
205
|
-
done();
|
|
206
|
-
});
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
});
|
|
210
|
-
});
|