Skip to content

Commit 52de8e4

Browse files
committed
feat: categories, kpidefinitions and kpiresults from database
1 parent c90de0d commit 52de8e4

31 files changed

Lines changed: 875 additions & 213 deletions

prisma/migrations/0_init/migration.sql

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CREATE TABLE `categories` (
33
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
44
`name` VARCHAR(255) NOT NULL,
55
`description` TEXT NULL,
6+
`type` VARCHAR(50) NOT NULL,
67
`created_at` TIMESTAMP(0) NULL,
78
`updated_at` TIMESTAMP(0) NULL,
89

@@ -62,27 +63,50 @@ CREATE TABLE `items` (
6263

6364
-- CreateTable
6465
CREATE TABLE `kpidefinitions` (
65-
`id` INTEGER NOT NULL AUTO_INCREMENT,
66+
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
67+
`parent_kpi_id` BIGINT UNSIGNED NULL,
68+
`kpi_number` VARCHAR(50) NOT NULL,
69+
`type` VARCHAR(50) NOT NULL,
6670
`name` VARCHAR(100) NOT NULL,
67-
`description` VARCHAR(5000) NOT NULL,
71+
`description` TEXT NOT NULL,
6872
`metric` VARCHAR(50) NOT NULL,
73+
`metric_description` TEXT NULL,
74+
`disclaimer` TEXT NULL,
75+
`progression_target` INTEGER NOT NULL,
76+
`min_value` FLOAT NULL,
77+
`max_value` FLOAT NULL,
6978
`created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
7079
`updated_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
7180

81+
INDEX `kpidefinitions_parent_kpi_id_index`(`parent_kpi_id`),
82+
INDEX `kpidefinitions_kpi_number_index`(`kpi_number`),
83+
PRIMARY KEY (`id`)
84+
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
85+
86+
-- CreateTable
87+
CREATE TABLE `kpidefinitions_category` (
88+
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
89+
`kpidefinition_id` BIGINT UNSIGNED NOT NULL,
90+
`category_id` BIGINT UNSIGNED NOT NULL,
91+
92+
INDEX `kpidefinitions_category_kpidefinition_id_foreign`(`kpidefinition_id`),
93+
INDEX `kpidefinitions_category_category_id_foreign`(`category_id`),
94+
UNIQUE INDEX `uniq_kpidefinition_category`(`kpidefinition_id`, `category_id`),
7295
PRIMARY KEY (`id`)
7396
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
7497

7598
-- CreateTable
7699
CREATE TABLE `kpiresults` (
77-
`id` INTEGER NOT NULL AUTO_INCREMENT,
78-
`kpidefinition_id` INTEGER NOT NULL,
79-
`value` VARCHAR(1000) NOT NULL,
100+
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
101+
`kpidefinition_id` BIGINT UNSIGNED NOT NULL,
102+
`living_lab_id` BIGINT UNSIGNED NOT NULL,
103+
`transport_mode_id` BIGINT UNSIGNED NULL,
104+
`value` FLOAT NOT NULL,
105+
`description` VARCHAR(5000) NULL,
80106
`date` DATE NOT NULL,
81107
`created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
82108
`updated_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
83-
`project_id` INTEGER NOT NULL,
84-
`user_id` INTEGER NULL,
85-
`description` VARCHAR(5000) NULL,
109+
`user_id` BIGINT UNSIGNED NULL,
86110

87111
PRIMARY KEY (`id`)
88112
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
@@ -243,6 +267,27 @@ ALTER TABLE `item_tag` ADD CONSTRAINT `item_tag_item_id_foreign` FOREIGN KEY (`i
243267
-- AddForeignKey
244268
ALTER TABLE `item_tag` ADD CONSTRAINT `item_tag_tag_id_foreign` FOREIGN KEY (`tag_id`) REFERENCES `tags`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
245269

270+
-- AddForeignKey
271+
ALTER TABLE `kpidefinitions` ADD CONSTRAINT `kpidefinitions_parent_kpi_id_index` FOREIGN KEY (`parent_kpi_id`) REFERENCES `kpidefinitions`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
272+
273+
-- AddForeignKey
274+
ALTER TABLE `kpidefinitions_category` ADD CONSTRAINT `kpidefinitions_category_kpidefinition_id_foreign` FOREIGN KEY (`kpidefinition_id`) REFERENCES `kpidefinitions`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
275+
276+
-- AddForeignKey
277+
ALTER TABLE `kpidefinitions_category` ADD CONSTRAINT `kpidefinitions_category_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
278+
279+
-- AddForeignKey
280+
ALTER TABLE `kpiresults` ADD CONSTRAINT `kpiresults_kpidefinition_id_fkey` FOREIGN KEY (`kpidefinition_id`) REFERENCES `kpidefinitions`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
281+
282+
-- AddForeignKey
283+
ALTER TABLE `kpiresults` ADD CONSTRAINT `kpiresults_living_lab_id_fkey` FOREIGN KEY (`living_lab_id`) REFERENCES `labs`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
284+
285+
-- AddForeignKey
286+
ALTER TABLE `kpiresults` ADD CONSTRAINT `kpiresults_transport_mode_id_fkey` FOREIGN KEY (`transport_mode_id`) REFERENCES `transport_mode`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
287+
288+
-- AddForeignKey
289+
ALTER TABLE `kpiresults` ADD CONSTRAINT `kpiresults_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;
290+
246291
-- AddForeignKey
247292
ALTER TABLE `labs` ADD CONSTRAINT `labs_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
248293

prisma/schema.prisma

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ datasource db {
88
}
99

1010
model categories {
11-
id BigInt @id @default(autoincrement()) @db.UnsignedBigInt
12-
name String @unique(map: "categories_name_unique") @db.VarChar(255)
13-
description String? @db.Text
14-
created_at DateTime? @db.Timestamp(0)
15-
updated_at DateTime? @db.Timestamp(0)
11+
id BigInt @id @default(autoincrement()) @db.UnsignedBigInt
12+
name String @unique(map: "categories_name_unique") @db.VarChar(255)
13+
description String? @db.Text
14+
type String @db.VarChar(50)
15+
created_at DateTime? @db.Timestamp(0)
16+
updated_at DateTime? @db.Timestamp(0)
17+
18+
kpidefinitions_category kpidefinitions_category[]
1619
}
1720

1821
model failed_jobs {
@@ -59,24 +62,60 @@ model items {
5962
}
6063

6164
model kpidefinitions {
62-
id Int @id @default(autoincrement())
63-
name String @db.VarChar(100)
64-
description String @db.VarChar(5000)
65-
metric String @db.VarChar(50)
66-
created_at DateTime @default(now()) @db.Timestamp(0)
67-
updated_at DateTime @default(now()) @db.Timestamp(0)
65+
id BigInt @id @default(autoincrement()) @db.UnsignedBigInt
66+
parent_kpi_id BigInt? @db.UnsignedBigInt
67+
kpi_number String @db.VarChar(50)
68+
type String @db.VarChar(50)
69+
name String @db.VarChar(100)
70+
description String @db.Text
71+
metric String @db.VarChar(50)
72+
metric_description String? @db.Text
73+
disclaimer String? @db.Text
74+
progression_target Int
75+
min_value Float? @db.Float
76+
max_value Float? @db.Float
77+
created_at DateTime @default(now()) @db.Timestamp(0)
78+
updated_at DateTime @default(now()) @db.Timestamp(0)
79+
80+
kpiresults kpiresults[]
81+
kpidefinitions_category kpidefinitions_category[]
82+
children_kpis kpidefinitions[] @relation("kpi_parent_child")
83+
84+
parent_kpi kpidefinitions? @relation("kpi_parent_child", fields: [parent_kpi_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "kpidefinitions_parent_kpi_id_index")
85+
86+
@@index([parent_kpi_id], map: "kpidefinitions_parent_kpi_id_index")
87+
@@index([kpi_number], map: "kpidefinitions_kpi_number_index")
88+
}
89+
90+
model kpidefinitions_category {
91+
id BigInt @id @default(autoincrement()) @db.UnsignedBigInt
92+
kpidefinition_id BigInt @db.UnsignedBigInt
93+
category_id BigInt @db.UnsignedBigInt
94+
95+
kpidefinition kpidefinitions @relation(fields: [kpidefinition_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "kpidefinitions_category_kpidefinition_id_foreign")
96+
category categories @relation(fields: [category_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "kpidefinitions_category_category_id_foreign")
97+
98+
@@index([kpidefinition_id], map: "kpidefinitions_category_kpidefinition_id_foreign")
99+
@@index([category_id], map: "kpidefinitions_category_category_id_foreign")
100+
@@unique([kpidefinition_id, category_id], map: "uniq_kpidefinition_category")
68101
}
69102

70103
model kpiresults {
71-
id Int @id @default(autoincrement())
72-
kpidefinition_id Int
73-
value String @db.VarChar(1000)
74-
date DateTime @db.Date
75-
created_at DateTime @default(now()) @db.Timestamp(0)
76-
updated_at DateTime @default(now()) @db.Timestamp(0)
77-
project_id Int
78-
user_id Int?
79-
description String? @db.VarChar(5000)
104+
id BigInt @id @default(autoincrement()) @db.UnsignedBigInt
105+
kpidefinition_id BigInt @db.UnsignedBigInt
106+
living_lab_id BigInt @db.UnsignedBigInt
107+
transport_mode_id BigInt? @db.UnsignedBigInt
108+
value Float @db.Float
109+
description String? @db.VarChar(5000)
110+
date DateTime @db.Date
111+
created_at DateTime @default(now()) @db.Timestamp(0)
112+
updated_at DateTime @default(now()) @db.Timestamp(0)
113+
user_id BigInt? @db.UnsignedBigInt
114+
115+
kpidefinition kpidefinitions @relation(fields: [kpidefinition_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
116+
living_lab labs @relation(fields: [living_lab_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
117+
transport_mode transport_mode? @relation(fields: [transport_mode_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
118+
user users? @relation(fields: [user_id], references: [id], onDelete: SetNull, onUpdate: NoAction)
80119
}
81120

82121
model labs {
@@ -101,7 +140,7 @@ model labs {
101140
validated_by users? @relation(name: "validated_by", fields: [validated_by_user_id], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "labs_validated_by_user_id_foreign")
102141
living_lab_projects_implementation living_lab_projects_implementation[]
103142
transport_mode_living_lab_implementation transport_mode_living_lab_implementation[]
104-
143+
kpiresults kpiresults[]
105144
106145
@@index([validated_by_user_id], map: "labs_validated_by_user_id_foreign")
107146
@@index([user_id], map: "labs_user_id_foreign")
@@ -137,7 +176,7 @@ model projects {
137176
description String? @db.Text
138177
created_at DateTime? @db.Timestamp(0)
139178
updated_at DateTime? @db.Timestamp(0)
140-
type String @db.VarChar(255)
179+
type String @db.VarChar(255)
141180
image_url String? @db.Text
142181
living_lab_projects_implementation living_lab_projects_implementation[]
143182
}
@@ -193,7 +232,7 @@ model users {
193232
updated_at DateTime? @db.Timestamp(0)
194233
195234
role roles @relation(fields: [role_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
196-
235+
kpiresults kpiresults[]
197236
created_labs labs[] @relation(name: "created_by") @ignore
198237
validated_labs labs[] @relation(name: "validated_by") @ignore
199238
@@ -208,6 +247,7 @@ model transport_mode {
208247
created_at DateTime? @db.Timestamp(0)
209248
210249
transport_mode_living_lab_implementation transport_mode_living_lab_implementation[]
250+
kpiresults kpiresults[]
211251
}
212252

213253
model transport_mode_living_lab_implementation {

prisma/seeds/categories.sql

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
INSERT INTO `categories` (`id`, `name`, `type`, `description`) VALUES
2+
(1, 'Policies', 'KPI_SIEF', NULL),
3+
(2, 'Transport System - Time', 'KPI_SIEF', NULL),
4+
(3, 'Transport System - Safety/Comfort', 'KPI_SIEF', NULL),
5+
(4, 'Transport System - Cost', 'KPI_SIEF', NULL),
6+
(5, 'Impact - Environment', 'KPI_SIEF', NULL),
7+
(6, 'Impact - Society', 'KPI_SIEF', NULL),
8+
(7, 'Impact - Economy', 'KPI_SIEF', NULL);
9+
10+
INSERT INTO `kpidefinitions_category` (`kpidefinition_id`, `category_id`) VALUES
11+
-- Category 1: Policies
12+
(1, 1),
13+
(2, 1),
14+
(3, 1),
15+
(4, 1),
16+
(5, 1),
17+
(6, 1),
18+
-- Category 2: Transport System - Time
19+
(12, 2),
20+
(13, 2),
21+
(14, 2),
22+
(15, 2),
23+
(16, 2),
24+
(17, 2),
25+
(18, 2),
26+
(19, 2),
27+
(33, 2),
28+
(34, 2),
29+
(35, 2),
30+
(36, 2),
31+
(37, 2),
32+
(38, 2),
33+
(39, 2),
34+
-- Category 3: Transport System - Safety/Comfort
35+
(8, 3),
36+
(9, 3),
37+
(10, 3),
38+
(11, 3),
39+
(40, 3),
40+
(41, 3),
41+
(42, 3),
42+
(43, 3),
43+
(44, 3),
44+
(45, 3),
45+
(46, 3),
46+
(47, 3),
47+
-- Category 4: Transport System - Cost
48+
(22, 4),
49+
(23, 4),
50+
(24, 4),
51+
(25, 4),
52+
(26, 4),
53+
(27, 4),
54+
(28, 4),
55+
(29, 4),
56+
(30, 4),
57+
(31, 4),
58+
(32, 4),
59+
(48, 4),
60+
-- Category 5: Impact - Environment
61+
(49, 5),
62+
(60, 5),
63+
(61, 5),
64+
(62, 5),
65+
-- Category 6: Impact - Society
66+
(63, 6),
67+
(64, 6),
68+
(65, 6),
69+
(66, 6),
70+
-- Category 7: Impact - Economy
71+
(50, 7),
72+
(67, 7),
73+
(68, 7),
74+
(69, 7),
75+
(70, 7);

0 commit comments

Comments
 (0)