express 프레임워크에 mysql을 typeORM으로 연결
import { AppDataSource } from "../data-source";
import { User } from "../entity/User";
import { Request, Response } from "express";
export default class UserController {
addUser = async (req: Request, res: Response) => {
let info = {
firstName: req.body.firstName,
lastName: req.body.lastName,
age: req.body.age,
};
const userRepo = AppDataSource.getRepository(User);
const user = userRepo.create(info);
await userRepo
.save(user)
.then((data) => {
res.json(data);
})
.catch((err) => console.log(err));
};
getUser = async (req: Request, res: Response) => {
let name = req.params.firstName;
const userRepo = AppDataSource.getRepository(User);
await userRepo
.findOne({ where: { firstName: name } })
.then((data) => {
res.json(data);
console.log("Get User: ", data);
})
.catch((err) => console.log(err));
};
getAllUsers = async (req: Request, res: Response) => {
const userRepo = AppDataSource.getRepository(User);
await userRepo
.find()
.then((data) => {
res.json(data);
console.log("Get all User: ", data);
})
.catch((err) => console.log(err));
};
updateUser = async (req: Request, res: Response) => {
const userRepo = AppDataSource.getRepository(User);
const beforeName = req.body.beforeName;
const newName = req.body.newName;
let user = await userRepo.findOne({ where: { lastName: beforeName } });
if (!user) {
return res.json({ MSG: "NOT_FOUND" });
}
user.lastName = newName;
await userRepo.save(user);
return res.json({
MSG: "SUCCESS",
});
};
deleteUser = async (req: Request, res: Response) => {
const userRepo = AppDataSource.getRepository(User);
const deleteNames = req.body.deleteNames;
// 첫번째 이름이 deleteNames 모든 row 찾기
let user = await userRepo.find({ where: { firstName: deleteNames } });
// if (user.length === 0) {
if (!user) {
return res.json({ MSG: "NOT_FOUND" });
} else {
await userRepo.remove(user);
return res.json({
MSG: "SUCCESS",
});
}
};
}
// 유저 생성
addUser = async (req: Request, res: Response) => {
let info = {
firstName: req.body.firstName,
lastName: req.body.lastName,
age: req.body.age,
};
const userRepo = AppDataSource.getRepository(User);
const user = userRepo.create(info);
await userRepo
.save(user)
.then((data) => {
res.json(data);
})
.catch((err) => console.log(err));
};
// 모든 유저 get
getUsers = async(req:Request, res:Response)=>{
const userRepo = AppDataSource.getRepository(User);
await userRepo.find().then((data)=>{
res.json(data)
console.log("all User" , data)
})
}
// 특정 한명의 유저 get
getSelectUsers = async(req:Request, res:Response)=>{
let name = req.params.firstName;
const userRepo = AppDataSource.getRepository(User);
await userRepo.findOne({where:{ firstName: name}})
.then((data)=>{
res.json(data)
})
}
// 업데이트
updateUser = async (req: Request, res: Response) => {
const userRepo = AppDataSource.getRepository(User);
const beforeName = req.body.beforeName;
const newName = req.body.newName;
let user = await userRepo.findOne({ where: { lastName: beforeName } });
if (!user) {
return res.json({ MSG: "NOT_FOUND" });
}
user.lastName = newName;
await userRepo.save(user);
return res.json({
MSG: "SUCCESS",
});
};
// 유저 삭제
deleteUser = async (req: Request, res: Response) => {
const userRepo = AppDataSource.getRepository(User);
const deleteNames = req.body.deleteNames;
// 첫번째 이름이 deleteNames 모든 row 찾기
let user = await userRepo.find({ where: { firstName: deleteNames } });
// .find에서 찾지못하여도 !user 는 빈배열로 나오기 때문에 true로 나온다.
if (user.length === 0) {
return res.json({ MSG: "NOT_FOUND" });
} else {
await userRepo.remove(user);
return res.json({
MSG: "SUCCESS",
});
}
};
repo.find({
where: {
firstName: 'a',
lastName:'b'
}
})
select * from repo
WHERE "firstName" = 'a' AND "lastName" = 'b'
repo.find({
where:[{'firstName':"a"},{"firstName':"b"}]
})
select * from repo
WHERE "firstName" = 'a' OR "firstName" = 'b'
repo.find({
order:{
firstName:"ASC",
lastName:"DESC"
}
})
SELECT * FROM "repo"
ORDER BY "firstName" ASC, "lastName" DESC
repo.find({
skip:10 ,
take:10
})
SELECT * FROM "repo"
OFFSET 10,
LIMIT 10