Initialisation de la base données côté serveur

This commit is contained in:
Emmy D'Anello 2024-12-07 10:17:47 +01:00
parent d08dcb9720
commit ab180a12ce
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
9 changed files with 327 additions and 6 deletions

15
.vscode/settings.json vendored Normal file
View File

@ -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
}

1
server/.env Normal file
View File

@ -0,0 +1 @@
DATABASE_URL="postgres://traintrapemoi:traintrapemoi@localhost:5432/traintrape-moi"

10
server/.gitignore vendored
View File

@ -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

View File

@ -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",

View File

@ -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"
}
}

View File

@ -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;

View File

@ -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"

View File

@ -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
}

26
server/prisma/seed.ts Normal file
View File

@ -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();
});