couchbase
Version:
The official Couchbase Node.js Client Library.
388 lines (387 loc) • 14.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CollectionManager = exports.isOptions = exports.ScopeSpec = exports.CollectionSpec = void 0;
const observability_1 = require("./observability");
const observabilityhandler_1 = require("./observabilityhandler");
const observabilitytypes_1 = require("./observabilitytypes");
const utilities_1 = require("./utilities");
/**
* Contains information about a collection.
*
* @category Management
*/
class CollectionSpec {
/**
* @internal
*/
constructor(data) {
this.name = data.name;
this.scopeName = data.scopeName;
this.maxExpiry = data.maxExpiry;
this.history = data.history;
}
/**
* @internal
*/
static _fromCppData(scopeName, data) {
return new CollectionSpec({
name: data.name,
scopeName: scopeName,
maxExpiry: data.max_expiry,
history: data.history,
});
}
}
exports.CollectionSpec = CollectionSpec;
/**
* Contains information about a scope.
*
* @category Management
*/
class ScopeSpec {
/**
* @internal
*/
constructor(data) {
this.name = data.name;
this.collections = data.collections;
}
/**
* @internal
*/
static _fromCppData(data) {
let collections;
if (data.collections.length > 0) {
const scopeName = data.name;
collections = data.collections.map((collectionData) => CollectionSpec._fromCppData(scopeName, collectionData));
}
else {
collections = [];
}
return new ScopeSpec({
name: data.name,
collections: collections,
});
}
}
exports.ScopeSpec = ScopeSpec;
const OPTIONS_KEYS = ['timeout', 'parentSpan'];
/**
* @internal
*/
function isOptions(obj, expectedKeys) {
if (!obj || typeof obj !== 'object')
return false;
// Check if at least one key in the object exists in our expected list
return Object.keys(obj).some((key) => expectedKeys.includes(key));
}
exports.isOptions = isOptions;
/**
* CollectionManager allows the management of collections within a Bucket.
*
* @category Management
*/
class CollectionManager {
/**
* @internal
*/
constructor(bucket) {
this._bucket = bucket;
}
get _cluster() {
return this._bucket.cluster;
}
/**
* @internal
*/
get observabilityInstruments() {
return this._bucket.cluster.observabilityInstruments;
}
/**
* Returns all configured scopes along with their collections.
*
* @param options Optional parameters for this operation.
* @param callback A node-style callback to be invoked after execution.
*/
async getAllScopes(options, callback) {
if (options instanceof Function) {
callback = arguments[0];
options = undefined;
}
if (!options) {
options = {};
}
const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.CollectionMgmtOp.ScopeGetAll, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan);
obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucket.name });
try {
const bucketName = this._bucket.name;
const timeout = options.timeout || this._cluster.managementTimeout;
return utilities_1.PromiseHelper.wrapAsync(async () => {
const [err, resp] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementScopeGetAll.bind(this._cluster.conn), {
bucket_name: bucketName,
timeout: timeout,
}, obsReqHandler);
if (err) {
obsReqHandler.endWithError(err);
throw err;
}
obsReqHandler.end();
return resp.manifest.scopes.map((scopeData) => ScopeSpec._fromCppData(scopeData));
}, callback);
}
catch (err) {
obsReqHandler.endWithError(err);
throw err;
}
}
/**
* @internal
*/
async createCollection() {
let collectionName = arguments[0];
let scopeName = arguments[1];
let settings = arguments[2];
let options = arguments[3];
let callback = arguments[4];
// Deprecated usage conversion for (CollectionSpec, options, callback)
if (typeof collectionName === 'object') {
const spec = collectionName;
collectionName = spec.name;
scopeName = spec.scopeName;
settings = {
maxExpiry: spec.maxExpiry,
history: spec.history,
};
options = arguments[1];
callback = arguments[2];
if (options instanceof Function) {
callback = arguments[1];
options = undefined;
}
}
// Handling of callbacks for alternative overloads
if (settings instanceof Function) {
callback = arguments[2];
settings = undefined;
}
else if (options instanceof Function) {
callback = arguments[3];
options = undefined;
}
if (isOptions(settings, OPTIONS_KEYS)) {
options = settings;
settings = undefined;
}
if (!options) {
options = {};
}
if (!settings) {
settings = {};
}
const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.CollectionMgmtOp.CollectionCreate, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan);
obsReqHandler.setRequestHttpAttributes({
bucketName: this._bucket.name,
scopeName: scopeName,
collectionName: collectionName,
});
try {
const bucketName = this._bucket.name;
const timeout = options.timeout || this._cluster.managementTimeout;
const maxExpiry = settings === null || settings === void 0 ? void 0 : settings.maxExpiry;
const history = settings === null || settings === void 0 ? void 0 : settings.history;
return utilities_1.PromiseHelper.wrapAsync(async () => {
const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementCollectionCreate.bind(this._cluster.conn), {
bucket_name: bucketName,
scope_name: scopeName,
collection_name: collectionName,
max_expiry: maxExpiry,
history: history,
timeout: timeout,
}, obsReqHandler);
if (err) {
obsReqHandler.endWithError(err);
throw err;
}
obsReqHandler.end();
}, callback);
}
catch (err) {
obsReqHandler.endWithError(err);
throw err;
}
}
/**
* Drops a collection from a scope.
*
* @param collectionName The name of the collection to drop.
* @param scopeName The name of the scope containing the collection to drop.
* @param options Optional parameters for this operation.
* @param callback A node-style callback to be invoked after execution.
*/
async dropCollection(collectionName, scopeName, options, callback) {
if (options instanceof Function) {
callback = arguments[2];
options = undefined;
}
if (!options) {
options = {};
}
const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.CollectionMgmtOp.CollectionDrop, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan);
obsReqHandler.setRequestHttpAttributes({
bucketName: this._bucket.name,
scopeName: scopeName,
collectionName: collectionName,
});
try {
const bucketName = this._bucket.name;
const timeout = options.timeout || this._cluster.managementTimeout;
return utilities_1.PromiseHelper.wrapAsync(async () => {
const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementCollectionDrop.bind(this._cluster.conn), {
bucket_name: bucketName,
scope_name: scopeName,
collection_name: collectionName,
timeout: timeout,
}, obsReqHandler);
if (err) {
obsReqHandler.endWithError(err);
throw err;
}
obsReqHandler.end();
}, callback);
}
catch (err) {
obsReqHandler.endWithError(err);
throw err;
}
}
/**
* Updates a collection in a scope.
*
* @param collectionName The name of the collection to update.
* @param scopeName The name of the scope containing the collection.
* @param settings The settings to update on the collection.
* @param options Optional parameters for this operation.
* @param callback A node-style callback to be invoked after execution.
*/
async updateCollection(collectionName, scopeName, settings, options, callback) {
if (options instanceof Function) {
callback = arguments[3];
options = undefined;
}
if (!options) {
options = {};
}
const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.CollectionMgmtOp.CollectionUpdate, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan);
obsReqHandler.setRequestHttpAttributes({
bucketName: this._bucket.name,
scopeName: scopeName,
collectionName: collectionName,
});
try {
const bucketName = this._bucket.name;
const timeout = options.timeout || this._cluster.managementTimeout;
return utilities_1.PromiseHelper.wrapAsync(async () => {
const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementCollectionUpdate.bind(this._cluster.conn), {
bucket_name: bucketName,
scope_name: scopeName,
collection_name: collectionName,
max_expiry: settings.maxExpiry,
history: settings.history,
timeout: timeout,
}, obsReqHandler);
if (err) {
obsReqHandler.endWithError(err);
throw err;
}
obsReqHandler.end();
}, callback);
}
catch (err) {
obsReqHandler.endWithError(err);
throw err;
}
}
/**
* Creates a new scope.
*
* @param scopeName The name of the new scope to create.
* @param options Optional parameters for this operation.
* @param callback A node-style callback to be invoked after execution.
*/
async createScope(scopeName, options, callback) {
if (options instanceof Function) {
callback = arguments[1];
options = undefined;
}
if (!options) {
options = {};
}
const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.CollectionMgmtOp.ScopeCreate, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan);
obsReqHandler.setRequestHttpAttributes({
bucketName: this._bucket.name,
scopeName: scopeName,
});
try {
const bucketName = this._bucket.name;
const timeout = options.timeout || this._cluster.managementTimeout;
return utilities_1.PromiseHelper.wrapAsync(async () => {
const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementScopeCreate.bind(this._cluster.conn), {
bucket_name: bucketName,
scope_name: scopeName,
timeout: timeout,
}, obsReqHandler);
if (err) {
obsReqHandler.endWithError(err);
throw err;
}
obsReqHandler.end();
}, callback);
}
catch (err) {
obsReqHandler.endWithError(err);
throw err;
}
}
/**
* Drops a scope.
*
* @param scopeName The name of the scope to drop.
* @param options Optional parameters for this operation.
* @param callback A node-style callback to be invoked after execution.
*/
async dropScope(scopeName, options, callback) {
if (options instanceof Function) {
callback = arguments[1];
options = undefined;
}
if (!options) {
options = {};
}
const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.CollectionMgmtOp.ScopeDrop, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan);
obsReqHandler.setRequestHttpAttributes({
bucketName: this._bucket.name,
scopeName: scopeName,
});
try {
const bucketName = this._bucket.name;
const timeout = options.timeout || this._cluster.managementTimeout;
return utilities_1.PromiseHelper.wrapAsync(async () => {
const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementScopeDrop.bind(this._cluster.conn), {
bucket_name: bucketName,
scope_name: scopeName,
timeout: timeout,
}, obsReqHandler);
if (err) {
obsReqHandler.endWithError(err);
throw err;
}
obsReqHandler.end();
}, callback);
}
catch (err) {
obsReqHandler.endWithError(err);
throw err;
}
}
}
exports.CollectionManager = CollectionManager;