Code Monkey home page Code Monkey logo

nest-couchdb's Introduction

CouchDB for NestJS

NestJS CouchDB

A CouchDB module for NestJS

CircleCI

ย 

Installation

$ npm i @scalio/nest-couchdb nano

Usage

@scalio/nest-couchdb 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 '@scalio/nest-couchdb';

@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 '@scalio/nest-couchdb';

@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 '@scalio/nest-couchdb';
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 '@scalio/nest-couchdb';
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 @zMotivat0r @ Scalio

About us



nest-couchdb's People

Contributors

michaelyali avatar amkartashov avatar bmgdev avatar ngomana avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.