Skip to content

Commit ab9f0c8

Browse files
committed
feat(module): added forRootAsync method to the CouchbaseModule
1 parent 623c707 commit ab9f0c8

17 files changed

+186
-55
lines changed

dist/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,6 @@ Created by [@zMotivat0r](https://github.com/zMotivat0r) @ [Scalio](https://scal.
9292
<p align="center">
9393
<br/>
9494
<a href="https://scal.io/">
95-
<img src="https://raw.githubusercontent.com/scalio/bazel-nestjs-starter/master/readme-assets/scalio.png"/>
95+
<img src="https://raw.githubusercontent.com/scalio/bazel-status/master/assets/scalio-logo.svg?sanitize=true"/>
9696
</a>
9797
</p>

dist/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@scalio-oss/nest-couchbase",
33
"description": "Couchbase module for Nest framework",
4-
"version": "0.1.0",
4+
"version": "0.2.0",
55
"license": "MIT",
66
"main": "index.js",
77
"typings": "index.d.ts",

e2e/__stubs__/cats.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Injectable } from '@nestjs/common';
2+
3+
import { InjectRepository, Repository } from '../../src';
4+
import { Cat } from './cat.entity';
5+
6+
@Injectable()
7+
export class CatsService {
8+
constructor(@InjectRepository(Cat) public repo: Repository<Cat>) {}
9+
}

e2e/__stubs__/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './config';
22
export * from './cat.entity';
3+
export * from './cats.service';

e2e/couchbase/couchbase.repository.mixin.spec.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

e2e/module/couchbase.module.spec.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { Test } from '@nestjs/testing';
2+
import { INestApplication, Injectable, Module, Global } from '@nestjs/common';
3+
import { CouchbaseError } from 'couchbase';
4+
5+
import { CouchbaseConnectionFactory } from '../../src/couchbase/couchbase.connection.factory';
6+
import { CouchbaseModule } from '../../src/module/couchbase.module';
7+
import { sleep, flattenPromise } from '../../src/utils';
8+
import { config, bucketOptions, Cat, CatsService } from '../__stubs__';
9+
10+
describe('#module', () => {
11+
describe('#CouchbaseModule', () => {
12+
const msg = 'The key does not exist on the server';
13+
let app: INestApplication;
14+
let conn: CouchbaseConnectionFactory;
15+
let service: CatsService;
16+
17+
async function removeBuckets() {
18+
const [_, buckets] = await conn.listBuckets();
19+
if (buckets && buckets.length) {
20+
for (let i = 0; i < buckets.length; i++) {
21+
await conn.removeBucket(buckets[0].name);
22+
}
23+
}
24+
}
25+
26+
beforeAll(async () => {
27+
conn = await CouchbaseConnectionFactory.create(config);
28+
await removeBuckets();
29+
await conn.createBucket(config.bucket, bucketOptions);
30+
await sleep(3500);
31+
});
32+
33+
afterAll(async () => {
34+
const [_, bucket] = await conn.getBucket(config.bucket);
35+
bucket.disconnect();
36+
await removeBuckets();
37+
});
38+
39+
describe('#forRoot && forFeature', () => {
40+
beforeAll(async () => {
41+
const fixture = await Test.createTestingModule({
42+
imports: [CouchbaseModule.forRoot(config), CouchbaseModule.forFeature([Cat])],
43+
providers: [CatsService],
44+
}).compile();
45+
app = fixture.createNestApplication();
46+
await app.init();
47+
service = app.get<CatsService>(CatsService);
48+
});
49+
50+
afterAll(async () => {
51+
await app.close();
52+
});
53+
54+
it('should throw an error', async () => {
55+
const [err] = await flattenPromise(service.repo.get)('key');
56+
expect(err.message).toBe(msg);
57+
});
58+
});
59+
60+
describe('#forRootAsync && forFeature', () => {
61+
beforeAll(async () => {
62+
@Injectable()
63+
class ConfigService {
64+
constructor() {}
65+
get() {
66+
return config;
67+
}
68+
}
69+
70+
@Global()
71+
@Module({
72+
providers: [ConfigService],
73+
exports: [ConfigService],
74+
})
75+
class ConfigModule {}
76+
77+
const fixture = await Test.createTestingModule({
78+
imports: [
79+
ConfigModule,
80+
CouchbaseModule.forRootAsync({
81+
useFactory: (configService: ConfigService) => configService.get(),
82+
inject: [ConfigService],
83+
}),
84+
CouchbaseModule.forFeature([Cat]),
85+
],
86+
providers: [CatsService],
87+
}).compile();
88+
app = fixture.createNestApplication();
89+
await app.init();
90+
service = app.get<CatsService>(CatsService);
91+
});
92+
93+
afterAll(async () => {
94+
await app.close();
95+
});
96+
97+
it('should throw an error', async () => {
98+
const [err] = await flattenPromise(service.repo.get)('key');
99+
expect(err.message).toBe(msg);
100+
});
101+
});
102+
});
103+
});

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@scalio-oss/nest-couchbase",
33
"description": "Couchbase module for Nest framework",
4-
"version": "0.1.0",
4+
"version": "0.2.0",
55
"license": "MIT",
66
"private": true,
77
"scripts": {
@@ -11,7 +11,6 @@
1111
"commit": "npx git-cz",
1212
"lint": "npx tslint 'src/*.ts'",
1313
"format": "npx pretty-quick --pattern 'src/**/*.ts'",
14-
"test": "npx jest -c=jest.config.js test/ --verbose --runInBand",
1514
"test:e2e": "npx jest -c=jest.config.js e2e/ --verbose --runInBand",
1615
"start": "npx nodemon -w ./integration -e ts node_modules/.bin/ts-node integration/main.ts"
1716
},

