1.0.1 • Published 7 months ago

@blendedbot/nest-couchdb-entity v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

Description

The CouchDB module for Nest framework

Installation

$ npm i @blendedbot/nest-couchdb-entity

Usage

@blendedbot/nest-couchdb-entity uses nano as a data provider for CouchDB and the Repository pattern to handle all documents related operations.

First, let's create an Entity:

import { Entity, CouchDbEntity } from '@blendedbot/nest-couchdb-entity';

@Entity('cats')
export class Cat extends CouchDbEntity {
  name: string;
}

Where cats is the CouchDB database name.

The CouchDbEntity is a base class which has some common properties:

class CouchDbEntity {
  _id: string;
  _rev: string;
}

Then, we need to import CouchDbModule in our ApplicationModule:

import { Module } from '@nestjs/common';
import { CouchDbModule } from '@blendedbot/nest-couchdb-entity';

@Module({
  imports: [
    CouchDbModule.forRoot({
      url: 'http://localhost:5984',
      username: 'couchdb',
      userpass: 'password',
      requestDefaults: { jar: true },
    }),
  ],
})
export class ApplicationModule {}

In our CatsModule we need to initiate repository for our Cat entity:

import { Module } from '@nestjs/common';
import { CouchDbModule } from '@blendedbot/nest-couchdb-entity';
import { CatsService } from './cats.service';
import { CatsController } from './cats.controller';
import { Cat } from './cat.entity';

@Module({
  imports: [CouchDbModule.forFeature([Cat])],
  providers: [CatsService],
  controllers: [CatsController],
})
export class CatsModule {}

And here is the usage of the repository in the service:

import { DocumentListResponse } from 'nano';
import { Injectable } from '@nestjs/common';
import { InjectRepository, Repository } from '@blendedbot/nest-couchdb-entity';
import { Cat } from './cat.entity';

@Injectable()
export class CatsService {
  constructor(
    @InjectRepository(Cat)
    private readonly catsRepository: Repository<Cat>,
  ) {}

  findAll(): Promise<DocumentListResponse<Cat> {
    return this.catsRepository.list();
  }
}

Test

$ docker-compose up -d
$ npm test

License

MIT

Credits

Created by @nicodejong @ blendedbot