UNPKG

7.84 kBJavaScriptView Raw
1// Copyright (c) 2018, 2023, Oracle and/or its affiliates.
2
3//-----------------------------------------------------------------------------
4//
5// This software is dual-licensed to you under the Universal Permissive License
6// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
7// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
8// either license.
9//
10// If you elect to accept the software under the Apache License, Version 2.0,
11// the following applies:
12//
13// Licensed under the Apache License, Version 2.0 (the "License");
14// you may not use this file except in compliance with the License.
15// You may obtain a copy of the License at
16//
17// https://www.apache.org/licenses/LICENSE-2.0
18//
19// Unless required by applicable law or agreed to in writing, software
20// distributed under the License is distributed on an "AS IS" BASIS,
21// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22// See the License for the specific language governing permissions and
23// limitations under the License.
24//
25//-----------------------------------------------------------------------------
26
27'use strict';
28
29const SodaDocCursor = require('./sodaDocCursor.js');
30const SodaDocument = require('./sodaDocument.js');
31const errors = require('./errors.js');
32const nodbUtil = require('./util.js');
33const settings = require('./settings.js');
34
35class SodaOperation {
36
37 constructor() {
38 this._options = {
39 autoCommit: settings.autoCommit,
40 fetchArraySize: settings.fetchArraySize
41 };
42 }
43
44 //---------------------------------------------------------------------------
45 // count()
46 //
47 // Return a count of the number of documents that match the search criteria.
48 //---------------------------------------------------------------------------
49 async count() {
50 errors.assertArgCount(arguments, 0, 0);
51 return await this._impl.count(this._options);
52 }
53
54 //---------------------------------------------------------------------------
55 // getCursor()
56 //
57 // Return a cursor which will return the documents that match the search
58 // criteria.
59 //---------------------------------------------------------------------------
60 async getCursor() {
61 errors.assertArgCount(arguments, 0, 0);
62 const cursor = new SodaDocCursor();
63 cursor._impl = await this._impl.getCursor(this._options);
64 return cursor;
65 }
66
67 //---------------------------------------------------------------------------
68 // getDocuments()
69 // Return an array of documents that match the search criteria.
70 //---------------------------------------------------------------------------
71 async getDocuments() {
72 errors.assertArgCount(arguments, 0, 0);
73 const docImpls = await this._impl.getDocuments(this._options);
74 const returnVal = new Array(docImpls.length);
75 for (let i = 0; i < docImpls.length; i++) {
76 returnVal[i] = new SodaDocument();
77 returnVal[i]._impl = docImpls[i];
78 }
79 return returnVal;
80 }
81
82 //---------------------------------------------------------------------------
83 // getOne()
84 //
85 // Return the first document that matches the search criteria.
86 //---------------------------------------------------------------------------
87 async getOne() {
88 errors.assertArgCount(arguments, 0, 0);
89 const docImpl = await this._impl.getOne(this._options);
90 if (docImpl) {
91 const doc = new SodaDocument;
92 doc._impl = docImpl;
93 return doc;
94 }
95 }
96
97
98 //---------------------------------------------------------------------------
99 // lock()
100 //
101 // Pessimistic locking - similar to SELECT FOR UPDATE, these documents
102 // cannot be updated by other threads until an explicit commit/rollback is
103 // called. With autoCommit set to true is applicable only for one immediate
104 // operation and is not recommended in this context
105 //---------------------------------------------------------------------------
106 lock() {
107 errors.assertArgCount(arguments, 0, 0);
108 this._options.lock = true;
109 return this;
110 }
111
112
113 //---------------------------------------------------------------------------
114 // replaceOne()
115 //
116 // Replace the first document that matches the search criteria with the
117 // specified document.
118 //---------------------------------------------------------------------------
119 async replaceOne(content) {
120 errors.assertArgCount(arguments, 1, 1);
121 content = nodbUtil.verifySodaDoc(content);
122 return await this._impl.replaceOne(this._options, content);
123 }
124
125 //---------------------------------------------------------------------------
126 // replaceOneAndGet()
127 //
128 // Replace the first document that matches the search criteria with the
129 // specified document and then return a result document containing metadata.
130 //---------------------------------------------------------------------------
131 async replaceOneAndGet(content) {
132 errors.assertArgCount(arguments, 1, 1);
133 content = nodbUtil.verifySodaDoc(content);
134 const docImpl = await this._impl.replaceOneAndGet(this._options, content);
135 if (docImpl) {
136 const doc = new SodaDocument();
137 doc._impl = docImpl;
138 return doc;
139 }
140 }
141
142 //---------------------------------------------------------------------------
143 // remove()
144 //
145 // Remove the documents that match the search criteria from the collection
146 // and return information about the operation to the caller.
147 //---------------------------------------------------------------------------
148 async remove() {
149 errors.assertArgCount(arguments, 0, 0);
150 return await this._impl.remove(this._options);
151 }
152
153 // fetchArraySize - a non-terminal function that can chain further
154 fetchArraySize(n) {
155 errors.assertArgCount(arguments, 1, 1);
156 errors.assertParamValue(Number.isInteger(n) && n >= 0, 1);
157 this._options.fetchArraySize = n;
158 return this;
159 }
160
161 // filter property - a non-terminal function and can chain further
162 filter(f) {
163 errors.assertArgCount (arguments, 1, 1);
164 errors.assertParamValue(nodbUtil.isObject(f), 1);
165 this._options.filter = JSON.stringify(f);
166 return this;
167 }
168
169 // hint - a non-terminal function and can chain further
170 hint(val) {
171 errors.assertArgCount(arguments, 1, 1);
172 errors.assertParamValue(typeof val === 'string', 1);
173 this._options.hint = val;
174 return this;
175 }
176
177 // key - a non-terminal function and can chain further
178 key(k) {
179 errors.assertArgCount(arguments, 1, 1);
180 errors.assertParamValue(typeof k === 'string', 1);
181 this._options.key = k;
182 this._options.keys = undefined;
183 return this;
184 }
185
186 // keys - a non-terminal function and can chain further
187 keys(arr) {
188 errors.assertArgCount(arguments, 1, 1);
189 errors.assertParamValue(Array.isArray(arr), 1);
190
191 for (let i = 0; i < arr.length; i++) {
192 errors.assertParamValue(typeof arr[i] === 'string', 1);
193 }
194
195 this._options.keys = arr;
196 this._options.key = undefined;
197 return this;
198 }
199
200 // limit property - a non-terminal function and can chain further
201 limit(n) {
202 errors.assertArgCount(arguments, 1, 1);
203 errors.assertParamValue(typeof n === 'number', 1);
204 this._options.limit = n;
205 return this;
206 }
207
208 // skip property - a non-terminal function and can chain further
209 skip(n) {
210 errors.assertArgCount(arguments, 1, 1);
211 errors.assertParamValue(typeof n === 'number', 1);
212 this._options.skip = n;
213 return this;
214 }
215
216 // version property - a non-terminal function and can chain further
217 version(v) {
218 errors.assertArgCount(arguments, 1, 1);
219 errors.assertParamValue(typeof v === 'string', 1);
220 this._options.version = v;
221 return this;
222 }
223
224}
225
226nodbUtil.wrapFns(SodaOperation.prototype,
227 "count",
228 "getCursor",
229 "getDocuments",
230 "getOne",
231 "remove",
232 "replaceOne",
233 "replaceOneAndGet");
234
235module.exports = SodaOperation;