Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/scalio/nest-couchbase

Couchbase module for NestJS
https://github.com/scalio/nest-couchbase

couchbase database nest nestjs nodejs orm

Last synced: 6 days ago
JSON representation

Couchbase module for NestJS

Awesome Lists containing this project

README

        

![Couchbase for Nest](https://raw.githubusercontent.com/scalio/nest-couchbase/master/scalio-nc.svg?sanitize=true)

NestJS Couchbase


A Couchbase module for NestJS

 

## Installation

```bash
$ npm i @scalio-oss/nest-couchbase couchbase
```

## Usage

`@scalio-oss/nest-couchbase` uses [couchbase](https://www.npmjs.com/package/couchbase) as a data provider and the `Repository` pattern to handle all items (documents) related operations.

First, let's create an `Entity`:

```typescript
import { Entity } from '@scalio-oss/nest-couchbase';

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

Where `cats` is the Couchbase bucket name (optional).

Then, we need to import `CouchbaseModule` in our root `AppModule`:

```typescript
import { Module } from '@nestjs/common';
import { CouchbaseModule } from '@scalio-oss/nest-couchbase';

@Module({
imports: [
CouchbaseModule.forRoot({
url: 'couchbase://127.0.0.1',
username: 'couchbase',
password: 'couchbase',
defaultBucket: {
name: 'test',
password: 'password',
},
buckets: [
{
name: 'another_bucket',
password: 'another_password',
},
],
}),
],
})
export class AppModule {}
```

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

```typescript
import { Module } from '@nestjs/common';
import { CouchbaseModule } from '@scalio-oss/nest-couchbase';
import { CatsService } from './cats.service';
import { CatsController } from './cats.controller';
import { Cat } from './cat.entity';

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

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

```typescript
import { Injectable } from '@nestjs/common';
import { InjectRepository, Repository } from '@scalio-oss/nest-couchbase';
import { Cat } from './cat.entity';

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

findOne(id: string): Promise {
return this.catsRepository.get(id);
}
}
```

## License

[MIT](LICENSE)

## Credits

Created by [@zMotivat0r](https://github.com/zMotivat0r) @ [Scalio](https://scal.io/)

## About us