From d8c8df295546a6515f6c497b27312b155bbffb42 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sat, 18 Jul 2020 23:54:25 -0700 Subject: [PATCH] Updated schema. --- lib/db.js | 17 +++++++++----- lib/utils.js | 1 + pages/api/collect.js | 4 ++-- prisma/schema.prisma | 48 +++++++++++++++++++++++---------------- sql/schema.postgresql.sql | 16 ++++++++----- 5 files changed, 52 insertions(+), 34 deletions(-) diff --git a/lib/db.js b/lib/db.js index 7304abe7..b925b52c 100644 --- a/lib/db.js +++ b/lib/db.js @@ -16,7 +16,7 @@ export async function getWebsite(website_id) { return runQuery( prisma.website.findOne({ where: { - website_id, + website_uuid: website_id, }, }), ); @@ -26,10 +26,10 @@ export async function createSession(website_id, session_id, data) { await runQuery( prisma.session.create({ data: { - session_id, + session_uuid: session_id, website: { connect: { - website_id, + website_uuid: website_id, }, }, ...data, @@ -42,19 +42,24 @@ export async function getSession(session_id) { return runQuery( prisma.session.findOne({ where: { - session_id, + session_uuid: session_id, }, }), ); } -export async function savePageView(session_id, url, referrer) { +export async function savePageView(website_id, session_id, url, referrer) { return runQuery( prisma.pageview.create({ data: { + website: { + connect: { + website_uuid: website_id, + }, + }, session: { connect: { - session_id, + session_uuid: session_id, }, }, url, diff --git a/lib/utils.js b/lib/utils.js index 60397e94..ccf8c43d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -103,6 +103,7 @@ export function parseCollectRequest(req) { return { success: 1, type, + website_id, session_id, url, referrer, diff --git a/pages/api/collect.js b/pages/api/collect.js index d1c827c5..2f47cf9a 100644 --- a/pages/api/collect.js +++ b/pages/api/collect.js @@ -8,10 +8,10 @@ export default async (req, res) => { const values = parseCollectRequest(req); if (values.success) { - const { type, session_id, url, referrer } = values; + const { type, website_id, session_id, url, referrer } = values; if (type === 'pageview') { - await savePageView(session_id, url, referrer).catch(() => { + await savePageView(website_id, session_id, url, referrer).catch(() => { values.success = 0; }); } diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 372681fd..dabebba4 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -12,9 +12,11 @@ model event { event_id Int @default(autoincrement()) @id event_type String event_value String - session_id String? + session_id Int url String - session session? @relation(fields: [session_id], references: [session_id]) + website_id Int + session session @relation(fields: [session_id], references: [session_id]) + website website @relation(fields: [website_id], references: [website_id]) @@index([created_at], name: "event_created_at_idx") } @@ -22,34 +24,40 @@ model event { model pageview { created_at DateTime? @default(now()) referrer String? - session_id String? + session_id Int url String view_id Int @default(autoincrement()) @id - session session? @relation(fields: [session_id], references: [session_id]) + website_id Int + session session @relation(fields: [session_id], references: [session_id]) + website website @relation(fields: [website_id], references: [website_id]) @@index([created_at], name: "pageview_created_at_idx") } model session { - browser String? - country String? - created_at DateTime? @default(now()) - hostname String? - language String? - os String? - screen String? - session_id String @id - website_id String? - website website? @relation(fields: [website_id], references: [website_id]) - event event[] - pageview pageview[] + browser String? + country String? + created_at DateTime? @default(now()) + hostname String? + language String? + os String? + screen String? + session_id Int @default(autoincrement()) @id + session_uuid String @unique + website_id Int + website website @relation(fields: [website_id], references: [website_id]) + event event[] + pageview pageview[] @@index([created_at], name: "session_created_at_idx") } model website { - created_at DateTime? @default(now()) - hostname String @unique - website_id String @id - session session[] + created_at DateTime? @default(now()) + hostname String + website_id Int @default(autoincrement()) @id + website_uuid String @unique + event event[] + pageview pageview[] + session session[] } diff --git a/sql/schema.postgresql.sql b/sql/schema.postgresql.sql index b5ddcbac..da65c4b3 100644 --- a/sql/schema.postgresql.sql +++ b/sql/schema.postgresql.sql @@ -1,12 +1,14 @@ create table website ( - website_id uuid primary key, - hostname varchar(100) unique not null, + website_id serial primary key, + website_uuid uuid unique not null, + hostname varchar(100) not null, created_at timestamp with time zone default current_timestamp ); create table session ( - session_id uuid primary key, - website_id uuid references website(website_id) on delete cascade, + session_id serial primary key, + session_uuid uuid unique not null, + website_id int not null references website(website_id) on delete cascade, created_at timestamp with time zone default current_timestamp, hostname varchar(100), browser varchar(20), @@ -18,7 +20,8 @@ create table session ( create table pageview ( view_id serial primary key, - session_id uuid references session(session_id) on delete cascade, + website_id int not null references website(website_id), + session_id int not null references session(session_id) on delete cascade, created_at timestamp with time zone default current_timestamp, url varchar(500) not null, referrer varchar(500) @@ -26,7 +29,8 @@ create table pageview ( create table event ( event_id serial primary key, - session_id uuid references session(session_id) on delete cascade, + website_id int not null references website(website_id), + session_id int not null references session(session_id) on delete cascade, created_at timestamp with time zone default current_timestamp, url varchar(500) not null, event_type varchar(50) not null,