333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved
 
//-----------------------------------------------------------------------------
//
// You may not use the identified files except in compliance with the Apache
// License, Version 2.0 (the "License.")
//
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-----------------------------------------------------------------------------
 
'use strict';
 
const nodbUtil = require('./util.js');
 
//-----------------------------------------------------------------------------
// createCollection()
//   Creates a SODA collection.
//-----------------------------------------------------------------------------
async function createCollection(name, a2) {
  let options = {};
 
  nodbUtil.checkArgCount(arguments, 1, 2);
  nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
 
  if (arguments.length == 2) {
    nodbUtil.assert(nodbUtil.isObject(a2), 'NJS-005', 2);
    options = a2;
    if (options.metaData) {
      if (!nodbUtil.isObject(options.metaData)) {
        throw new Error(nodbUtil.getErrorMessage('NJS-005', 2));
      }
      options.metaData = JSON.stringify(options.metaData);
    }
  }
 
  return await this._createCollection(name, options);
}
 
 
//-----------------------------------------------------------------------------
// openCollection()
//   Open an existing SODA collection and return it to the caller.
//-----------------------------------------------------------------------------
async function openCollection(name) {
  nodbUtil.checkArgCount(arguments, 1, 1);
  nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
  return await this._openCollection(name);
}
 
 
//-----------------------------------------------------------------------------
// getCollectionNames()
//   Return an array of the names of the collections in the database.
//-----------------------------------------------------------------------------
async function getCollectionNames(a1) {
  let options = {};
 
  nodbUtil.checkArgCount(arguments, 0, 1);
  if (arguments.length == 1) {
    nodbUtil.assert(nodbUtil.isObject(a1), 'NJS-005', 1);
    options = a1;
  }
  return await this._getCollectionNames(options);
}
 
 
class SodaDatabase {
 
  _extend() {
    this.createCollection = nodbUtil.callbackify(createCollection);
    this.getCollectionNames = nodbUtil.callbackify(getCollectionNames);
    this.openCollection = nodbUtil.callbackify(openCollection);
  }
 
  // To create a SODA document object based content and (optional) other fields
  createDocument(content, a2) {
    let options = {};
 
    nodbUtil.checkArgCount(arguments, 1, 2);
    nodbUtil.assert(Buffer.isBuffer(content) || typeof content === 'string' ||
        nodbUtil.isObject(content), 'NJS-005', 1);
    if (arguments.length > 1) {
      nodbUtil.assert(nodbUtil.isObject(a2), 'NJS-005', 2);
      options = a2;
    }
 
    if (typeof content === 'string') {
      content = Buffer.from(content);
    } else if (nodbUtil.isObject(content)) {
      content = Buffer.from(JSON.stringify(content));
    }
 
    return this._createDocument(content, options);
  }
 
}
 
 
module.exports = SodaDatabase;