hadron-document 7.7.0 → 7.10.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/README.md +5 -5
- package/dist/document.d.ts +53 -0
- package/dist/document.d.ts.map +1 -0
- package/dist/document.js +248 -0
- package/dist/document.js.map +1 -0
- package/dist/editor/date.d.ts +13 -0
- package/dist/editor/date.d.ts.map +1 -0
- package/dist/editor/date.js +65 -0
- package/dist/editor/date.js.map +1 -0
- package/dist/editor/decimal128.d.ts +9 -0
- package/dist/editor/decimal128.d.ts.map +1 -0
- package/dist/editor/decimal128.js +32 -0
- package/dist/editor/decimal128.js.map +1 -0
- package/dist/editor/double.d.ts +10 -0
- package/dist/editor/double.d.ts.map +1 -0
- package/dist/editor/double.js +44 -0
- package/dist/editor/double.js.map +1 -0
- package/dist/editor/index.d.ts +38 -0
- package/dist/editor/index.d.ts.map +1 -0
- package/dist/editor/index.js +51 -0
- package/dist/editor/index.js.map +1 -0
- package/dist/editor/int32.d.ts +7 -0
- package/dist/editor/int32.d.ts.map +1 -0
- package/dist/editor/int32.js +17 -0
- package/dist/editor/int32.js.map +1 -0
- package/dist/editor/int64.d.ts +9 -0
- package/dist/editor/int64.d.ts.map +1 -0
- package/dist/editor/int64.js +32 -0
- package/dist/editor/int64.js.map +1 -0
- package/dist/editor/null.d.ts +8 -0
- package/dist/editor/null.d.ts.map +1 -0
- package/dist/editor/null.js +20 -0
- package/dist/editor/null.js.map +1 -0
- package/dist/editor/objectid.d.ts +10 -0
- package/dist/editor/objectid.d.ts.map +1 -0
- package/dist/editor/objectid.js +38 -0
- package/dist/editor/objectid.js.map +1 -0
- package/dist/editor/standard.d.ts +16 -0
- package/dist/editor/standard.d.ts.map +1 -0
- package/dist/editor/standard.js +49 -0
- package/dist/editor/standard.js.map +1 -0
- package/dist/editor/string.d.ts +7 -0
- package/dist/editor/string.d.ts.map +1 -0
- package/dist/editor/string.js +15 -0
- package/dist/editor/string.js.map +1 -0
- package/dist/editor/undefined.d.ts +8 -0
- package/dist/editor/undefined.d.ts.map +1 -0
- package/dist/editor/undefined.js +20 -0
- package/dist/editor/undefined.js.map +1 -0
- package/dist/element-events.d.ts +11 -0
- package/dist/element-events.d.ts.map +1 -0
- package/dist/element-events.js +12 -0
- package/dist/element-events.js.map +1 -0
- package/dist/element.d.ts +112 -0
- package/dist/element.d.ts.map +1 -0
- package/dist/element.js +652 -0
- package/dist/element.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/dist/object-generator.d.ts +9 -0
- package/dist/object-generator.d.ts.map +1 -0
- package/dist/object-generator.js +82 -0
- package/dist/object-generator.js.map +1 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +37 -0
- package/dist/utils.js.map +1 -0
- package/package.json +45 -28
- package/.babelrc.js +0 -14
- package/.depcheckrc +0 -8
- package/.eslintignore +0 -1
- package/index.js +0 -1
- package/lib/document.js +0 -557
- package/lib/element.js +0 -1236
- package/lib/index.js +0 -4
- package/lib/object-generator.js +0 -180
- package/src/document.js +0 -391
- package/src/element.js +0 -951
- package/src/index.js +0 -2
- package/src/object-generator.js +0 -96
package/lib/index.js
DELETED
package/lib/object-generator.js
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
/**
|
|
3
|
-
* Generates javascript objects from elements.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
7
|
-
|
|
8
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
9
|
-
|
|
10
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
11
|
-
|
|
12
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
13
|
-
|
|
14
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
15
|
-
|
|
16
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
17
|
-
|
|
18
|
-
var ObjectGenerator = /*#__PURE__*/function () {
|
|
19
|
-
function ObjectGenerator() {
|
|
20
|
-
_classCallCheck(this, ObjectGenerator);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
_createClass(ObjectGenerator, [{
|
|
24
|
-
key: "generate",
|
|
25
|
-
value:
|
|
26
|
-
/**
|
|
27
|
-
* Generate a javascript object from the elements.
|
|
28
|
-
*
|
|
29
|
-
* @param {Array} elements - The elements.
|
|
30
|
-
*
|
|
31
|
-
* @returns {Object} The javascript object.
|
|
32
|
-
*/
|
|
33
|
-
function generate(elements) {
|
|
34
|
-
if (elements) {
|
|
35
|
-
var object = {};
|
|
36
|
-
|
|
37
|
-
var _iterator = _createForOfIteratorHelper(elements),
|
|
38
|
-
_step;
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
42
|
-
var element = _step.value;
|
|
43
|
-
|
|
44
|
-
if (!element.isRemoved() && element.currentKey !== '') {
|
|
45
|
-
object[element.currentKey] = element.generateObject();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
} catch (err) {
|
|
49
|
-
_iterator.e(err);
|
|
50
|
-
} finally {
|
|
51
|
-
_iterator.f();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return object;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return elements;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Generate a javascript object from the elements with their original keys
|
|
61
|
-
* and values. This can be used in a query with an update to
|
|
62
|
-
* ensure the values on the document to edit are still up to date.
|
|
63
|
-
*
|
|
64
|
-
* @param {Array} elements - The elements.
|
|
65
|
-
*
|
|
66
|
-
* @returns {Object} The javascript object.
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
|
-
}, {
|
|
70
|
-
key: "generateOriginal",
|
|
71
|
-
value: function generateOriginal(elements) {
|
|
72
|
-
if (elements) {
|
|
73
|
-
var object = {};
|
|
74
|
-
|
|
75
|
-
var _iterator2 = _createForOfIteratorHelper(elements),
|
|
76
|
-
_step2;
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
80
|
-
var element = _step2.value;
|
|
81
|
-
|
|
82
|
-
if (!element.isAdded()) {
|
|
83
|
-
object[element.key] = element.generateOriginalObject();
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
} catch (err) {
|
|
87
|
-
_iterator2.e(err);
|
|
88
|
-
} finally {
|
|
89
|
-
_iterator2.f();
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return object;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return elements;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Generate an array from the elements.
|
|
99
|
-
*
|
|
100
|
-
* @param {Array} elements - The elements.
|
|
101
|
-
*
|
|
102
|
-
* @returns {Array} The array.
|
|
103
|
-
*/
|
|
104
|
-
|
|
105
|
-
}, {
|
|
106
|
-
key: "generateArray",
|
|
107
|
-
value: function generateArray(elements) {
|
|
108
|
-
if (elements) {
|
|
109
|
-
var array = [];
|
|
110
|
-
|
|
111
|
-
var _iterator3 = _createForOfIteratorHelper(elements),
|
|
112
|
-
_step3;
|
|
113
|
-
|
|
114
|
-
try {
|
|
115
|
-
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
116
|
-
var element = _step3.value;
|
|
117
|
-
|
|
118
|
-
if (!element.isRemoved()) {
|
|
119
|
-
if (element.elements) {
|
|
120
|
-
array.push(element.generateObject());
|
|
121
|
-
} else {
|
|
122
|
-
array.push(element.currentValue);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
} catch (err) {
|
|
127
|
-
_iterator3.e(err);
|
|
128
|
-
} finally {
|
|
129
|
-
_iterator3.f();
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return array;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return elements;
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Generate an array from the elements using their original values.
|
|
139
|
-
*
|
|
140
|
-
* @param {Array} elements - The elements.
|
|
141
|
-
*
|
|
142
|
-
* @returns {Array} The array.
|
|
143
|
-
*/
|
|
144
|
-
|
|
145
|
-
}, {
|
|
146
|
-
key: "generateOriginalArray",
|
|
147
|
-
value: function generateOriginalArray(elements) {
|
|
148
|
-
if (elements) {
|
|
149
|
-
var array = [];
|
|
150
|
-
|
|
151
|
-
var _iterator4 = _createForOfIteratorHelper(elements),
|
|
152
|
-
_step4;
|
|
153
|
-
|
|
154
|
-
try {
|
|
155
|
-
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
156
|
-
var element = _step4.value;
|
|
157
|
-
|
|
158
|
-
if (element.originalExpandableValue) {
|
|
159
|
-
array.push(element.generateOriginalObject());
|
|
160
|
-
} else if (!element.isAdded()) {
|
|
161
|
-
array.push(element.value);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
} catch (err) {
|
|
165
|
-
_iterator4.e(err);
|
|
166
|
-
} finally {
|
|
167
|
-
_iterator4.f();
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return array;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return elements;
|
|
174
|
-
}
|
|
175
|
-
}]);
|
|
176
|
-
|
|
177
|
-
return ObjectGenerator;
|
|
178
|
-
}();
|
|
179
|
-
|
|
180
|
-
module.exports = new ObjectGenerator();
|
package/src/document.js
DELETED
|
@@ -1,391 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const EventEmitter = require('eventemitter3');
|
|
4
|
-
const Element = require('./element');
|
|
5
|
-
const LinkedList = Element.LinkedList;
|
|
6
|
-
const ObjectGenerator = require('./object-generator');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* The event constant.
|
|
10
|
-
*/
|
|
11
|
-
const Events = {
|
|
12
|
-
'Cancel': 'Document::Cancel'
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* The id field.
|
|
17
|
-
*/
|
|
18
|
-
const ID = '_id';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Represents a document.
|
|
22
|
-
*/
|
|
23
|
-
class Document extends EventEmitter {
|
|
24
|
-
/**
|
|
25
|
-
* Send cancel event.
|
|
26
|
-
*/
|
|
27
|
-
cancel() {
|
|
28
|
-
for (let element of this.elements) {
|
|
29
|
-
element.cancel();
|
|
30
|
-
}
|
|
31
|
-
this.emit(Events.Cancel);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Create the new document from the provided object.
|
|
36
|
-
*
|
|
37
|
-
* @param {Object} doc - The document.
|
|
38
|
-
* @param {boolean} cloned - If it is a cloned document.
|
|
39
|
-
*/
|
|
40
|
-
constructor(doc, cloned) {
|
|
41
|
-
super();
|
|
42
|
-
this.doc = doc;
|
|
43
|
-
this.cloned = cloned || false;
|
|
44
|
-
this.isUpdatable = true;
|
|
45
|
-
this.elements = this._generateElements();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Generate the javascript object for this document.
|
|
50
|
-
*
|
|
51
|
-
* @returns {Object} The javascript object.
|
|
52
|
-
*/
|
|
53
|
-
generateObject() {
|
|
54
|
-
return ObjectGenerator.generate(this.elements);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Generate the javascript object with the original elements in this document.
|
|
59
|
-
*
|
|
60
|
-
* @returns {Object} The original javascript object.
|
|
61
|
-
*/
|
|
62
|
-
generateOriginalObject() {
|
|
63
|
-
return ObjectGenerator.generateOriginal(this.elements);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Generate the `query` and `updateDoc` to be used in an update operation
|
|
68
|
-
* where the update only succeeds when the changed document's elements have
|
|
69
|
-
* not been changed in the background.
|
|
70
|
-
*
|
|
71
|
-
* @param {Object} alwaysIncludeKeys - An object whose keys are used as keys
|
|
72
|
-
* that are always included in the generated query.
|
|
73
|
-
*
|
|
74
|
-
* @returns {Object} An object containing the `query` and `updateDoc` to be
|
|
75
|
-
* used in an update operation.
|
|
76
|
-
*/
|
|
77
|
-
generateUpdateUnlessChangedInBackgroundQuery(
|
|
78
|
-
alwaysIncludeKeys = null
|
|
79
|
-
) {
|
|
80
|
-
// Build a query that will find the document to update only if it has the
|
|
81
|
-
// values of elements that were changed with their original value.
|
|
82
|
-
// This query won't find the document if an updated element's value isn't
|
|
83
|
-
// the same value as it was when it was originally loaded.
|
|
84
|
-
const originalFieldsThatWillBeUpdated = this.getOriginalKeysAndValuesForFieldsThatWereUpdated(
|
|
85
|
-
alwaysIncludeKeys
|
|
86
|
-
);
|
|
87
|
-
const query = {
|
|
88
|
-
_id: this.getId(),
|
|
89
|
-
...originalFieldsThatWillBeUpdated
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
// Build the update document to be used in an update operation with `$set`
|
|
93
|
-
// and `$unset` reflecting the changes that have occured in the document.
|
|
94
|
-
const setUpdateObject = this.getSetUpdateForDocumentChanges();
|
|
95
|
-
const unsetUpdateObject = this.getUnsetUpdateForDocumentChanges();
|
|
96
|
-
const updateDoc = { };
|
|
97
|
-
if (setUpdateObject && Object.keys(setUpdateObject).length > 0) {
|
|
98
|
-
updateDoc.$set = setUpdateObject;
|
|
99
|
-
}
|
|
100
|
-
if (unsetUpdateObject && Object.keys(unsetUpdateObject).length > 0) {
|
|
101
|
-
updateDoc.$unset = unsetUpdateObject;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return {
|
|
105
|
-
query,
|
|
106
|
-
updateDoc
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Get an element by its key.
|
|
112
|
-
*
|
|
113
|
-
* @param {String} key
|
|
114
|
-
*
|
|
115
|
-
* @returns {Element} The element.
|
|
116
|
-
*/
|
|
117
|
-
get(key) {
|
|
118
|
-
return this.elements.get(key);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Get an element by a series of segment names.
|
|
123
|
-
*
|
|
124
|
-
* @param {Array} path - The series of fieldnames. Cannot be empty.
|
|
125
|
-
*
|
|
126
|
-
* @returns {Element} The element.
|
|
127
|
-
*/
|
|
128
|
-
getChild(path) {
|
|
129
|
-
if (!path) {
|
|
130
|
-
return undefined;
|
|
131
|
-
}
|
|
132
|
-
let element = (this.currentType === 'Array') ? this.elements.at(path[0]) : this.elements.get(path[0]);
|
|
133
|
-
let i = 1;
|
|
134
|
-
while (i < path.length) {
|
|
135
|
-
if (element === undefined) {
|
|
136
|
-
return undefined;
|
|
137
|
-
}
|
|
138
|
-
element = element.currentType === 'Array' ? element.at(path[i]) : element.get(path[i]);
|
|
139
|
-
i++;
|
|
140
|
-
}
|
|
141
|
-
return element;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Get the _id value for the document.
|
|
146
|
-
*
|
|
147
|
-
* @returns {Object} The id.
|
|
148
|
-
*/
|
|
149
|
-
getId() {
|
|
150
|
-
const element = this.get(ID);
|
|
151
|
-
return element ? element.generateObject() : null;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Generate the query javascript object reflecting the elements that
|
|
156
|
-
* were updated in this document. The values of this object are the original
|
|
157
|
-
* values, this can be used when querying for an update to see if the original
|
|
158
|
-
* document was changed in the background while it was being updated elsewhere.
|
|
159
|
-
*
|
|
160
|
-
* @param {Object} alwaysIncludeKeys - An object whose keys are used as keys
|
|
161
|
-
* that are always included in the generated query.
|
|
162
|
-
*
|
|
163
|
-
* @returns {Object} The javascript object.
|
|
164
|
-
*/
|
|
165
|
-
getOriginalKeysAndValuesForFieldsThatWereUpdated(
|
|
166
|
-
alwaysIncludeKeys = null
|
|
167
|
-
) {
|
|
168
|
-
const object = {};
|
|
169
|
-
|
|
170
|
-
if (this.elements) {
|
|
171
|
-
for (const element of this.elements) {
|
|
172
|
-
if ((element.isModified() && !element.isAdded()) ||
|
|
173
|
-
(alwaysIncludeKeys && element.key in alwaysIncludeKeys)) {
|
|
174
|
-
// Using `.key` instead of `.currentKey` to ensure we look at
|
|
175
|
-
// the original field's value.
|
|
176
|
-
object[element.key] = element.generateOriginalObject();
|
|
177
|
-
}
|
|
178
|
-
if (element.isAdded() && element.currentKey !== '') {
|
|
179
|
-
// When a new field is added, check if that field
|
|
180
|
-
// was already added in the background.
|
|
181
|
-
object[element.currentKey] = { $exists: false };
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return object;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Generate the query javascript object reflecting the elements that
|
|
191
|
-
* are specified by the keys listed in `keys`. The values of this object are
|
|
192
|
-
* the original values, this can be used when querying for an update based
|
|
193
|
-
* on multiple criteria.
|
|
194
|
-
*
|
|
195
|
-
* @param {Object} keys - An object whose keys are used as keys
|
|
196
|
-
* that are included in the generated query.
|
|
197
|
-
*
|
|
198
|
-
* @returns {Object} The javascript object.
|
|
199
|
-
*/
|
|
200
|
-
getOriginalKeysAndValuesForSpecifiedKeys(keys) {
|
|
201
|
-
const object = {};
|
|
202
|
-
|
|
203
|
-
if (this.elements) {
|
|
204
|
-
for (const element of this.elements) {
|
|
205
|
-
if (element.key in keys) {
|
|
206
|
-
// Using `.key` instead of `.currentKey` to ensure we look at
|
|
207
|
-
// the original field's value.
|
|
208
|
-
object[element.key] = element.generateOriginalObject();
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return object;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Generate an $set javascript object, that can be used in update operations to
|
|
218
|
-
* set the changes which have occured in the document since it was loaded.
|
|
219
|
-
*
|
|
220
|
-
* @returns {Object} The javascript update object.
|
|
221
|
-
**/
|
|
222
|
-
getSetUpdateForDocumentChanges() {
|
|
223
|
-
const object = {};
|
|
224
|
-
|
|
225
|
-
if (this.elements) {
|
|
226
|
-
for (const element of this.elements) {
|
|
227
|
-
if (
|
|
228
|
-
!element.isRemoved()
|
|
229
|
-
&& element.currentKey !== ''
|
|
230
|
-
&& element.isModified()
|
|
231
|
-
) {
|
|
232
|
-
// Include the full modified element.
|
|
233
|
-
// We don't individually set nested fields because we can't guarantee a
|
|
234
|
-
// path to the element using '.' dot notation will update
|
|
235
|
-
// the correct field, because field names can contain dots as of 3.6.
|
|
236
|
-
// When a nested field has been altered (changed/added/removed) it is
|
|
237
|
-
// set at the top level field. This means we overwrite possible
|
|
238
|
-
// background changes that occur within sub documents.
|
|
239
|
-
object[element.currentKey] = element.generateObject();
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
return object;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Get the _id value as a string. Required if _id is not always an ObjectId.
|
|
248
|
-
*
|
|
249
|
-
* @returns {String} The string id.
|
|
250
|
-
*/
|
|
251
|
-
getStringId() {
|
|
252
|
-
const element = this.get(ID);
|
|
253
|
-
if (!element) {
|
|
254
|
-
return null;
|
|
255
|
-
} else if (element.currentType === 'Array' || element.currentType === 'Object') {
|
|
256
|
-
return JSON.stringify(element.generateObject());
|
|
257
|
-
}
|
|
258
|
-
return '' + element.value;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Generate an $unset javascript object, that can be used in update
|
|
263
|
-
* operations, with the removals from the document.
|
|
264
|
-
*
|
|
265
|
-
* @returns {Object} The javascript update object.
|
|
266
|
-
**/
|
|
267
|
-
getUnsetUpdateForDocumentChanges() {
|
|
268
|
-
const object = {};
|
|
269
|
-
|
|
270
|
-
if (this.elements) {
|
|
271
|
-
for (const element of this.elements) {
|
|
272
|
-
if (!element.isAdded() && element.isRemoved() && element.key !== '') {
|
|
273
|
-
object[element.key] = true;
|
|
274
|
-
}
|
|
275
|
-
if (!element.isAdded() && element.isRenamed() && element.key !== '') {
|
|
276
|
-
// Remove the original field when an element is renamed.
|
|
277
|
-
object[element.key] = true;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return object;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* Insert a placeholder element at the end of the document.
|
|
286
|
-
*
|
|
287
|
-
* @returns {Element} The placeholder element.
|
|
288
|
-
*/
|
|
289
|
-
insertPlaceholder() {
|
|
290
|
-
return this.insertEnd('', '');
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* Add a new element to this document.
|
|
295
|
-
*
|
|
296
|
-
* @param {String} key - The element key.
|
|
297
|
-
* @param {Object} value - The value.
|
|
298
|
-
*
|
|
299
|
-
* @returns {Element} The new element.
|
|
300
|
-
*/
|
|
301
|
-
insertEnd(key, value) {
|
|
302
|
-
var newElement = this.elements.insertEnd(key, value, true, this);
|
|
303
|
-
this.emit(Element.Events.Added);
|
|
304
|
-
return newElement;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Insert an element after the provided element.
|
|
309
|
-
*
|
|
310
|
-
* @param {Element} element - The element to insert after.
|
|
311
|
-
* @param {String} key - The key.
|
|
312
|
-
* @param {Object} value - The value.
|
|
313
|
-
*
|
|
314
|
-
* @returns {Element} The new element.
|
|
315
|
-
*/
|
|
316
|
-
insertAfter(element, key, value) {
|
|
317
|
-
var newElement = this.elements.insertAfter(element, key, value, true, this);
|
|
318
|
-
this.emit(Element.Events.Added);
|
|
319
|
-
return newElement;
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
* A document always exists, is never added.
|
|
324
|
-
*
|
|
325
|
-
* @returns {false} Always false.
|
|
326
|
-
*/
|
|
327
|
-
isAdded() {
|
|
328
|
-
return false;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* Determine if the element is modified at all.
|
|
333
|
-
*
|
|
334
|
-
* @returns {Boolean} If the element is modified.
|
|
335
|
-
*/
|
|
336
|
-
isModified() {
|
|
337
|
-
for (let element of this.elements) {
|
|
338
|
-
if (element.isModified()) {
|
|
339
|
-
return true;
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
return false;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* A document is never removed
|
|
347
|
-
*
|
|
348
|
-
* @returns {false} Always false.
|
|
349
|
-
*/
|
|
350
|
-
isRemoved() {
|
|
351
|
-
return false;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* The document object is always the root object.
|
|
356
|
-
*
|
|
357
|
-
* @returns {true} Always true.
|
|
358
|
-
*/
|
|
359
|
-
isRoot() {
|
|
360
|
-
return true;
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Handle the next element in the document.
|
|
365
|
-
*/
|
|
366
|
-
next() {
|
|
367
|
-
this.elements.flush();
|
|
368
|
-
const lastElement = this.elements.lastElement;
|
|
369
|
-
if (lastElement && lastElement.isAdded()) {
|
|
370
|
-
if (lastElement.isBlank()) {
|
|
371
|
-
lastElement.remove();
|
|
372
|
-
} else {
|
|
373
|
-
this.insertPlaceholder();
|
|
374
|
-
}
|
|
375
|
-
} else {
|
|
376
|
-
this.insertPlaceholder();
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* Generates a sequence of elements.
|
|
382
|
-
*
|
|
383
|
-
* @returns {Array} The elements.
|
|
384
|
-
*/
|
|
385
|
-
_generateElements() {
|
|
386
|
-
return new LinkedList(this, this.doc);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
module.exports = Document;
|
|
391
|
-
module.exports.Events = Events;
|