02 Nov 2022 · 5 min read

Kecil, part 2 - database creation

Two dependencies to start:

npm i mysql dotenv

mysql talks to the server, dotenv keeps credentials out of the source. Installing anything creates node_modules, which is the cue to add a .gitignore before the first commit:

node_modules/

The schema

  • user(#id, first_name, last_name, email, created_at, last_seen)
  • pw(#user_id, hashed_password)
  • link(#id, owner_id, source_id, target_url, created_at)

The password lives in its own table on purpose. Every ordinary read of a user - the dashboard, the settings page, the session lookup - then physically cannot pull the hash along with it. You only touch pw on login, register and password change.

The queries live behind a small db.js wrapper so nothing in the app writes raw SQL inline. That wrapper is what part 3 is about.

Terminal output reading SUCCESS CONNECT TO DB after running node app.js
First run: the app boots and the MySQL connection answers.
VS Code explorer showing packages/db with db.js and queries.js, plus .env and app.js
End of part 2: db.js and queries.js under packages/db, credentials in .env, node_modules ignored.

Share this

← All writing