Next.js Example - Fall 2023

  • Next.js
  • Postgres.js

Database Setup

Copy the .env.example file to a new file called .env (ignored from Git) and fill in the necessary information.

Follow the instructions from the PostgreSQL step in UpLeveled's System Setup Instructions.

Then, connect to the built-in postgres database as administrator in order to create the database:

Windows

If it asks for a password, use postgres.

psql -U postgres

macOS

psql postgres

Linux

sudo -u postgres psql

Once you have connected, run the following to create the database:

CREATE DATABASE <database name>;
CREATE USER <user name> WITH ENCRYPTED PASSWORD '<user password>';
GRANT ALL PRIVILEGES ON DATABASE <database name> TO <user name>;
\connect <database name>;
CREATE SCHEMA <user name> AUTHORIZATION <user name>;

Quit psql using the following command:

\q

On Linux, you will also need to create a Linux system user with a name matching the user name you used in the database. It will prompt you to create a password for the user - choose the same password as for the database above.

sudo adduser <user name>

Once you're ready to use the new user, reconnect using the following command.

Windows and macOS:

psql -U <user name> <database name>

Linux:

sudo -u <user name> psql -U <user name> <database name>

API design

export type Animal = {
  id: number;
  firstName: string;
  type: string;
  accessory: string | null;
};

type Error = {
  message: string;
};

- /api/animals/
  - GET    => Animal[] | Error query: limit and offset (read animals)
  - POST   => Animal   | Error   (create animal)

- /api/animals/:id (dynamic route)
  - GET    => Animal   | Error   (read animal)
  - PUT    => Animal   | Error   (update animal)
  - Delete => Animal   | Error   (delete animal)