Updated schema.

This commit is contained in:
Mike Cao 2020-07-18 23:54:25 -07:00
parent eebf145efc
commit d8c8df2955
5 changed files with 52 additions and 34 deletions

View File

@ -16,7 +16,7 @@ export async function getWebsite(website_id) {
return runQuery( return runQuery(
prisma.website.findOne({ prisma.website.findOne({
where: { where: {
website_id, website_uuid: website_id,
}, },
}), }),
); );
@ -26,10 +26,10 @@ export async function createSession(website_id, session_id, data) {
await runQuery( await runQuery(
prisma.session.create({ prisma.session.create({
data: { data: {
session_id, session_uuid: session_id,
website: { website: {
connect: { connect: {
website_id, website_uuid: website_id,
}, },
}, },
...data, ...data,
@ -42,19 +42,24 @@ export async function getSession(session_id) {
return runQuery( return runQuery(
prisma.session.findOne({ prisma.session.findOne({
where: { 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( return runQuery(
prisma.pageview.create({ prisma.pageview.create({
data: { data: {
website: {
connect: {
website_uuid: website_id,
},
},
session: { session: {
connect: { connect: {
session_id, session_uuid: session_id,
}, },
}, },
url, url,

View File

@ -103,6 +103,7 @@ export function parseCollectRequest(req) {
return { return {
success: 1, success: 1,
type, type,
website_id,
session_id, session_id,
url, url,
referrer, referrer,

View File

@ -8,10 +8,10 @@ export default async (req, res) => {
const values = parseCollectRequest(req); const values = parseCollectRequest(req);
if (values.success) { if (values.success) {
const { type, session_id, url, referrer } = values; const { type, website_id, session_id, url, referrer } = values;
if (type === 'pageview') { if (type === 'pageview') {
await savePageView(session_id, url, referrer).catch(() => { await savePageView(website_id, session_id, url, referrer).catch(() => {
values.success = 0; values.success = 0;
}); });
} }

View File

@ -12,9 +12,11 @@ model event {
event_id Int @default(autoincrement()) @id event_id Int @default(autoincrement()) @id
event_type String event_type String
event_value String event_value String
session_id String? session_id Int
url String 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") @@index([created_at], name: "event_created_at_idx")
} }
@ -22,10 +24,12 @@ model event {
model pageview { model pageview {
created_at DateTime? @default(now()) created_at DateTime? @default(now())
referrer String? referrer String?
session_id String? session_id Int
url String url String
view_id Int @default(autoincrement()) @id 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") @@index([created_at], name: "pageview_created_at_idx")
} }
@ -38,9 +42,10 @@ model session {
language String? language String?
os String? os String?
screen String? screen String?
session_id String @id session_id Int @default(autoincrement()) @id
website_id String? session_uuid String @unique
website website? @relation(fields: [website_id], references: [website_id]) website_id Int
website website @relation(fields: [website_id], references: [website_id])
event event[] event event[]
pageview pageview[] pageview pageview[]
@ -49,7 +54,10 @@ model session {
model website { model website {
created_at DateTime? @default(now()) created_at DateTime? @default(now())
hostname String @unique hostname String
website_id String @id website_id Int @default(autoincrement()) @id
website_uuid String @unique
event event[]
pageview pageview[]
session session[] session session[]
} }

View File

@ -1,12 +1,14 @@
create table website ( create table website (
website_id uuid primary key, website_id serial primary key,
hostname varchar(100) unique not null, website_uuid uuid unique not null,
hostname varchar(100) not null,
created_at timestamp with time zone default current_timestamp created_at timestamp with time zone default current_timestamp
); );
create table session ( create table session (
session_id uuid primary key, session_id serial primary key,
website_id uuid references website(website_id) on delete cascade, 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, created_at timestamp with time zone default current_timestamp,
hostname varchar(100), hostname varchar(100),
browser varchar(20), browser varchar(20),
@ -18,7 +20,8 @@ create table session (
create table pageview ( create table pageview (
view_id serial primary key, 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, created_at timestamp with time zone default current_timestamp,
url varchar(500) not null, url varchar(500) not null,
referrer varchar(500) referrer varchar(500)
@ -26,7 +29,8 @@ create table pageview (
create table event ( create table event (
event_id serial primary key, 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, created_at timestamp with time zone default current_timestamp,
url varchar(500) not null, url varchar(500) not null,
event_type varchar(50) not null, event_type varchar(50) not null,