← Back to Lessons

Actions examples on Express

Example actions using TypeORM & Express

Example actions using TypeORM & Express

Each of these actions can be matched with a URL like this:

router.get('/some_url', yourAction);

But we are not going to be focusing on the URL's but only the actions, here is the table of contents:

  1. How to create
  2. How to edit/update
  3. How to delete
  4. How to get one user
  5. How to get all users
  6. Update logged in user

1) How to create?

We must always start with validations, the best way to notify a validation error to the user is by throwing exceptions like this:

1if(something_wrong) throw new Exception("Message to the user")

Here is the sample user creation:

1export const createUser = async (req: Request, res:Response): Promise<Response> =>{ 2 3 // important validations to avoid ambiguous errors, the client needs to understand what went wrong 4 if(!req.body.first_name) throw new Exception("Please provide a first_name") 5 if(!req.body.last_name) throw new Exception("Please provide a last_name") 6 if(!req.body.email) throw new Exception("Please provide an email") 7 if(!req.body.password) throw new Exception("Please provide a password") 8 9 const userRepo = getRepository(Users)// to manipulate users I need the user repository 10 // fetch for any user with this email 11 const user = await userRepo.findOne({ where: {email: req.body.email }}) 12 if(user) throw new Exception("User already exists with this email") 13 14 const newUser = getRepository(Users).create(req.body); //Create the new user based on the incoming json body 15 const results = await getRepository(Users).save(newUser); //commit to the database 16 return res.json(results); 17}

2) How to update entities?

Very similar to the user creation, we need to start with validations and then proceed to update the user:

1export const updateUser = async (req: Request, res:Response): Promise<Response> =>{ 2 const userRepo = getRepository(Users) // I need the userRepo to manage users 3 4 // find user by id 5 const user = await userRepo.findOne(req.params.id); 6 if(!user) throw new Exception("Not User found"); 7 8 // better to merge, that way we can do partial update (only a couple of properties) 9 userRepo.merge(user, req.body); 10 const results = await userRepo.save(user); // commit to DB 11 return res.json(results); 12}

3) How to delete?

1export const deleteUser = async (req: Request, res: Response): Promise<Response> =>{ 2 const users = await getRepository(Users).delete(req.params.id); 3 return res.json(users); 4}

4) How to get one single user?

Get a single user is simple using the findOne, but the cool part is that you can also retrieve the user planets by passing a second param to the findOne function. { relations: ["planets"] }

Note: there are other ways to find, you can read more about find here.

1export const getUser = async (req: Request, res: Response): Promise<Response> =>{ 2 3 // we can pass a second param to the findOne with the extra relations that we need 4 const user = await getRepository(Users).findOne(req.params.id, { relations: ["planets"] }); 5 if(!user) throw new Exception("User not found", 404) 6 7 return res.json(user); 8}

5) Get all users

Similar to the single user find, but we use the function find instead of findOne.

1export const getUsers = async (req: Request, res: Response): Promise<Response> =>{ 2 const users = await getRepository(Users).find(); 3 return res.json(users); 4}

6) Update currently logged in user

It's very similar to updating any other user, the difference is that we can get the current user ID using req.user.id.

1export const updateCurrentUser = async (req: Request, res:Response): Promise<Response> =>{ 2 const userRepo = getRepository(Users) // I need the userRepo to manage users 3 4 /** 5 * We can guess the current user from the authentication, more information about that here: 6 * get-the-authenticated-user 7 */ 8 if(!req.user) throw new Exception("No user was found on the session token") 9 const user_id = (req.user as ObjectLiteral).id 10 const user = await userRepo.findOne(user_id); 11 if(!user) throw new Exception("User not found"); 12 13 // better to merge, that way we can do partial update (only a couple of properties) 14 userRepo.merge(user, req.body); 15 const results = await userRepo.save(user); // commit to DB 16 return res.json(results); 17}