From ab180a12ce0b2e84685990f21be05a59e087d7bd Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 7 Dec 2024 10:17:47 +0100 Subject: [PATCH] =?UTF-8?q?Initialisation=20de=20la=20base=20donn=C3=A9es?= =?UTF-8?q?=20c=C3=B4t=C3=A9=20serveur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 15 +++ server/.env | 1 + server/.gitignore | 10 +- server/package-lock.json | 85 +++++++++++++- server/package.json | 5 + .../20241207090423_init/migration.sql | 108 ++++++++++++++++++ server/prisma/migrations/migration_lock.toml | 3 + server/prisma/schema.prisma | 80 +++++++++++++ server/prisma/seed.ts | 26 +++++ 9 files changed, 327 insertions(+), 6 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 server/.env create mode 100644 server/prisma/migrations/20241207090423_init/migration.sql create mode 100644 server/prisma/migrations/migration_lock.toml create mode 100644 server/prisma/schema.prisma create mode 100644 server/prisma/seed.ts diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ef5d934 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,15 @@ +{ + "sqltools.connections": [ + { + "previewLimit": 50, + "server": "localhost", + "port": 5432, + "driver": "PostgreSQL", + "name": "Traintrape-moi", + "database": "traintrape-moi", + "username": "traintrapemoi", + "socketPath": "/run/postgresql/.s.PGSQL.5432" + } + ], + "sqltools.useNodeRuntime": true +} \ No newline at end of file diff --git a/server/.env b/server/.env new file mode 100644 index 0000000..9cb7700 --- /dev/null +++ b/server/.env @@ -0,0 +1 @@ +DATABASE_URL="postgres://traintrapemoi:traintrapemoi@localhost:5432/traintrape-moi" diff --git a/server/.gitignore b/server/.gitignore index 4b56acf..6044d1b 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -3,6 +3,10 @@ /node_modules /build +# Databases +*.sqlite3 +*.sqlite3-journal + # Logs logs *.log @@ -36,11 +40,7 @@ lerna-debug.log* !.vscode/extensions.json # dotenv environment variable files -.env -.env.development.local -.env.test.local -.env.production.local -.env.local +.env*.local # temp directory .temp diff --git a/server/package-lock.json b/server/package-lock.json index a8eef57..334f61c 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -12,6 +12,8 @@ "@nestjs/common": "^10.0.0", "@nestjs/core": "^10.0.0", "@nestjs/platform-express": "^10.0.0", + "@prisma/client": "^6.0.1", + "prisma": "^6.0.1", "reflect-metadata": "^0.2.0", "rxjs": "^7.8.1" }, @@ -1841,6 +1843,69 @@ "url": "https://opencollective.com/unts" } }, + "node_modules/@prisma/client": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-6.0.1.tgz", + "integrity": "sha512-60w7kL6bUxz7M6Gs/V+OWMhwy94FshpngVmOY05TmGD0Lhk+Ac0ZgtjlL6Wll9TD4G03t4Sq1wZekNVy+Xdlbg==", + "hasInstallScript": true, + "license": "Apache-2.0", + "engines": { + "node": ">=18.18" + }, + "peerDependencies": { + "prisma": "*" + }, + "peerDependenciesMeta": { + "prisma": { + "optional": true + } + } + }, + "node_modules/@prisma/debug": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@prisma/debug/-/debug-6.0.1.tgz", + "integrity": "sha512-jQylgSOf7ibTVxqBacnAlVGvek6fQxJIYCQOeX2KexsfypNzXjJQSS2o5s+Mjj2Np93iSOQUaw6TvPj8syhG4w==", + "license": "Apache-2.0" + }, + "node_modules/@prisma/engines": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-6.0.1.tgz", + "integrity": "sha512-4hxzI+YQIR2uuDyVsDooFZGu5AtixbvM2psp+iayDZ4hRrAHo/YwgA17N23UWq7G6gRu18NvuNMb48qjP3DPQw==", + "hasInstallScript": true, + "license": "Apache-2.0", + "dependencies": { + "@prisma/debug": "6.0.1", + "@prisma/engines-version": "5.23.0-27.5dbef10bdbfb579e07d35cc85fb1518d357cb99e", + "@prisma/fetch-engine": "6.0.1", + "@prisma/get-platform": "6.0.1" + } + }, + "node_modules/@prisma/engines-version": { + "version": "5.23.0-27.5dbef10bdbfb579e07d35cc85fb1518d357cb99e", + "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.23.0-27.5dbef10bdbfb579e07d35cc85fb1518d357cb99e.tgz", + "integrity": "sha512-JmIds0Q2/vsOmnuTJYxY4LE+sajqjYKhLtdOT6y4imojqv5d/aeVEfbBGC74t8Be1uSp0OP8lxIj2OqoKbLsfQ==", + "license": "Apache-2.0" + }, + "node_modules/@prisma/fetch-engine": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@prisma/fetch-engine/-/fetch-engine-6.0.1.tgz", + "integrity": "sha512-T36bWFVGeGYYSyYOj9d+O9G3sBC+pAyMC+jc45iSL63/Haq1GrYjQPgPMxrEj9m739taXrupoysRedQ+VyvM/Q==", + "license": "Apache-2.0", + "dependencies": { + "@prisma/debug": "6.0.1", + "@prisma/engines-version": "5.23.0-27.5dbef10bdbfb579e07d35cc85fb1518d357cb99e", + "@prisma/get-platform": "6.0.1" + } + }, + "node_modules/@prisma/get-platform": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@prisma/get-platform/-/get-platform-6.0.1.tgz", + "integrity": "sha512-zspC9vlxAqx4E6epMPMLLBMED2VD8axDe8sPnquZ8GOsn6tiacWK0oxrGK4UAHYzYUVuMVUApJbdXB2dFpLhvg==", + "license": "Apache-2.0", + "dependencies": { + "@prisma/debug": "6.0.1" + } + }, "node_modules/@sinclair/typebox": { "version": "0.27.8", "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz", @@ -4782,7 +4847,6 @@ "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", - "dev": true, "hasInstallScript": true, "license": "MIT", "optional": true, @@ -7231,6 +7295,25 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, + "node_modules/prisma": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/prisma/-/prisma-6.0.1.tgz", + "integrity": "sha512-CaMNFHkf+DDq8zq3X/JJsQ4Koy7dyWwwtOKibkT/Am9j/tDxcfbg7+lB1Dzhx18G/+RQCMgjPYB61bhRqteNBQ==", + "hasInstallScript": true, + "license": "Apache-2.0", + "dependencies": { + "@prisma/engines": "6.0.1" + }, + "bin": { + "prisma": "build/index.js" + }, + "engines": { + "node": ">=18.18" + }, + "optionalDependencies": { + "fsevents": "2.3.3" + } + }, "node_modules/process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", diff --git a/server/package.json b/server/package.json index d40b272..46ae97b 100644 --- a/server/package.json +++ b/server/package.json @@ -23,6 +23,8 @@ "@nestjs/common": "^10.0.0", "@nestjs/core": "^10.0.0", "@nestjs/platform-express": "^10.0.0", + "@prisma/client": "^6.0.1", + "prisma": "^6.0.1", "reflect-metadata": "^0.2.0", "rxjs": "^7.8.1" }, @@ -65,5 +67,8 @@ ], "coverageDirectory": "../coverage", "testEnvironment": "node" + }, + "prisma": { + "seed": "ts-node prisma/seed.ts" } } diff --git a/server/prisma/migrations/20241207090423_init/migration.sql b/server/prisma/migrations/20241207090423_init/migration.sql new file mode 100644 index 0000000..de3d1b5 --- /dev/null +++ b/server/prisma/migrations/20241207090423_init/migration.sql @@ -0,0 +1,108 @@ +-- CreateEnum +CREATE TYPE "MoneyUpdateType" AS ENUM ('START', 'WIN_CHALLENGE', 'BUY_TRAIN'); + +-- CreateTable +CREATE TABLE "User" ( + "id" SERIAL NOT NULL, + "name" TEXT NOT NULL, + "money" INTEGER NOT NULL DEFAULT 0, + "currentRunner" BOOLEAN NOT NULL DEFAULT false, + + CONSTRAINT "User_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Geolocation" ( + "id" SERIAL NOT NULL, + "userId" INTEGER NOT NULL, + "longitude" DOUBLE PRECISION NOT NULL, + "latitude" DOUBLE PRECISION NOT NULL, + "timestamp" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "Geolocation_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Challenge" ( + "id" SERIAL NOT NULL, + "title" TEXT NOT NULL, + "description" TEXT NOT NULL, + "reward" INTEGER NOT NULL, + + CONSTRAINT "Challenge_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "ChallengeAction" ( + "id" SERIAL NOT NULL, + "userId" INTEGER NOT NULL, + "challengeId" INTEGER NOT NULL, + "active" BOOLEAN NOT NULL DEFAULT false, + "success" BOOLEAN NOT NULL DEFAULT false, + + CONSTRAINT "ChallengeAction_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "TrainTrip" ( + "id" TEXT NOT NULL, + "userId" INTEGER NOT NULL, + "distance" DOUBLE PRECISION NOT NULL, + "from" TEXT NOT NULL, + "to" TEXT NOT NULL, + "departureTime" TIMESTAMP(3) NOT NULL, + "arrivalTime" TIMESTAMP(3) NOT NULL, + "infoJson" JSONB NOT NULL, + "geometry" TEXT NOT NULL, + + CONSTRAINT "TrainTrip_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "MoneyUpdate" ( + "id" SERIAL NOT NULL, + "userId" INTEGER NOT NULL, + "before" INTEGER NOT NULL, + "after" INTEGER NOT NULL, + "reason" "MoneyUpdateType" NOT NULL, + "actionId" INTEGER, + "tripId" TEXT, + + CONSTRAINT "MoneyUpdate_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "User_name_key" ON "User"("name"); + +-- CreateIndex +CREATE UNIQUE INDEX "Challenge_title_key" ON "Challenge"("title"); + +-- CreateIndex +CREATE UNIQUE INDEX "ChallengeAction_challengeId_key" ON "ChallengeAction"("challengeId"); + +-- CreateIndex +CREATE UNIQUE INDEX "MoneyUpdate_actionId_key" ON "MoneyUpdate"("actionId"); + +-- CreateIndex +CREATE UNIQUE INDEX "MoneyUpdate_tripId_key" ON "MoneyUpdate"("tripId"); + +-- AddForeignKey +ALTER TABLE "Geolocation" ADD CONSTRAINT "Geolocation_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "ChallengeAction" ADD CONSTRAINT "ChallengeAction_challengeId_fkey" FOREIGN KEY ("challengeId") REFERENCES "Challenge"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "ChallengeAction" ADD CONSTRAINT "ChallengeAction_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "TrainTrip" ADD CONSTRAINT "TrainTrip_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "MoneyUpdate" ADD CONSTRAINT "MoneyUpdate_actionId_fkey" FOREIGN KEY ("actionId") REFERENCES "ChallengeAction"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "MoneyUpdate" ADD CONSTRAINT "MoneyUpdate_tripId_fkey" FOREIGN KEY ("tripId") REFERENCES "TrainTrip"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "MoneyUpdate" ADD CONSTRAINT "MoneyUpdate_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/server/prisma/migrations/migration_lock.toml b/server/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..fbffa92 --- /dev/null +++ b/server/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "postgresql" \ No newline at end of file diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma new file mode 100644 index 0000000..71b9f46 --- /dev/null +++ b/server/prisma/schema.prisma @@ -0,0 +1,80 @@ +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "postgresql" + url = env("DATABASE_URL") +} + +model User { + id Int @id @default(autoincrement()) + name String @unique + money Int @default(0) + currentRunner Boolean @default(false) + actions ChallengeAction[] + geolocations Geolocation[] + moneyUpdates MoneyUpdate[] + trips TrainTrip[] +} + +model Geolocation { + id Int @id @default(autoincrement()) + userId Int + longitude Float + latitude Float + timestamp DateTime + user User @relation(fields: [userId], references: [id]) +} + +model Challenge { + id Int @id @default(autoincrement()) + title String @unique + description String + reward Int + action ChallengeAction? +} + +model ChallengeAction { + id Int @id @default(autoincrement()) + userId Int + challengeId Int @unique + active Boolean @default(false) + success Boolean @default(false) + challenge Challenge @relation(fields: [challengeId], references: [id]) + user User @relation(fields: [userId], references: [id]) + moneyUpdate MoneyUpdate? +} + +model TrainTrip { + id String @id + userId Int + distance Float + from String + to String + departureTime DateTime + arrivalTime DateTime + infoJson Json + geometry String + moneyUpdate MoneyUpdate? + user User @relation(fields: [userId], references: [id]) +} + +model MoneyUpdate { + id Int @id @default(autoincrement()) + userId Int + before Int + after Int + reason MoneyUpdateType + actionId Int? @unique + tripId String? @unique + action ChallengeAction? @relation(fields: [actionId], references: [id]) + trip TrainTrip? @relation(fields: [tripId], references: [id]) + user User @relation(fields: [userId], references: [id]) +} + +enum MoneyUpdateType { + START + WIN_CHALLENGE + BUY_TRAIN +} diff --git a/server/prisma/seed.ts b/server/prisma/seed.ts new file mode 100644 index 0000000..a624731 --- /dev/null +++ b/server/prisma/seed.ts @@ -0,0 +1,26 @@ +import { PrismaClient } from '@prisma/client'; + +const prisma = new PrismaClient(); + +async function main() { + const emmy = await prisma.user.upsert({ + where: { id: 1 }, + update: { name: 'Emmy' }, + create: { name: 'Emmy' }, + }); + const tamina = await prisma.user.upsert({ + where: { id: 2 }, + update: { name: 'Tamina' }, + create: { name: 'Tamina' }, + }); + console.log({ emmy, tamina }); +} + +main() + .catch((e) => { + console.error(e); + process.exit(1); + }) + .finally(async () => { + await prisma.$disconnect(); + });