2020-07-17 10:03:38 +02:00
|
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
|
2020-07-24 19:56:45 +02:00
|
|
|
export const prisma = new PrismaClient({
|
2020-07-25 02:00:56 +02:00
|
|
|
log: [
|
|
|
|
{
|
|
|
|
emit: 'event',
|
|
|
|
level: 'query',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
prisma.on('query', e => {
|
|
|
|
if (process.env.LOG_QUERY) {
|
2020-07-26 09:12:42 +02:00
|
|
|
console.log(`${e.params} -> ${e.query} (${e.duration}ms)`);
|
2020-07-25 02:00:56 +02:00
|
|
|
}
|
2020-07-21 04:24:33 +02:00
|
|
|
});
|
2020-07-17 10:03:38 +02:00
|
|
|
|
|
|
|
export async function runQuery(query) {
|
2020-07-24 19:56:45 +02:00
|
|
|
return query.catch(e => {
|
|
|
|
console.error(e);
|
|
|
|
throw e;
|
|
|
|
});
|
2020-07-17 10:03:38 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 10:54:21 +02:00
|
|
|
export async function getWebsite(website_uuid) {
|
2020-07-17 10:03:38 +02:00
|
|
|
return runQuery(
|
|
|
|
prisma.website.findOne({
|
|
|
|
where: {
|
2020-07-20 10:54:21 +02:00
|
|
|
website_uuid,
|
2020-07-17 10:03:38 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:54:21 +02:00
|
|
|
export async function createSession(website_id, data) {
|
|
|
|
return runQuery(
|
2020-07-17 10:03:38 +02:00
|
|
|
prisma.session.create({
|
|
|
|
data: {
|
|
|
|
website: {
|
|
|
|
connect: {
|
2020-07-20 10:54:21 +02:00
|
|
|
website_id,
|
2020-07-17 10:03:38 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
...data,
|
|
|
|
},
|
2020-07-20 10:54:21 +02:00
|
|
|
select: {
|
|
|
|
session_id: true,
|
|
|
|
},
|
2020-07-17 10:03:38 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:54:21 +02:00
|
|
|
export async function getSession(session_uuid) {
|
2020-07-17 10:03:38 +02:00
|
|
|
return runQuery(
|
|
|
|
prisma.session.findOne({
|
|
|
|
where: {
|
2020-07-20 10:54:21 +02:00
|
|
|
session_uuid,
|
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: {
|
2020-07-20 10:54:21 +02:00
|
|
|
website_id,
|
2020-07-19 08:54:25 +02:00
|
|
|
},
|
|
|
|
},
|
2020-07-17 10:03:38 +02:00
|
|
|
session: {
|
|
|
|
connect: {
|
2020-07-20 10:54:21 +02:00
|
|
|
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: {
|
2020-07-20 10:54:21 +02:00
|
|
|
website_id,
|
2020-07-19 10:57:01 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
session: {
|
|
|
|
connect: {
|
2020-07-20 10:54:21 +02:00
|
|
|
session_id,
|
2020-07-19 10:57:01 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
url,
|
2020-07-19 11:23:15 +02:00
|
|
|
event_type,
|
|
|
|
event_value,
|
2020-07-19 10:57:01 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2020-07-23 05:45:09 +02:00
|
|
|
|
|
|
|
export async function getAccount(username = '') {
|
|
|
|
return runQuery(
|
|
|
|
prisma.account.findOne({
|
|
|
|
where: {
|
|
|
|
username,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2020-07-26 01:31:07 +02:00
|
|
|
|
2020-07-26 09:12:42 +02:00
|
|
|
export async function getPageviews(website_id, start_at, end_at) {
|
2020-07-26 01:31:07 +02:00
|
|
|
return runQuery(
|
|
|
|
prisma.pageview.findMany({
|
|
|
|
where: {
|
|
|
|
website_id,
|
2020-07-26 09:12:42 +02:00
|
|
|
created_at: {
|
|
|
|
gte: start_at,
|
|
|
|
lte: end_at,
|
|
|
|
},
|
2020-07-26 01:31:07 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|