viewdb 0.5.11 → 0.6.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/lib/observe.js CHANGED
@@ -1,53 +1,53 @@
1
1
  var _ = require('lodash');
2
2
  var merge = require('./merger');
3
3
 
4
- var logger = {
5
- added: function() { console.log("added: "); console.log(arguments); },
6
- removed: function() { console.log("removed: "); console.log(arguments); },
7
- changed: function() { console.log("changed: "); console.log(arguments); },
8
- moved: function() { console.log("moved: "); console.log(arguments); },
9
- }
4
+ var Observer = function (query, queryOptions, collection, options) {
5
+ this._query = query;
6
+ this._queryOptions = queryOptions;
10
7
 
11
- var Observer = function(query, queryOptions, collection, options) {
12
- this._query = query;
13
- this._queryOptions = queryOptions;
14
-
15
- this._options = options;
16
- //this._options = logger;
17
- this._collection = collection;
18
- this._cache = [];
19
- var self = this;
20
- var listener = function() {
21
- self.refresh();
22
- };
23
- collection.on("change", listener);
24
- this.refresh(true);
25
- return {
26
- stop: function() {
27
- this._cache = null;
28
- collection.removeListener("change", listener);
29
- }
30
- }
31
- }
8
+ this._options = options;
9
+ //this._options = logger;
10
+ this._collection = collection;
11
+ this._cache = [];
12
+ var self = this;
13
+ var listener = function () {
14
+ self.refresh();
15
+ };
16
+ collection.on('change', listener);
17
+ this.refresh(true);
18
+ return {
19
+ stop: function () {
20
+ self._cache = null;
21
+ collection.removeListener('change', listener);
22
+ }
23
+ };
24
+ };
32
25
 
33
- Observer.prototype.refresh = function(initial) {
34
- var self = this;
26
+ Observer.prototype.refresh = function (initial) {
27
+ var self = this;
35
28
 
36
- this._collection._getDocuments(this._query, function(err, result) {
37
- if(initial && self._options.init) {
38
- self._cache = result;
39
- self._options.init(result);
40
- } else {
29
+ this._collection._getDocuments(this._query, function (err, result) {
30
+ if (initial && self._options.init) {
31
+ self._cache = result;
32
+ self._options.init(result);
33
+ } else {
41
34
  var old = self._cache;
42
35
 
43
- self._cache = merge(old, result, _.defaults({
44
- comparatorId: function (a, b) {
45
- return _.get(a, '_id') === _.get(b, '_id')
46
- }
47
- }, self._options));
36
+ self._cache = merge(
37
+ old,
38
+ result,
39
+ _.defaults(
40
+ {
41
+ comparatorId: function (a, b) {
42
+ return _.get(a, '_id') === _.get(b, '_id');
43
+ }
44
+ },
45
+ self._options
46
+ )
47
+ );
48
48
  //rewind cursor for next query...
49
49
  }
50
- });
51
- }
50
+ });
51
+ };
52
52
 
53
53
  module.exports = Observer;
@@ -59,10 +59,10 @@ var ViewDBTimestampPlugin = function (viewDb) {
59
59
  }
60
60
  clonedUpdate.$set = set;
61
61
  oldFindAndModify.apply(this, [query, sort, clonedUpdate, options, cb]);
62
- }
62
+ };
63
63
  }
64
64
  return coll;
65
- }
65
+ };
66
66
  };
67
67
 
68
68
  module.exports = ViewDBTimestampPlugin;
@@ -10,8 +10,7 @@ var ViewDBVersioningPlugin = function (viewDb) {
10
10
  var oldSave = coll.save;
11
11
  coll.save = function (docs, options) {
12
12
  var newdocs = docs;
13
- if (!(options && options.skipVersioning))
14
- {
13
+ if (!(options && options.skipVersioning)) {
15
14
  if (!_.isArray(docs)) {
16
15
  newdocs = [docs];
17
16
  }
@@ -25,8 +24,7 @@ var ViewDBVersioningPlugin = function (viewDb) {
25
24
 
26
25
  var oldInsert = coll.insert;
27
26
  coll.insert = function (docs, options) {
28
- if (!(options && options.skipVersioning))
29
- {
27
+ if (!(options && options.skipVersioning)) {
30
28
  if (!_.isArray(docs)) {
31
29
  docs = [docs];
32
30
  }
@@ -36,7 +34,7 @@ var ViewDBVersioningPlugin = function (viewDb) {
36
34
  }
37
35
  }
38
36
  oldInsert.apply(this, arguments);
39
- }
37
+ };
40
38
 
41
39
  var oldFindAndModify = coll.findAndModify;
42
40
  coll.findAndModify = function (query, sort, update, options, cb) {
@@ -49,11 +47,11 @@ var ViewDBVersioningPlugin = function (viewDb) {
49
47
  }
50
48
  }
51
49
  oldFindAndModify.apply(this, arguments);
52
- }
50
+ };
53
51
  }
54
52
  return coll;
55
- }
56
- }
53
+ };
54
+ };
57
55
 
