UNPKG

5.11 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 { Buffer } = require('buffer');
30const SodaCollection = require('./sodaCollection.js');
31const SodaDocument = require('./sodaDocument.js');
32const errors = require('./errors.js');
33const nodbUtil = require('./util.js');
34const settings = require('./settings.js');
35
36class SodaDatabase {
37
38 _getConnection() {
39 return this._connection;
40 }
41
42 //---------------------------------------------------------------------------
43 // createCollection()
44 //
45 // Creates a SODA collection.
46 //---------------------------------------------------------------------------
47 async createCollection(name, a2) {
48 const options = {autoCommit: settings.autoCommit};
49
50 errors.assertArgCount(arguments, 1, 2);
51 errors.assertParamValue(typeof name === 'string', 1);
52
53 if (arguments.length == 2) {
54 errors.assertParamValue(nodbUtil.isObject(a2), 2);
55 if (a2.mode !== undefined) {
56 errors.assertParamPropValue(Number.isInteger(a2.mode) && a2.mode > 0,
57 2, "mode");
58 options.mode = a2.mode;
59 }
60 if (a2.metaData !== undefined) {
61 errors.assertParamPropValue(nodbUtil.isObject(a2.metaData), 2,
62 "metaData");
63 options.metaData = JSON.stringify(a2.metaData);
64 }
65 }
66
67 const coll = new SodaCollection();
68 coll._impl = await this._impl.createCollection(name, options);
69 return coll;
70 }
71
72 //---------------------------------------------------------------------------
73 // createDocument()
74 //
75 // Creates a SODA document.
76 //---------------------------------------------------------------------------
77 createDocument(content, a2) {
78 let options = {};
79
80 errors.assertArgCount(arguments, 1, 2);
81 errors.assertParamValue(Buffer.isBuffer(content) ||
82 typeof content === 'string' || nodbUtil.isObject(content), 1);
83 if (arguments.length > 1) {
84 errors.assertParamValue(nodbUtil.isObject(a2), 2);
85 options = a2;
86 errors.assertParamPropString(options, 2, "key");
87 errors.assertParamPropString(options, 2, "mediaType");
88 }
89
90 if (typeof content === 'string') {
91 content = Buffer.from(content);
92 } else if (nodbUtil.isObject(content)) {
93 content = Buffer.from(JSON.stringify(content));
94 }
95
96 const doc = new SodaDocument();
97 doc._impl = this._impl.createDocument(content, options);
98 return doc;
99 }
100
101 //---------------------------------------------------------------------------
102 // getCollectionNames()
103 //
104 // Return an array of the names of the collections in the database.
105 //---------------------------------------------------------------------------
106 async getCollectionNames(a1) {
107 let options = {};
108
109 errors.assertArgCount(arguments, 0, 1);
110 if (arguments.length == 1) {
111 errors.assertParamValue(nodbUtil.isObject(a1), 1);
112 options = a1;
113 if (options.startsWith !== undefined) {
114 errors.assertParamPropValue(typeof options.startsWith === 'string', 1,
115 "startsWith");
116 }
117 if (options.limit !== undefined) {
118 errors.assertParamPropValue(Number.isInteger(options.limit), 1,
119 "limit");
120 }
121 }
122 return await this._impl.getCollectionNames(options);
123 }
124
125 //---------------------------------------------------------------------------
126 // openCollection()
127 //
128 // Open an existing SODA collection and return it to the caller.
129 //---------------------------------------------------------------------------
130 async openCollection(name) {
131 errors.assertArgCount(arguments, 1, 1);
132 errors.assertParamValue(typeof name === 'string', 1);
133 const options = {autoCommit: settings.autoCommit};
134 const collImpl = await this._impl.openCollection(name, options);
135 if (collImpl) {
136 const coll = new SodaCollection();
137 coll._impl = collImpl;
138 return coll;
139 }
140 }
141
142}
143
144nodbUtil.wrapFns(SodaDatabase.prototype,
145 "createCollection",
146 "getCollectionNames",
147 "openCollection");
148
149module.exports = SodaDatabase;