UNPKG

9.79 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 SodaDocument = require('./sodaDocument.js');
30const SodaOperation = require('./sodaOperation.js');
31const errors = require('./errors.js');
32const nodbUtil = require('./util.js');
33const settings = require('./settings.js');
34
35class SodaCollection {
36
37 //---------------------------------------------------------------------------
38 // createIndex()
39 //
40 // Create an index on the collection.
41 //---------------------------------------------------------------------------
42 async createIndex(spec) {
43 errors.assertArgCount(arguments, 1, 1);
44 errors.assertParamValue(nodbUtil.isObject(spec), 1);
45 const options = {autoCommit: settings.autoCommit};
46 return await this._impl.createIndex(JSON.stringify(spec), options);
47 }
48
49 //---------------------------------------------------------------------------
50 // drop()
51 //
52 // Drop the collection.
53 //---------------------------------------------------------------------------
54 async drop() {
55 errors.assertArgCount(arguments, 0, 0);
56 const options = {autoCommit: settings.autoCommit};
57 return await this._impl.drop(options);
58 }
59
60 //---------------------------------------------------------------------------
61 // dropIndex()
62 //
63 // Drop an index on the collection.
64 //---------------------------------------------------------------------------
65 async dropIndex(indexName, a2) {
66 const options = {autoCommit: settings.autoCommit};
67
68 errors.assertArgCount(arguments, 1, 2);
69 errors.assertParamValue(typeof indexName === 'string', 1);
70 if (arguments.length == 2) {
71 errors.assertParamValue(typeof a2 === 'object', 2);
72 if (a2.force !== undefined) {
73 errors.assertParamPropValue(typeof a2.force === 'boolean', 2, "force");
74 options.force = a2.force;
75 }
76 }
77 return await this._impl.dropIndex(indexName, options);
78 }
79
80 //---------------------------------------------------------------------------
81 // find()
82 //
83 // Returns a SODA operation associated with the collection.
84 //---------------------------------------------------------------------------
85 find() {
86 errors.assertArgCount(arguments, 0, 0);
87 const op = new SodaOperation();
88 op._impl = this._impl.find();
89 return op;
90 }
91
92 //---------------------------------------------------------------------------
93 // getDataGuide()
94 // Return the data guide for the collection.
95 //---------------------------------------------------------------------------
96 async getDataGuide() {
97 errors.assertArgCount(arguments, 0, 0);
98 const doc = new SodaDocument();
99 doc._impl = await this._impl.getDataGuide();
100 return doc;
101 }
102
103 //---------------------------------------------------------------------------
104 // insertMany()
105 //
106 // Insert an array of documents into the collection in a single round-trip.
107 //---------------------------------------------------------------------------
108 async insertMany(docs) {
109 errors.assertArgCount(arguments, 1, 1);
110 errors.assertParamValue(Array.isArray(docs) && docs.length > 0, 1);
111
112 const actualDocs = Array(docs.length);
113 for (let i = 0; i < docs.length; i++) {
114 actualDocs[i] = nodbUtil.verifySodaDoc(docs[i]);
115 }
116
117 const options = {autoCommit: settings.autoCommit};
118 await this._impl.insertMany(actualDocs, options);
119 }
120
121 //---------------------------------------------------------------------------
122 // insertManyAndGet()
123 //
124 // Insert an array of documents into the collection in a single round-trip
125 // and return a set of result documents containing metadata.
126 //---------------------------------------------------------------------------
127 async insertManyAndGet(docs, a2) {
128 const options = {autoCommit: settings.autoCommit};
129
130 errors.assertArgCount(arguments, 1, 2);
131 errors.assertParamValue(Array.isArray(docs) && docs.length > 0, 1);
132
133 if (arguments.length == 2) {
134 errors.assertParamValue(nodbUtil.isObject(a2), 2);
135 if (a2.hint !== undefined) {
136 errors.assertParamPropValue(typeof a2.hint === 'string', 2, "hint");
137 options.hint = a2.hint;
138 }
139 }
140
141 const actualDocs = Array(docs.length);
142 for (let i = 0; i < docs.length; i++) {
143 actualDocs[i] = nodbUtil.verifySodaDoc(docs[i]);
144 }
145
146 const docImpls = await this._impl.insertManyAndGet(actualDocs, options);
147 for (let i = 0; i < docs.length; i++) {
148 const doc = actualDocs[i] = new SodaDocument();
149 doc._impl = docImpls[i];
150 }
151
152 return actualDocs;
153 }
154
155 //---------------------------------------------------------------------------
156 // insertOne()
157 //
158 // Inserts a single document into the collection.
159 //---------------------------------------------------------------------------
160 async insertOne(content) {
161 errors.assertArgCount(arguments, 1, 1);
162 content = nodbUtil.verifySodaDoc(content);
163 const options = {autoCommit: settings.autoCommit};
164 await this._impl.insertOne(content, options);
165 }
166
167 //---------------------------------------------------------------------------
168 // insertOneAndGet()
169 //
170 // Inserts a single document into the collection and returns a result
171 // document containing metadata.
172 //---------------------------------------------------------------------------
173 async insertOneAndGet(content, a2) {
174 const options = {autoCommit: settings.autoCommit};
175
176 errors.assertArgCount(arguments, 1, 2);
177 content = nodbUtil.verifySodaDoc(content);
178 if (arguments.length == 2) {
179 errors.assertParamValue(nodbUtil.isObject(a2), 2);
180 if (a2.hint !== undefined) {
181 errors.assertParamPropValue(typeof a2.hint === 'string', 2, "hint");
182 options.hint = a2.hint;
183 }
184 }
185
186 const doc = new SodaDocument();
187 doc._impl = await this._impl.insertOneAndGet(content, options);
188 return doc;
189 }
190
191 //---------------------------------------------------------------------------
192 // listIndexes()
193 //
194 // To obtain all indices from the collection
195 //---------------------------------------------------------------------------
196 async listIndexes() {
197 const arr = await this._impl.listIndexes();
198 return arr.map(i => JSON.parse(i));
199 }
200
201 //---------------------------------------------------------------------------
202 // metaData()
203 //
204 // Property for the metadata associated with the collection.
205 //---------------------------------------------------------------------------
206 get metaData() {
207 return JSON.parse(this._impl.getMetaData());
208 }
209
210 //---------------------------------------------------------------------------
211 // name()
212 //
213 // Property for the name of the collection.
214 //---------------------------------------------------------------------------
215 get name() {
216 return this._impl.getName();
217 }
218
219 //---------------------------------------------------------------------------
220 // save()
221 //
222 // Saves a single document into the collection.
223 //---------------------------------------------------------------------------
224 async save(content) {
225 errors.assertArgCount(arguments, 1, 1);
226 content = nodbUtil.verifySodaDoc(content);
227 const options = {autoCommit: settings.autoCommit};
228 await this._impl.save(content, options);
229 }
230
231 //---------------------------------------------------------------------------
232 // saveAndGet()
233 //
234 // Saves a single document into the collection and returns a result document
235 // containing metadata.
236 //---------------------------------------------------------------------------
237 async saveAndGet(content, a2) {
238 errors.assertArgCount(arguments, 1, 2);
239 content = nodbUtil.verifySodaDoc(content);
240 const options = {autoCommit: settings.autoCommit};
241 if (arguments.length == 2) {
242 errors.assertParamValue(nodbUtil.isObject(a2), 2);
243 if (a2.hint !== undefined) {
244 errors.assertParamPropValue(typeof a2.hint === 'string', 2, "hint");
245 options.hint = a2.hint;
246 }
247 }
248
249 const doc = new SodaDocument();
250 doc._impl = await this._impl.saveAndGet(content, options);
251 return doc;
252 }
253
254 //---------------------------------------------------------------------------
255 // truncate()
256 //
257 // Remove all of the documents from a collection.
258 //---------------------------------------------------------------------------
259 async truncate() {
260 errors.assertArgCount(arguments, 0, 0);
261 await this._impl.truncate();
262 }
263
264}
265
266nodbUtil.wrapFns(SodaCollection.prototype,
267 "createIndex",
268 "drop",
269 "dropIndex",
270 "getDataGuide",
271 "insertMany",
272 "insertManyAndGet",
273 "insertOne",
274 "insertOneAndGet",
275 "listIndexes",
276 "save",
277 "saveAndGet",
278 "truncate");
279
280module.exports = SodaCollection;