Add Asset table, extend schema for related entities, and update .env setup

- Create `Asset` table with relevant columns and constraints.
- Extend `Organization`, `Building`, `Floor`, `Space`, and `User` tables with new fields referencing `Asset`.
- Add `Reservation` table with foreign key constraints for users and spaces.
- Introduce `.env.example` for environment variable management.
- Update `DOCKER_SETUP.md` with initialization and environment configuration instructions.
main
Pau Costa Ferrer 2026-03-28 19:21:13 +01:00
parent 7b6750e17d
commit 07e647b582
4 changed files with 88 additions and 16 deletions

14
.env.example Normal file
View File

@ -0,0 +1,14 @@
# Database Configuration
DB_USER=desker
DB_PASSWORD=desker
DB_NAME=desker
DB_PORT_EXTERNAL=5433
# API Configuration
API_PORT_EXTERNAL=3891
JWT_SECRET=super-secret-change-me-for-security
WEB_ORIGIN=http://localhost:3890
# Web Application Configuration
WEB_PORT_EXTERNAL=3890
VITE_API_BASE_URL=http://localhost:3891

View File

@ -6,13 +6,6 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="6c8b3c54-eabb-40e5-967f-c7b594c750bc" name="Changes" comment="Migrate Prisma seed configuration to `prisma.config.ts` and update API Dockerfile&#10;&#10;- Move `prisma.seed` from `package.json` to new `prisma.config.ts`.&#10;- Modify API Dockerfile to include `prisma.config.ts` in the build process."> <list default="true" id="6c8b3c54-eabb-40e5-967f-c7b594c750bc" name="Changes" comment="Migrate Prisma seed configuration to `prisma.config.ts` and update API Dockerfile&#10;&#10;- Move `prisma.seed` from `package.json` to new `prisma.config.ts`.&#10;- Modify API Dockerfile to include `prisma.config.ts` in the build process.">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/apps/api/Dockerfile" beforeDir="false" afterPath="$PROJECT_DIR$/apps/api/Dockerfile" afterDir="false" />
<change beforePath="$PROJECT_DIR$/apps/api/package.json" beforeDir="false" afterPath="$PROJECT_DIR$/apps/api/package.json" afterDir="false" />
<change beforePath="$PROJECT_DIR$/apps/api/prisma.config.ts" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/apps/api/src/main.ts" beforeDir="false" afterPath="$PROJECT_DIR$/apps/api/src/main.ts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/apps/web/src/routes/admin/floorplan/[id]/+page.svelte" beforeDir="false" afterPath="$PROJECT_DIR$/apps/web/src/routes/admin/floorplan/[id]/+page.svelte" afterDir="false" />
<change beforePath="$PROJECT_DIR$/apps/web/src/routes/admin/floorplan/[id]/+page.ts" beforeDir="false" afterPath="$PROJECT_DIR$/apps/web/src/routes/admin/floorplan/[id]/+page.ts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/docker-compose.yml" beforeDir="false" afterPath="$PROJECT_DIR$/docker-compose.yml" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -134,7 +127,7 @@
<updated>1774643312599</updated> <updated>1774643312599</updated>
<workItem from="1774643313611" duration="74000" /> <workItem from="1774643313611" duration="74000" />
<workItem from="1774643422240" duration="5278000" /> <workItem from="1774643422240" duration="5278000" />
<workItem from="1774706361502" duration="14661000" /> <workItem from="1774706361502" duration="15401000" />
</task> </task>
<task id="LOCAL-00001" summary="Initialize Hot Desking app with web and API services &#10;&#10;- Set up SvelteKit for the web interface with Tailwind CSS. &#10;- Build the API using NestJS with Prisma ORM for database interaction. &#10;- Add environment variable management and docker-compose for PostgreSQL and PgAdmin instances. &#10;- Create shared data models for consistent typing across services. &#10;- Establish basic routing and layouts for web and API services."> <task id="LOCAL-00001" summary="Initialize Hot Desking app with web and API services &#10;&#10;- Set up SvelteKit for the web interface with Tailwind CSS. &#10;- Build the API using NestJS with Prisma ORM for database interaction. &#10;- Add environment variable management and docker-compose for PostgreSQL and PgAdmin instances. &#10;- Create shared data models for consistent typing across services. &#10;- Establish basic routing and layouts for web and API services.">
<option name="closed" value="true" /> <option name="closed" value="true" />
@ -408,7 +401,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1774720600350</updated> <updated>1774720600350</updated>
</task> </task>
<option name="localTasksCounter" value="35" /> <task id="LOCAL-00035" summary="Migrate Prisma seed configuration to `prisma.config.ts` and update API Dockerfile&#10;&#10;- Move `prisma.seed` from `package.json` to new `prisma.config.ts`.&#10;- Modify API Dockerfile to include `prisma.config.ts` in the build process.">
<option name="closed" value="true" />
<created>1774721058640</created>
<option name="number" value="00035" />
<option name="presentableId" value="LOCAL-00035" />
<option name="project" value="LOCAL" />
<updated>1774721058640</updated>
</task>
<option name="localTasksCounter" value="36" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">

View File