58
56
  function _getVersion(version) {
59
57
  var newVersion;
@@ -66,4 +64,4 @@ function _getVersion(version) {
66
64
  return newVersion;
67
65
  }
68
66
 
69
- module.exports = ViewDBVersioningPlugin;
67
+ module.exports = ViewDBVersioningPlugin;
package/lib/viewdb.js CHANGED
@@ -1,23 +1,22 @@
1
1
  var Promise = require('bluebird');
2
2
  var InMemoryStore = require('./inmemory/store');
3
3
 
4
- var ViewDB = function(store) {
5
- this._store = store || new InMemoryStore();
4
+ var ViewDB = function (store) {
5
+ this._store = store || new InMemoryStore();
6
6
  };
7
7
 
8
- ViewDB.prototype.open = function() {
9
- if(this._store.open) {
10
- return this._store.open().then(function() {
11
- return this
12
- });
13
- }
14
- else {
15
- return Promise.resolve(this);
16
- }
8
+ ViewDB.prototype.open = function () {
9
+ if (this._store.open) {
10
+ return this._store.open().then(function () {
11
+ return this;
12
+ });
13
+ } else {
14
+ return Promise.resolve(this);
15
+ }
17
16
  };
18
17
 
19
- ViewDB.prototype.collection = function(collectionName, callback) {
20
- return this._store.collection(collectionName, callback);
18
+ ViewDB.prototype.collection = function (collectionName, callback) {
19
+ return this._store.collection(collectionName, callback);
21
20
  };
22
21
 
23
22
  module.exports = ViewDB;
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "viewdb",
3
- "version": "0.5.11",
3
+ "version": "0.6.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "directories": {
7
7
  "test": "test"
8
8
  },
9
9
  "scripts": {
10
- "test": "mocha test --recursive",
11
- "test.watch": "mocha test --recursive --watch",
12
- "coverage": "istanbul cover node_modules/mocha/bin/_mocha -- --recursive test -u exports -R spec",
13
- "coverage-start": "istanbul cover node_modules/mocha/bin/_mocha test --recursive -- -u exports -R spec && start coverage/lcov-report/index.html",
10
+ "test": "jest test",
11
+ "test.watch": "jest test --watch",
12
+ "coverage": "jest test --collectCoverage",
13
+ "coverage-start": "npm run coverage && start coverage/lcov-report/index.html",
14
+ "format": "prettier --write lib test",
14
15
  "jshint": "jshint ./lib",
15
16
  "jscs": "jscs -p google ./lib"
16
17
  },
@@ -22,15 +23,16 @@
22
23
  "author": "",
23
24
  "license": "MIT",
24
25
  "devDependencies": {
25
- "istanbul": "^0.3.2",
26
+ "es-jest": "^2.1.0",
27
+ "jest": "^29.5.0",
26
28
  "jshint": "^2.5.10",
27
- "mocha": "^2.0.1",
28
- "should": "^4.1.0"
29
+ "prettier": "^2.8.7",
30
+ "prettier-config-surikaterna": "^1.0.1"
29
31
  },
30
32
  "dependencies": {
31
33
  "bluebird": "^2.9.30",
32
- "kuery": "^0.5.3",
33
- "lodash": "^4.13.1",
34
+ "kuery": "^0.5.5",
35
+ "lodash": "^4.17.21",
34
36
  "node-uuid": "^1.4.3"
35
37
  }
36
38
  }
package/test/cursor.js CHANGED
@@ -1,86 +1,101 @@
1
1
  var Cursor = require('../lib/cursor');
2
- var should = require('should');
3
2
  var ViewDB = require('..');
4
3
 
5
- describe('Cursor', function () {
6
- it('#toArray', function (done) {
4
+ describe('Cursor', () => {
5
+ it('#toArray', (done) => {
7
6
  var cursor = new Cursor(null, {}, null, function (query, callback) {
8
7
  callback(null, [1, 2, 3, 4]);
9
8
  });
10
9
  cursor.toArray(function (err, result) {
11
- result.length.should.equal(4);
10
+ expect(result.length).toBe(4);
12
11
  done();
13
12
  });
14
13
  });
15
- it('#forEach', function (done) {
14
+ it('#forEach', (done) => {
16
15
  var cursor = new Cursor(null, {}, null, function (query, callback) {
17
16
  callback(null, [1, 2, 3, 4]);
18
17
  });
19
18
  var calls = 0;
20
19
  cursor.forEach(function (result) {
21
- result.should.be.ok;
20
+ expect(result).toBeTruthy();
22
21
  calls++;
23
22
  });
24
23
  setTimeout(function () {
25
- calls.should.equal(4);
24
+ expect(calls).toBe(4);
26
25
  done();
27
26
  }, 0);
28
27
  });
29
- it('#skip', function (done) {
28
+ it('#skip', (done) => {
30
29
  var db = new ViewDB();
31
30
  var collection = db.collection('documents');
32
31
  for (var i = 0; i < 10; i++) {
33
32
  collection.insert({ a: 'a', id: i });
34
33
  }
35
- collection.find({ a: 'a' }).skip(5).toArray(function (err, res) {
36
- res.length.should.equal(5);
37
- done();
38
- });
34
+ collection
35
+ .find({ a: 'a' })
36
+ .skip(5)
37
+ .toArray(function (err, res) {
38
+ expect(res.length).toBe(5);
39
+ done();
40
+ });
39
41
  });
40
- it('#limit', function (done) {
42
+ it('#limit', (done) => {
41
43
  var db = new ViewDB();
42
44
  var collection = db.collection('documents');
43
45
  for (var i = 0; i < 10; i++) {
44
46
  collection.insert({ a: 'a', id: i });
45
47
  }
46
- collection.find({ a: 'a' }).limit(9).toArray(function (err, res) {
47
- res[8].id.should.equal(8);
48
- res.length.should.equal(9);
49
- done();
50
- });
48
+ collection
49
+ .find({ a: 'a' })
50
+ .limit(9)
51
+ .toArray(function (err, res) {
52
+ expect(res[8].id).toBe(8);
53
+ expect(res.length).toBe(9);
54
+ done();
55
+ });
51
56
  });
52
- it('#sort', function (done) {
57
+ it('#sort', (done) => {
53
58
  var db = new ViewDB();
54
59
  var collection = db.collection('documents');
55
60
  for (var i = 0; i < 10; i++) {
56
61
  collection.insert({ a: 'a', id: i });
57
62
  }
58
- collection.find({}).sort({id: 1}).toArray(function (err, res) {
59
- res[0].id.should.equal(0);
60
- done();
61
- });
63
+ collection
64
+ .find({})
65
+ .sort({ id: 1 })
66
+ .toArray(function (err, res) {
67
+ expect(res[0].id).toBe(0);
68
+ done();
69
+ });
62
70
  });
63
- it('#sort desc', function (done) {
71
+ it('#sort desc', (done) => {
64
72
  var db = new ViewDB();
65
73
  var collection = db.collection('documents');
66
74
  for (var i = 0; i < 10; i++) {
67
75
  collection.insert({ a: 'a', id: i });
68
76
  }
69
- collection.find({}).sort({id: -1}).toArray(function (err, res) {
70
- res[0].id.should.equal(9);
71
- done();
72
- });
77
+ collection
78
+ .find({})
79
+ .sort({ id: -1 })
80
+ .toArray(function (err, res) {
81
+ expect(res[0].id).toBe(9);
82
+ done();
83
+ });
73
84
  });
74
- it('#skip/limit', function (done) {
85
+ it('#skip/limit', (done) => {
75
86
  var db = new ViewDB();
76
87
  var collection = db.collection('documents');
77
88
  for (var i = 0; i < 10; i++) {
78
89
  collection.insert({ a: 'a', id: i });
79
90
  }
80
- collection.find({ a: 'a' }).skip(8).limit(10).toArray(function (err, res) {
81
- res[1].id.should.equal(9);
82
- res.length.should.equal(2); // only 2 left after skipping 8/10
83
- done();
84
- });
91
+ collection
92
+ .find({ a: 'a' })
93
+ .skip(8)
94
+ .limit(10)
95
+ .toArray(function (err, res) {
96
+ expect(res[1].id).toBe(9);
97
+ expect(res.length).toBe(2); // only 2 left after skipping 8/10
98
+ done();
99
+ });
85
100
  });
86
- })
101
+ });