2020-07-17 10:03:38 +02:00
|
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
|
|
|
|
export async function runQuery(query) {
|
|
|
|
return query
|
|
|
|
.catch(e => {
|
2020-07-19 11:23:15 +02:00
|
|
|
console.error(e);
|
2020-07-17 10:03:38 +02:00
|
|
|
throw e;
|
|
|
|
})
|
|
|
|
.finally(async () => {
|
|
|
|
await prisma.disconnect();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getWebsite(website_id) {
|
|
|
|
return runQuery(
|
|
|
|
prisma.website.findOne({
|
|
|
|
where: {
|
2020-07-19 08:54:25 +02:00
|
|
|
website_uuid: website_id,
|
2020-07-17 10:03:38 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createSession(website_id, session_id, data) {
|
|
|
|
await runQuery(
|
|
|
|
prisma.session.create({
|
|
|
|
data: {
|
2020-07-19 08:54:25 +02:00
|
|
|
session_uuid: session_id,
|
2020-07-17 10:03:38 +02:00
|
|
|
website: {
|
|
|
|
connect: {
|
2020-07-19 08:54:25 +02:00
|
|
|
website_uuid: website_id,
|
2020-07-17 10:03:38 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
...data,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSession(session_id) {
|
|
|
|
return runQuery(
|
|
|
|
prisma.session.findOne({
|
|
|
|
where: {
|
2020-07-19 08:54:25 +02:00
|
|
|
session_uuid: session_id,
|
2020-07-17 10:03:38 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-19 08:54:25 +02:00
|
|
|
export async function savePageView(website_id, session_id, url, referrer) {
|
2020-07-17 10:03:38 +02:00
|
|
|
return runQuery(
|
|
|
|
prisma.pageview.create({
|
|
|
|
data: {
|
2020-07-19 08:54:25 +02:00
|
|
|
website: {
|
|
|
|
connect: {
|
|
|
|
website_uuid: website_id,
|
|
|
|
},
|
|
|
|
},
|
2020-07-17 10:03:38 +02:00
|
|
|
session: {
|
|
|
|
connect: {
|
2020-07-19 08:54:25 +02:00
|
|
|
session_uuid: session_id,
|
2020-07-17 10:03:38 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
url,
|
|
|
|
referrer,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2020-07-19 10:57:01 +02:00
|
|
|
|
2020-07-19 11:23:15 +02:00
|
|
|
export async function saveEvent(website_id, session_id, url, event_type, event_value) {
|
2020-07-19 10:57:01 +02:00
|
|
|
return runQuery(
|
2020-07-19 11:23:15 +02:00
|
|
|
prisma.event.create({
|
2020-07-19 10:57:01 +02:00
|
|
|
data: {
|
|
|
|
website: {
|
|
|
|
connect: {
|
|
|
|
website_uuid: website_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
session: {
|
|
|
|
connect: {
|
|
|
|
session_uuid: session_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
url,
|
2020-07-19 11:23:15 +02:00
|
|
|
event_type,
|
|
|
|
event_value,
|
2020-07-19 10:57:01 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|