Skip to content

Commit

Permalink
setting: test code 작성 전 기본 setting
Browse files Browse the repository at this point in the history
  • Loading branch information
taehunss committed Mar 7, 2023
1 parent f032755 commit c603142
Show file tree
Hide file tree
Showing 27 changed files with 1,394 additions and 91 deletions.
9 changes: 8 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir : __dirname,
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
Expand All @@ -21,5 +21,12 @@ module.exports = {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',

'prettier/prettier': [
'error',
{
endOfLine: 'auto',
},
],
},
};
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ lerna-debug.log*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/extensions.json

# config
/config
/src/graphql
8 changes: 6 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"trailingComma": "all"
}
"useTabs": false,
"endOfLine": "lf"
}
36 changes: 27 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,39 @@
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"start": "cross-env NODE_ENV=local nest start",
"start:dev": "cross-env NODE_ENV=dev nest start --watch",
"start:debug": "cross-env NODE_ENV=dev nest start --debug --watch",
"start:prod": "npm run build && cross-env NODE_ENV=prod node dist/main",
"typeorm": "cross-env NODE_ENV=local ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js",
"lint": "eslint \"{src,apps,libs}/**/*.ts\" --fix",
"test": "jest --modulePath=src",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "jest --config ./test/jest-e2e.json",
"postinstall": "patch-package"
},
"dependencies": {
"@nestjs/apollo": "^10.2.0",
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.3.1",
"@nestjs/core": "^9.0.0",
"@nestjs/graphql": "^10.2.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/typeorm": "^9.0.1",
"apollo-server-core": "^3.12.0",
"apollo-server-express": "^3.12.0",
"class-transformer": "^0.5.1",
"cross-env": "^7.0.3",
"graphql": "^16.6.0",
"mysql": "^2.18.1",
"patch-package": "^6.5.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0"
"rxjs": "^7.2.0",
"typeorm": "^0.3.12",
"uuid": "^9.0.0"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
Expand Down Expand Up @@ -66,6 +81,9 @@
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"testEnvironment": "node",
"moduleNameMapper": {
"^@src/(.*)$": "<rootDir>/$1"
}
}
}
22 changes: 0 additions & 22 deletions src/app.controller.spec.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/app.controller.ts

This file was deleted.

25 changes: 20 additions & 5 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { configOptions } from './common/options';
import { ormOptions } from './common/ormconfig';
import { GraphQLWithUploadModule } from './graphql.module';
import { MyModule } from './modules/my/my.module';

const ENV = process.env.NODE_ENV;

@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
imports: [
ConfigModule.forRoot(configOptions),
TypeOrmModule.forRoot(ormOptions),
GraphQLWithUploadModule.forRoot(),
MyModule,
],
providers: [
{
provide: 'NODE_ENV',
useValue: ENV,
},
],
})
export class AppModule {}
8 changes: 0 additions & 8 deletions src/app.service.ts

This file was deleted.

5 changes: 5 additions & 0 deletions src/common/baseconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { registerAs } from '@nestjs/config';

export const baseConfig = registerAs('base', () => ({
host: process.env.HOST,
}));
63 changes: 63 additions & 0 deletions src/common/common.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Field, GraphQLISODateTime, ObjectType } from '@nestjs/graphql';
import { Exclude } from 'class-transformer';
import {
BaseEntity,
BeforeInsert,
BeforeUpdate,
Column,
CreateDateColumn,
DeleteDateColumn,
Index,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import { uuid } from './utils/uuid-transformer';

@ObjectType()
export class CommonEntity extends BaseEntity {
@PrimaryGeneratedColumn()
@Exclude()
id: number;

@Field(() => String, { description: 'uuid code' })
@Column({ name: 'code', type: 'varchar', length: 36 })
@Index({ unique: true })
code: string = uuid();

@Field(() => GraphQLISODateTime)
@CreateDateColumn({
name: 'created_at',
type: 'timestamp',
})
createdAt: Date;

@Field(() => GraphQLISODateTime)
@UpdateDateColumn({
name: 'updated_at',
type: 'timestamp',
})
updatedAt: Date;

@Field(() => GraphQLISODateTime, { nullable: true })
@DeleteDateColumn({
name: 'deleted_at',
type: 'timestamp',
default: null,
nullable: true,
})
deletedAt?: Date;

@BeforeInsert()
setCreatedAt() {
if (!this.createdAt) {
this.createdAt = new Date();
}
}

@BeforeUpdate()
setUpdatedAt() {
if (!this.updatedAt) {
this.updatedAt = new Date();
}
}
}
4 changes: 4 additions & 0 deletions src/common/data-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { DataSource } from 'typeorm';
import { ormOptions } from './ormconfig';

export const dataSource: DataSource = new DataSource(ormOptions);
5 changes: 5 additions & 0 deletions src/common/env-variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface EnvVariables {
NODE_ENV: string;
SERVER_PORT: number;
DB_URL: string;
}
15 changes: 15 additions & 0 deletions src/common/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ConfigModuleOptions } from '@nestjs/config';
import * as dotenv from 'dotenv';
import * as path from 'path';
import { baseConfig } from './baseconfig';

