viewdb 0.10.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/lib/observe.js CHANGED
@@ -1,53 +1,53 @@
1
- var _ = require('lodash');
2
- var merge = require('./merger');
3
-
4
- var Observer = function (query, queryOptions, collection, options) {
5
- this._query = query;
6
- this._queryOptions = queryOptions;
7
-
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
- };
25
-
26
- Observer.prototype.refresh = function (initial) {
27
- var self = this;
28
-
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 {
34
- var old = self._cache;
35
-
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
- //rewind cursor for next query...
49
- }
50
- });
51
- };
52
-
53
- module.exports = Observer;
1
+ var _ = require('lodash');
2
+ var merge = require('./merger');
3
+
4
+ var Observer = function (query, queryOptions, collection, options) {
5
+ this._query = query;
6
+ this._queryOptions = queryOptions;
7
+
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
+ };
25
+
26
+ Observer.prototype.refresh = function (initial) {
27
+ var self = this;
28
+
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 {
34
+ var old = self._cache;
35
+
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
+ //rewind cursor for next query...
49
+ }
50
+ });
51
+ };
52
+
53
+ module.exports = Observer;
@@ -1,2 +1,2 @@
1
- module.exports.VersioningPlugin = require('./viewdb_versioning_plugin');
2
- module.exports.TimestampPlugin = require('./viewdb_timestamp_plugin');
1
+ module.exports.VersioningPlugin = require('./viewdb_versioning_plugin');
2
+ module.exports.TimestampPlugin = require('./viewdb_timestamp_plugin');
@@ -1,106 +1,106 @@
1
- var _ = require('lodash');
2
-
3
- var ViewDBTimestampPlugin = function (viewDb) {
4
- var oldCollection = viewDb.collection;
5
- viewDb.collection = function () {
6
- var coll = oldCollection.apply(this, arguments);
7
- if (!coll.__plugins_timestamp) {
8
- coll.__plugins_timestamp = true;
9
-
10
- var oldSave = coll.save;
11
- coll.save = function (docs, options) {
12
- var newdocs = docs;
13
- if (!(options && options.skipTimestamp)) {
14
- var timestamp = new Date().valueOf();
15
- if (!_.isArray(docs)) {
16
- newdocs = [docs];
17
- }
18
- for (var i = 0; i < newdocs.length; i++) {
19
- var doc = newdocs[i];
20
- if (!doc.createDateTime) {
21
- doc.createDateTime = timestamp;
22
- }
23
- doc.changeDateTime = timestamp;
24
- }
25
- }
26
- oldSave.apply(this, arguments);
27
- };
28
-
29
- var oldInsert = coll.insert;
30
- coll.insert = function (docs, options) {
31
- if (!(options && options.skipTimestamp)) {
32
- if (!_.isArray(docs)) {
33
- docs = [docs];
34
- }
35
- var timestamp = new Date().valueOf();
36
- for (var i = 0; i < docs.length; i++) {
37
- var doc = docs[i];
38
- doc.createDateTime = timestamp;
39
- doc.changeDateTime = timestamp;
40
- }
41
- }
42
- oldInsert.apply(this, arguments);
43
- };
44
-
45
- var oldFindAndModify = coll.findAndModify;
46
- coll.findAndModify = function (query, sort, update, options, cb) {
47
- var timestamp = new Date().valueOf();
48
- var clonedUpdate = _.clone(update);
49
- var setOnInsert = clonedUpdate.$setOnInsert || {};
50
- setOnInsert.createDateTime = timestamp;
51
- clonedUpdate.$setOnInsert = setOnInsert;
52
-
53
- var set = clonedUpdate.$set || {};
54
- set.changeDateTime = timestamp;
55
-
56
- // if consumer tries to $set createDateTime it will lead to conflict. remove it
57
- if (set.createDateTime) {
58
- delete set.createDateTime;
59
- }
60
- clonedUpdate.$set = set;
61
- oldFindAndModify.apply(this, [query, sort, clonedUpdate, options, cb]);
62
- };
63
-
64
- var oldUpdateMany = coll.updateMany;
65
- coll.updateMany = function (query, update, options, cb) {
66
- var timestamp = new Date().valueOf();
67
- var clonedUpdate = _.clone(update);
68
- var setOnInsert = clonedUpdate.$setOnInsert || {};
69
- setOnInsert.createDateTime = timestamp;
70
- clonedUpdate.$setOnInsert = setOnInsert;
71
-
72
- var set = clonedUpdate.$set || {};
73
- set.changeDateTime = timestamp;
74
-
75
- // if consumer tries to $set createDateTime it will lead to conflict. remove it
76
- if (set.createDateTime) {
77
- delete set.createDateTime;
78
- }
79
- clonedUpdate.$set = set;
80
- oldUpdateMany.apply(this, [query, clonedUpdate, options, cb]);
81
- };
82
-
83
- var oldUpdateOne = coll.updateOne;
84
- coll.updateOne = function (query, update, options, cb) {
85
- var timestamp = new Date().valueOf();
86
- var clonedUpdate = _.clone(update);
87
- var setOnInsert = clonedUpdate.$setOnInsert || {};
88
- setOnInsert.createDateTime = timestamp;
89
- clonedUpdate.$setOnInsert = setOnInsert;
90
-
91
- var set = clonedUpdate.$set || {};
92
- set.changeDateTime = timestamp;
93
-
94
- // if consumer tries to $set createDateTime it will lead to conflict. remove it
95
- if (set.createDateTime) {
96
- delete set.createDateTime;
97
- }
98
- clonedUpdate.$set = set;
99
- oldUpdateOne.apply(this, [query, clonedUpdate, options, cb]);
100
- };
101
- }
102
- return coll;
103
- };
104
- };
105
-
106
- module.exports = ViewDBTimestampPlugin;
1
+ var _ = require('lodash');
2
+
3
+ var ViewDBTimestampPlugin = function (viewDb) {
4
+ var oldCollection = viewDb.collection;
5
+ viewDb.collection = function () {
6
+ var coll = oldCollection.apply(this, arguments);
7
+ if (!coll.__plugins_timestamp) {
8
+ coll.__plugins_timestamp = true;
9
+
10
+ var oldSave = coll.save;
11
+ coll.save = function (docs, options) {
12
+ var newdocs = docs;
13
+ if (!(options && options.skipTimestamp)) {
14
+ var timestamp = new Date().valueOf();
15
+ if (!_.isArray(docs)) {
16
+ newdocs = [docs];
17
+ }
18
+ for (var i = 0; i < newdocs.length; i++) {
19
+ var doc = newdocs[i];
20
+ if (!doc.createDateTime) {
21
+ doc.createDateTime = timestamp;
22
+ }
23
+ doc.changeDateTime = timestamp;
24
+ }
25
+ }
26
+ oldSave.apply(this, arguments);
27
+ };
28
+
29
+ var oldInsert = coll.insert;
30
+ coll.insert = function (docs, options) {
31
+ if (!(options && options.skipTimestamp)) {
32
+ if (!_.isArray(docs)) {
33
+ docs = [docs];
34
+ }
35
+ var timestamp = new Date().valueOf();
36
+ for (var i = 0; i < docs.length; i++) {
37
+ var doc = docs[i];
38
+ doc.createDateTime = timestamp;
39
+ doc.changeDateTime = timestamp;
40
+ }
41
+ }
42
+ oldInsert.apply(this, arguments);
43
+ };
44
+
45
+ var oldFindAndModify = coll.findAndModify;
46
+ coll.findAndModify = function (query, sort, update, options, cb) {
47
+ var timestamp = new Date().valueOf();
48
+ var clonedUpdate = _.clone(update);
49
+ var setOnInsert = clonedUpdate.$setOnInsert || {};
50
+ setOnInsert.createDateTime = timestamp;
51
+ clonedUpdate.$setOnInsert = setOnInsert;
52
+
53
+ var set = clonedUpdate.$set || {};
54
+ set.changeDateTime = timestamp;
55
+
56
+ // if consumer tries to $set createDateTime it will lead to conflict. remove it
57
+ if (set.createDateTime) {
58
+ delete set.createDateTime;
59
+ }
60
+ clonedUpdate.$set = set;
61
+ oldFindAndModify.apply(this, [query, sort, clonedUpdate, options, cb]);
62
+ };
63
+
64
+ var oldUpdateMany = coll.updateMany;
65
+ coll.updateMany = function (query, update, options, cb) {
66
+ var timestamp = new Date().valueOf();
67
+ var clonedUpdate = _.clone(update);
68
+ var setOnInsert = clonedUpdate.$setOnInsert || {};
69
+ setOnInsert.createDateTime = timestamp;
70
+ clonedUpdate.$setOnInsert = setOnInsert;
71
+
72
+ var set = clonedUpdate.$set || {};
73
+ set.changeDateTime = timestamp;
74
+
75
+ // if consumer tries to $set createDateTime it will lead to conflict. remove it
76
+ if (set.createDateTime) {
77
+ delete set.createDateTime;
78
+ }
79
+ clonedUpdate.$set = set;
80
+ oldUpdateMany.apply(this, [query, clonedUpdate, options, cb]);
81
+ };
82
+
83
+ var oldUpdateOne = coll.updateOne;
84
+ coll.updateOne = function (query, update, options, cb) {
85
+ var timestamp = new Date().valueOf();
86
+ var clonedUpdate = _.clone(update);
87
+ var setOnInsert = clonedUpdate.$setOnInsert || {};
88
+ setOnInsert.createDateTime = timestamp;
89
+ clonedUpdate.$setOnInsert = setOnInsert;
90
+
91
+ var set = clonedUpdate.$set || {};
92
+ set.changeDateTime = timestamp;
93
+
94
+ // if consumer tries to $set createDateTime it will lead to conflict. remove it
95
+ if (set.createDateTime) {
96
+ delete set.createDateTime;
97
+ }
98
+ clonedUpdate.$set = set;
99
+ oldUpdateOne.apply(this, [query, clonedUpdate, options, cb]);
100
+ };
101
+ }
102
+ return coll;
103
+ };
104
+ };
105
+
106
+ module.exports = ViewDBTimestampPlugin;
@@ -1,93 +1,93 @@
1
- var _ = require('lodash');
2
-
3
- var ViewDBVersioningPlugin = function (viewDb) {
4
- var oldCollection = viewDb.collection;
5
- viewDb.collection = function () {
6
- var coll = oldCollection.apply(this, arguments);
7
- if (!coll.__plugins_versioning) {
8
- coll.__plugins_versioning = true;
9
-
10
- var oldSave = coll.save;
11
- coll.save = function (docs, options) {
12
- var newdocs = docs;
13
- if (!(options && options.skipVersioning)) {
14
- if (!_.isArray(docs)) {
15
- newdocs = [docs];
16
- }
17
- for (var i = 0; i < newdocs.length; i++) {
18
- var doc = newdocs[i];
19
- doc.version = _getVersion(doc.version);
20
- }
21
- }
22
- oldSave.apply(this, arguments);
23
- };
24
-
25
- var oldInsert = coll.insert;
26
- coll.insert = function (docs, options) {
27
- if (!(options && options.skipVersioning)) {
28
- if (!_.isArray(docs)) {
29
- docs = [docs];
30
- }
31
- for (var i = 0; i < docs.length; i++) {
32
- var doc = docs[i];
33
- doc.version = _getVersion(doc.version);
34
- }
35
- }
36
- oldInsert.apply(this, arguments);
37
- };
38
-
39
- var oldFindAndModify = coll.findAndModify;
40
- coll.findAndModify = function (query, sort, update, options, cb) {
41
- if (!(options && options.skipVersioning)) {
42
- var inc = update.$inc || {};
43
- inc.version = 1;
44
- update.$inc = inc;
45
- if (update.$set && update.$set.version >= 0) {
46
- delete update.$set.version;
47
- }
48
- }
49
- oldFindAndModify.apply(this, arguments);
50
- };
51
-
52
- var oldUpdateMany = coll.updateMany;
53
- coll.updateMany = function (query, update, options, cb) {
54
- if (!(options && options.skipVersioning)) {
55
- var inc = update.$inc || {};
56
- inc.version = 1;
57
- update.$inc = inc;
58
- if (update.$set && update.$set.version >= 0) {
59
- delete update.$set.version;
60
- }
61
- }
62
- oldUpdateMany.apply(this, arguments);
63
- };
64
-
65
- var oldUpdateOne = coll.updateOne;
66
- coll.updateOne = function (query, update, options, cb) {
67
- if (!(options && options.skipVersioning)) {
68
- var inc = update.$inc || {};
69
- inc.version = 1;
70
- update.$inc = inc;
71
- if (update.$set && update.$set.version >= 0) {
72
- delete update.$set.version;
73
- }
74
- }
75
- oldUpdateOne.apply(this, arguments);
76
- };
77
- }
78
- return coll;
79
- };
80
- };
81
-
82
- function _getVersion(version) {
83
- var newVersion;
84
- if (_.isUndefined(version)) {
85
- newVersion = 0;
86
- } else {
87
- newVersion = version + 1;
88
- }
89
-
90
- return newVersion;
91
- }
92
-
93
- module.exports = ViewDBVersioningPlugin;
1
+ var _ = require('lodash');
2
+
3
+ var ViewDBVersioningPlugin = function (viewDb) {
4
+ var oldCollection = viewDb.collection;
5
+ viewDb.collection = function () {
6
+ var coll = oldCollection.apply(this, arguments);
7
+ if (!coll.__plugins_versioning) {
8
+ coll.__plugins_versioning = true;
9
+
10
+ var oldSave = coll.save;
11
+ coll.save = function (docs, options) {
12
+ var newdocs = docs;
13
+ if (!(options && options.skipVersioning)) {
14
+ if (!_.isArray(docs)) {
15
+ newdocs = [docs];
16
+ }
17
+ for (var i = 0; i < newdocs.length; i++) {
18
+ var doc = newdocs[i];
19
+ doc.version = _getVersion(doc.version);
20
+ }
21
+ }
22
+ oldSave.apply(this, arguments);
23
+ };
24
+
25
+ var oldInsert = coll.insert;
26
+ coll.insert = function (docs, options) {
27
+ if (!(options && options.skipVersioning)) {
28
+ if (!_.isArray(docs)) {
29
+ docs = [docs];
30
+ }
31
+ for (var i = 0; i < docs.length; i++) {
32
+ var doc = docs[i];
33
+ doc.version = _getVersion(doc.version);
34
+ }
35
+ }
36
+ oldInsert.apply(this, arguments);
37
+ };
38
+
39
+ var oldFindAndModify = coll.findAndModify;
40
+ coll.findAndModify = function (query, sort, update, options, cb) {
41
+ if (!(options && options.skipVersioning)) {
42
+ var inc = update.$inc || {};
43
+ inc.version = 1;
44
+ update.$inc = inc;
45
+ if (update.$set && update.$set.version >= 0) {
46
+ delete update.$set.version;
47
+ }
48
+ }
49
+ oldFindAndModify.apply(this, arguments);
50
+ };
51
+
52
+ var oldUpdateMany = coll.updateMany;
53
+ coll.updateMany = function (query, update, options, cb) {
54
+ if (!(options && options.skipVersioning)) {
55
+ var inc = update.$inc || {};
56
+ inc.version = 1;
57
+ update.$inc = inc;
58
+ if (update.$set && update.$set.version >= 0) {
59
+ delete update.$set.version;
60
+ }
61
+ }
62
+ oldUpdateMany.apply(this, arguments);
63
+ };
64
+
65
+ var oldUpdateOne = coll.updateOne;
66
+ coll.updateOne = function (query, update, options, cb) {
67
+ if (!(options && options.skipVersioning)) {
68
+ var inc = update.$inc || {};
69
+ inc.version = 1;
70
+ update.$inc = inc;
71
+ if (update.$set && update.$set.version >= 0) {
72
+ delete update.$set.version;
73
+ }
74
+ }
75
+ oldUpdateOne.apply(this, arguments);
76
+ };
77
+ }
78
+ return coll;
79
+ };
80
+ };
81
+
82
+ function _getVersion(version) {
83
+ var newVersion;
84
+ if (_.isUndefined(version)) {
85
+ newVersion = 0;
86
+ } else {
87
+ newVersion = version + 1;
88
+ }
89
+
90
+ return newVersion;
91
+ }
92
+
93
+ module.exports = ViewDBVersioningPlugin;
package/package.json CHANGED
@@ -1,37 +1,37 @@
1
- {
2
- "name": "viewdb",
3
- "version": "0.10.0",
4
- "description": "",
5
- "main": "index.js",
6
- "directories": {
7
- "test": "test"
8
- },
9
- "scripts": {
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",
15
- "jshint": "jshint ./lib",
16
- "jscs": "jscs -p google ./lib"
17
- },
18
- "repository": {
19
- "type": "git",
20
- "url": "https://github.com/surikaterna/viewdb"
21
- },
22
- "keywords": [],
23
- "author": "",
24
- "license": "MIT",
25
- "devDependencies": {
26
- "es-jest": "^2.1.0",
27
- "jest": "^29.5.0",
28
- "jshint": "^2.13.6",
29
- "prettier": "^2.8.7",
30
- "prettier-config-surikaterna": "^1.0.1"
31
- },
32
- "dependencies": {
33
- "kuery": "^0.6.0",
34
- "lodash": "^4.17.21",
35
- "uuid": "^9.0.0"
36
- }
37
- }
1
+ {
2
+ "name": "viewdb",
3
+ "version": "0.11.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "directories": {
7
+ "test": "test"
8
+ },
9
+ "scripts": {
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",
15
+ "jshint": "jshint ./lib",
16
+ "jscs": "jscs -p google ./lib"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/surikaterna/viewdb"
21
+ },
22
+ "keywords": [],
23
+ "author": "",
24
+ "license": "MIT",
25
+ "devDependencies": {
26
+ "es-jest": "^2.1.0",
27
+ "jest": "^29.5.0",
28
+ "jshint": "^2.13.6",
29
+ "prettier": "^2.8.7",
30
+ "prettier-config-surikaterna": "^1.0.1"
31
+ },
32
+ "dependencies": {
33
+ "kuery": "^0.6.0",
34
+ "lodash": "^4.17.21",
35
+ "uuid": "^9.0.0"
36
+ }
37
+ }