UNPKG

2.64 kBJavaScriptView Raw
1// Copyright (c) 2018, 2024, 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 errors = require('./errors.js');
30const nodbUtil = require('./util.js');
31const SodaDocument = require('./sodaDocument.js');
32
33class SodaDocCursor {
34
35 //---------------------------------------------------------------------------
36 // close()
37 //
38 // Close the cursor and make it unusable for further operations.
39 //--------------------------------------------------------------------------
40 async close() {
41 errors.assertArgCount(arguments, 0, 0);
42 errors.assert(this._impl, errors.ERR_INVALID_SODA_DOC_CURSOR);
43 await this._impl.close();
44 delete this._impl;
45 }
46
47 //---------------------------------------------------------------------------
48 // getNext()
49 //
50 // Return the next document available from the cursor.
51 //---------------------------------------------------------------------------
52 async getNext() {
53 errors.assertArgCount(arguments, 0, 0);
54 errors.assert(this._impl, errors.ERR_INVALID_SODA_DOC_CURSOR);
55 const docImpl = await this._impl.getNext();
56 if (docImpl) {
57 const doc = new SodaDocument();
58 doc._impl = docImpl;
59 return doc;
60 }
61 }
62
63 [Symbol.asyncIterator]() {
64 const cursor = this;
65 return {
66 async next() {
67 const doc = await cursor.getNext();
68 return {value: doc, done: doc === undefined};
69 },
70 return() {
71 return {done: true};
72 }
73 };
74 }
75
76}
77
78nodbUtil.wrapFns(SodaDocCursor.prototype,
79 "close",
80 "getNext");
81
82module.exports = SodaDocCursor;