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(
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,

View File

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

View File

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

View File

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

View File

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