hironomiu / typescript-apollo-server-express

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

typescript-apollo-server-express

.env

変数名 設定値
DB_DATABASE session store で使用
DB_HOST session store で使用
DB_PASSWORD session store で使用
DB_PORT session store で使用
DB_USER session store で使用
DATABASE_URL prisma で使用
SESSION_SECRET bcrypt で使用
CORS_URLS CORS の除外(スペース区切りで複数可)
SERVER_PORT server listen port
PRODUCTION_MODE 動作環境 dev or prod

Database

create database graphql;
drop table user_roles;
drop table roles;
drop table user_books;
drop table books;
drop table users;

create table books(
    id int unsigned not null auto_increment,
    title varchar(100) not null,
    author varchar(30) not null,
    created_at datetime not null DEFAULT CURRENT_TIMESTAMP,
    updated_at datetime not null DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    primary key(id)
);

insert into books(title,author) values
("とても凄い本とても凄い本とても凄い本","太郎"),
("衝撃的な本衝撃的な本衝撃的な本","Mike"),
("圧倒的な本圧倒的な本圧倒的な本","花子");

create table roles(
  id int unsigned not null auto_increment,
  role varchar(100) not null,
  created_at datetime not null DEFAULT CURRENT_TIMESTAMP,
  updated_at datetime not null DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  primary key (id),
  unique key(role)
);

insert into roles(id,role) values(1,"ADMIN");

create table users(
  id int unsigned not null auto_increment,
  nickname varchar(100) not null,
  email varchar(100) not null,
  password varchar(100) not null,
  created_at datetime not null DEFAULT CURRENT_TIMESTAMP,
  updated_at datetime not null DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  primary key (id),
  unique key(email)
);

insert into users(nickname,email,password) values
('太郎','taro@example.com','$2b$10$wFi8RBzI3EpHt6XxqxLdLO41437B8RniV6ytM6NAACNPdFbjPj3je'),
('花子','hanako@example.com','$2b$10$OaDQnNzHPyS4RKihI3loxuCQPogfuBz5/WYDEtvBpV0B2FTR4l0MW');

create table user_roles(
  id int unsigned not null auto_increment,
  user_id int unsigned not null,
  role_id int unsigned not null,
  created_at datetime not null DEFAULT CURRENT_TIMESTAMP,
  updated_at datetime not null DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  primary key(id),
  unique key(user_id,role_id),
  constraint user_roles_fk_1 foreign key (user_id) references users (id),
  constraint user_roles_fk_2 foreign key (role_id) references roles (id)
);

insert into user_roles(user_id,role_id) values(1,1);

create table user_books(
  id binary(16) not null,
  user_id int unsigned not null,
  book_id int unsigned not null,
  comment text,
  created_at datetime not null DEFAULT CURRENT_TIMESTAMP,
  updated_at datetime not null DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  primary key(id),
  unique key(user_id,book_id),
  constraint user_books_fk_1 foreign key (user_id) references users (id),
  constraint user_books_fk_2 foreign key (book_id) references books (id)
);

insert into user_books(id,user_id,book_id,comment) values
(uuid_to_bin(uuid(),1),1,1,"comment1"),
(uuid_to_bin(uuid(),1),1,2,"comment2"),
(uuid_to_bin(uuid(),1),2,1,"comment3"),
(uuid_to_bin(uuid(),1),1,3,"comment4");

client

client:READMEを参照

Install Memo

npm install --save-dev typescript @types/node ts-node
npx tsc --init
npm install apollo-server-express apollo-server-core express graphql bcrypt express-session cookie-parser mysql2 express-mysql-session
npm install --save-dev @types/bcrypt @types/express-session @types/cookie-parser types/mysql2# @types/express-mysql-session
npm install --save-dev nodemon

Prisma

公式:Relational databases(MySQL)

公式:Install Prisma Client

リポジトリ clone 時も DB&Table の作成後以下は行う

npx prisma db pull
npx prisma generate

reset

npx prisma migrate reset

migrate

npx prisma migrate dev --create-only --name initdb
npx prisma migrate dev

Memo

作成した Model の型定義はnode_modules内で管理している。型定義を利用したい場合は以下で可能

import { users } from '@prisma/client'

About


Languages

Language:TypeScript 95.9%Language:HTML 3.7%Language:JavaScript 0.2%Language:CSS 0.1%