Skip to content

Commit bcb4ded

Browse files
Update Project
1 parent 78942f9 commit bcb4ded

24 files changed

+2295
-1996
lines changed

.eslintignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ tsconfig*
6161
schema.gql
6262

6363
# Serverless
64-
serverless.ts
64+
/serverless.ts
6565
serverless.d.ts
6666
serverless.js
6767
serverless.js.map

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ schema.gql
6464
serverless.d.ts
6565
serverless.js
6666
serverless.js.map
67+
68+
# NPM
69+
package-lock.json

.vscode/settings.json

+26-10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@
4040
"javascript.updateImportsOnFileMove.enabled": "always",
4141
"javascript.suggest.autoImports": true,
4242

43+
// ======================= Formatters ===========================
44+
45+
"[javascript]": {
46+
"editor.defaultFormatter": null
47+
},
48+
"[typescript]": {
49+
"editor.defaultFormatter": null
50+
},
51+
"[javascriptreact]": {
52+
"editor.defaultFormatter": null
53+
},
54+
"[typescriptreact]": {
55+
"editor.defaultFormatter": null
56+
},
57+
4358
// =========== Plugins & Extensions General Config ===============
4459

4560
// GitLens
@@ -92,20 +107,21 @@
92107

93108
// Spell Checker
94109
"cSpell.words": [
95-
"techmmunity",
96-
"nestjs",
97-
"vercel",
98-
"sonarjs",
99-
"tsbuildinfo",
100-
"prebuild",
101-
"nodemodules",
102-
"dotenv",
103110
"conta",
111+
"dotenv",
104112
"fastify",
105113
"injectables",
106114
"jwks",
107-
"webfactory",
115+
"MONGODB",
116+
"nestjs",
117+
"nodemodules",
118+
"prebuild",
119+
"sonarjs",
120+
"techmmunity",
121+
"tsbuildinfo",
108122
"typeorm",
109-
"MONGODB"
123+
"vendia",
124+
"vercel",
125+
"webfactory"
110126
]
111127
}

package.json

