Skip to content

Commit 42d8216

Browse files
Merge pull request #225 from andrechristikan/development
Development
2 parents 14fd259 + 62d7737 commit 42d8216

File tree

6 files changed

+694
-590
lines changed

6 files changed

+694
-590
lines changed

README.md

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ If you change env value of `APP_ENV` to `production` that will
3838

3939
You can see our `e2e testing file` or read [section environment](ack-doc-env).
4040

41+
## Next Todo
42+
43+
Next development
44+
45+
- [x] Implement Repository Design Pattern / Data Access Object Design Pattern
46+
- [x] Swagger for API Documentation
47+
- [ ] Update Documentation
48+
- [ ] Export to excel and Import from excel add options to background process
49+
- [ ] AuthApi Controller
50+
- [ ] Basic Token as ApiKey
51+
- [ ] OAuth2 Client Credentials
52+
- [ ] Kafka Module Security
53+
- [ ] Optimize Kafka Module
54+
4155
## Build with
4256

4357
Describes which version .
@@ -71,8 +85,7 @@ ack-nestjs-mongoose-kafka have some objective.
7185
- NestJs v9.x 🥳
7286
- Typescript 🚀
7387
- Production Ready 🔥
74-
- Support Serverless
75-
- Authentication and Authorization (JWT, OAuth2, API Key, Basic Auth, Role Management) 💪
88+
- Authentication and Authorization (JWT, API Key, Basic, Role Management) 💪
7689
- User Agent Awareness
7790
- Timezone Awareness, and Custom Timezone
7891
- MongoDB Integrate by Using Mongoose Package 🎉
@@ -110,21 +123,7 @@ We assume that everyone who comes here is **`programmer with intermediate knowle
110123
8. Optional,[The Twelve Factor Apps](https://12factor.net)
111124
9. Optional, Understand [Docker](ref-docker) that can help you to run the project
112125

113-
## Todo
114-
115-
Next development
116-
117-
- [x] Implement Repository Design Pattern / Data Access Object Design Pattern
118-
- [x] Swagger for API Documentation
119-
- [ ] Update Documentation
120-
- [ ] Export to excel and Import from excel add options to background process
121-
- [ ] AuthApi Controller
122-
- [ ] Basic Token as ApiKey
123-
- [ ] OAuth2 Client Credentials
124-
- [ ] Kafka Module Security
125-
- [ ] Optimize Kafka Module
126-
127-
## Documentation
126+
### Getting Started
128127

129128
Before we start, we need to install some packages and tools.
130129
Recommend version is LTS Version for every tool and package
@@ -135,8 +134,7 @@ Recommend version is LTS Version for every tool and package
135134
2. [MongoDB as Replication](https://docs.mongodb.com/manual/replication/)
136135
3. [Yarn](https://yarnpkg.com)
137136
4. [Git](https://git-scm.com)
138-
139-
### Getting Started
137+
5. [Kafka](https://kafka.apache.org/documentation/)
140138

141139
#### Clone Repo
142140

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ack-nestjs-mongoose-kafka",
3-
"version": "2.1.2",
3+
"version": "2.1.3",
44
"description": "Ack NestJs Mongoose Kafka",
55
"repository": {
66
"type": "git",
@@ -53,19 +53,19 @@
5353
"rollback:user": "nestjs-command remove:user"
5454
},
5555
"dependencies": {
56-
"@aws-sdk/client-s3": "^3.171.0",
56+
"@aws-sdk/client-s3": "^3.178.0",
5757
"@faker-js/faker": "^7.5.0",
5858
"@joi/date": "^2.1.0",
5959
"@nestjs/axios": "^0.1.0",
60-
"@nestjs/common": "^9.1.1",
60+
"@nestjs/common": "^9.1.2",
6161
"@nestjs/config": "^2.2.0",
62-
"@nestjs/core": "^9.1.1",
62+
"@nestjs/core": "^9.1.2",
6363
"@nestjs/jwt": "^9.0.0",
6464
"@nestjs/mapped-types": "^1.1.0",
65-
"@nestjs/microservices": "^9.1.1",
65+
"@nestjs/microservices": "^9.1.2",
6666
"@nestjs/mongoose": "^9.2.0",
6767
"@nestjs/passport": "^9.0.0",
68-
"@nestjs/platform-express": "^9.1.1",
68+
"@nestjs/platform-express": "^9.1.2",
6969
"@nestjs/schedule": "^2.1.0",
7070
"@nestjs/swagger": "^6.1.2",
7171
"@nestjs/terminus": "^9.1.1",
@@ -79,7 +79,7 @@
7979
"express-rate-limit": "^6.6.0",
8080
"geolib": "^3.3.3",
8181
"helmet": "^6.0.0",
82-
"joi": "^17.6.0",
82+
"joi": "^17.6.1",
8383
"kafkajs": "^2.2.0",
8484
"moment": "^2.29.4",
8585
"moment-timezone": "^0.5.37",
@@ -106,7 +106,7 @@
106106
"devDependencies": {
107107
"@nestjs/cli": "^9.1.3",
108108
"@nestjs/schematics": "^9.0.3",
109-
"@nestjs/testing": "^9.1.1",
109+
"@nestjs/testing": "^9.1.2",
110110
"@types/bcrypt": "^5.0.0",
111111
"@types/bytes": "^3.1.1",
112112
"@types/compression": "^1.7.2",
@@ -120,24 +120,24 @@
120120
"@types/morgan": "^1.9.3",
121121
"@types/ms": "^0.7.31",
122122
"@types/multer": "^1.4.7",
123-
"@types/node": "^18.7.18",
123+
"@types/node": "^18.7.19",
124124
"@types/passport-jwt": "^3.0.6",
125125
"@types/supertest": "^2.0.12",
126126
"@types/ua-parser-js": "^0.7.36",
127127
"@types/uuid": "^8.3.4",
128128
"@types/ws": "^8.5.3",
129129
"@typescript-eslint/eslint-plugin": "^5.38.0",
130130
"@typescript-eslint/parser": "^5.38.0",
131-
"cspell": "^6.10.0",
132-
"eslint": "^8.23.1",
131+
"cspell": "^6.10.1",
132+
"eslint": "^8.24.0",
133133
"eslint-config-prettier": "^8.5.0",
134134
"eslint-plugin-import": "^2.26.0",
135135
"husky": "^8.0.1",
136136
"jest": "^29.0.3",
137137
"prettier": "^2.7.1",
138138
"supertest": "^6.2.4",
139139
"ts-jest": "^29.0.1",
140-
"ts-loader": "^9.4.0",
140+
"ts-loader": "^9.4.1",
141141
"ts-node": "^10.9.1",
142142
"ts-prune": "^0.10.3",
143143
"tsconfig-paths": "^4.1.0",

src/common/database/abstracts/database.mongo-repository.abstract.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { Model, PipelineStage, PopulateOptions, Types } from 'mongoose';
22
import {
33
IDatabaseCreateOptions,
44
IDatabaseExistOptions,
5+
IDatabaseFindAllAggregateOptions,
56
IDatabaseFindAllOptions,
7+
IDatabaseFindOneAggregateOptions,
68
IDatabaseFindOneOptions,
9+
IDatabaseGetTotalAggregateOptions,
710
IDatabaseOptions,
811
} from 'src/common/database/interfaces/database.interface';
912
import { IDatabaseRepositoryAbstract } from 'src/common/database/interfaces/database.repository.interface';
@@ -61,6 +64,39 @@ export abstract class DatabaseMongoRepositoryAbstract<T>
6164
return findAll.lean();
6265
}
6366

67+
async findAllAggregate<N>(
68+
pipeline: PipelineStage[],
69+
options?: IDatabaseFindAllAggregateOptions
70+
): Promise<N[]> {
71+
if (
72+
options &&
73+
options.limit !== undefined &&
74+
options.skip !== undefined
75+
) {
76+
pipeline.push({
77+
$skip: options.skip,
78+
});
79+
80+
pipeline.push({
81+
$limit: options.limit,
82+
});
83+
}
84+
85+
if (options && options.sort) {
86+
pipeline.push({
87+
$sort: options.sort,
88+
});
89+
}
90+
91+
const aggregate = this._repository.aggregate<N>(pipeline);
92+
93+
if (options && options.session) {
94+
aggregate.session(options.session);
95+
}
96+
97+
return aggregate;
98+
}
99+
64100
async findOne<Y = T>(
65101
find: Record<string, any>,
66102
options?: IDatabaseFindOneOptions
@@ -115,6 +151,24 @@ export abstract class DatabaseMongoRepositoryAbstract<T>
115151
return findOne.lean();
116152
}
117153

154+
async findOneAggregate<Y = T>(
155+
pipeline: PipelineStage[],
156+
options?: IDatabaseFindOneAggregateOptions
157+
): Promise<Y> {
158+
pipeline.push({
159+
$limit: 1,
160+
});
161+
162+
const aggregate = this._repository.aggregate<Y>(pipeline);
163+
164+
if (options && options.session) {
165+
aggregate.session(options.session);
166+
}
167+
168+
const findOne = await aggregate;
169+
return findOne && findOne.length > 0 ? findOne[0] : undefined;
170+
}
171+
118172
async getTotal(
119173
find?: Record<string, any>,
120174
options?: IDatabaseOptions
@@ -127,6 +181,26 @@ export abstract class DatabaseMongoRepositoryAbstract<T>
127181

128182
return count;
129183
}
184+
async getTotalAggregate(
185+
pipeline: PipelineStage[],
186+
options?: IDatabaseGetTotalAggregateOptions
187+
): Promise<number> {
188+
pipeline.push({
189+
$group: {
190+
_id: null,
191+
count: { $sum: options && options.field ? options.field : 1 },
192+
},
193+
});
194+
195+
const aggregate = this._repository.aggregate(pipeline);
196+
197+
if (options && options.session) {
198+
aggregate.session(options.session);
199+
}
200+
201+
const count = await aggregate;
202+
return count && count.length > 0 ? count[0].count : 0;
203+
}
130204

131205
async exists(
132206
find: Record<string, any>,

src/common/database/interfaces/database.interface.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@ export interface IDatabaseFindOneOptions {
77
session?: ClientSession;
88
}
99

10+
export type IDatabaseFindOneAggregateOptions = Pick<
11+
IDatabaseFindOneOptions,
12+
'session'
13+
>;
14+
1015
export interface IDatabaseFindAllOptions
1116
extends IPaginationOptions,
1217
IDatabaseFindOneOptions {}
1318

19+
export interface IDatabaseFindAllAggregateOptions
20+
extends IPaginationOptions,
21+
Pick<IDatabaseFindOneOptions, 'session'> {}
22+
1423
export type IDatabaseOptions = Pick<IDatabaseFindOneOptions, 'session'>;
1524

1625
export interface IDatabaseCreateOptions extends IDatabaseOptions {
@@ -20,3 +29,7 @@ export interface IDatabaseCreateOptions extends IDatabaseOptions {
2029
export interface IDatabaseExistOptions extends IDatabaseOptions {
2130
excludeId?: string;
2231
}
32+
33+
export interface IDatabaseGetTotalAggregateOptions extends IDatabaseOptions {
34+
field?: string;
35+
}

src/common/database/interfaces/database.repository.interface.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import { PipelineStage } from 'mongoose';
12
import {
23
IDatabaseCreateOptions,
34
IDatabaseExistOptions,
5+
IDatabaseFindAllAggregateOptions,
46
IDatabaseFindAllOptions,
7+
IDatabaseFindOneAggregateOptions,
58
IDatabaseFindOneOptions,
9+
IDatabaseGetTotalAggregateOptions,
610
IDatabaseOptions,
711
} from './database.interface';
812

@@ -12,6 +16,11 @@ export interface IDatabaseRepositoryAbstract<T> {
1216
options?: IDatabaseFindAllOptions
1317
): Promise<Y[]>;
1418

19+
findAllAggregate<Y = T>(
20+
pipeline: PipelineStage[],
21+
options?: IDatabaseFindAllAggregateOptions
22+
): Promise<Y[]>;
23+
1524
findOne<Y = T>(
1625
find: Record<string, any>,
1726
options?: IDatabaseFindOneOptions
@@ -22,11 +31,21 @@ export interface IDatabaseRepositoryAbstract<T> {
2231
options?: IDatabaseFindOneOptions
2332
): Promise<Y>;
2433

34+
findOneAggregate<Y = T>(
35+
pipeline: PipelineStage[],
36+
options?: IDatabaseFindOneAggregateOptions
37+
): Promise<Y[]>;
38+
2539
getTotal(
2640
find?: Record<string, any>,
2741
options?: IDatabaseOptions
2842
): Promise<number>;
2943

44+
getTotalAggregate(
45+
pipeline: PipelineStage[],
46+
options?: IDatabaseGetTotalAggregateOptions
47+
): Promise<number>;
48+
3049
aggregate<N>(
3150
pipeline: Record<string, any>[],
3251
options?: IDatabaseOptions

0 commit comments

Comments
 (0)