장재혁

Merge branch 'add-database' into 'master'

Add database



See merge request !2
......@@ -6,10 +6,10 @@
},
"scripts": {
"build": "lerna run build",
"build:api" : "lerna exec --scrop @graphql-community/api --stream yarn build",
"build:web": "lerna exec --scrop @graphql-community/api --stream yarn build",
"dev:web": "lerna exec --scrop @graphql-community/web --stream yarn dev",
"dev:api": "lerna exec --scrop @graphql-community/api --stream yarn start:dev"
"build:api" : "lerna exec --scope @graphql-community/api --stream yarn build",
"build:web": "lerna exec --scope @graphql-community/api --stream yarn build",
"dev:web": "lerna exec --scope @graphql-community/web --stream yarn dev",
"dev:api": "lerna exec --scope @graphql-community/api --stream yarn start:dev"
},
"workspaces": [
"packages/**"
......
No preview for this file type
......@@ -5,6 +5,7 @@ import { join } from 'path';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MypageModule } from './mypage/mypage.module';
import { PostModule } from './post/post.module';
@Module({
imports: [
......@@ -14,11 +15,13 @@ import { MypageModule } from './mypage/mypage.module';
}),
TypeOrmModule.forRoot({
type: 'sqlite',
database: ':memory:',
database: 'database.db',
entities: ['dist/**/*.entity{.ts,.js}'],
synchronize: true,
synchronize: false,
logging: false,
}),
MypageModule,
PostModule,
],
controllers: [AppController],
providers: [AppService],
......
......@@ -2,7 +2,7 @@
import { ObjectType, Field, Int } from '@nestjs/graphql';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
@Entity({ name: 'my_pagelist' })
@ObjectType()
export class MyPage {
@PrimaryGeneratedColumn()
......
import { Field, InputType } from '@nestjs/graphql';
import { Entity } from 'typeorm';
@Entity()
@InputType()
export class GetPostInput {
@Field()
id: number;
@Field({ nullable: true })
author?: string;
@Field({ nullable: true })
category?: string;
}
/* eslint-disable @typescript-eslint/no-unused-vars */
import { ObjectType, Field, Int } from '@nestjs/graphql';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity({ name: 'post' })
@ObjectType()
export class Post {
@PrimaryGeneratedColumn()
@Field((type) => Int)
id: number;
@Column()
@Field()
author: string;
@Column()
@Field()
created_date: string;
@Column({ nullable: true })
@Field({ nullable: true })
updated_date?: string;
@Column()
@Field()
title: string;
@Column()
@Field()
content: string;
@Column()
@Field()
category: string;
}
import { Module } from '@nestjs/common';
import { PostService } from './post.service';
import { PostResolver } from './post.resolver';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Post } from './post.entity';
@Module({
imports: [TypeOrmModule.forFeature([Post])],
providers: [PostService, PostResolver],
})
export class PostModule {}
import { Test, TestingModule } from '@nestjs/testing';
import { PostResolver } from './post.resolver';
describe('PostResolver', () => {
let resolver: PostResolver;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PostResolver],
}).compile();
resolver = module.get<PostResolver>(PostResolver);
});
it('should be defined', () => {
expect(resolver).toBeDefined();
});
});
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { PostService } from './post.service';
import { Post } from './post.entity';
import { GetPostInput } from './dto/post.input';
@Resolver((of) => Post)
export class PostResolver {
constructor(private postService: PostService) {}
@Query((returns) => [Post])
getPosts(): Promise<Post[]> {
return this.postService.findAll();
}
@Query((returns) => Post)
getPost(@Args('id') id: number): Promise<Post> {
return this.postService.findOne(id);
}
}
import { Test, TestingModule } from '@nestjs/testing';
import { PostService } from './post.service';
describe('PostService', () => {
let service: PostService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PostService],
}).compile();
service = module.get<PostService>(PostService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { GetPostInput } from './dto/post.input';
import { Post } from './post.entity';
@Injectable()
export class PostService {
constructor(
@InjectRepository(Post) private postRepository: Repository<Post>,
) {}
async findAll(): Promise<Post[]> {
return this.postRepository.find();
}
async findOne(id: number): Promise<Post> {
return this.postRepository.findOne(id);
}
}
......@@ -17,6 +17,18 @@ type MyPage {
type: String
}
type Post {
author: String!
category: String!
content: String!
created_date: String!
id: Int!
title: String!
updated_date: String
}
type Query {
getPost(id: Float!): Post!
getPosts: [Post!]!
myPage: [MyPage!]!
}
......
export enum CATEGORY {
test = 'test_category',
test2 = 'test2_category',
}
{
"name": "@graphql-community/shared",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"lodash": "4.17.20"
},
"license": "MIT"
}
No preview for this file type