@ -24,16 +24,17 @@ Create a `.env` file in the root directory. The following variables are supporte
## Deployment Steps ## Deployment Steps
1. **Build and start the containers:** 1. **Copy the environment variables template:**
```bash
cp .env.example .env
```
*(Ensure you have a `.env.example` or create one from the table above)*.
2. **Build and start the containers:**
```bash ```bash
docker-compose up -d --build docker-compose up -d --build
``` ```
PostgreSQL and the API will start. The API will automatically run migrations during startup.
2. **Run Database Migrations:**
Since the API uses Prisma, you need to run migrations to set up the database schema:
```bash
docker exec -it hotdesking_api npx prisma migrate deploy
```
3. **(Optional) Seed the Database:** 3. **(Optional) Seed the Database:**
If you have a seed script defined in `apps/api/prisma/seed.ts`: If you have a seed script defined in `apps/api/prisma/seed.ts`:
@ -41,6 +42,18 @@ Create a `.env` file in the root directory. The following variables are supporte
docker exec -it hotdesking_api npx prisma db seed docker exec -it hotdesking_api npx prisma db seed
``` ```
## Important Note: PostgreSQL Initial Configuration
When you run `docker-compose up` for the **first time**, the PostgreSQL container uses the `DB_USER`, `DB_PASSWORD`, and `DB_NAME` values to initialize the database. These values are stored in a persistent volume (`postgres_data`).
If you change these environment variables **after** the first run, the database will **not** be re-initialized. This can cause connection errors (like `role "desker" does not exist`).
To apply new database credentials, you must remove the volume and start over:
```bash
docker-compose down -v
docker-compose up -d
```
**Warning: This will delete all data in your database.**
4. **Access the Applications:** 4. **Access the Applications:**
- Web App: [http://localhost:5173](http://localhost:5173) - Web App: [http://localhost:5173](http://localhost:5173)
- API: [http://localhost:3000](http://localhost:3000) - API: [http://localhost:3000](http://localhost:3000)

View File

@ -1,3 +1,14 @@
-- CreateTable
CREATE TABLE "Asset" (
"id" TEXT NOT NULL,
"data" BYTEA NOT NULL,
"mimeType" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "Asset_pkey" PRIMARY KEY ("id")
);
-- AlterTable -- AlterTable
ALTER TABLE "Organization" ADD COLUMN "language" TEXT NOT NULL DEFAULT 'en', ALTER TABLE "Organization" ADD COLUMN "language" TEXT NOT NULL DEFAULT 'en',
ADD COLUMN "logoId" TEXT, ADD COLUMN "logoId" TEXT,
@ -8,3 +19,36 @@ CREATE UNIQUE INDEX "Organization_logoId_key" ON "Organization"("logoId");
-- AddForeignKey -- AddForeignKey
ALTER TABLE "Organization" ADD CONSTRAINT "Organization_logoId_fkey" FOREIGN KEY ("logoId") REFERENCES "Asset"("id") ON DELETE SET NULL ON UPDATE CASCADE; ALTER TABLE "Organization" ADD CONSTRAINT "Organization_logoId_fkey" FOREIGN KEY ("logoId") REFERENCES "Asset"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- Also add missing columns for other models that are in schema.prisma but not in init
ALTER TABLE "Building" ADD COLUMN "description" TEXT, ADD COLUMN "imageUrl" TEXT, ADD COLUMN "imageId" TEXT;
CREATE UNIQUE INDEX "Building_imageId_key" ON "Building"("imageId");
ALTER TABLE "Building" ADD CONSTRAINT "Building_imageId_fkey" FOREIGN KEY ("imageId") REFERENCES "Asset"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "Floor" ADD COLUMN "planImageUrl" TEXT, ADD COLUMN "name" TEXT, ADD COLUMN "planImageId" TEXT;
CREATE UNIQUE INDEX "Floor_planImageId_key" ON "Floor"("planImageId");
ALTER TABLE "Floor" ADD CONSTRAINT "Floor_planImageId_fkey" FOREIGN KEY ("planImageId") REFERENCES "Asset"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "Space" ADD COLUMN "height" DOUBLE PRECISION, ADD COLUMN "width" DOUBLE PRECISION, ADD COLUMN "rotation" DOUBLE PRECISION DEFAULT 0;
ALTER TABLE "User" ADD COLUMN "name" TEXT, ADD COLUMN "avatarUrl" TEXT, ADD COLUMN "avatarId" TEXT;
CREATE UNIQUE INDEX "User_avatarId_key" ON "User"("avatarId");
ALTER TABLE "User" ADD CONSTRAINT "User_avatarId_fkey" FOREIGN KEY ("avatarId") REFERENCES "Asset"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- CreateTable
CREATE TABLE "Reservation" (
"id" TEXT NOT NULL,
"startTime" TIMESTAMP(3) NOT NULL,
"endTime" TIMESTAMP(3) NOT NULL,
"userId" TEXT,
"spaceId" TEXT NOT NULL,
"note" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "Reservation_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "Reservation" ADD CONSTRAINT "Reservation_spaceId_fkey" FOREIGN KEY ("spaceId") REFERENCES "Space"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "Reservation" ADD CONSTRAINT "Reservation_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;