src/module/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const COUCHBASE_CONNECTION_TOKEN = 'COUCHBASE_CONNECTION_TOKEN';
2+
export const COUCHBASE_MODULE_OPTIONS = 'COUCHBASE_MODULE_OPTIONS';

src/module/couchbase-core.module.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
1-
import { Global, Module, DynamicModule } from '@nestjs/common';
1+
import {
2+
Global,
3+
Module,
4+
DynamicModule,
5+
Provider,
6+
OnApplicationShutdown,
7+
} from '@nestjs/common';
28

3-
import { CouchbaseConnectionConfig } from '../couchbase';
4-
import { createCouchbaseConnectionProviders } from './providers';
9+
import { CouchbaseConnectionConfig, CouchbaseConnectionFactory } from '../couchbase';
10+
import { CouchbaseModuleAsyncOptions } from './interfaces';
11+
import {
12+
createCouchbaseConnectionProviders,
13+
createCouchbaseAsyncConnectionProviders,
14+
} from './providers';
15+
import { InjectConnection } from './couchbase.decorators';
516

617
@Global()
718
@Module({})
8-
export class CouchbaseCoreModule {
19+
export class CouchbaseCoreModule implements OnApplicationShutdown {
20+
constructor(@InjectConnection() private conn: CouchbaseConnectionFactory) {}
21+
22+
onApplicationShutdown() {
23+
Object.keys(this.conn.buckets).forEach((bucket) =>
24+
this.conn.buckets[bucket].disconnect(),
25+
);
26+
}
27+
928
static forRoot(config: CouchbaseConnectionConfig): DynamicModule {
1029
const providers = createCouchbaseConnectionProviders(config);
30+
return CouchbaseCoreModule.outputDynamicModule(providers);
31+
}
32+
33+
static forRootAsync(options: CouchbaseModuleAsyncOptions): DynamicModule {
34+
const providers = createCouchbaseAsyncConnectionProviders(options);
35+
return CouchbaseCoreModule.outputDynamicModule(providers);
36+
}
37+
38+
private static outputDynamicModule(providers: Provider[]): DynamicModule {
1139
return {
1240
module: CouchbaseCoreModule,
1341
providers,

src/module/couchbase.module.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Module, DynamicModule } from '@nestjs/common';
22

33
import { CouchbaseConnectionConfig } from '../couchbase';
4+
import { CouchbaseModuleAsyncOptions } from './interfaces';
45
import { CouchbaseCoreModule } from './couchbase-core.module';
56
import { createCouchbaseProviders } from './providers';
67

@@ -13,6 +14,13 @@ export class CouchbaseModule {
1314
};
1415
}
1516

17+
static forRootAsync(options: CouchbaseModuleAsyncOptions): DynamicModule {
18+
return {
19+
module: CouchbaseModule,
20+
imports: [CouchbaseCoreModule.forRootAsync(options)],
21+
};
22+
}
23+
1624
static forFeature(entities: Function[]): DynamicModule {
1725
const providers = createCouchbaseProviders(entities);
1826
return {

0 commit comments

Comments
 (0)