Skip to content

Commit d7d239b

Browse files
committed
WIP service
1 parent e2db174 commit d7d239b

35 files changed

+1841
-0
lines changed

service/.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# compiled output
2+
/dist
3+
/node_modules
4+
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
pnpm-debug.log*
10+
yarn-debug.log*
11+
yarn-error.log*
12+
lerna-debug.log*
13+
14+
# OS
15+
.DS_Store
16+
17+
# Tests
18+
/coverage
19+
/.nyc_output
20+
21+
# IDEs and editors
22+
/.idea
23+
.project
24+
.classpath
25+
.c9/
26+
*.launch
27+
.settings/
28+
*.sublime-workspace
29+
30+
# IDE - VSCode
31+
.vscode/*
32+
!.vscode/settings.json
33+
!.vscode/tasks.json
34+
!.vscode/launch.json
35+
!.vscode/extensions.json

service/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<p align="center">
2+
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="200" alt="Nest Logo" /></a>
3+
</p>
4+
5+
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6+
[circleci-url]: https://circleci.com/gh/nestjs/nest
7+
8+
<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
9+
<p align="center">
10+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
11+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
12+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
13+
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
14+
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
15+
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
16+
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
17+
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
18+
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg"/></a>
19+
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
20+
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
21+
</p>
22+
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
23+
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
24+
25+
## Description
26+
27+
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
28+
29+
## Installation
30+
31+
```bash
32+
$ yarn install
33+
```
34+
35+
## Running the app
36+
37+
```bash
38+
# development
39+
$ yarn run start
40+
41+
# watch mode
42+
$ yarn run start:dev
43+
44+
# production mode
45+
$ yarn run start:prod
46+
```
47+
48+
## Test
49+
50+
```bash
51+
# unit tests
52+
$ yarn run test
53+
54+
# e2e tests
55+
$ yarn run test:e2e
56+
57+
# test coverage
58+
$ yarn run test:cov
59+
```
60+
61+
## Support
62+
63+
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
64+
65+
## Stay in touch
66+
67+
- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
68+
- Website - [https://nestjs.com](https://nestjs.com/)
69+
- Twitter - [@nestframework](https://twitter.com/nestframework)
70+
71+
## License
72+
73+
Nest is [MIT licensed](LICENSE).

service/nest-cli.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://json.schemastore.org/nest-cli",
3+
"collection": "@nestjs/schematics",
4+
"sourceRoot": "src",
5+
"compilerOptions": {
6+
"deleteOutDir": true
7+
}
8+
}

service/package.json

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"name": "@libertech-fr/teaket_service",
3+
"version": "0.0.1",
4+
"license": "MIT",
5+
"scripts": {
6+
"build": "nest build",
7+
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
8+
"start": "nest start",
9+
"start:dev": "nest start --watch",
10+
"start:debug": "nest start --debug --watch",
11+
"start:prod": "node dist/main",
12+
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
13+
"test": "jest",
14+
"test:watch": "jest --watch",
15+
"test:cov": "jest --coverage",
16+
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
17+
"test:e2e": "jest --config ./test/jest-e2e.json"
18+
},
19+
"dependencies": {
20+
"@libertech-fr/teaket_common": "*",
21+
"@nestjs-modules/ioredis": "^1.0.1",
22+
"@nestjs/common": "^9.0.0",
23+
"@nestjs/config": "^3.0.0",
24+
"@nestjs/core": "^9.0.0",
25+
"@nestjs/jwt": "^10.1.0",
26+
"@nestjs/mongoose": "^10.0.1",
27+
"@nestjs/passport": "^10.0.1",
28+
"@nestjs/platform-express": "^9.0.0",
29+
"@nestjs/swagger": "^7.1.10",
30+
"dayjs": "^1.11.9",
31+
"helmet": "^7.0.0",
32+
"ioredis": "^5.3.2",
33+
"mongoose": "^7.5.0",
34+
"mongoose-unique-validator": "^4.0.0",
35+
"passport": "^0.6.0",
36+
"passport-jwt": "^4.0.1",
37+
"reflect-metadata": "^0.1.13",
38+
"rxjs": "^7.2.0",
39+
"swagger-themes": "^1.2.30",
40+
"types-package-json": "^2.0.39"
41+
},
42+
"devDependencies": {
43+
"@kradihsoy/lt-schematics": "^1.0.12",
44+
"@nestjs/cli": "^9.0.0",
45+
"@nestjs/schematics": "^9.0.0",
46+
"@nestjs/testing": "^9.0.0",
47+
"@types/express": "^4.17.13",
48+
"@types/jest": "29.5.0",
49+
"@types/node": "18.15.11",
50+
"@types/passport-jwt": "^3.0.9",
51+
"@types/supertest": "^2.0.11",
52+
"@typescript-eslint/eslint-plugin": "^5.0.0",
53+
"@typescript-eslint/parser": "^5.0.0",
54+
"eslint": "^8.0.1",
55+
"eslint-config-prettier": "^8.3.0",
56+
"eslint-plugin-prettier": "^4.0.0",
57+
"jest": "29.5.0",
58+
"prettier": "^2.3.2",
59+
"source-map-support": "^0.5.20",
60+
"supertest": "^6.1.3",
61+
"ts-jest": "29.0.5",
62+
"ts-loader": "^9.2.3",
63+
"ts-node": "^10.0.0",
64+
"tsconfig-paths": "4.2.0",
65+
"typescript": "^4.7.4"
66+
},
67+
"jest": {
68+
"moduleFileExtensions": [
69+
"js",
70+
"json",
71+
"ts"
72+
],
73+
"rootDir": "src",
74+
"testRegex": ".*\\.spec\\.ts$",
75+
"transform": {
76+
"^.+\\.(t|j)s$": "ts-jest"
77+
},
78+
"collectCoverageFrom": [
79+
"**/*.(t|j)s"
80+
],
81+
"coverageDirectory": "../coverage",
82+
"testEnvironment": "node"
83+
}
84+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ModuleRef } from '@nestjs/core'
2+
3+
export interface AbstractControllerContext {
4+
[key: string | number]: any
5+
6+
moduleRef?: ModuleRef
7+
}
8+
9+
export abstract class AbstractController {
10+
protected moduleRef?: ModuleRef
11+
12+
public constructor(context?: AbstractControllerContext) {
13+
this.moduleRef = context?.moduleRef
14+
}
15+
16+
public get controllerName(): string {
17+
return this.constructor.name.replace(/Controller$/, '')
18+
}
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Prop } from '@nestjs/mongoose'
2+
import { Types, Document } from 'mongoose'
3+
import { MetadataPartSchema, MetadataPartSchemaSchema } from './parts/metadata.part.schema'
4+
5+
export abstract class AbstractSchema extends Document {
6+
public readonly _id: Types.ObjectId | any
7+
8+
@Prop({ type: MetadataPartSchemaSchema })
9+
public config: MetadataPartSchema
10+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { Injectable, Logger, NotFoundException } from '@nestjs/common'
2+
import { AbstractSchema } from './abstract.schema'
3+
import { FilterQuery, Model, ProjectionType, QueryOptions, Document, Query, Types, SaveOptions, UpdateQuery, ModifyResult } from 'mongoose'
4+
import { AbstractService, AbstractServiceContext } from './abstract.service'
5+
import { ServiceSchemaInterface } from './interfaces/service.schema.interface'
6+
7+
@Injectable()
8+
export abstract class AbstractServiceSchema extends AbstractService implements ServiceSchemaInterface {
9+
protected abstract _model: Model<AbstractSchema | Document>
10+
11+
public constructor(context?: AbstractServiceContext) {
12+
super(context)
13+
}
14+
15+
public get model(): Model<AbstractSchema | Document> {
16+
return this._model
17+
}
18+
19+
public async find<T extends AbstractSchema | Document>(
20+
filter?: FilterQuery<T>,
21+
projection?: ProjectionType<T> | null | undefined,
22+
options?: QueryOptions<T> | null | undefined,
23+
): Promise<Query<Array<T>, T, any, T>[]> {
24+
this.logger.debug(['find', JSON.stringify(Object.values(arguments))].join(' '))
25+
return await this._model.find<Query<Array<T>, T, any, T>>(filter, projection, options).exec()
26+
}
27+
28+
public async count<T extends AbstractSchema | Document>(filter?: FilterQuery<T>, options?: QueryOptions<T>): Promise<number> {
29+
this.logger.debug(['count', JSON.stringify(Object.values(arguments))].join(' '))
30+
return await this._model.countDocuments(filter, options).exec()
31+
}
32+
33+
public async findAndCount<T extends AbstractSchema | Document>(
34+
filter?: FilterQuery<T>,
35+
projection?: ProjectionType<T> | null | undefined,
36+
options?: QueryOptions<T> | null | undefined,
37+
): Promise<[Query<Array<T>, T, any, T>[], number]> {
38+
this.logger.debug(['findAndCount', JSON.stringify(Object.values(arguments))].join(' '))
39+
const count = await this._model.countDocuments(filter, options).exec()
40+
if (!count) return [[], 0]
41+
return [await this._model.find<Query<Array<T>, T, any, T>>(filter, projection, options).exec(), count]
42+
}
43+
44+
public async findById<T extends AbstractSchema | Document>(
45+
_id: Types.ObjectId | any,
46+
projection?: ProjectionType<T> | null | undefined,
47+
options?: QueryOptions<T> | null | undefined,
48+
): Promise<Query<T, T, any, T>> {
49+
this.logger.debug(['findById', JSON.stringify(Object.values(arguments))].join(' '))
50+
const data = await this._model.findById<Query<T | null, T, any, T>>(_id, projection, options).exec()
51+
if (!data) throw new NotFoundException()
52+
return data
53+
}
54+
55+
public async findOne<T extends AbstractSchema | Document>(
56+
filter?: FilterQuery<T>,
57+
projection?: ProjectionType<T> | null | undefined,
58+
options?: QueryOptions<T> | null | undefined,
59+
): Promise<Query<T, T, any, T>> {
60+
this.logger.debug(['findOne', JSON.stringify(Object.values(arguments))].join(' '))
61+
const data = await this._model.findOne<Query<T | null, T, any, T>>(filter, projection, options).exec()
62+
if (!data) throw new NotFoundException()
63+
return data
64+
}
65+
66+
public async create<T extends AbstractSchema | Document>(data?: any, options?: SaveOptions): Promise<Document<T, any, T>> {
67+
this.logger.debug(['create', JSON.stringify(Object.values(arguments))].join(' '))
68+
const document: Document<T, any, T> = new this._model(data)
69+
return document.save(options)
70+
}
71+
72+
public async update<T extends AbstractSchema | Document>(
73+
_id: Types.ObjectId | any,
74+
update: UpdateQuery<T>,
75+
options?: QueryOptions<T> & { rawResult: true },
76+
): Promise<ModifyResult<Query<T, T, any, T>>> {
77+
this.logger.debug(['update', JSON.stringify(Object.values(arguments))].join(' '))
78+
const document = await this._model
79+
.findByIdAndUpdate<Query<T | null, T, any, T>>({ _id }, update, {
80+
new: true,
81+
runValidators: true,
82+
...options,
83+
})
84+
.exec()
85+
if (!document) throw new NotFoundException()
86+
return document
87+
}
88+
89+
public async delete<T extends AbstractSchema | Document>(
90+
_id: Types.ObjectId | any,
91+
options?: QueryOptions<T> | null | undefined,
92+
): Promise<Query<T, T, any, T>> {
93+
this.logger.debug(['delete', JSON.stringify(Object.values(arguments))].join(' '))
94+
const document = await this._model.findByIdAndDelete<Query<T | null, T, any, T>>({ _id }, options).exec()
95+
if (!document) throw new NotFoundException()
96+
return document
97+
}
98+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Injectable, Logger } from "@nestjs/common"
2+
import { ModuleRef } from "@nestjs/core"
3+
4+
export interface AbstractServiceContext {
5+
[key: string | number]: any
6+
7+
moduleRef?: ModuleRef
8+
request?: Request & { user: any }
9+
}
10+
11+
@Injectable()
12+
export abstract class AbstractService {
13+
protected logger: Logger
14+
protected moduleRef: ModuleRef
15+
protected request?: Request & { user?: any }
16+
17+
protected constructor(context?: AbstractServiceContext) {
18+
this.moduleRef = context!.moduleRef
19+
this.request = context.request
20+
this.logger = new Logger(this.serviceName)
21+
}
22+
23+
public get serviceName(): string {
24+
return this.constructor.name.replace(/Service$/, '')
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Document, FilterQuery, ModifyResult, ProjectionType, Query, QueryOptions, SaveOptions, Types, UpdateQuery } from 'mongoose'
2+
import { AbstractSchema } from '../abstract.schema'
3+
4+
export interface ServiceSchemaInterface {
5+
find<T extends AbstractSchema | Document>(
6+
filter?: FilterQuery<T>,
7+
projection?: ProjectionType<T> | null | undefined,
8+
options?: QueryOptions<T> | null | undefined,
9+
): Promise<Query<Array<T>, T, any, T>[]>
10+
11+
count<T extends AbstractSchema | Document>(filter?: FilterQuery<T>, options?: QueryOptions<T> | null | undefined): Promise<number>
12+
13+
findAndCount<T extends AbstractSchema | Document>(
14+
filter?: FilterQuery<T>,
15+
projection?: ProjectionType<T> | null | undefined,
16+
options?: QueryOptions<T> | null | undefined,
17+
): Promise<[Query<Array<T>, T, any, T>[], number]>
18+
19+
findById<T extends AbstractSchema | Document>(id: Types.ObjectId | any, projection?: ProjectionType<T> | null | undefined, options?: QueryOptions<T> | null | undefined): Promise<Query<T, T, any, T>>
20+
21+
findOne<T extends AbstractSchema | Document>(
22+
filter?: FilterQuery<T>,
23+
projection?: ProjectionType<T> | null | undefined,
24+
options?: QueryOptions<T> | null | undefined,
25+
): Promise<Query<T, T, any, T>>
26+
27+
create<T extends AbstractSchema | Document>(data?: any, options?: SaveOptions): Promise<Document<T, any, T>>
28+
29+
update<T extends AbstractSchema | Document>(
30+
_id: Types.ObjectId | any,
31+
update: UpdateQuery<T> | null | undefined,
32+
options?: QueryOptions<T> | null | undefined,
33+
): Promise<ModifyResult<Query<T, T, any, T>>>
34+
35+
delete<T extends AbstractSchema | Document>(_id: Types.ObjectId | any, options?: QueryOptions<T> | null | undefined): Promise<Query<T, T, any, T>>
36+
}

0 commit comments

Comments
 (0)