UNPKG

15.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.BucketManager = exports.BucketSettings = exports.CompressionMode = exports.EvictionPolicy = exports.StorageBackend = exports.BucketType = exports.ConflictResolutionType = void 0;
4const bindingutilities_1 = require("./bindingutilities");
5const utilities_1 = require("./utilities");
6/**
7 * Represents the various conflict resolution modes which can be used for
8 * XDCR synchronization against a bucket.
9 *
10 * @category Management
11 */
12var ConflictResolutionType;
13(function (ConflictResolutionType) {
14 /**
15 * Indicates that timestamps should be used for conflict resolution. The most
16 * recently modified document (according to each server, ie: time synchronization
17 * is important) is the one selected to win.
18 */
19 ConflictResolutionType["Timestamp"] = "lww";
20 /**
21 * Indicates that the seqno of the document should be used for conflict resolution.
22 */
23 ConflictResolutionType["SequenceNumber"] = "seqno";
24 /**
25 * Indicates that custom conflict resolution should be used.
26 *
27 * @experimental This mode is only available in Couchbase Server 7.1 with the
28 * "developer-preview" mode enabled.
29 */
30 ConflictResolutionType["Custom"] = "custom";
31})(ConflictResolutionType || (exports.ConflictResolutionType = ConflictResolutionType = {}));
32/**
33 * Represents the type of a bucket.
34 *
35 * @category Management
36 */
37var BucketType;
38(function (BucketType) {
39 /**
40 * Indicates the bucket should be a Couchbase bucket.
41 */
42 BucketType["Couchbase"] = "membase";
43 /**
44 * Indicates the bucket should be a Memcached bucket.
45 */
46 BucketType["Memcached"] = "memcached";
47 /**
48 * Indicates the bucket should be a Ephemeral bucket.
49 */
50 BucketType["Ephemeral"] = "ephemeral";
51})(BucketType || (exports.BucketType = BucketType = {}));
52/**
53 * Represents the storage backend to use for a bucket.
54 *
55 * @category Management
56 */
57var StorageBackend;
58(function (StorageBackend) {
59 /**
60 * Indicates the bucket should use the Couchstore storage engine.
61 */
62 StorageBackend["Couchstore"] = "couchstore";
63 /**
64 * Indicates the bucket should use the Magma storage engine.
65 */
66 StorageBackend["Magma"] = "magma";
67})(StorageBackend || (exports.StorageBackend = StorageBackend = {}));
68/**
69 * Represents the eviction policy that should be used for a bucket.
70 *
71 * @category Management
72 */
73var EvictionPolicy;
74(function (EvictionPolicy) {
75 /**
76 * Indicates that both the document meta-data and value should be evicted.
77 */
78 EvictionPolicy["FullEviction"] = "fullEviction";
79 /**
80 * Indicates that only the value of a document should be evicted.
81 */
82 EvictionPolicy["ValueOnly"] = "valueOnly";
83 /**
84 * Indicates that the least recently used documents are evicted.
85 */
86 EvictionPolicy["NotRecentlyUsed"] = "nruEviction";
87 /**
88 * Indicates that nothing should be evicted.
89 */
90 EvictionPolicy["NoEviction"] = "noEviction";
91})(EvictionPolicy || (exports.EvictionPolicy = EvictionPolicy = {}));
92/**
93 * Specifies the compression mode that should be used for a bucket.
94 *
95 * @category Management
96 */
97var CompressionMode;
98(function (CompressionMode) {
99 /**
100 * Indicates that compression should not be used on the server.
101 */
102 CompressionMode["Off"] = "off";
103 /**
104 * Indicates that compression should be used passively. That is that if the
105 * client sends data which is encrypted, it is stored on the server in its
106 * compressed form, but the server does not actively compress documents.
107 */
108 CompressionMode["Passive"] = "passive";
109 /**
110 * Indicates that compression should be performed actively. Even if the
111 * client does not transmit the document in a compressed form.
112 */
113 CompressionMode["Active"] = "active";
114})(CompressionMode || (exports.CompressionMode = CompressionMode = {}));
115/**
116 * Represents the configured options for a bucket.
117 *
118 * @category Management
119 */
120class BucketSettings {
121 /**
122 * @internal
123 */
124 constructor(data) {
125 this.name = data.name;
126 this.flushEnabled = data.flushEnabled;
127 this.ramQuotaMB = data.ramQuotaMB;
128 this.numReplicas = data.numReplicas;
129 this.replicaIndexes = data.replicaIndexes;
130 this.bucketType = data.bucketType;
131 this.storageBackend = data.storageBackend;
132 this.evictionPolicy = data.evictionPolicy;
133 this.maxExpiry = data.maxExpiry;
134 this.compressionMode = data.compressionMode;
135 this.minimumDurabilityLevel = data.minimumDurabilityLevel;
136 this.historyRetentionCollectionDefault =
137 data.historyRetentionCollectionDefault;
138 this.historyRetentionDuration = data.historyRetentionDuration;
139 this.historyRetentionBytes = data.historyRetentionBytes;
140 }
141 /**
142 * Same as {@link IBucketSettings.maxExpiry}.
143 *
144 * @deprecated Use {@link IBucketSettings.maxExpiry} instead.
145 */
146 get maxTTL() {
147 var _a;
148 return (_a = this.maxExpiry) !== null && _a !== void 0 ? _a : 0;
149 }
150 set maxTTL(val) {
151 this.maxExpiry = val;
152 }
153 /**
154 * Same as {@link IBucketSettings.evictionPolicy}.
155 *
156 * @deprecated Use {@link IBucketSettings.evictionPolicy} instead.
157 */
158 get ejectionMethod() {
159 return this.evictionPolicy;
160 }
161 set ejectionMethod(val) {
162 this.evictionPolicy = val;
163 }
164 /**
165 * Same as {@link IBucketSettings.minimumDurabilityLevel}, but represented as
166 * the raw server-side configuration string.
167 *
168 * @deprecated Use {@link IBucketSettings.minimumDurabilityLevel} instead.
169 */
170 get durabilityMinLevel() {
171 return (0, utilities_1.duraLevelToNsServerStr)(this.minimumDurabilityLevel);
172 }
173 /**
174 * @internal
175 */
176 static _toCppData(data) {
177 return {
178 name: data.name,
179 bucket_type: (0, bindingutilities_1.bucketTypeToCpp)(data.bucketType),
180 ram_quota_mb: data.ramQuotaMB,
181 max_expiry: data.maxTTL || data.maxExpiry,
182 compression_mode: (0, bindingutilities_1.bucketCompressionModeToCpp)(data.compressionMode),
183 minimum_durability_level: (0, bindingutilities_1.durabilityToCpp)((0, utilities_1.nsServerStrToDuraLevel)(data.durabilityMinLevel)) ||
184 (0, bindingutilities_1.durabilityToCpp)(data.minimumDurabilityLevel),
185 num_replicas: data.numReplicas,
186 replica_indexes: data.replicaIndexes,
187 flush_enabled: data.flushEnabled,
188 eviction_policy: (0, bindingutilities_1.bucketEvictionPolicyToCpp)(data.evictionPolicy),
189 storage_backend: (0, bindingutilities_1.bucketStorageBackendToCpp)(data.storageBackend),
190 history_retention_collection_default: data.historyRetentionCollectionDefault,
191 history_retention_bytes: data.historyRetentionBytes,
192 history_retention_duration: data.historyRetentionDuration,
193 };
194 }
195 /**
196 * @internal
197 */
198 static _fromCppData(data) {
199 return new BucketSettings({
200 name: data.name,
201 flushEnabled: data.flush_enabled,
202 ramQuotaMB: data.ram_quota_mb,
203 numReplicas: data.num_replicas,
204 replicaIndexes: data.replica_indexes,
205 bucketType: (0, bindingutilities_1.bucketTypeFromCpp)(data.bucket_type),
206 storageBackend: (0, bindingutilities_1.bucketStorageBackendFromCpp)(data.storage_backend),
207 evictionPolicy: (0, bindingutilities_1.bucketEvictionPolicyFromCpp)(data.eviction_policy),
208 maxExpiry: data.max_expiry,
209 compressionMode: (0, bindingutilities_1.bucketCompressionModeFromCpp)(data.compression_mode),
210 historyRetentionCollectionDefault: data.history_retention_collection_default,
211 historyRetentionBytes: data.history_retention_bytes,
212 historyRetentionDuration: data.history_retention_duration,
213 minimumDurabilityLevel: (0, bindingutilities_1.durabilityFromCpp)(data.minimum_durability_level),
214 maxTTL: 0,
215 durabilityMinLevel: '',
216 ejectionMethod: '',
217 });
218 }
219}
220exports.BucketSettings = BucketSettings;
221/**
222 * We intentionally do not export this class as it is never returned back
223 * to the user, but we still need the ability to translate to NS data.
224 *
225 * @internal
226 */
227class CreateBucketSettings extends BucketSettings {
228 /**
229 * @internal
230 */
231 constructor(data) {
232 super(data);
233 this.conflictResolutionType = data.conflictResolutionType;
234 }
235 /**
236 * @internal
237 */
238 static _toCppData(data) {
239 return {
240 ...BucketSettings._toCppData(data),
241 conflict_resolution_type: (0, bindingutilities_1.bucketConflictResolutionTypeToCpp)(data.conflictResolutionType),
242 };
243 }
244}
245/**
246 * BucketManager provides an interface for adding/removing/updating
247 * buckets within the cluster.
248 *
249 * @category Management
250 */
251class BucketManager {
252 /**
253 * @internal
254 */
255 constructor(cluster) {
256 this._cluster = cluster;
257 }
258 /**
259 * Creates a new bucket.
260 *
261 * @param settings The settings to use for the new bucket.
262 * @param options Optional parameters for this operation.
263 * @param callback A node-style callback to be invoked after execution.
264 */
265 async createBucket(settings, options, callback) {
266 if (options instanceof Function) {
267 callback = arguments[1];
268 options = undefined;
269 }
270 if (!options) {
271 options = {};
272 }
273 const timeout = options.timeout || this._cluster.managementTimeout;
274 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
275 const bucketData = CreateBucketSettings._toCppData(settings);
276 this._cluster.conn.managementBucketCreate({
277 bucket: bucketData,
278 timeout: timeout,
279 }, (cppErr) => {
280 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
281 if (err) {
282 return wrapCallback(err, null);
283 }
284 wrapCallback(err);
285 });
286 }, callback);
287 }
288 /**
289 * Updates the settings for an existing bucket.
290 *
291 * @param settings The new settings to use for the bucket.
292 * @param options Optional parameters for this operation.
293 * @param callback A node-style callback to be invoked after execution.
294 */
295 async updateBucket(settings, options, callback) {
296 if (options instanceof Function) {
297 callback = arguments[1];
298 options = undefined;
299 }
300 if (!options) {
301 options = {};
302 }
303 const timeout = options.timeout || this._cluster.managementTimeout;
304 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
305 const bucketData = BucketSettings._toCppData(settings);
306 this._cluster.conn.managementBucketUpdate({
307 bucket: bucketData,
308 timeout: timeout,
309 }, (cppErr) => {
310 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
311 if (err) {
312 return wrapCallback(err, null);
313 }
314 wrapCallback(err);
315 });
316 }, callback);
317 }
318 /**
319 * Drops an existing bucket.
320 *
321 * @param bucketName The name of the bucket to drop.
322 * @param options Optional parameters for this operation.
323 * @param callback A node-style callback to be invoked after execution.
324 */
325 async dropBucket(bucketName, options, callback) {
326 if (options instanceof Function) {
327 callback = arguments[1];
328 options = undefined;
329 }
330 if (!options) {
331 options = {};
332 }
333 const timeout = options.timeout || this._cluster.managementTimeout;
334 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
335 this._cluster.conn.managementBucketDrop({
336 name: bucketName,
337 timeout: timeout,
338 }, (cppErr) => {
339 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
340 if (err) {
341 return wrapCallback(err, null);
342 }
343 wrapCallback(err);
344 });
345 }, callback);
346 }
347 /**
348 * Fetches the settings in use for a specified bucket.
349 *
350 * @param bucketName The name of the bucket to fetch settings for.
351 * @param options Optional parameters for this operation.
352 * @param callback A node-style callback to be invoked after execution.
353 */
354 async getBucket(bucketName, options, callback) {
355 if (options instanceof Function) {
356 callback = arguments[1];
357 options = undefined;
358 }
359 if (!options) {
360 options = {};
361 }
362 const timeout = options.timeout || this._cluster.managementTimeout;
363 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
364 this._cluster.conn.managementBucketGet({
365 name: bucketName,
366 timeout: timeout,
367 }, (cppErr, resp) => {
368 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
369 if (err) {
370 return wrapCallback(err, null);
371 }
372 const bucket = BucketSettings._fromCppData(resp.bucket);
373 wrapCallback(null, bucket);
374 });
375 }, callback);
376 }
377 /**
378 * Returns a list of existing buckets in the cluster.
379 *
380 * @param options Optional parameters for this operation.
381 * @param callback A node-style callback to be invoked after execution.
382 */
383 async getAllBuckets(options, callback) {
384 if (options instanceof Function) {
385 callback = arguments[0];
386 options = undefined;
387 }
388 if (!options) {
389 options = {};
390 }
391 const timeout = options.timeout || this._cluster.managementTimeout;
392 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
393 this._cluster.conn.managementBucketGetAll({
394 timeout: timeout,
395 }, (cppErr, resp) => {
396 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
397 if (err) {
398 return wrapCallback(err, null);
399 }
400 const buckets = resp.buckets.map((bucketData) => BucketSettings._fromCppData(bucketData));
401 wrapCallback(null, buckets);
402 });
403 }, callback);
404 }
405 /**
406 * Flushes the bucket, deleting all the existing data that is stored in it.
407 *
408 * @param bucketName The name of the bucket to flush.
409 * @param options Optional parameters for this operation.
410 * @param callback A node-style callback to be invoked after execution.
411 */
412 async flushBucket(bucketName, options, callback) {
413 if (options instanceof Function) {
414 callback = arguments[1];
415 options = undefined;
416 }
417 if (!options) {
418 options = {};
419 }
420 const timeout = options.timeout || this._cluster.managementTimeout;
421 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
422 this._cluster.conn.managementBucketFlush({
423 name: bucketName,
424 timeout: timeout,
425 }, (cppErr) => {
426 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
427 if (err) {
428 return wrapCallback(err, null);
429 }
430 wrapCallback(err);
431 });
432 }, callback);
433 }
434}
435exports.BucketManager = BucketManager;