hadron-document 7.9.0 → 7.12.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 +54 -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 +114 -0
- package/dist/element.d.ts.map +1 -0
- package/dist/element.js +660 -0
- package/dist/element.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/object-generator.d.ts +12 -0
- package/dist/object-generator.d.ts.map +1 -0
- package/dist/object-generator.js +94 -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 +42 -28
- package/.babelrc.js +0 -14
- package/.depcheckrc +0 -6
- package/.eslintignore +0 -1
- package/index.d.ts +0 -192
- package/index.js +0 -1
- package/lib/document.js +0 -624
- package/lib/editor/date.js +0 -151
- package/lib/editor/decimal128.js +0 -88
- package/lib/editor/double.js +0 -113
- package/lib/editor/index.js +0 -50
- package/lib/editor/int32.js +0 -68
- package/lib/editor/int64.js +0 -88
- package/lib/editor/null.js +0 -81
- package/lib/editor/objectid.js +0 -98
- package/lib/editor/standard.js +0 -108
- package/lib/editor/string.js +0 -48
- package/lib/editor/undefined.js +0 -81
- package/lib/element-events.js +0 -15
- package/lib/element.js +0 -1307
- package/lib/index.js +0 -7
- package/lib/object-generator.js +0 -180
- package/lib/utils.js +0 -10
- package/src/document.js +0 -432
- package/src/editor/date.js +0 -95
- package/src/editor/decimal128.js +0 -46
- package/src/editor/double.js +0 -70
- package/src/editor/index.js +0 -40
- package/src/editor/int32.js +0 -29
- package/src/editor/int64.js +0 -46
- package/src/editor/null.js +0 -42
- package/src/editor/objectid.js +0 -54
- package/src/editor/standard.js +0 -78
- package/src/editor/string.js +0 -20
- package/src/editor/undefined.js +0 -42
- package/src/element-events.js +0 -14
- package/src/element.js +0 -1002
- package/src/index.js +0 -5
- package/src/object-generator.js +0 -96
- package/src/utils.js +0 -6
package/lib/document.js
DELETED
|
@@ -1,624 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
|
-
|
|
5
|
-
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(_e2) { throw _e2; }, 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(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
6
|
-
|
|
7
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
8
|
-
|
|
9
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
10
|
-
|
|
11
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
12
|
-
|
|
13
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
14
|
-
|
|
15
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
16
|
-
|
|
17
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
18
|
-
|
|
19
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
20
|
-
|
|
21
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
22
|
-
|
|
23
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
24
|
-
|
|
25
|
-
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); }
|
|
26
|
-
|
|
27
|
-
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; }
|
|
28
|
-
|
|
29
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
30
|
-
|
|
31
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
32
|
-
|
|
33
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
34
|
-
|
|
35
|
-
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); } }
|
|
36
|
-
|
|
37
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
38
|
-
|
|
39
|
-
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
40
|
-
|
|
41
|
-
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
42
|
-
|
|
43
|
-
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
44
|
-
|
|
45
|
-
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
46
|
-
|
|
47
|
-
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
48
|
-
|
|
49
|
-
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
50
|
-
|
|
51
|
-
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
52
|
-
|
|
53
|
-
var EventEmitter = require('eventemitter3');
|
|
54
|
-
|
|
55
|
-
var uuid = require('uuid');
|
|
56
|
-
|
|
57
|
-
var Element = require('./element');
|
|
58
|
-
|
|
59
|
-
var LinkedList = Element.LinkedList;
|
|
60
|
-
|
|
61
|
-
var ObjectGenerator = require('./object-generator');
|
|
62
|
-
/**
|
|
63
|
-
* The event constant.
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
var Events = {
|
|
68
|
-
'Cancel': 'Document::Cancel'
|
|
69
|
-
};
|
|
70
|
-
/**
|
|
71
|
-
* The id field.
|
|
72
|
-
*/
|
|
73
|
-
|
|
74
|
-
var ID = '_id';
|
|
75
|
-
/**
|
|
76
|
-
* Represents a document.
|
|
77
|
-
*/
|
|
78
|
-
|
|
79
|
-
var Document = /*#__PURE__*/function (_EventEmitter) {
|
|
80
|
-
_inherits(Document, _EventEmitter);
|
|
81
|
-
|
|
82
|
-
var _super = _createSuper(Document);
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Create the new document from the provided object.
|
|
86
|
-
*
|
|
87
|
-
* @param {Object} doc - The document.
|
|
88
|
-
* @param {boolean} cloned - If it is a cloned document.
|
|
89
|
-
*/
|
|
90
|
-
function Document(doc, cloned) {
|
|
91
|
-
var _this;
|
|
92
|
-
|
|
93
|
-
_classCallCheck(this, Document);
|
|
94
|
-
|
|
95
|
-
_this = _super.call(this);
|
|
96
|
-
_this.uuid = uuid.v4();
|
|
97
|
-
_this.doc = doc;
|
|
98
|
-
_this.cloned = cloned || false;
|
|
99
|
-
_this.isUpdatable = true;
|
|
100
|
-
_this.elements = _this._generateElements();
|
|
101
|
-
_this.type = 'Document';
|
|
102
|
-
_this.currentType = 'Document';
|
|
103
|
-
return _this;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
_createClass(Document, [{
|
|
107
|
-
key: "cancel",
|
|
108
|
-
value:
|
|
109
|
-
/**
|
|
110
|
-
* Send cancel event.
|
|
111
|
-
*/
|
|
112
|
-
function cancel() {
|
|
113
|
-
// Cancel will remove elements from iterator, clone it before iterating
|
|
114
|
-
// otherwise we will skip items
|
|
115
|
-
for (var _i = 0, _Array$from = Array.from(this.elements); _i < _Array$from.length; _i++) {
|
|
116
|
-
var element = _Array$from[_i];
|
|
117
|
-
element.cancel();
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
this.emit(Events.Cancel);
|
|
121
|
-
}
|
|
122
|
-
}, {
|
|
123
|
-
key: "apply",
|
|
124
|
-
value: function apply(doc) {
|
|
125
|
-
var updatedKeys = [];
|
|
126
|
-
var prevKey = null;
|
|
127
|
-
|
|
128
|
-
for (var _i2 = 0, _Object$entries = Object.entries(doc); _i2 < _Object$entries.length; _i2++) {
|
|
129
|
-
var _Object$entries$_i = _slicedToArray(_Object$entries[_i2], 2),
|
|
130
|
-
key = _Object$entries$_i[0],
|
|
131
|
-
value = _Object$entries$_i[1];
|
|
132
|
-
|
|
133
|
-
if (this.get(key)) {
|
|
134
|
-
this.get(key).edit(value);
|
|
135
|
-
} else if (prevKey) {
|
|
136
|
-
this.insertAfter(this.get(prevKey), key, value);
|
|
137
|
-
} else {
|
|
138
|
-
this.insertBeginning(key, value);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
prevKey = key;
|
|
142
|
-
updatedKeys.push(key);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
for (var _i3 = 0, _arr2 = _toConsumableArray(this.elements); _i3 < _arr2.length; _i3++) {
|
|
146
|
-
var el = _arr2[_i3];
|
|
147
|
-
|
|
148
|
-
if (!updatedKeys.includes(el.currentKey)) {
|
|
149
|
-
el.remove();
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Generate the javascript object for this document.
|
|
155
|
-
*
|
|
156
|
-
* @returns {Object} The javascript object.
|
|
157
|
-
*/
|
|
158
|
-
|
|
159
|
-
}, {
|
|
160
|
-
key: "generateObject",
|
|
161
|
-
value: function generateObject() {
|
|
162
|
-
return ObjectGenerator.generate(this.elements);
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Generate the javascript object with the original elements in this document.
|
|
166
|
-
*
|
|
167
|
-
* @returns {Object} The original javascript object.
|
|
168
|
-
*/
|
|
169
|
-
|
|
170
|
-
}, {
|
|
171
|
-
key: "generateOriginalObject",
|
|
172
|
-
value: function generateOriginalObject() {
|
|
173
|
-
return ObjectGenerator.generateOriginal(this.elements);
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Generate the `query` and `updateDoc` to be used in an update operation
|
|
177
|
-
* where the update only succeeds when the changed document's elements have
|
|
178
|
-
* not been changed in the background.
|
|
179
|
-
*
|
|
180
|
-
* @param {Object} alwaysIncludeKeys - An object whose keys are used as keys
|
|
181
|
-
* that are always included in the generated query.
|
|
182
|
-
*
|
|
183
|
-
* @returns {Object} An object containing the `query` and `updateDoc` to be
|
|
184
|
-
* used in an update operation.
|
|
185
|
-
*/
|
|
186
|
-
|
|
187
|
-
}, {
|
|
188
|
-
key: "generateUpdateUnlessChangedInBackgroundQuery",
|
|
189
|
-
value: function generateUpdateUnlessChangedInBackgroundQuery() {
|
|
190
|
-
var alwaysIncludeKeys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
191
|
-
// Build a query that will find the document to update only if it has the
|
|
192
|
-
// values of elements that were changed with their original value.
|
|
193
|
-
// This query won't find the document if an updated element's value isn't
|
|
194
|
-
// the same value as it was when it was originally loaded.
|
|
195
|
-
var originalFieldsThatWillBeUpdated = this.getOriginalKeysAndValuesForFieldsThatWereUpdated(alwaysIncludeKeys);
|
|
196
|
-
|
|
197
|
-
var query = _objectSpread({
|
|
198
|
-
_id: this.getId()
|
|
199
|
-
}, originalFieldsThatWillBeUpdated); // Build the update document to be used in an update operation with `$set`
|
|
200
|
-
// and `$unset` reflecting the changes that have occured in the document.
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
var setUpdateObject = this.getSetUpdateForDocumentChanges();
|
|
204
|
-
var unsetUpdateObject = this.getUnsetUpdateForDocumentChanges();
|
|
205
|
-
var updateDoc = {};
|
|
206
|
-
|
|
207
|
-
if (setUpdateObject && Object.keys(setUpdateObject).length > 0) {
|
|
208
|
-
updateDoc.$set = setUpdateObject;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (unsetUpdateObject && Object.keys(unsetUpdateObject).length > 0) {
|
|
212
|
-
updateDoc.$unset = unsetUpdateObject;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return {
|
|
216
|
-
query: query,
|
|
217
|
-
updateDoc: updateDoc
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* Get an element by its key.
|
|
222
|
-
*
|
|
223
|
-
* @param {String} key
|
|
224
|
-
*
|
|
225
|
-
* @returns {Element} The element.
|
|
226
|
-
*/
|
|
227
|
-
|
|
228
|
-
}, {
|
|
229
|
-
key: "get",
|
|
230
|
-
value: function get(key) {
|
|
231
|
-
return this.elements.get(key);
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* Get an element by a series of segment names.
|
|
235
|
-
*
|
|
236
|
-
* @param {Array} path - The series of fieldnames. Cannot be empty.
|
|
237
|
-
*
|
|
238
|
-
* @returns {Element} The element.
|
|
239
|
-
*/
|
|
240
|
-
|
|
241
|
-
}, {
|
|
242
|
-
key: "getChild",
|
|
243
|
-
value: function getChild(path) {
|
|
244
|
-
if (!path) {
|
|
245
|
-
return undefined;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
var element = this.currentType === 'Array' ? this.elements.at(path[0]) : this.elements.get(path[0]);
|
|
249
|
-
var i = 1;
|
|
250
|
-
|
|
251
|
-
while (i < path.length) {
|
|
252
|
-
if (element === undefined) {
|
|
253
|
-
return undefined;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
element = element.currentType === 'Array' ? element.at(path[i]) : element.get(path[i]);
|
|
257
|
-
i++;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
return element;
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Get the _id value for the document.
|
|
264
|
-
*
|
|
265
|
-
* @returns {Object} The id.
|
|
266
|
-
*/
|
|
267
|
-
|
|
268
|
-
}, {
|
|
269
|
-
key: "getId",
|
|
270
|
-
value: function getId() {
|
|
271
|
-
var element = this.get(ID);
|
|
272
|
-
return element ? element.generateObject() : null;
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Generate the query javascript object reflecting the elements that
|
|
276
|
-
* were updated in this document. The values of this object are the original
|
|
277
|
-
* values, this can be used when querying for an update to see if the original
|
|
278
|
-
* document was changed in the background while it was being updated elsewhere.
|
|
279
|
-
*
|
|
280
|
-
* @param {Object} alwaysIncludeKeys - An object whose keys are used as keys
|
|
281
|
-
* that are always included in the generated query.
|
|
282
|
-
*
|
|
283
|
-
* @returns {Object} The javascript object.
|
|
284
|
-
*/
|
|
285
|
-
|
|
286
|
-
}, {
|
|
287
|
-
key: "getOriginalKeysAndValuesForFieldsThatWereUpdated",
|
|
288
|
-
value: function getOriginalKeysAndValuesForFieldsThatWereUpdated() {
|
|
289
|
-
var alwaysIncludeKeys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
290
|
-
var object = {};
|
|
291
|
-
|
|
292
|
-
if (this.elements) {
|
|
293
|
-
var _iterator = _createForOfIteratorHelper(this.elements),
|
|
294
|
-
_step;
|
|
295
|
-
|
|
296
|
-
try {
|
|
297
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
298
|
-
var element = _step.value;
|
|
299
|
-
|
|
300
|
-
if (element.isModified() && !element.isAdded() || alwaysIncludeKeys && element.key in alwaysIncludeKeys) {
|
|
301
|
-
// Using `.key` instead of `.currentKey` to ensure we look at
|
|
302
|
-
// the original field's value.
|
|
303
|
-
object[element.key] = element.generateOriginalObject();
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
if (element.isAdded() && element.currentKey !== '') {
|
|
307
|
-
// When a new field is added, check if that field
|
|
308
|
-
// was already added in the background.
|
|
309
|
-
object[element.currentKey] = {
|
|
310
|
-
$exists: false
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
} catch (err) {
|
|
315
|
-
_iterator.e(err);
|
|
316
|
-
} finally {
|
|
317
|
-
_iterator.f();
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
return object;
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Generate the query javascript object reflecting the elements that
|
|
325
|
-
* are specified by the keys listed in `keys`. The values of this object are
|
|
326
|
-
* the original values, this can be used when querying for an update based
|
|
327
|
-
* on multiple criteria.
|
|
328
|
-
*
|
|
329
|
-
* @param {Object} keys - An object whose keys are used as keys
|
|
330
|
-
* that are included in the generated query.
|
|
331
|
-
*
|
|
332
|
-
* @returns {Object} The javascript object.
|
|
333
|
-
*/
|
|
334
|
-
|
|
335
|
-
}, {
|
|
336
|
-
key: "getOriginalKeysAndValuesForSpecifiedKeys",
|
|
337
|
-
value: function getOriginalKeysAndValuesForSpecifiedKeys(keys) {
|
|
338
|
-
var object = {};
|
|
339
|
-
|
|
340
|
-
if (this.elements) {
|
|
341
|
-
var _iterator2 = _createForOfIteratorHelper(this.elements),
|
|
342
|
-
_step2;
|
|
343
|
-
|
|
344
|
-
try {
|
|
345
|
-
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
346
|
-
var element = _step2.value;
|
|
347
|
-
|
|
348
|
-
if (element.key in keys) {
|
|
349
|
-
// Using `.key` instead of `.currentKey` to ensure we look at
|
|
350
|
-
// the original field's value.
|
|
351
|
-
object[element.key] = element.generateOriginalObject();
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
} catch (err) {
|
|
355
|
-
_iterator2.e(err);
|
|
356
|
-
} finally {
|
|
357
|
-
_iterator2.f();
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
return object;
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* Generate an $set javascript object, that can be used in update operations to
|
|
365
|
-
* set the changes which have occured in the document since it was loaded.
|
|
366
|
-
*
|
|
367
|
-
* @returns {Object} The javascript update object.
|
|
368
|
-
**/
|
|
369
|
-
|
|
370
|
-
}, {
|
|
371
|
-
key: "getSetUpdateForDocumentChanges",
|
|
372
|
-
value: function getSetUpdateForDocumentChanges() {
|
|
373
|
-
var object = {};
|
|
374
|
-
|
|
375
|
-
if (this.elements) {
|
|
376
|
-
var _iterator3 = _createForOfIteratorHelper(this.elements),
|
|
377
|
-
_step3;
|
|
378
|
-
|
|
379
|
-
try {
|
|
380
|
-
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
381
|
-
var element = _step3.value;
|
|
382
|
-
|
|
383
|
-
if (!element.isRemoved() && element.currentKey !== '' && element.isModified()) {
|
|
384
|
-
// Include the full modified element.
|
|
385
|
-
// We don't individually set nested fields because we can't guarantee a
|
|
386
|
-
// path to the element using '.' dot notation will update
|
|
387
|
-
// the correct field, because field names can contain dots as of 3.6.
|
|
388
|
-
// When a nested field has been altered (changed/added/removed) it is
|
|
389
|
-
// set at the top level field. This means we overwrite possible
|
|
390
|
-
// background changes that occur within sub documents.
|
|
391
|
-
object[element.currentKey] = element.generateObject();
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
} catch (err) {
|
|
395
|
-
_iterator3.e(err);
|
|
396
|
-
} finally {
|
|
397
|
-
_iterator3.f();
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
return object;
|
|
402
|
-
}
|
|
403
|
-
/**
|
|
404
|
-
* Get the _id value as a string. Required if _id is not always an ObjectId.
|
|
405
|
-
*
|
|
406
|
-
* @returns {String} The string id.
|
|
407
|
-
*/
|
|
408
|
-
|
|
409
|
-
}, {
|
|
410
|
-
key: "getStringId",
|
|
411
|
-
value: function getStringId() {
|
|
412
|
-
var element = this.get(ID);
|
|
413
|
-
|
|
414
|
-
if (!element) {
|
|
415
|
-
return null;
|
|
416
|
-
} else if (element.currentType === 'Array' || element.currentType === 'Object') {
|
|
417
|
-
return JSON.stringify(element.generateObject());
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
return '' + element.value;
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Generate an $unset javascript object, that can be used in update
|
|
424
|
-
* operations, with the removals from the document.
|
|
425
|
-
*
|
|
426
|
-
* @returns {Object} The javascript update object.
|
|
427
|
-
**/
|
|
428
|
-
|
|
429
|
-
}, {
|
|
430
|
-
key: "getUnsetUpdateForDocumentChanges",
|
|
431
|
-
value: function getUnsetUpdateForDocumentChanges() {
|
|
432
|
-
var object = {};
|
|
433
|
-
|
|
434
|
-
if (this.elements) {
|
|
435
|
-
var _iterator4 = _createForOfIteratorHelper(this.elements),
|
|
436
|
-
_step4;
|
|
437
|
-
|
|
438
|
-
try {
|
|
439
|
-
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
440
|
-
var element = _step4.value;
|
|
441
|
-
|
|
442
|
-
if (!element.isAdded() && element.isRemoved() && element.key !== '') {
|
|
443
|
-
object[element.key] = true;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
if (!element.isAdded() && element.isRenamed() && element.key !== '') {
|
|
447
|
-
// Remove the original field when an element is renamed.
|
|
448
|
-
object[element.key] = true;
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
} catch (err) {
|
|
452
|
-
_iterator4.e(err);
|
|
453
|
-
} finally {
|
|
454
|
-
_iterator4.f();
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
return object;
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* Insert a placeholder element at the end of the document.
|
|
462
|
-
*
|
|
463
|
-
* @returns {Element} The placeholder element.
|
|
464
|
-
*/
|
|
465
|
-
|
|
466
|
-
}, {
|
|
467
|
-
key: "insertPlaceholder",
|
|
468
|
-
value: function insertPlaceholder() {
|
|
469
|
-
return this.insertEnd('', '');
|
|
470
|
-
}
|
|
471
|
-
/**
|
|
472
|
-
* Add a new element to this document.
|
|
473
|
-
*
|
|
474
|
-
* @param {String} key - The element key.
|
|
475
|
-
* @param {Object} value - The value.
|
|
476
|
-
*
|
|
477
|
-
* @returns {Element} The new element.
|
|
478
|
-
*/
|
|
479
|
-
|
|
480
|
-
}, {
|
|
481
|
-
key: "insertBeginning",
|
|
482
|
-
value: function insertBeginning(key, value) {
|
|
483
|
-
var newElement = this.elements.insertBeginning(key, value, true, this);
|
|
484
|
-
|
|
485
|
-
newElement._bubbleUp(Element.Events.Added, newElement, this);
|
|
486
|
-
|
|
487
|
-
return newElement;
|
|
488
|
-
}
|
|
489
|
-
/**
|
|
490
|
-
* Add a new element to this document.
|
|
491
|
-
*
|
|
492
|
-
* @param {String} key - The element key.
|
|
493
|
-
* @param {Object} value - The value.
|
|
494
|
-
*
|
|
495
|
-
* @returns {Element} The new element.
|
|
496
|
-
*/
|
|
497
|
-
|
|
498
|
-
}, {
|
|
499
|
-
key: "insertEnd",
|
|
500
|
-
value: function insertEnd(key, value) {
|
|
501
|
-
var newElement = this.elements.insertEnd(key, value, true, this);
|
|
502
|
-
|
|
503
|
-
newElement._bubbleUp(Element.Events.Added, newElement, this);
|
|
504
|
-
|
|
505
|
-
return newElement;
|
|
506
|
-
}
|
|
507
|
-
/**
|
|
508
|
-
* Insert an element after the provided element.
|
|
509
|
-
*
|
|
510
|
-
* @param {Element} element - The element to insert after.
|
|
511
|
-
* @param {String} key - The key.
|
|
512
|
-
* @param {Object} value - The value.
|
|
513
|
-
*
|
|
514
|
-
* @returns {Element} The new element.
|
|
515
|
-
*/
|
|
516
|
-
|
|
517
|
-
}, {
|
|
518
|
-
key: "insertAfter",
|
|
519
|
-
value: function insertAfter(element, key, value) {
|
|
520
|
-
var newElement = this.elements.insertAfter(element, key, value, true, this);
|
|
521
|
-
|
|
522
|
-
newElement._bubbleUp(Element.Events.Added, newElement, this);
|
|
523
|
-
|
|
524
|
-
return newElement;
|
|
525
|
-
}
|
|
526
|
-
/**
|
|
527
|
-
* A document always exists, is never added.
|
|
528
|
-
*
|
|
529
|
-
* @returns {false} Always false.
|
|
530
|
-
*/
|
|
531
|
-
|
|
532
|
-
}, {
|
|
533
|
-
key: "isAdded",
|
|
534
|
-
value: function isAdded() {
|
|
535
|
-
return false;
|
|
536
|
-
}
|
|
537
|
-
/**
|
|
538
|
-
* Determine if the element is modified at all.
|
|
539
|
-
*
|
|
540
|
-
* @returns {Boolean} If the element is modified.
|
|
541
|
-
*/
|
|
542
|
-
|
|
543
|
-
}, {
|
|
544
|
-
key: "isModified",
|
|
545
|
-
value: function isModified() {
|
|
546
|
-
var _iterator5 = _createForOfIteratorHelper(this.elements),
|
|
547
|
-
_step5;
|
|
548
|
-
|
|
549
|
-
try {
|
|
550
|
-
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
|
|
551
|
-
var element = _step5.value;
|
|
552
|
-
|
|
553
|
-
if (element.isModified()) {
|
|
554
|
-
return true;
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
} catch (err) {
|
|
558
|
-
_iterator5.e(err);
|
|
559
|
-
} finally {
|
|
560
|
-
_iterator5.f();
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
return false;
|
|
564
|
-
}
|
|
565
|
-
/**
|
|
566
|
-
* A document is never removed
|
|
567
|
-
*
|
|
568
|
-
* @returns {false} Always false.
|
|
569
|
-
*/
|
|
570
|
-
|
|
571
|
-
}, {
|
|
572
|
-
key: "isRemoved",
|
|
573
|
-
value: function isRemoved() {
|
|
574
|
-
return false;
|
|
575
|
-
}
|
|
576
|
-
/**
|
|
577
|
-
* The document object is always the root object.
|
|
578
|
-
*
|
|
579
|
-
* @returns {true} Always true.
|
|
580
|
-
*/
|
|
581
|
-
|
|
582
|
-
}, {
|
|
583
|
-
key: "isRoot",
|
|
584
|
-
value: function isRoot() {
|
|
585
|
-
return true;
|
|
586
|
-
}
|
|
587
|
-
/**
|
|
588
|
-
* Handle the next element in the document.
|
|
589
|
-
*/
|
|
590
|
-
|
|
591
|
-
}, {
|
|
592
|
-
key: "next",
|
|
593
|
-
value: function next() {
|
|
594
|
-
this.elements.flush();
|
|
595
|
-
var lastElement = this.elements.lastElement;
|
|
596
|
-
|
|
597
|
-
if (lastElement && lastElement.isAdded()) {
|
|
598
|
-
if (lastElement.isBlank()) {
|
|
599
|
-
lastElement.remove();
|
|
600
|
-
} else {
|
|
601
|
-
this.insertPlaceholder();
|
|
602
|
-
}
|
|
603
|
-
} else {
|
|
604
|
-
this.insertPlaceholder();
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
/**
|
|
608
|
-
* Generates a sequence of elements.
|
|
609
|
-
*
|
|
610
|
-
* @returns {Array} The elements.
|
|
611
|
-
*/
|
|
612
|
-
|
|
613
|
-
}, {
|
|
614
|
-
key: "_generateElements",
|
|
615
|
-
value: function _generateElements() {
|
|
616
|
-
return new LinkedList(this, this.doc);
|
|
617
|
-
}
|
|
618
|
-
}]);
|
|
619
|
-
|
|
620
|
-
return Document;
|
|
621
|
-
}(EventEmitter);
|
|
622
|
-
|
|
623
|
-
module.exports = Document;
|
|
624
|
-
module.exports.Events = Events;
|