Skip to content

Commit b4c119d

Browse files
authored
Merge pull request #6 from scalio/fix/buckets-config
fix(couchbase): added defaultBucket, buckets and connection with password
2 parents 0215f28 + f7c68fc commit b4c119d

12 files changed

+109
-47
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</p>
88

99
&nbsp;
10+
1011
## Installation
1112

1213
```bash
@@ -28,7 +29,7 @@ export class Cat {
2829
}
2930
```
3031

31-
Where `cats` is the Couchbase bucket name.
32+
Where `cats` is the Couchbase bucket name (optional).
3233

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

@@ -42,7 +43,16 @@ import { CouchbaseModule } from '@scalio-oss/nest-couchbase';
4243
url: 'couchbase://127.0.0.1',
4344
username: 'couchbase',
4445
password: 'couchbase',
45-
bucket: 'test',
46+
defaultBucket: {
47+
name: 'test',
48+
password: 'password',
49+
},
50+
buckets: [
51+
{
52+
name: 'another_bucket',
53+
password: 'another_password',
54+
},
55+
],
4656
}),
4757
],
4858
})
@@ -94,8 +104,8 @@ export class CatsService {
94104

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

97-
98107
## About us
108+
99109
<p align="center">
100110
<br/>
101111
<a href="https://scal.io/">

dist/README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
## Description
1+
![Couchbase for Nest](https://raw.githubusercontent.com/scalio/nest-couchbase/master/scalio-nc.svg?sanitize=true)
22

3-
The [Couchbase](https://www.couchbase.com/) module for Nest framework
3+
<h1 align="center">NestJS Couchbase</h1>
4+
5+
<p align="center">
6+
A <a href="https://www.couchbase.com/">Couchbase</a> module for <a href="https://nestjs.com/">NestJS</a>
7+
</p>
8+
9+
&nbsp;
410

511
## Installation
612

@@ -23,7 +29,7 @@ export class Cat {
2329
}
2430
```
2531

26-
Where `cats` is the Couchbase bucket name.
32+
Where `cats` is the Couchbase bucket name (optional).
2733

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

@@ -37,7 +43,16 @@ import { CouchbaseModule } from '@scalio-oss/nest-couchbase';
3743
url: 'couchbase://127.0.0.1',
3844
username: 'couchbase',
3945
password: 'couchbase',
40-
bucket: 'test',
46+
defaultBucket: {
47+
name: 'test',
48+
password: 'password',
49+
},
50+
buckets: [
51+
{
52+
name: 'another_bucket',
53+
password: 'another_password',
54+
},
55+
],
4156
}),
4257
],
4358
})
@@ -89,9 +104,12 @@ export class CatsService {
89104

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

107+
## About us
108+
92109
<p align="center">
93110
<br/>
94111
<a href="https://scal.io/">
95-
<img src="https://raw.githubusercontent.com/scalio/bazel-status/master/assets/scalio-logo.svg?sanitize=true"/>
112+
<img src="https://raw.githubusercontent.com/scalio/bazel-status/master/assets/scalio-logo.svg?sanitize=true" />
96113
</a>
114+
<br/>
97115
</p>

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ volumes:
1313

1414
services:
1515
couchbase:
16-
container_name: couchbase
16+
container_name: nest_couchbase
1717
image: couchbase:community
1818
volumes:
1919
- type: volume

e2e/__stubs__/config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ export const config: CouchbaseConnectionConfig = {
44
url: 'couchbase://127.0.0.1',
55
username: 'couchbase',
66
password: 'couchbase',
7-
bucket: 'e2e_test',
7+
defaultBucket: {
8+
name: 'e2e_test',
9+
},
10+
buckets: [
11+
{
12+
name: 'e2e_test_secure',
13+
password: 'password',
14+
},
15+
],
816
};
917

1018
export const bucketOptions = { bucketType: 'ephemeral', replicaNumber: 0 };

e2e/couchbase/couchbase.connection.factory.spec.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,19 @@ describe('#couchbase', () => {
4747

4848
describe('#createBucket', () => {
4949
it('should create a bucket', async () => {
50-
const [err, ok] = await conn.createBucket(config.bucket, bucketOptions);
50+
const [err, ok] = await conn.createBucket(
51+
config.defaultBucket.name,
52+
bucketOptions,
53+
);
5154
expect(err).toBeUndefined();
5255
expect(ok).toBe(true);
5356
await sleep(3500);
5457
});
5558
it('should return an error', async () => {
56-
const [err, _] = await conn.createBucket(config.bucket, bucketOptions);
59+
const [err, _] = await conn.createBucket(
60+
config.defaultBucket.name,
61+
bucketOptions,
62+
);
5763
expect(err).toBeInstanceOf(Error);
5864
});
5965
});
@@ -71,12 +77,12 @@ describe('#couchbase', () => {
7177
expect(err).toBeInstanceOf(Error);
7278
});
7379
it('should return a bucket', async () => {
74-
const [_, bucket] = await conn.getBucket(config.bucket);
80+
const [_, bucket] = await conn.getBucket(config.defaultBucket.name);
7581
expect(bucket).toBeDefined();
7682
bucket.disconnect();
7783
});
7884
it('should return a bucket with mock', async () => {
79-
const [_, bucket] = await mocked.getBucket(config.bucket);
85+
const [_, bucket] = await mocked.getBucket(config.defaultBucket.name);
8086
expect(bucket).toBeDefined();
8187
});
8288
});
@@ -87,7 +93,7 @@ describe('#couchbase', () => {
8793
expect(err).toBeInstanceOf(Error);
8894
});
8995
it('should remove a bucket', async () => {
90-
const [_, ok] = await conn.removeBucket(config.bucket);
96+
const [_, ok] = await conn.removeBucket(config.defaultBucket.name);
9197
expect(ok).toBe(true);
9298
});
9399
});

e2e/couchbase/couchbase.repository.factory.spec.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ describe('#couchbase', () => {
2727
conn = await CouchbaseConnectionFactory.create(config);
2828
mocked = await CouchbaseConnectionFactory.create({ ...config, mock: true });
2929
await removeBuckets();
30-
await conn.createBucket(config.bucket, bucketOptions);
30+
await conn.createBucket(config.defaultBucket.name, bucketOptions);
3131
await sleep(3500);
3232
});
3333

3434
afterAll(async () => {
35-
const [_, bucket] = await conn.getBucket(config.bucket);
35+
const [_, bucket] = await conn.getBucket(config.defaultBucket.name);
3636
bucket.disconnect();
3737
await removeBuckets();
3838
});
@@ -49,14 +49,6 @@ describe('#couchbase', () => {
4949
const [err] = await flattenPromise(CouchbaseRepositoryFactory.create)(conn);
5050
expect(err).toBeInstanceOf(Error);
5151
});
52-
it('should throw an error, 3', async () => {
53-
class InvalidTestEntity {}
54-
const [err] = await flattenPromise(CouchbaseRepositoryFactory.create)(
55-
conn,
56-
InvalidTestEntity,
57-
);
58-
expect(err).toBeInstanceOf(CouchbaseException);
59-
});
6052
it('should throw an error, 4', async () => {
6153
@Entity('invalid')
6254
class InvalidTestEntity {}
@@ -66,12 +58,19 @@ describe('#couchbase', () => {
6658
);
6759
expect(err).toBeInstanceOf(Error);
6860
});
69-
it('should create new Repository', async () => {
61+
it('should create new Repository, 1', async () => {
7062
const repo = await CouchbaseRepositoryFactory.create(conn, Cat);
7163
expect(repo).toBeDefined();
7264
expect(typeof repo).toBe('object');
7365
expect(repo.entity).toBeDefined();
7466
});
67+
it('should create new Repository, 2', async () => {
68+
class TestEntity {}
69+
const repo = await CouchbaseRepositoryFactory.create(conn, TestEntity);
70+
expect(repo).toBeDefined();
71+
expect(typeof repo).toBe('object');
72+
expect(repo.entity).toBeDefined();
73+
});
7574
it('should create new Repository with mock', async () => {
7675
const repo = await CouchbaseRepositoryFactory.create(mocked, Cat);
7776
expect(repo).toBeDefined();

e2e/module/couchbase.module.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ describe('#module', () => {
2626
beforeAll(async () => {
2727
conn = await CouchbaseConnectionFactory.create(config);
2828
await removeBuckets();
29-
await conn.createBucket(config.bucket, bucketOptions);
29+
await conn.createBucket(config.defaultBucket.name, bucketOptions);
3030
await sleep(3500);
3131
});
3232

3333
afterAll(async () => {
34-
const [_, bucket] = await conn.getBucket(config.bucket);
34+
const [_, bucket] = await conn.getBucket(config.defaultBucket.name);
3535
bucket.disconnect();
3636
await removeBuckets();
3737
});

integration/couchbase.config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { CouchbaseConnectionConfig } from '../src/couchbase';
22

33
export const config: CouchbaseConnectionConfig = {
44
url: 'couchbase://127.0.0.1',
5-
username: 'couchbase',
6-
password: 'couchbase',
7-
bucket: 'test',
5+
username: undefined,
6+
password: undefined,
7+
defaultBucket: {
8+
name: 'test',
9+
password: '123456',
10+
},
811
};

integration/users/users.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ export class UsersService {
88
constructor(@InjectRepository(User) private repo: Repository<User>) {}
99

1010
async get(id: string) {
11-
return this.repo.getFlat(id);
11+
return (this.repo as any).getFlat(id);
1212
}
1313
}

src/couchbase/couchbase.connection.factory.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ export class CouchbaseConnectionFactory {
2323
return conn;
2424
}
2525

26-
async getBucket(name: string): Promise<[Error, Bucket]> {
26+
async getBucket(name: string, password?: string): Promise<[Error, Bucket]> {
2727
return this.config.mock
2828
? [undefined, this.cluster.openBucket()]
29-
: await this.openBucket(name);
29+
: await this.openBucket(name, password);
3030
}
3131

3232
async createBucket(
@@ -64,7 +64,7 @@ export class CouchbaseConnectionFactory {
6464
}
6565
}
6666

67-
private async openBucket(name: string): Promise<[Error, Bucket]> {
67+
private async openBucket(name: string, password?: string): Promise<[Error, Bucket]> {
6868
/* istanbul ignore if */
6969
if (this.buckets[name]) {
7070
return [undefined, this.buckets[name]];
@@ -75,6 +75,7 @@ export class CouchbaseConnectionFactory {
7575
new Promise((resolve, reject) => {
7676
const bucket = this.cluster.openBucket.bind(this.cluster)(
7777
name,
78+
password,
7879
(err: Error) => {
7980
if (err) {
8081
reject(err);

0 commit comments

Comments
 (0)