# Class: ConnectionManager

Creates a connection instance. Provide functions to work with cluster, bucket and collection on the current connection. Supports multiple instances.


 import { connect } from "ottoman";
 const connection: ConnectionManager
                       = connect("couchbase://localhost/travel-sample@admin:password");

# Hierarchy

  • ConnectionManager

# Constructors

# constructor

+ new ConnectionManager(cluster: any, bucketName: string, couchbase: any): ConnectionManager


Name Type
cluster any
bucketName string
couchbase any

Returns: ConnectionManager

# Properties

# bucket

bucket: any

Bucket represents a storage grouping of data within a Couchbase Server cluster.

# bucketName

bucketName: string

Contains the name of the current bucket.

# cluster

cluster: any

Cluster represents an entire Couchbase Server cluster.

# collectionManager

collectionManager: any

CollectionManager allows the management of collections within a Bucket.

# couchbase

couchbase: any

Stores a reference to couchbase instance.

# models

models: object

Dictionary for all models registered on this connection.

# Type declaration:

# queryIndexManager

queryIndexManager: any

QueryIndexManager provides an interface for managing the query indexes on the cluster.

# viewIndexManager

viewIndexManager: any

ViewIndexManager is an interface which enables the management of view indexes on the cluster.

# Methods

# close

close(): void

Closes the current connection


connection.close().then(() => console.log('connection closed'));

Returns: void

# getCollection

getCollection(collectionName: string, scopeName: string): any

Return a collection from the given collectionName in this bucket Or default collection if collectionName is missing


Name Type Default
collectionName string DEFAULT_COLLECTION
scopeName string DEFAULT_SCOPE

Returns: any

# getModel

getModel(name: string): ModelTypes

Returns a Model constructor from the given name


const User = connection.getModel('User');


Name Type
name string

Returns: ModelTypes

# model

model(name: string, schema: Schema | Record‹string, unknown›, options?: any): object & object

Creates a Model on this connection.


const User = connection.model('User', { name: String }, {collectionName: 'users'});


Name Type
name string
schema Schema | Record‹string, unknown›
options? any

Returns: object & object

# query

query(query: string): Promise‹any›

Executes N1QL Queries.

Ottoman provides a powerful Query Builder system to create valid N1QL queries in a easier way. See the example below:


const expr_where = {$or: [{ address: { $like: '%57-59%' } }, { free_breakfast: true }]};
const query = new Query({}, 'travel-sample');
const n1qlQuery = query.select([{$field: 'address'}]).where(expr_where).build()

connection.query(n1qlQuery).then(result => console.log(result))

The above example will run this query:

SELECT address FROM travel-sample WHERE (address LIKE '%57-59%' OR free_breakfast = true)


Name Type
query string

Returns: Promise‹any›

# start

start(__namedParameters: object): Promise‹void›

Ensure that Ottoman start correcty


Default value __namedParameters: object= {}

Name Type Default Description
ensureIndexes boolean true is a flag that defines when Ottoman must ensure that all indexes are created on the server
useCollections boolean false is a flag to create scopes/collections.

Returns: Promise‹void›