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 | 'use strict';
|
28 |
|
29 | const { Buffer } = require('buffer');
|
30 | const SodaCollection = require('./sodaCollection.js');
|
31 | const SodaDocument = require('./sodaDocument.js');
|
32 | const errors = require('./errors.js');
|
33 | const nodbUtil = require('./util.js');
|
34 | const settings = require('./settings.js');
|
35 |
|
36 | class SodaDatabase {
|
37 |
|
38 | _getConnection() {
|
39 | return this._connection;
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
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 |
|
74 |
|
75 |
|
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 |
|
103 |
|
104 |
|
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 |
|
127 |
|
128 |
|
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 |
|
144 | nodbUtil.wrapFns(SodaDatabase.prototype,
|
145 | "createCollection",
|
146 | "getCollectionNames",
|
147 | "openCollection");
|
148 |
|
149 | module.exports = SodaDatabase;
|