UNPKG

15.6 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.MutateInSpec = exports.LookupInSpec = exports.MutateInMacro = exports.LookupInMacro = void 0;
7const binding_1 = __importDefault(require("./binding"));
8/**
9 * Represents a macro that can be passed to a lookup-in operation to
10 * fetch special values such as the expiry, cas, etc...
11 *
12 * @category Key-Value
13 */
14class LookupInMacro {
15 constructor(value) {
16 this._value = value;
17 }
18 /**
19 * A macro which references the entirety of the document meta-data.
20 */
21 static get Document() {
22 return new LookupInMacro('$document');
23 }
24 /**
25 * A macro which references the expiry of a document.
26 */
27 static get Expiry() {
28 return new LookupInMacro('$document.exptime');
29 }
30 /**
31 * A macro which references the cas of a document.
32 */
33 static get Cas() {
34 return new LookupInMacro('$document.CAS');
35 }
36 /**
37 * A macro which references the seqno of a document.
38 */
39 static get SeqNo() {
40 return new LookupInMacro('$document.seqno');
41 }
42 /**
43 * A macro which references the last modified time of a document.
44 */
45 static get LastModified() {
46 return new LookupInMacro('$document.last_modified');
47 }
48 /**
49 * A macro which references the deletion state of a document. This
50 * only makes sense to use in concert with the internal AccessDeleted
51 * flags, which are internal.
52 */
53 static get IsDeleted() {
54 return new LookupInMacro('$document.deleted');
55 }
56 /**
57 * A macro which references the size of a document, expressed in bytes.
58 */
59 static get ValueSizeBytes() {
60 return new LookupInMacro('$document.value_bytes');
61 }
62 /**
63 * A macro which references the revision id of a document.
64 */
65 static get RevId() {
66 return new LookupInMacro('$document.revid');
67 }
68}
69exports.LookupInMacro = LookupInMacro;
70/**
71 * Represents a macro that can be passed to a mutate-in operation to
72 * write special values such as the expiry, cas, etc...
73 *
74 * @category Key-Value
75 */
76class MutateInMacro {
77 constructor(value) {
78 this._value = value;
79 }
80 /**
81 * A macro which references the cas of a document.
82 */
83 static get Cas() {
84 return new MutateInMacro('${Mutation.CAS}');
85 }
86 /**
87 * A macro which references the seqno of a document.
88 */
89 static get SeqNo() {
90 return new MutateInMacro('${Mutation.seqno}');
91 }
92 /**
93 * A macro which references the crc32 of the value of a document.
94 */
95 static get ValueCrc32c() {
96 return new MutateInMacro('${Mutation.value_crc32c}');
97 }
98}
99exports.MutateInMacro = MutateInMacro;
100/**
101 * Represents a sub-operation to perform within a lookup-in operation.
102 *
103 * @category Key-Value
104 */
105class LookupInSpec {
106 /**
107 * BUG(JSCBC-756): Previously provided access to the expiry macro for a
108 * lookup-in operation.
109 *
110 * @deprecated Use {@link LookupInMacro.Expiry} instead.
111 */
112 static get Expiry() {
113 return LookupInMacro.Expiry;
114 }
115 constructor(op, path, flags) {
116 this._op = op;
117 this._path = path;
118 this._flags = flags;
119 }
120 static _create(op, path, options) {
121 if (!options) {
122 options = {};
123 }
124 let flags = 0;
125 if (path instanceof LookupInMacro) {
126 path = path._value;
127 flags |=
128 binding_1.default.protocol_lookup_in_request_body_lookup_in_specs_path_flag.xattr;
129 }
130 if (options.xattr) {
131 flags |=
132 binding_1.default.protocol_lookup_in_request_body_lookup_in_specs_path_flag.xattr;
133 }
134 return new LookupInSpec(op, path, flags);
135 }
136 /**
137 * Creates a LookupInSpec for fetching a field from the document.
138 *
139 * @param path The path to the field.
140 * @param options Optional parameters for this operation.
141 * @param options.xattr
142 * Whether this operation should reference the document body or the extended
143 * attributes data for the document.
144 */
145 static get(path, options) {
146 if (!path) {
147 return this._create(binding_1.default.protocol_subdoc_opcode.get_doc, '', options);
148 }
149 return this._create(binding_1.default.protocol_subdoc_opcode.get, path, options);
150 }
151 /**
152 * Returns whether a specific field exists in the document.
153 *
154 * @param path The path to the field.
155 * @param options Optional parameters for this operation.
156 * @param options.xattr
157 * Whether this operation should reference the document body or the extended
158 * attributes data for the document.
159 */
160 static exists(path, options) {
161 return this._create(binding_1.default.protocol_subdoc_opcode.exists, path, options);
162 }
163 /**
164 * Returns the number of elements in the array reference by the path.
165 *
166 * @param path The path to the field.
167 * @param options Optional parameters for this operation.
168 * @param options.xattr
169 * Whether this operation should reference the document body or the extended
170 * attributes data for the document.
171 */
172 static count(path, options) {
173 return this._create(binding_1.default.protocol_subdoc_opcode.get_count, path, options);
174 }
175}
176exports.LookupInSpec = LookupInSpec;
177/**
178 * Represents a sub-operation to perform within a mutate-in operation.
179 *
180 * @category Key-Value
181 */
182class MutateInSpec {
183 /**
184 * BUG(JSCBC-756): Previously provided access to the document cas mutate
185 * macro.
186 *
187 * @deprecated Use {@link MutateInMacro.Cas} instead.
188 */
189 static get CasPlaceholder() {
190 return MutateInMacro.Cas;
191 }
192 constructor(op, path, flags, data) {
193 this._op = op;
194 this._path = path;
195 this._flags = flags;
196 this._data = data;
197 }
198 static _create(op, path, value, options) {
199 if (!options) {
200 options = {};
201 }
202 let flags = 0;
203 if (options.createPath) {
204 flags |=
205 binding_1.default.protocol_mutate_in_request_body_mutate_in_specs_path_flag
206 .create_parents;
207 }
208 if (value instanceof MutateInMacro) {
209 value = value._value;
210 flags |=
211 binding_1.default.protocol_mutate_in_request_body_mutate_in_specs_path_flag
212 .expand_macros |
213 binding_1.default.protocol_mutate_in_request_body_mutate_in_specs_path_flag.xattr;
214 }
215 else if (options.xattr) {
216 flags |=
217 binding_1.default.protocol_mutate_in_request_body_mutate_in_specs_path_flag.xattr;
218 }
219 if (value !== undefined) {
220 // BUG(JSCBC-755): As a solution to our oversight of not accepting arrays of
221 // values to various sub-document operations, we have exposed an option instead.
222 if (!options.multi) {
223 value = JSON.stringify(value);
224 }
225 else {
226 if (!Array.isArray(value)) {
227 throw new Error('value must be an array for a multi operation');
228 }
229 value = JSON.stringify(value);
230 value = value.substr(1, value.length - 2);
231 }
232 }
233 return new MutateInSpec(op, path, flags, value);
234 }
235 /**
236 * Creates a MutateInSpec for inserting a field into the document. Failing if
237 * the field already exists at the specified path.
238 *
239 * @param path The path to the field.
240 * @param value The value to insert.
241 * @param options Optional parameters for this operation.
242 * @param options.createPath
243 * Whether or not the path to the field should be created if it does not
244 * already exist.
245 * @param options.xattr
246 * Whether this operation should reference the document body or the extended
247 * attributes data for the document.
248 */
249 static insert(path, value, options) {
250 return this._create(binding_1.default.protocol_subdoc_opcode.dict_add, path, value, options);
251 }
252 /**
253 * Creates a MutateInSpec for upserting a field on a document. This updates
254 * the value of the specified field, or creates the field if it does not exits.
255 *
256 * @param path The path to the field.
257 * @param value The value to write.
258 * @param options Optional parameters for this operation.
259 * @param options.createPath
260 * Whether or not the path to the field should be created if it does not
261 * already exist.
262 * @param options.xattr
263 * Whether this operation should reference the document body or the extended
264 * attributes data for the document.
265 */
266 static upsert(path, value, options) {
267 if (!path) {
268 return this._create(binding_1.default.protocol_subdoc_opcode.set_doc, '', value, options);
269 }
270 return this._create(binding_1.default.protocol_subdoc_opcode.dict_upsert, path, value, options);
271 }
272 /**
273 * Creates a MutateInSpec for replacing a field on a document. This updates
274 * the value of the specified field, failing if the field does not exits.
275 *
276 * @param path The path to the field.
277 * @param value The value to write.
278 * @param options Optional parameters for this operation.
279 * @param options.xattr
280 * Whether this operation should reference the document body or the extended
281 * attributes data for the document.
282 */
283 static replace(path, value, options) {
284 return this._create(binding_1.default.protocol_subdoc_opcode.replace, path, value, options);
285 }
286 /**
287 * Creates a MutateInSpec for remove a field from a document.
288 *
289 * @param path The path to the field.
290 * @param options Optional parameters for this operation.
291 * @param options.xattr
292 * Whether this operation should reference the document body or the extended
293 * attributes data for the document.
294 */
295 static remove(path, options) {
296 if (!path) {
297 return this._create(binding_1.default.protocol_subdoc_opcode.remove_doc, '', undefined, options);
298 }
299 return this._create(binding_1.default.protocol_subdoc_opcode.remove, path, undefined, options);
300 }
301 /**
302 * Creates a MutateInSpec for adding a value to the end of an array in a document.
303 *
304 * @param path The path to the field.
305 * @param value The value to add.
306 * @param options Optional parameters for this operation.
307 * @param options.createPath
308 * Whether or not the path to the field should be created if it does not
309 * already exist.
310 * @param options.multi
311 * If set, this enables an array of values to be passed as value, and each
312 * element of the passed array is added to the array.
313 * @param options.xattr
314 * Whether this operation should reference the document body or the extended
315 * attributes data for the document.
316 */
317 static arrayAppend(path, value, options) {
318 return this._create(binding_1.default.protocol_subdoc_opcode.array_push_last, path, value, options);
319 }
320 /**
321 * Creates a MutateInSpec for adding a value to the beginning of an array in a document.
322 *
323 * @param path The path to the field.
324 * @param value The value to add.
325 * @param options Optional parameters for this operation.
326 * @param options.createPath
327 * Whether or not the path to the field should be created if it does not
328 * already exist.
329 * @param options.multi
330 * If set, this enables an array of values to be passed as value, and each
331 * element of the passed array is added to the array.
332 * @param options.xattr
333 * Whether this operation should reference the document body or the extended
334 * attributes data for the document.
335 */
336 static arrayPrepend(path, value, options) {
337 return this._create(binding_1.default.protocol_subdoc_opcode.array_push_first, path, value, options);
338 }
339 /**
340 * Creates a MutateInSpec for adding a value to a specified location in an array in a
341 * document. The path should specify a specific index in the array and the new values
342 * are inserted at this location.
343 *
344 * @param path The path to an element of an array.
345 * @param value The value to add.
346 * @param options Optional parameters for this operation.
347 * @param options.createPath
348 * Whether or not the path to the field should be created if it does not
349 * already exist.
350 * @param options.multi
351 * If set, this enables an array of values to be passed as value, and each
352 * element of the passed array is added to the array.
353 * @param options.xattr
354 * Whether this operation should reference the document body or the extended
355 * attributes data for the document.
356 */
357 static arrayInsert(path, value, options) {
358 return this._create(binding_1.default.protocol_subdoc_opcode.array_insert, path, value, options);
359 }
360 /**
361 * Creates a MutateInSpec for adding unique values to an array in a document. This
362 * operation will only add values if they do not already exist elsewhere in the array.
363 *
364 * @param path The path to the field.
365 * @param value The value to add.
366 * @param options Optional parameters for this operation.
367 * @param options.createPath
368 * Whether or not the path to the field should be created if it does not
369 * already exist.
370 * @param options.multi
371 * If set, this enables an array of values to be passed as value, and each
372 * element of the passed array is added to the array.
373 * @param options.xattr
374 * Whether this operation should reference the document body or the extended
375 * attributes data for the document.
376 */
377 static arrayAddUnique(path, value, options) {
378 return this._create(binding_1.default.protocol_subdoc_opcode.array_add_unique, path, value, options);
379 }
380 /**
381 * Creates a MutateInSpec for incrementing the value of a field in a document.
382 *
383 * @param path The path to the field.
384 * @param value The value to add.
385 * @param options Optional parameters for this operation.
386 * @param options.createPath
387 * Whether or not the path to the field should be created if it does not
388 * already exist.
389 * @param options.xattr
390 * Whether this operation should reference the document body or the extended
391 * attributes data for the document.
392 */
393 static increment(path, value, options) {
394 return this._create(binding_1.default.protocol_subdoc_opcode.counter, path, +value, options);
395 }
396 /**
397 * Creates a MutateInSpec for decrementing the value of a field in a document.
398 *
399 * @param path The path to the field.
400 * @param value The value to subtract.
401 * @param options Optional parameters for this operation.
402 * @param options.createPath
403 * Whether or not the path to the field should be created if it does not
404 * already exist.
405 * @param options.xattr
406 * Whether this operation should reference the document body or the extended
407 * attributes data for the document.
408 */
409 static decrement(path, value, options) {
410 return this._create(binding_1.default.protocol_subdoc_opcode.counter, path, +value, options);
411 }
412}
413exports.MutateInSpec = MutateInSpec;