🍬 Technologies: typeOrm, typeScript, nodejs, Expressjs, postgres
Every API endpoint is consided of one route
and one action
.
- Open the routes.ts file and add a new route to the list of entpoints, for example, if we want to build an endpoint to retrieve a single user information:
import { Router } from 'express';
import { safe } from './utils';
import { getUser } from './actions';
const router = Router();
router.get('/user/:id', safe(getUser));
Note: please note that the save
function must always be called before your action or the API errors will be silent
- Open the
actions.ts
and add or re-use one of the action functions, for example:
export const getUser = async (req: Request, res: Response): Promise<Response> =>{
const users = await getRepository(Users).findOne(req.params.id);
return res.json(users);
}
- Generate a new migration file after changes were made to the models:
$ typeorm migration:generate -n <pick_a_migration_name>
- Run your all of your pending migrations:
$ typeorm migration:run
This will give you an auto-starting PostgreSQL server (it should auto-start every time you open a new Terminal), plus a few utility scripts that you can run in a Terminal or in a .gitpod.yml command:
pg_start: start the PostgreSQL service
pg_stop: stop the PostgreSQL service
pg_ctl status: check if the PostgreSQL service is running
Once the PostgreSQL server is running, you can use the psql CLI as usual:
$ psql -h localhost -d postgres
psql (10.8 (Ubuntu 10.8-0ubuntu0.18.10.1))
Type "help" for help.
postgres=#