def gen_collection(connection_string, *args, **kwargs): try: base_cluster = Cluster(connection_string, *args, **kwargs) base_bucket = base_cluster.bucket(*args, **kwargs) return base_bucket.default_collection() except Exception as e: raise
async def get_couchbase(): cluster = Cluster( "couchbase://localhost", ClusterOptions(PasswordAuthenticator("Administrator", "password"))) bucket = cluster.bucket("travel-sample") await bucket.on_connect() return cluster, bucket
async def init_cb(app): conf = app['config']['couchbase'] cluster = Cluster("{host}:{port}".format(host=conf['host'], port=conf['port']), options=ClusterOptions( PasswordAuthenticator(username=conf['user'], password=conf['password']))) bucket = cluster.bucket(str(conf['bucket'])) bucket.on_connect() collection = bucket.collection(conf['collection']) app['cb'] = cluster app['db'] = collection
async def connect(self, **kwargs): # note: kwargs would be how one could pass in # more info for client config # note: use couchbases:// if using https conn_str = 'couchbase://{0}'.format(self.host) try: cluster_opts = ClusterOptions(authenticator=PasswordAuthenticator( self.username, self.password)) self._cluster = Cluster(conn_str, options=cluster_opts) self._bucket = self._cluster.bucket(self.bucket_name) await self._bucket.on_connect() self._collection = self._bucket.default_collection() except CouchbaseException as error: print('Could not connect to cluster. Error: {}'.format(error)) raise
class CouchbaseClient(object): @classmethod async def create_client(_, *args, **kwargs): self = CouchbaseClient(*args) client = await self.ping() # need to check for more than just a result # shown as example starting point if not client: await self.connect(**kwargs) return self _instance = None def __new__(cls, host, bucket, username, pw): if CouchbaseClient._instance is None: CouchbaseClient._instance = object.__new__(cls) CouchbaseClient._instance.host = host CouchbaseClient._instance.bucket_name = bucket CouchbaseClient._instance.username = username CouchbaseClient._instance.password = pw return CouchbaseClient._instance async def connect(self, **kwargs): # note: kwargs would be how one could pass in # more info for client config # note: use couchbases:// if using https conn_str = 'couchbase://{0}'.format(self.host) try: cluster_opts = ClusterOptions(authenticator=PasswordAuthenticator( self.username, self.password)) self._cluster = Cluster(conn_str, options=cluster_opts) self._bucket = self._cluster.bucket(self.bucket_name) await self._bucket.on_connect() self._collection = self._bucket.default_collection() except CouchbaseException as error: print('Could not connect to cluster. Error: {}'.format(error)) raise async def ping(self): try: return not self._bucket.closed except AttributeError: # if the _bucket attr doesn't exist, neither does the client return False async def get(self, key, **kwargs): # note: kwargs would be how one could pass in # more info for GetOptions return await self._collection.get(key) async def insert(self, key, doc, **kwargs): opts = InsertOptions(expiry=kwargs.get('expiry', None)) return await self._collection.insert(key, doc, opts) async def upsert(self, key, doc, **kwargs): opts = UpsertOptions(expiry=kwargs.get('expiry', None)) return await self._collection.upsert(key, doc, opts) async def remove(self, key, **kwargs): return await self._collection.remove(key)