|
1 | | -# database-services |
| 1 | +# database-services |
| 2 | + |
| 3 | +# Database Services - Backend as a Service (BaaS) |
| 4 | + |
| 5 | +This project provides a dynamic backend system offering Database as a Service capabilities using Node.js, TypeScript, Express, and PostgreSQL. It supports flexible DDL & DML operations through a REST API. |
| 6 | +GitHub Repository: [https://github.com/feedloop/database-services](https://github.com/feedloop/database-services) |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +* Dynamic DDL operations to create, alter, or drop tables and columns via API. |
| 11 | +* Dynamic DML operations for inserting, selecting, updating, and deleting data. |
| 12 | +* Authentication with JWT and support for API keys. |
| 13 | +* Interactive Swagger documentation. |
| 14 | +* Runs entirely in Docker using Docker Compose – no manual setup required. |
| 15 | + |
| 16 | +## Environment Setup |
| 17 | + |
| 18 | +All required environment variables are already configured in `docker-compose.yml`. However, if you prefer using a `.env` file, make sure it includes: |
| 19 | + |
| 20 | +```env |
| 21 | +PORT=3000 |
| 22 | +JWT_SECRET=supersecretkey |
| 23 | +JWT_EXPIRES_IN=1h |
| 24 | +NODE_ENV=development |
| 25 | +DB_USERNAME=postgres |
| 26 | +DB_PASSWORD=12345678 |
| 27 | +DB_NAME=database_services |
| 28 | +DB_HOST=db |
| 29 | +DB_PORT=5432 |
| 30 | +``` |
| 31 | + |
| 32 | +## Running with Docker Compose |
| 33 | + |
| 34 | +To start both the backend and PostgreSQL database: |
| 35 | + |
| 36 | +```bash |
| 37 | +docker-compose up --build |
| 38 | +``` |
| 39 | + |
| 40 | +The backend will be available at: |
| 41 | + |
| 42 | +``` |
| 43 | +http://localhost:3000 |
| 44 | +``` |
| 45 | + |
| 46 | +Swagger API docs: |
| 47 | + |
| 48 | +``` |
| 49 | +http://localhost:3000/api-docs |
| 50 | +``` |
| 51 | + |
| 52 | +## Sequelize Sync & Database Initialization |
| 53 | + |
| 54 | +This project uses `sequelize.sync({ alter: true })`, which will: |
| 55 | + |
| 56 | +* Automatically create or update tables to match model definitions. |
| 57 | +* Ensure your database structure is always in sync on startup. |
| 58 | + |
| 59 | +### Running Without Docker |
| 60 | + |
| 61 | +If you prefer to run the backend without Docker: |
| 62 | + |
| 63 | +1. Make sure PostgreSQL is running locally on your machine. |
| 64 | +2. Create a database named `database_services` with user `postgres` and password `12345678`, or update your `.env` accordingly. |
| 65 | +3. Install dependencies: |
| 66 | + |
| 67 | +```bash |
| 68 | +yarn install |
| 69 | +``` |
| 70 | + |
| 71 | +4. Build the project: |
| 72 | + |
| 73 | +```bash |
| 74 | +yarn build |
| 75 | +``` |
| 76 | + |
| 77 | +5. Start the server: |
| 78 | + |
| 79 | +```bash |
| 80 | +yarn start |
| 81 | +``` |
| 82 | + |
| 83 | +During development, you can use: |
| 84 | + |
| 85 | +```bash |
| 86 | +yarn dev |
| 87 | +``` |
| 88 | + |
| 89 | +> Sequelize will automatically sync the database on startup. |
| 90 | +
|
| 91 | +## Main API Endpoints |
| 92 | + |
| 93 | +All endpoints are prefixed with `/api`. |
| 94 | + |
| 95 | +* `POST /auth/login` – Authenticate and receive a JWT. |
| 96 | +* `POST /apikey/generate` – Generate an API key (requires JWT). |
| 97 | +* `POST /migrate` – Perform DDL operations like creating, altering, or dropping tables and columns. |
| 98 | +* `GET /schemas` – Retrieve metadata of available tables and columns. |
| 99 | +* `POST /execute` – Execute dynamic DML operations such as insert, select, update, or delete. |
| 100 | +* `POST /query` – Run custom SQL-like queries on existing dynamic tables. |
| 101 | +* `GET /users` – Development-only endpoint to retrieve all users (requires JWT). |
| 102 | + |
| 103 | +## API Documentation |
| 104 | + |
| 105 | +Swagger UI is available at: |
| 106 | + |
| 107 | +``` |
| 108 | +http://localhost:3000/api-docs |
| 109 | +``` |
| 110 | + |
| 111 | +## Health Check |
| 112 | + |
| 113 | +To verify the server is running: |
| 114 | + |
| 115 | +```bash |
| 116 | +curl http://localhost:3000/health |
| 117 | +``` |
| 118 | + |
| 119 | +## Running Tests (Optional) |
| 120 | + |
| 121 | +If test files are present, you can run: |
| 122 | + |
| 123 | +```bash |
| 124 | +yarn test |
| 125 | +``` |
| 126 | + |
| 127 | +(If using Docker: exec into the container and run inside) |
| 128 | + |
| 129 | +## Author |
| 130 | + |
| 131 | +This project is developed by Rifa Sania as part of the MBKM Mandiri internship at PT Feedloop Global Teknologi. |
0 commit comments