+15-13
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,34 @@
88
"dependencies": {
99
"@nestjs/common": "^8.0.0",
1010
"@nestjs/core": "^8.0.0",
11-
"@nestjs/platform-fastify": "^8.0.6",
12-
"@nestjs/swagger": "^5.1.0",
13-
"@nestjs/typeorm": "^8.0.2",
14-
"aws-serverless-fastify": "^1.0.27",
11+
"@nestjs/platform-express": "^8.1.1",
12+
"@techmmunity/symbiosis": "^0.0.7",
13+
"@techmmunity/symbiosis-dynamodb": "^0.0.2",
14+
"@techmmunity/symbiosis-nestjs": "^0.0.1",
15+
"@vendia/serverless-express": "^4.5.2",
16+
"aws-lambda": "^1.0.6",
1517
"dayjs": "^1.10.7",
16-
"fastify": "^3.21.1",
17-
"mongodb": "^4.1.2",
18+
"express": "^4.17.1",
1819
"reflect-metadata": "^0.1.13",
1920
"rimraf": "^3.0.2",
2021
"rxjs": "^7.2.0",
2122
"serverless-plugin-optimize": "^4.2.1-rc.1",
22-
"typeorm": "^0.2.37",
23+
"uuid": "^8.3.2",
2324
"yup": "^0.32.9"
2425
},
2526
"devDependencies": {
2627
"@nestjs/cli": "^8.0.0",
2728
"@nestjs/schematics": "^8.0.0",
2829
"@nestjs/testing": "^8.0.0",
2930
"@serverless/typescript": "^2.61.0",
30-
"@techmmunity/eslint-config": "^5.0.1",
31+
"@techmmunity/eslint-config": "^5.0.4",
3132
"@types/aws-lambda": "^8.10.83",
33+
"@types/express": "^4.17.13",
3234
"@types/jest": "^27.0.1",
3335
"@types/node": "^16.0.0",
3436
"@types/supertest": "^2.0.11",
35-
"eslint": "^7.30.0",
37+
"@types/uuid": "^8.3.1",
38+
"eslint": "^8.0.1",
3639
"husky": "^7.0.2",
3740
"jest": "^27.0.6",
3841
"lint-staged": "^11.1.2",
@@ -54,12 +57,11 @@
5457
},
5558
"scripts": {
5659
"prebuild": "yarn clear",
57-
"postinstall": "husky install",
58-
"build": "nest build",
60+
"clear": "rimraf dist && rimraf tsconfig.build.tsbuildinfo && rimraf tsconfig.tsbuildinfo",
61+
"prepare": "husky install",
62+
"build": "serverless package",
5963
"start": "nest start",
60-
"start:dev": "ts-node-dev src/main.ts",
6164
"start:prod": "node dist/index.js",
62-
"clear": "rimraf dist && rimraf tsconfig.build.tsbuildinfo && rimraf tsconfig.tsbuildinfo",
6365
"format": "eslint . --fix --quiet",
6466
"lint": "eslint . --quiet",
6567
"dev": "serverless offline",

serverless.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { v1 } from "./src/v1";
55
import type { AWS } from "@serverless/typescript";
66

77
const serverlessConfiguration: AWS = {
8-
service: "course-service",
8+
service: "base-project-serverless",
99
frameworkVersion: "2",
10+
useDotenv: true,
1011
package: {
1112
individually: true,
1213
},
@@ -15,6 +16,7 @@ const serverlessConfiguration: AWS = {
1516
webpackConfig: "./webpack.config.js",
1617
includeModules: true,
1718
},
19+
optimize: ["swagger-ui-dist"]
1820
},
1921
plugins: ["serverless-webpack", "serverless-offline"],
2022
provider: {

src/helpers/make-handler.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { NestFactory } from "@nestjs/core";
2+
import serverlessExpress from "@vendia/serverless-express";
3+
import type { Handler } from "aws-lambda";
4+
5+
let cachedServer: Handler;
6+
7+
const isWarmUp = (event: any) => event.source === "serverless-plugin-warmup";
8+
9+
const bootstrap = async (module: any, version: string): Promise<Handler> => {
10+
const nestApp = await NestFactory.create(module);
11+
12+
nestApp.enableCors();
13+
14+
await nestApp.init();
15+
16+
const expressApp = nestApp.getHttpAdapter().getInstance();
17+
18+
return serverlessExpress({ app: expressApp });
19+
};
20+
21+
export const makeHandler =
22+
(module: any, version: string): Handler =>
23+
async (event, context, callback) => {
24+
// https://github.com/vendia/serverless-express/issues/86
25+
event.path = event.pathParameters.proxy;
26+
27+
if (isWarmUp(event)) {
28+
/**
29+
* If it's an event to keep the lambda warm,
30+
* return here to stop the lambda execution
31+
* as soon as possible
32+
*/
33+
return;
34+
}
35+
36+
if (!cachedServer) {
37+
// eslint-disable-next-line require-atomic-updates
38+
cachedServer = await bootstrap(module, version);
39+
}
40+
41+
return cachedServer(event, context, callback);
42+
};

src/helpers/set-swagger.ts

-26
This file was deleted.

src/types/env.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ declare global {
44
namespace NodeJS {
55
interface ProcessEnv {
66
NODE_ENV: "dev" | "homolog" | "production" | "test";
7-
MONGODB_URL: string;
7+
DYNAMODB_ACCESS_KEY_ID: string;
8+
DYNAMODB_SECRET_ACCESS_KEY: string;
89
}
910
}
1011
}
+3-28
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,14 @@
1-
/* eslint-disable multiline-comment-style */
2-
/* eslint-disable capitalized-comments */
31
import { Body, Controller, Post } from "@nestjs/common";
4-
import { ApiTags, ApiCreatedResponse } from "@nestjs/swagger";
52
import { ExampleService } from "./example.service";
63
import { V1CreateExampleInputSchema } from "./service/create/schemas/input.schema";
74
import { API_VERSION } from "v1/config";
8-
// import { LazyModuleLoader } from "@nestjs/core";
9-
// import { App2Service } from "@app/module2/app2.service";
10-
// import { AppService } from "./app.service";
11-
// import { App2Module } from "@app/module2/app2.module";
125

13-
@ApiTags(`${API_VERSION} - Example`)
146
@Controller(`${API_VERSION}/example`)
15-
export class AppController {
16-
// private app2Service: App2Service;
17-
18-
public constructor(
19-
private readonly exampleService: ExampleService, // private readonly lazyLoadModuler: LazyModuleLoader,
20-
) {}
7+
export class ExampleController {
8+
public constructor(private readonly exampleService: ExampleService) {}
219

2210
@Post()
23-
@ApiCreatedResponse({
24-
type: V1CreateExampleInputSchema,
25-
})
26-
public create(@Body() data: any) {
11+
public create(@Body() data: V1CreateExampleInputSchema) {
2712
return this.exampleService.create(data);
2813
}
29-
30-
// @Post("/lazy-module")
31-
// public async getHello2(): Promise<string> {
32-
// if (!this.app2Service) {
33-
// const context = await this.lazyLoadModuler.load(() => App2Module);
34-
// this.app2Service = context.get(App2Service);
35-
// }
36-
37-
// return this.app2Service.getHello();
38-
// }
3914
}

src/v1/api/example/example.entity.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import {
2-
BaseEntity,
32
Column,
43
Entity,
4+
PrimaryColumn,
55
Repository,
6-
ObjectIdColumn,
7-
ObjectID,
8-
} from "typeorm";
6+
} from "@techmmunity/symbiosis";
97

10-
@Entity("example")
11-
export class ExampleEntity extends BaseEntity {
12-
@ObjectIdColumn({
13-
name: "_id",
14-
})
15-
public id: ObjectID;
8+
@Entity()
9+
export class ExampleEntity {
10+
@PrimaryColumn()
11+
public id: string;
1612

1713
@Column()
1814
public thisIsAnParam: string;

src/v1/api/example/example.module.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Module } from "@nestjs/common";
2-
import { TypeOrmModule } from "@nestjs/typeorm";
2+
import { SymbiosisModule } from "@techmmunity/symbiosis-nestjs";
33

44
import { ExampleService } from "./example.service";
55

6+
import { ExampleController } from "./example.controller";
67
import { ExampleEntity } from "./example.entity";
78

89
@Module({
9-
imports: [TypeOrmModule.forFeature([ExampleEntity])],
10+
imports: [SymbiosisModule.forFeature([ExampleEntity])],
1011
providers: [ExampleService],
12+
controllers: [ExampleController],
1113
})
1214
export class ExampleModule {}

src/v1/api/example/example.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from "@nestjs/common";
2-
import { InjectRepository } from "@nestjs/typeorm";
32

3+
import { InjectRepository } from "@techmmunity/symbiosis-nestjs";
44
import { create } from "./service/create";
55
import { V1CreateExampleInputSchema } from "./service/create/schemas/input.schema";
66

src/v1/api/example/service/create/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { v4 } from "uuid";
12
import { V1CreateExampleInputSchema } from "./schemas/input.schema";
23

34
import { validate } from "./validate";
@@ -14,5 +15,8 @@ export const create = async (
1415
) => {
1516
const data = await validate(params);
1617

17-
return exampleRepository.save(data);
18+
return exampleRepository.save({
19+
id: v4(),
20+
...data,
21+
});
1822
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import { ApiProperty } from "@nestjs/swagger";
2-
31
export class V1CreateExampleInputSchema {
4-
@ApiProperty({
5-
description: "Example param",
6-
example: "example",
7-
})
82
public thisIsAnParam: string;
93
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
import { ApiProperty } from "@nestjs/swagger";
2-
31
export class V1CreateExampleOutputSchema {
4-
@ApiProperty({
5-
description: "Example id",
6-
example: "id",
7-
})
8-
public id: string;
9-
10-
@ApiProperty({
11-
description: "Example param",
12-
example: "example",
13-
})
142
public thisIsAnParam: string;
153
}

src/v1/config/dynamodb.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { SymbiosisModule } from "@techmmunity/symbiosis-nestjs";
2+
import {
3+
DynamodbConnection,
4+
DynamoDbConnectionOptions,
5+
} from "@techmmunity/symbiosis-dynamodb";
6+
7+
import { DynamicModule } from "@nestjs/common";
8+
import { ExampleEntity } from "v1/api/example/example.entity";
9+
10+
const { DYNAMODB_ACCESS_KEY_ID, DYNAMODB_SECRET_ACCESS_KEY } = process.env;
11+
12+
export const DYNAMO_CONNECT: DynamicModule =
13+
SymbiosisModule.forRoot<DynamoDbConnectionOptions>(DynamodbConnection, {
14+
entities: [ExampleEntity],
15+
namingStrategy: {
16+
entity: "snake_case",
17+
column: "camelCase",
18+
},
19+
suffix: {
20+
entity: {
21+
remove: "_entity",
22+
},
23+
},
24+
databaseConfig: {
25+
region: "us-east-1",
26+
credentials: {
27+
accessKeyId: DYNAMODB_ACCESS_KEY_ID,
28+
secretAccessKey: DYNAMODB_SECRET_ACCESS_KEY,
29+
},
30+
},
31+
});

0 commit comments

Comments
 (0)