UNPKG

10.3 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.CollectionManager = exports.ScopeSpec = exports.CollectionSpec = void 0;
4const bindingutilities_1 = require("./bindingutilities");
5const utilities_1 = require("./utilities");
6/**
7 * Contains information about a collection.
8 *
9 * @category Management
10 */
11class CollectionSpec {
12 /**
13 * @internal
14 */
15 constructor(data) {
16 this.name = data.name;
17 this.scopeName = data.scopeName;
18 this.maxExpiry = data.maxExpiry;
19 this.history = data.history;
20 }
21 /**
22 * @internal
23 */
24 static _fromCppData(scopeName, data) {
25 return new CollectionSpec({
26 name: data.name,
27 scopeName: scopeName,
28 maxExpiry: data.max_expiry,
29 history: data.history,
30 });
31 }
32}
33exports.CollectionSpec = CollectionSpec;
34/**
35 * Contains information about a scope.
36 *
37 * @category Management
38 */
39class ScopeSpec {
40 /**
41 * @internal
42 */
43 constructor(data) {
44 this.name = data.name;
45 this.collections = data.collections;
46 }
47 /**
48 * @internal
49 */
50 static _fromCppData(data) {
51 let collections;
52 if (data.collections.length > 0) {
53 const scopeName = data.name;
54 collections = data.collections.map((collectionData) => CollectionSpec._fromCppData(scopeName, collectionData));
55 }
56 else {
57 collections = [];
58 }
59 return new ScopeSpec({
60 name: data.name,
61 collections: collections,
62 });
63 }
64}
65exports.ScopeSpec = ScopeSpec;
66/**
67 * CollectionManager allows the management of collections within a Bucket.
68 *
69 * @category Management
70 */
71class CollectionManager {
72 /**
73 * @internal
74 */
75 constructor(bucket) {
76 this._bucket = bucket;
77 }
78 get _cluster() {
79 return this._bucket.cluster;
80 }
81 /**
82 * Returns all configured scopes along with their collections.
83 *
84 * @param options Optional parameters for this operation.
85 * @param callback A node-style callback to be invoked after execution.
86 */
87 async getAllScopes(options, callback) {
88 if (options instanceof Function) {
89 callback = arguments[0];
90 options = undefined;
91 }
92 if (!options) {
93 options = {};
94 }
95 const bucketName = this._bucket.name;
96 const timeout = options.timeout || this._cluster.managementTimeout;
97 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
98 this._cluster.conn.managementScopeGetAll({
99 bucket_name: bucketName,
100 timeout: timeout,
101 }, (cppErr, resp) => {
102 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
103 if (err) {
104 return wrapCallback(err, null);
105 }
106 const scopes = resp.manifest.scopes.map((scopeData) => ScopeSpec._fromCppData(scopeData));
107 wrapCallback(null, scopes);
108 });
109 }, callback);
110 }
111 /**
112 * @internal
113 */
114 async createCollection() {
115 let collectionName = arguments[0];
116 let scopeName = arguments[1];
117 let settings = arguments[2];
118 let options = arguments[3];
119 let callback = arguments[4];
120 // Deprecated usage conversion for (CollectionSpec, options, callback)
121 if (typeof collectionName === 'object') {
122 const spec = collectionName;
123 collectionName = spec.name;
124 scopeName = spec.scopeName;
125 settings = {
126 maxExpiry: spec.maxExpiry,
127 history: spec.history,
128 };
129 options = arguments[1];
130 callback = arguments[2];
131 if (options instanceof Function) {
132 callback = arguments[1];
133 options = undefined;
134 }
135 }
136 // Handling of callbacks for alternative overloads
137 if (settings instanceof Function) {
138 callback = arguments[2];
139 settings = undefined;
140 }
141 else if (options instanceof Function) {
142 callback = arguments[3];
143 options = undefined;
144 }
145 if (!options) {
146 options = {};
147 }
148 if (!settings) {
149 settings = {};
150 }
151 const bucketName = this._bucket.name;
152 const timeout = options.timeout || this._cluster.managementTimeout;
153 const maxExpiry = settings.maxExpiry || 0;
154 const history = settings.history || undefined;
155 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
156 this._cluster.conn.managementCollectionCreate({
157 bucket_name: bucketName,
158 scope_name: scopeName,
159 collection_name: collectionName,
160 max_expiry: maxExpiry,
161 history: history,
162 timeout: timeout,
163 }, (cppErr) => {
164 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
165 if (err) {
166 return wrapCallback(err, null);
167 }
168 wrapCallback(err);
169 });
170 }, callback);
171 }
172 /**
173 * Drops a collection from a scope.
174 *
175 * @param collectionName The name of the collection to drop.
176 * @param scopeName The name of the scope containing the collection to drop.
177 * @param options Optional parameters for this operation.
178 * @param callback A node-style callback to be invoked after execution.
179 */
180 async dropCollection(collectionName, scopeName, options, callback) {
181 if (options instanceof Function) {
182 callback = arguments[2];
183 options = undefined;
184 }
185 if (!options) {
186 options = {};
187 }
188 const bucketName = this._bucket.name;
189 const timeout = options.timeout || this._cluster.managementTimeout;
190 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
191 this._cluster.conn.managementCollectionDrop({
192 bucket_name: bucketName,
193 scope_name: scopeName,
194 collection_name: collectionName,
195 timeout: timeout,
196 }, (cppErr) => {
197 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
198 if (err) {
199 return wrapCallback(err, null);
200 }
201 wrapCallback(err);
202 });
203 }, callback);
204 }
205 /**
206 * Updates a collection in a scope.
207 *
208 * @param collectionName The name of the collection to update.
209 * @param scopeName The name of the scope containing the collection.
210 * @param settings The settings to update on the collection.
211 * @param options Optional parameters for this operation.
212 * @param callback A node-style callback to be invoked after execution.
213 */
214 async updateCollection(collectionName, scopeName, settings, options, callback) {
215 if (options instanceof Function) {
216 callback = arguments[3];
217 options = undefined;
218 }
219 if (!options) {
220 options = {};
221 }
222 const bucketName = this._bucket.name;
223 const timeout = options.timeout || this._cluster.managementTimeout;
224 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
225 this._cluster.conn.managementCollectionUpdate({
226 bucket_name: bucketName,
227 scope_name: scopeName,
228 collection_name: collectionName,
229 max_expiry: settings.maxExpiry,
230 history: settings.history,
231 timeout: timeout,
232 }, (cppErr) => {
233 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
234 if (err) {
235 return wrapCallback(err, null);
236 }
237 wrapCallback(err);
238 });
239 }, callback);
240 }
241 /**
242 * Creates a new scope.
243 *
244 * @param scopeName The name of the new scope to create.
245 * @param options Optional parameters for this operation.
246 * @param callback A node-style callback to be invoked after execution.
247 */
248 async createScope(scopeName, options, callback) {
249 if (options instanceof Function) {
250 callback = arguments[1];
251 options = undefined;
252 }
253 if (!options) {
254 options = {};
255 }
256 const bucketName = this._bucket.name;
257 const timeout = options.timeout || this._cluster.managementTimeout;
258 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
259 this._cluster.conn.managementScopeCreate({
260 bucket_name: bucketName,
261 scope_name: scopeName,
262 timeout: timeout,
263 }, (cppErr) => {
264 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
265 if (err) {
266 return wrapCallback(err, null);
267 }
268 wrapCallback(err);
269 });
270 }, callback);
271 }
272 /**
273 * Drops a scope.
274 *
275 * @param scopeName The name of the scope to drop.
276 * @param options Optional parameters for this operation.
277 * @param callback A node-style callback to be invoked after execution.
278 */
279 async dropScope(scopeName, options, callback) {
280 if (options instanceof Function) {
281 callback = arguments[1];
282 options = undefined;
283 }
284 if (!options) {
285 options = {};
286 }
287 const bucketName = this._bucket.name;
288 const timeout = options.timeout || this._cluster.managementTimeout;
289 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
290 this._cluster.conn.managementScopeDrop({
291 bucket_name: bucketName,
292 scope_name: scopeName,
293 timeout: timeout,
294 }, (cppErr) => {
295 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
296 if (err) {
297 return wrapCallback(err, null);
298 }
299 wrapCallback(err);
300 });
301 }, callback);
302 }
303}
304exports.CollectionManager = CollectionManager;