GithubHelp home page GithubHelp logo

ferlab-ste-justine / users-api Goto Github PK

View Code? Open in Web Editor NEW
0.0 6.0 5.0 545 KB

API to manage users

License: Apache License 2.0

Shell 0.11% JavaScript 1.21% TypeScript 97.20% Dockerfile 0.44% PLpgSQL 1.04%

users-api's Introduction

๐Ÿ‘ฅ Users API

This project allows to persist users preferences, queries and such.

๐Ÿ”ฉ Development

This service needs to communicate to a PostgreSQL database. You can either communicate with a remote database or spin up one locally. Best practices suggest to use docker && docker-compose.

๐ŸŽ“ Pre-requisites

  • Node 18+ (if using local node interpreter)
  • Docker

๐Ÿƒ Run whole project

First, you need to have an .env file. With, minimally:

  • KEYCLOAK_URL
  • KEYCLOAK_REALM
  • KEYCLOAK_CLIENT
  • PGHOST
  • PGPORT
  • PGDATABASE
  • PGUSER
  • PGPASSWORD

Then,

# In a terminal (at the root of the project)
docker-compose up OR docker compose up # Spins up the database 
# In another terminal
docker run --rm -it --network users-api_default -p "1212:1212" -v $PWD:/app --workdir /app node:16.13-alpine sh
npm install # if needed
npm run migrate up # if needed
npm run dev

Please note that you may need to tweak some parameters in the above commands according to your setup.

๐Ÿ”จ Run tests

docker run --rm -it --network users-api_default -p "1212:1212" -u node -v $PWD:/app --workdir /app node:18.8-alpine3.15 sh
npm run test

โš ๏ธ If you want to use nodemon make sure that you do not run your container as root (you could use -u node)

๐Ÿ”ง Update database schema

  • Run npm run migrate create <describe what you want to change>, for example: npm run migrate create add users email column
  • It creates a file XXX_add-users-email-column.sql in migrations directory
  • Open it up and add your changes inside -- Up Migration directive and also add how to roll back these changes inside the -- Down Migration directive.

Example:

-- Up Migration
ALTER TABLE users ADD COLUMN email VARCHAR(255);

-- Down Migration
ALTER TABLE users DROP COLUMN email;
  • Run npm run migrate up, it will apply your last changes.
  • You need to rollback? Run npm run migrate down, it will roll back your last changes based on what you defined inside -- Down Migration.
  • To rollback more than 1 migration, run npm run migrate down {N} where N is the number of migrations to rollback.

๐Ÿ‘€ Access Postgres cli locally

Assuming that the postgres container is running and that you know its ID

docker exec -it <CONTAINER_ID> bash

# In the bash terminal run
psql postgres://<POSTGRES_USER>:<POSTGRES_PASSWORD>@<HOST>:<PORT>

# For example:
psql postgres://postgres:password@localhost:5432 

Here are some examples of useful commands

postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 users     | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
(4 rows)
postgres=# \c users
You are now connected to database "users" as user "postgres".
users=# \dt
Did not find any relations.
users=# \q

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.