Add sql param logic.

pull/1745/head
Brian Cao 2023-01-16 12:58:51 -08:00
parent cc46cfdcbf
commit d93c2f9dd7
9 changed files with 46 additions and 32 deletions

View File

@ -36,6 +36,18 @@ function logQuery(e) {
log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`)); log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`));
} }
function toUuid() {
const db = getDatabaseType(process.env.DATABASE_URL);
if (db === POSTGRESQL) {
return '::uuid';
}
if (db === MYSQL) {
return '';
}
}
function getClient(options) { function getClient(options) {
const prisma = new PrismaClient(options); const prisma = new PrismaClient(options);
@ -248,6 +260,7 @@ const prisma = global[PRISMA] || getClient(PRISMA_OPTIONS);
export default { export default {
client: prisma, client: prisma,
log, log,
toUuid,
getDateQuery, getDateQuery,
getTimestampInterval, getTimestampInterval,
getFilterQuery, getFilterQuery,

View File

@ -10,8 +10,8 @@ export async function getEventData(...args) {
} }
async function relationalQuery(websiteId, { startDate, endDate, event_name, columns, filters }) { async function relationalQuery(websiteId, { startDate, endDate, event_name, columns, filters }) {
const { rawQuery, getEventDataColumnsQuery, getEventDataFilterQuery } = prisma; const { rawQuery, getEventDataColumnsQuery, getEventDataFilterQuery, toUuid } = prisma;
const params = [startDate, endDate]; const params = [websiteId, startDate, endDate];
return rawQuery( return rawQuery(
`select `select
@ -21,8 +21,8 @@ async function relationalQuery(websiteId, { startDate, endDate, event_name, colu
on event.website_id = website.website_id on event.website_id = website.website_id
join event_data join event_data
on event.event_id = event_data.event_id on event.event_id = event_data.event_id
where website_uuid='${websiteId}' where website_uuid = $1${toUuid()}
and event.created_at between $1 and $2 and event.created_at between $2 and $3
${event_name ? `and event_name = ${event_name}` : ''} ${event_name ? `and event_name = ${event_name}` : ''}
${ ${
Object.keys(filters).length > 0 Object.keys(filters).length > 0

View File

@ -17,8 +17,8 @@ async function relationalQuery(
unit = 'day', unit = 'day',
filters = {}, filters = {},
) { ) {
const { rawQuery, getDateQuery, getFilterQuery } = prisma; const { rawQuery, getDateQuery, getFilterQuery, toUuid } = prisma;
const params = [start_at, end_at]; const params = [websiteId, start_at, end_at];
return rawQuery( return rawQuery(
`select `select
@ -28,8 +28,8 @@ async function relationalQuery(
from event from event
join website join website
on event.website_id = website.website_id on event.website_id = website.website_id
where website_uuid='${websiteId}' where website_uuid = $1${toUuid()}
and event.created_at between $1 and $2 and event.created_at between $2 and $3
${getFilterQuery('event', filters, params)} ${getFilterQuery('event', filters, params)}
group by 1, 2 group by 1, 2
order by 2`, order by 2`,

View File

@ -10,8 +10,8 @@ export async function getPageviewMetrics(...args) {
} }
async function relationalQuery(websiteId, { startDate, endDate, column, table, filters = {} }) { async function relationalQuery(websiteId, { startDate, endDate, column, table, filters = {} }) {
const { rawQuery, parseFilters } = prisma; const { rawQuery, parseFilters, toUuid } = prisma;
const params = [startDate, endDate]; const params = [websiteId, startDate, endDate];
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters( const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
table, table,
column, column,
@ -24,8 +24,8 @@ async function relationalQuery(websiteId, { startDate, endDate, column, table, f
from ${table} from ${table}
${` join website on ${table}.website_id = website.website_id`} ${` join website on ${table}.website_id = website.website_id`}
${joinSession} ${joinSession}
where website.website_uuid='${websiteId}' where website.website_uuid = $1${toUuid()}
and ${table}.created_at between $1 and $2 and ${table}.created_at between $2 and $3
${pageviewQuery} ${pageviewQuery}
${joinSession && sessionQuery} ${joinSession && sessionQuery}
${eventQuery} ${eventQuery}

View File

@ -9,8 +9,8 @@ export async function getPageviewParams(...args) {
} }
async function relationalQuery(websiteId, start_at, end_at, column, table, filters = {}) { async function relationalQuery(websiteId, start_at, end_at, column, table, filters = {}) {
const { parseFilters, rawQuery } = prisma; const { parseFilters, rawQuery, toUuid } = prisma;
const params = [start_at, end_at]; const params = [websiteId, start_at, end_at];
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters( const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
table, table,
column, column,
@ -24,8 +24,8 @@ async function relationalQuery(websiteId, start_at, end_at, column, table, filte
from ${table} from ${table}
${` join website on ${table}.website_id = website.website_id`} ${` join website on ${table}.website_id = website.website_id`}
${joinSession} ${joinSession}
where website.website_uuid='${websiteId}' where website.website_uuid = $1${toUuid()}
and ${table}.created_at between $1 and $2 and ${table}.created_at between $2 and $3
and ${table}.url like '%?%' and ${table}.url like '%?%'
${pageviewQuery} ${pageviewQuery}
${joinSession && sessionQuery} ${joinSession && sessionQuery}

View File

@ -21,8 +21,8 @@ async function relationalQuery(
sessionKey = 'session_id', sessionKey = 'session_id',
}, },
) { ) {
const { getDateQuery, parseFilters, rawQuery } = prisma; const { getDateQuery, parseFilters, rawQuery, toUuid } = prisma;
const params = [start_at, end_at]; const params = [websiteId, start_at, end_at];
const { pageviewQuery, sessionQuery, joinSession } = parseFilters( const { pageviewQuery, sessionQuery, joinSession } = parseFilters(
'pageview', 'pageview',
null, null,
@ -37,8 +37,8 @@ async function relationalQuery(
join website join website
on pageview.website_id = website.website_id on pageview.website_id = website.website_id
${joinSession} ${joinSession}
where website.website_uuid='${websiteId}' where website.website_uuid = $1${toUuid()}
and pageview.created_at between $1 and $2 and pageview.created_at between $2 and $3
${pageviewQuery} ${pageviewQuery}
${sessionQuery} ${sessionQuery}
group by 1`, group by 1`,

View File

@ -10,8 +10,8 @@ export async function getSessionMetrics(...args) {
} }
async function relationalQuery(websiteId, { startDate, endDate, field, filters = {} }) { async function relationalQuery(websiteId, { startDate, endDate, field, filters = {} }) {
const { parseFilters, rawQuery } = prisma; const { parseFilters, rawQuery, toUuid } = prisma;
const params = [startDate, endDate]; const params = [websiteId, startDate, endDate];
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(null, filters, params); const { pageviewQuery, sessionQuery, joinSession } = parseFilters(null, filters, params);
return rawQuery( return rawQuery(
@ -23,8 +23,8 @@ async function relationalQuery(websiteId, { startDate, endDate, field, filters =
join website join website
on pageview.website_id = website.website_id on pageview.website_id = website.website_id
${joinSession} ${joinSession}
where website.website_uuid='${websiteId}' where website.website_uuid = $1${toUuid()}
and pageview.created_at between $1 and $2 and pageview.created_at between $2 and $3
${pageviewQuery} ${pageviewQuery}
${sessionQuery} ${sessionQuery}
) )

View File

@ -11,16 +11,17 @@ export async function getActiveVisitors(...args) {
} }
async function relationalQuery(websiteId) { async function relationalQuery(websiteId) {
const { rawQuery, toUuid } = prisma;
const date = subMinutes(new Date(), 5); const date = subMinutes(new Date(), 5);
const params = [date]; const params = [websiteId, date];
return prisma.rawQuery( return rawQuery(
`select count(distinct session_id) x `select count(distinct session_id) x
from pageview from pageview
join website join website
on pageview.website_id = website.website_id on pageview.website_id = website.website_id
where website.website_uuid = '${websiteId}' where website.website_uuid = $1${toUuid()}
and pageview.created_at >= $1`, and pageview.created_at >= $2`,
params, params,
); );
} }

View File

@ -10,8 +10,8 @@ export async function getWebsiteStats(...args) {
} }
async function relationalQuery(websiteId, { start_at, end_at, filters = {} }) { async function relationalQuery(websiteId, { start_at, end_at, filters = {} }) {
const { getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma; const { getDateQuery, getTimestampInterval, parseFilters, rawQuery, toUuid } = prisma;
const params = [start_at, end_at]; const params = [websiteId, start_at, end_at];
const { pageviewQuery, sessionQuery, joinSession } = parseFilters( const { pageviewQuery, sessionQuery, joinSession } = parseFilters(
'pageview', 'pageview',
null, null,
@ -33,8 +33,8 @@ async function relationalQuery(websiteId, { start_at, end_at, filters = {} }) {
join website join website
on pageview.website_id = website.website_id on pageview.website_id = website.website_id
${joinSession} ${joinSession}
where website.website_uuid='${websiteId}' where website.website_uuid = $1${toUuid()}
and pageview.created_at between $1 and $2 and pageview.created_at between $2 and $3
${pageviewQuery} ${pageviewQuery}
${sessionQuery} ${sessionQuery}
group by 1, 2 group by 1, 2