feat: graphql + typeorm + config + postgresql

This commit is contained in:
Ivan Li 2021-01-31 13:32:15 +08:00
parent a77e22edd0
commit 7ba5e220d9
8 changed files with 4666 additions and 231 deletions

11
config.yml Normal file
View File

@ -0,0 +1,11 @@
env: dev
http:
port: 7122
db:
postgres:
host: 192.168.31.195
port: 5432
database: fennec
username: fennec
password:

4808
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -22,11 +22,20 @@
},
"dependencies": {
"@nestjs/common": "^7.5.1",
"@nestjs/config": "^0.6.2",
"@nestjs/core": "^7.5.1",
"@nestjs/graphql": "^7.9.8",
"@nestjs/platform-express": "^7.5.1",
"@nestjs/typeorm": "^7.1.5",
"apollo-server-express": "^2.19.2",
"graphql": "^15.5.0",
"graphql-tools": "^7.0.2",
"js-yaml": "^4.0.0",
"pg": "^8.5.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.3"
"rxjs": "^6.6.3",
"typeorm": "^0.2.30"
},
"devDependencies": {
"@nestjs/cli": "^7.5.1",
@ -34,6 +43,7 @@
"@nestjs/testing": "^7.5.1",
"@types/express": "^4.17.8",
"@types/jest": "^26.0.15",
"@types/js-yaml": "^4.0.0",
"@types/node": "^14.14.6",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.6.1",

View File

@ -1,10 +1,42 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { GraphQLModule } from '@nestjs/graphql';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppResolver } from './app.resolver';
import { AppService } from './app.service';
import configuration from './commons/config/configuration';
@Module({
imports: [],
imports: [
ConfigModule.forRoot({
load: [configuration],
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (cnfigService: ConfigService) => ({
type: 'postgres',
host: cnfigService.get<string>('db.postgres.host'),
port: cnfigService.get<number>('db.postgres.port'),
username: cnfigService.get<string>('db.postgres.username'),
password: cnfigService.get<string>('db.postgres.password'),
database: cnfigService.get<string>('db.postgres.database'),
synchronize: true,
autoLoadEntities: true,
}),
inject: [ConfigService],
}),
GraphQLModule.forRootAsync({
imports: [ConfigModule],
useFactory: (cnfigService: ConfigService) => ({
debug: cnfigService.get<string>('env') !== 'prod',
playground: true,
autoSchemaFile: true,
}),
inject: [ConfigService],
}),
],
controllers: [AppController],
providers: [AppService],
providers: [AppService, AppResolver],
})
export class AppModule {}

10
src/app.resolver.ts Normal file
View File

@ -0,0 +1,10 @@
import { Query, Resolver } from '@nestjs/graphql';
import { Hello } from './hello';
@Resolver(() => Hello)
export class AppResolver {
@Query(() => Hello)
async hello() {
return { message: 'Hello, World!' };
}
}

View File

@ -0,0 +1,9 @@
import { readFileSync } from 'fs';
import * as yaml from 'js-yaml';
import { join } from 'path';
export default () => {
return yaml.load(
readFileSync(join(__dirname, '../../../config.yml'), 'utf8'),
) as unknown;
};

7
src/hello.ts Normal file
View File

@ -0,0 +1,7 @@
import { Field, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class Hello {
@Field()
message: string;
}

View File

@ -1,8 +1,10 @@
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(7122);
const configService = app.get(ConfigService);
await app.listen(configService.get<number>('http.port'));
}
bootstrap();