const ENV = process.env.NODE_ENV;
dotenv.config({
path: path.join(__dirname, `../../config/.${ENV}.env`),
});

export const configOptions: ConfigModuleOptions = {
isGlobal: true,
envFilePath: `config/.${ENV}.env`,
load: [baseConfig],
};
19 changes: 19 additions & 0 deletions src/common/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as dotenv from 'dotenv';
import * as path from 'path';
import { My } from 'src/modules/my/entities/my.entity';
import { DataSourceOptions } from 'typeorm';

const ENV = process.env.NODE_ENV;
dotenv.config({
path: path.join(__dirname, `../../config/.${ENV}.env`),
});

export const ormOptions: DataSourceOptions = {
type: 'mysql',
url: process.env.DB_URL,
logging: true,
logger: 'file',
synchronize: true,
charset: 'utf8mb4',
entities: [My],
};
6 changes: 6 additions & 0 deletions src/common/utils/uuid-transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { v4 as uuidv4 } from 'uuid';

export const uuid = () => {
const tokens = uuidv4().split('-');
return tokens[2] + tokens[1] + tokens[0] + tokens[3] + tokens[4];
};
47 changes: 47 additions & 0 deletions src/graphql.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { DynamicModule, Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config/dist/config.service';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloServerPluginLandingPageLocalDefault } from 'apollo-server-core';
import path from 'path';

@Module({})
export class GraphQLWithUploadModule {
static forRoot(): DynamicModule {
return {
module: GraphQLWithUploadModule,
imports: [
GraphQLModule.forRootAsync<ApolloDriverConfig>({
inject: [ConfigService],
driver: ApolloDriver,
useFactory: async (configService: ConfigService) => ({
autoSchemaFile: path.join(process.cwd(), 'src/graphql/schema.gql'),
driver: ApolloDriver,
playground: false,
plugins:
configService.get('NODE_ENV') === 'dev'
? [ApolloServerPluginLandingPageLocalDefault({ embed: true })]
: undefined,
context: ({ req, res, connection }) => {
if (req) {
const user = req.headers.authorization;
return { ...req, ...res, user };
} else {
return connection;
}
},
cors: {
credentials: true,
origin: [
'http://localhost:3000',
'https://studio.apollographql.com',
],
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
allowedHeaders: ['Content-Type', 'Authorization'],
},
}),
}),
],
};
}
}
11 changes: 9 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { INestApplication } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { GraphQLSchemaHost } from '@nestjs/graphql';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
const app: INestApplication = await NestFactory.create(AppModule);
const configService: ConfigService<unknown, boolean> = app.get(ConfigService);
const port: number = configService.get<number>('SERVER_PORT');
app.get(GraphQLSchemaHost);

await app.listen(port);
}
bootstrap();
5 changes: 5 additions & 0 deletions src/modules/my/dto/create-my.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { InputType } from '@nestjs/graphql';
import { My } from '../entities/my.entity';

@InputType()
export class CreateMyInput extends My {}
5 changes: 5 additions & 0 deletions src/modules/my/dto/update-my.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { InputType, PartialType } from '@nestjs/graphql';
import { CreateMyInput } from './create-my.input';

@InputType()
export class UpdateMyInput extends PartialType(CreateMyInput) {}
Loading

0 comments on commit c603142

Please sign in to comment.