npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-ottoman

v3.0.0

Published

Downloads

32

Readme

Description

A Couchbase module for Nestjs, built on top of the ODM ottoman 2.x

Supported Couchbase version

  • Server version 6.x, 7.x
  • Nodejs SDK 4.x (supports Couchbase SDK API 3)

Usage

Installation

Yarn

yarn add nestjs-ottoman

NPM

npm install nestjs-ottoman --save

Integration

  1. Import CouchbaseModule in the root App module(or whatever other module that contains other modules), this provides initialized ottoman connection instance that is available to other modules by injection. Use CouchbaseModule.forRootAsync() if the module options depend on asynchronous processing. You can also bootstrap multiple Ottoman connections corresponding to different cluster/buckets in one CouchbaseModule.
import { Module } from '@nestjs/common';
import { CouchbaseModule } from 'nestjs-ottoman';
import configuration from './src/configuration';

@Module({
  imports: [
    ConfigModule.forRoot({
      load: [configuration],
    }),
    CouchbaseModule.forRootAsync([{ // or CouchbaseModule.forRoot with a static option
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => {
        return configService.get('couchbase');
      },
      inject: [ConfigService],
    }, {
      connectionName: 'pet',
      ottomanConnectionOptions: {
        connectionString: 'couchbase://localhost',
        bucketName: 'pet_bucket',
        username: 'Administrator',
        password: 'password',
      },
    }
    ]),
  ],
})
export class AppModule {}

The configuration file looks like,

export default () => ({
  couchbase: {
    connectionName: 'cat',
    ottomanConnectionOptions: {
      connectionString: 'couchbase://localhost',
      bucketName: 'cat_bucket',
      username: 'Administrator',
      password: 'password',
    },
  }
});
  1. Define an Ottoman Model and schema, user CouchbaseModule.forFeature() to initialize and inject the Model object to other app modules.
import { Module } from '@nestjs/common';
import { CouchbaseModule } from 'nestjs-ottoman';
import { CatModelDefinition } from './cat.schema';
@Module({
  //'cat' has to match the "connectionName" in step 1
  imports: [CouchbaseModule.forFeature([CatModelDefinition], 'cat')], 
  
})
export class CatModule {}
  1. Inject initialized Model object to your service class. See the Ottoman documentation for Model API details.
import { Injectable } from '@nestjs/common';
import { Model, Ottoman } from 'ottoman';
import { InjectModel } from 'nestjs-ottoman';
import { CatModelDefinition } from './cat.schema';
@Injectable()
export class CatService {
  constructor(
    @InjectModel(CatModelDefinition.name) private catsModel: Model<any>
  ) {}

  async findById(name: string): Promise<any> {
    return this.catsModel.findById(name);
  }

  // other codes ...

}
  1. Optionally, you can also inject a global ottoman connection pools in case of you need to use the low level Couchbase SDK directly. You can get the specific connection by connectionName, then the bucket property on the connection is a reference to bucket class of Couchabse Nodejs SDK.
import { Injectable } from '@nestjs/common';
import { Model, Ottoman } from 'ottoman';
import { InjectConnections } from 'nestjs-ottoman';
@Injectable()
export class CatService {
  constructor(
    @InjectConnections() private connections: Map<String, Ottoman>,
  ) {}

  // a function using the map-reduce view query
  async findByBreed(breed: string): Promise<any> {
    const res = await this.connections
      .get('cat') // `connectionName` in step1,
      .bucket.viewQuery('_default_default', 'findByBreed', {
        key: breed,
      });
    return res.rows;
  }
}

Complete example code can be found in test folder.

Couchbase Indexes

According to the Ottoman Document, building the index can cause a significant performance impact, this Nestjs-Ottoman won't take care of index building, you have to have another script or application to handle the index initialization.

The index defined on schema will have index name like ${bucketName}_${scopeName}_${collectionName}_${modelName}_${indexFields} (or ${bucketName}_${scopeName}_${collectionName}_${indexFields} in nestjs-ottoman version v1.*.*). When create indexes of different models, same index name may cause conflict, in that case, you can specify scopeName and collectionName in model definition to overwrite the default value '_default'.

Design

Modeling

                                                           ╔══════════════════════════════════════╗
                                                           ║                                      ║
                                                           ║                  (scope:collection-1)║
                                                           ║  ┌───────────┐  ┌──────────┐         ║
                                                       ┌───╬─▶│  Model1   │  │ Schema1  │         ║
                                                       │   ║  └───────────┘  └──────────┘         ║
                                                       │   ║                                      ║
┌────────────────────┐     ┌─────────────────────┐     │   ║                                      ║
│       bucket       │     │                     │     │   ╚══════════════════════════════════════╝
│                    │     │  Ottoman instance1  │     │   ╔══════════════════════════════════════╗
│  (cluster:bucket)  │────▶│    (connection1)    │─────┤   ║                                      ║
│                    │     │                     │     │   ║                  (scope:collection-2)║
└────────────────────┘     └─────────────────────┘     │   ║   ┌──────────┐  ┌───────────┐        ║
                                                       └───╬─▶ │  Model2  │  │  Schema2  │        ║
                                                           ║   └──────────┘  └───────────┘        ║
                                                           ║                                      ║
                                                           ║                                      ║
                                                           ╚══════════════════════════════════════╝
                                                                                                   
                                                           ╔══════════════════════════════════════╗
                                                           ║                                      ║
                                                           ║                  (scope:collection-1)║
                                                           ║   ┌───────────┐  ┌──────────┐        ║
                                                        ┌──╬──▶│  Model3   │  │ Schema3  │        ║
 ┌─────────────────────┐    ┌─────────────────────┐     │  ║   └───────────┘  └──────────┘        ║
 │       bucket        │    │                     │     │  ║                                      ║
 │                     │    │  Ottoman instance2  │     │  ║                                      ║
 │  (cluster:bucket)   │───▶│    (connection2)    │─────┤  ║                                      ║
 │                     │    │                     │     │  ║    ┌──────────┐  ┌───────────┐       ║
 └─────────────────────┘    └─────────────────────┘     └──╬──▶ │  Model4  │  │  Schema4  │       ║
                                                           ║    └──────────┘  └───────────┘       ║
                                                           ║                                      ║
                                                           ╚══════════════════════════════════════╝
                                                                                   

Ottoman Models spawning

                                                ┌─────────────────────────────────────────┐       
┌────────────────────────────────────┐          │                                         │       
│Connection pool Provider            │          │                                         │       
│                                    │          │                                         │       
│             ┌────────────────┐     │          │                                         │       
│             │  connection1   │     │          │    ForFeature('model def', connName)    │       
│             └────────────────┘     │◀───use───│                                         │       
│             ┌────────────────┐     │          │                                         │       
│             │  connenction2  │     │          │                                         │       
│             └────────────────┘     │          │                                         │       
└────────────────────────────────────┘          │                                         │       
                                                └─────────────────────────────────────────┘       
                                                                     │                            
                                                                     │                            
                                               ┌─────────generate────┴──generate───────┐          
                                               │                                       │          
                                               ▼                                       ▼          
                                    ┌────────────────────┐                  ┌────────────────────┐
                                    │                    │                  │                    │
                                    │  Provider-Model2   │                  │  Provider-Model1   │
                                    │                    │                  │                    │
                                    │                    │                  │                    │
                                    │                    │                  │                    │
                                    └────────────────────┘                  └────────────────────┘

Development

Installation

$ yarn install

Test

Bootstrap the Couchbase servcie.

$ ./dockers/up.sh

Run the test code

$ yarn test

License

MIT licensed.