Skip to content

Commit

Permalink
Merge pull request #225 from andrechristikan/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
andrechristikan authored Sep 24, 2022
2 parents 14fd259 + 62d7737 commit 42d8216
Show file tree
Hide file tree
Showing 6 changed files with 694 additions and 590 deletions.
36 changes: 17 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ If you change env value of `APP_ENV` to `production` that will

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

## Next Todo

Next development

- [x] Implement Repository Design Pattern / Data Access Object Design Pattern
- [x] Swagger for API Documentation
- [ ] Update Documentation
- [ ] Export to excel and Import from excel add options to background process
- [ ] AuthApi Controller
- [ ] Basic Token as ApiKey
- [ ] OAuth2 Client Credentials
- [ ] Kafka Module Security
- [ ] Optimize Kafka Module

## Build with

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

## Todo

Next development

- [x] Implement Repository Design Pattern / Data Access Object Design Pattern
- [x] Swagger for API Documentation
- [ ] Update Documentation
- [ ] Export to excel and Import from excel add options to background process
- [ ] AuthApi Controller
- [ ] Basic Token as ApiKey
- [ ] OAuth2 Client Credentials
- [ ] Kafka Module Security
- [ ] Optimize Kafka Module

## Documentation
### Getting Started

Before we start, we need to install some packages and tools.
Recommend version is LTS Version for every tool and package
Expand All @@ -135,8 +134,7 @@ Recommend version is LTS Version for every tool and package
2. [MongoDB as Replication](https://docs.mongodb.com/manual/replication/)
3. [Yarn](https://yarnpkg.com)
4. [Git](https://git-scm.com)

### Getting Started
5. [Kafka](https://kafka.apache.org/documentation/)

#### Clone Repo

Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ack-nestjs-mongoose-kafka",
"version": "2.1.2",
"version": "2.1.3",
"description": "Ack NestJs Mongoose Kafka",
"repository": {
"type": "git",
Expand Down Expand Up @@ -53,19 +53,19 @@
"rollback:user": "nestjs-command remove:user"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.171.0",
"@aws-sdk/client-s3": "^3.178.0",
"@faker-js/faker": "^7.5.0",
"@joi/date": "^2.1.0",
"@nestjs/axios": "^0.1.0",
"@nestjs/common": "^9.1.1",
"@nestjs/common": "^9.1.2",
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^9.1.1",
"@nestjs/core": "^9.1.2",
"@nestjs/jwt": "^9.0.0",
"@nestjs/mapped-types": "^1.1.0",
"@nestjs/microservices": "^9.1.1",
"@nestjs/microservices": "^9.1.2",
"@nestjs/mongoose": "^9.2.0",
"@nestjs/passport": "^9.0.0",
"@nestjs/platform-express": "^9.1.1",
"@nestjs/platform-express": "^9.1.2",
"@nestjs/schedule": "^2.1.0",
"@nestjs/swagger": "^6.1.2",
"@nestjs/terminus": "^9.1.1",
Expand All @@ -79,7 +79,7 @@
"express-rate-limit": "^6.6.0",
"geolib": "^3.3.3",
"helmet": "^6.0.0",
"joi": "^17.6.0",
"joi": "^17.6.1",
"kafkajs": "^2.2.0",
"moment": "^2.29.4",
"moment-timezone": "^0.5.37",
Expand All @@ -106,7 +106,7 @@
"devDependencies": {
"@nestjs/cli": "^9.1.3",
"@nestjs/schematics": "^9.0.3",
"@nestjs/testing": "^9.1.1",
"@nestjs/testing": "^9.1.2",
"@types/bcrypt": "^5.0.0",
"@types/bytes": "^3.1.1",
"@types/compression": "^1.7.2",
Expand All @@ -120,24 +120,24 @@
"@types/morgan": "^1.9.3",
"@types/ms": "^0.7.31",
"@types/multer": "^1.4.7",
"@types/node": "^18.7.18",
"@types/node": "^18.7.19",
"@types/passport-jwt": "^3.0.6",
"@types/supertest": "^2.0.12",
"@types/ua-parser-js": "^0.7.36",
"@types/uuid": "^8.3.4",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.38.0",
"@typescript-eslint/parser": "^5.38.0",
"cspell": "^6.10.0",
"eslint": "^8.23.1",
"cspell": "^6.10.1",
"eslint": "^8.24.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"husky": "^8.0.1",
"jest": "^29.0.3",
"prettier": "^2.7.1",
"supertest": "^6.2.4",
"ts-jest": "^29.0.1",
"ts-loader": "^9.4.0",
"ts-loader": "^9.4.1",
"ts-node": "^10.9.1",
"ts-prune": "^0.10.3",
"tsconfig-paths": "^4.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { Model, PipelineStage, PopulateOptions, Types } from 'mongoose';
import {
IDatabaseCreateOptions,
IDatabaseExistOptions,
IDatabaseFindAllAggregateOptions,
IDatabaseFindAllOptions,
IDatabaseFindOneAggregateOptions,
IDatabaseFindOneOptions,
IDatabaseGetTotalAggregateOptions,
IDatabaseOptions,
} from 'src/common/database/interfaces/database.interface';
import { IDatabaseRepositoryAbstract } from 'src/common/database/interfaces/database.repository.interface';
Expand Down Expand Up @@ -61,6 +64,39 @@ export abstract class DatabaseMongoRepositoryAbstract<T>
return findAll.lean();
}

async findAllAggregate<N>(
pipeline: PipelineStage[],
options?: IDatabaseFindAllAggregateOptions
): Promise<N[]> {
if (
options &&
options.limit !== undefined &&
options.skip !== undefined
) {
pipeline.push({
$skip: options.skip,
});

pipeline.push({
$limit: options.limit,
});
}

if (options && options.sort) {
pipeline.push({
$sort: options.sort,
});
}

const aggregate = this._repository.aggregate<N>(pipeline);

if (options && options.session) {
aggregate.session(options.session);
}

return aggregate;
}

async findOne<Y = T>(
find: Record<string, any>,
options?: IDatabaseFindOneOptions
Expand Down Expand Up @@ -115,6 +151,24 @@ export abstract class DatabaseMongoRepositoryAbstract<T>
return findOne.lean();
}

async findOneAggregate<Y = T>(
pipeline: PipelineStage[],
options?: IDatabaseFindOneAggregateOptions
): Promise<Y> {
pipeline.push({
$limit: 1,
});

const aggregate = this._repository.aggregate<Y>(pipeline);

if (options && options.session) {
aggregate.session(options.session);
}

const findOne = await aggregate;
return findOne && findOne.length > 0 ? findOne[0] : undefined;
}

async getTotal(
find?: Record<string, any>,
options?: IDatabaseOptions
Expand All @@ -127,6 +181,26 @@ export abstract class DatabaseMongoRepositoryAbstract<T>

return count;
}
async getTotalAggregate(
pipeline: PipelineStage[],
options?: IDatabaseGetTotalAggregateOptions
): Promise<number> {
pipeline.push({
$group: {
_id: null,
count: { $sum: options && options.field ? options.field : 1 },
},
});

const aggregate = this._repository.aggregate(pipeline);

if (options && options.session) {
aggregate.session(options.session);
}

const count = await aggregate;
return count && count.length > 0 ? count[0].count : 0;
}

async exists(
find: Record<string, any>,
Expand Down
13 changes: 13 additions & 0 deletions src/common/database/interfaces/database.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ export interface IDatabaseFindOneOptions {
session?: ClientSession;
}

export type IDatabaseFindOneAggregateOptions = Pick<
IDatabaseFindOneOptions,
'session'
>;

export interface IDatabaseFindAllOptions
extends IPaginationOptions,
IDatabaseFindOneOptions {}

export interface IDatabaseFindAllAggregateOptions
extends IPaginationOptions,
Pick<IDatabaseFindOneOptions, 'session'> {}

export type IDatabaseOptions = Pick<IDatabaseFindOneOptions, 'session'>;

export interface IDatabaseCreateOptions extends IDatabaseOptions {
Expand All @@ -20,3 +29,7 @@ export interface IDatabaseCreateOptions extends IDatabaseOptions {
export interface IDatabaseExistOptions extends IDatabaseOptions {
excludeId?: string;
}

export interface IDatabaseGetTotalAggregateOptions extends IDatabaseOptions {
field?: string;
}
19 changes: 19 additions & 0 deletions src/common/database/interfaces/database.repository.interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { PipelineStage } from 'mongoose';
import {
IDatabaseCreateOptions,
IDatabaseExistOptions,
IDatabaseFindAllAggregateOptions,
IDatabaseFindAllOptions,
IDatabaseFindOneAggregateOptions,
IDatabaseFindOneOptions,
IDatabaseGetTotalAggregateOptions,
IDatabaseOptions,
} from './database.interface';

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

findAllAggregate<Y = T>(
pipeline: PipelineStage[],
options?: IDatabaseFindAllAggregateOptions
): Promise<Y[]>;

findOne<Y = T>(
find: Record<string, any>,
options?: IDatabaseFindOneOptions
Expand All @@ -22,11 +31,21 @@ export interface IDatabaseRepositoryAbstract<T> {
options?: IDatabaseFindOneOptions
): Promise<Y>;

findOneAggregate<Y = T>(
pipeline: PipelineStage[],
options?: IDatabaseFindOneAggregateOptions
): Promise<Y[]>;

getTotal(
find?: Record<string, any>,
options?: IDatabaseOptions
): Promise<number>;

getTotalAggregate(
pipeline: PipelineStage[],
options?: IDatabaseGetTotalAggregateOptions
): Promise<number>;

aggregate<N>(
pipeline: Record<string, any>[],
options?: IDatabaseOptions
Expand Down
Loading

0 comments on commit 42d8216

Please sign in to comment.