Refactored queries.

This commit is contained in:
Mike Cao 2023-07-29 22:03:34 -07:00
parent f36a689817
commit 8904b7b4ed
27 changed files with 137 additions and 144 deletions

View file

@ -13,19 +13,29 @@ export async function getReportById(reportId: string): Promise<Report> {
});
}
export async function getReports(where: Prisma.ReportWhereInput): Promise<Report[]> {
export async function getUserReports(userId: string): Promise<Report[]> {
return prisma.client.report.findMany({
where,
where: {
userId,
},
});
}
export async function getWebsiteReports(websiteId: string): Promise<Report[]> {
return prisma.client.report.findMany({
where: {
websiteId,
},
});
}
export async function updateReport(
reportId: string,
data: Prisma.ReportUpdateInput,
where: Prisma.ReportWhereUniqueInput,
): Promise<Report> {
return prisma.client.report.update({ data, where });
return prisma.client.report.update({ where: { id: reportId }, data });
}
export async function deleteReport(where: Prisma.ReportWhereUniqueInput): Promise<Report> {
return prisma.client.report.delete({ where });
export async function deleteReport(reportId: string): Promise<Report> {
return prisma.client.report.delete({ where: { id: reportId } });
}

View file

@ -3,15 +3,29 @@ import prisma from 'lib/prisma';
import { ROLES } from 'lib/constants';
import { uuid } from 'lib/crypto';
export async function getTeam(where: Prisma.TeamWhereInput): Promise<Team> {
export interface GetTeamOptions {
includeTeamUser?: boolean;
}
async function getTeam(where: Prisma.TeamWhereInput, options: GetTeamOptions = {}): Promise<Team> {
const { includeTeamUser = false } = options;
return prisma.client.team.findFirst({
where,
include: {
teamUser: true,
teamUser: includeTeamUser,
},
});
}
export function getTeamById(teamId: string, options: GetTeamOptions = {}) {
return getTeam({ id: teamId }, options);
}
export function getTeamByAccessCode(accessCode: string, options: GetTeamOptions = {}) {
return getTeam({ accessCode }, options);
}
export async function getTeams(where: Prisma.TeamWhereInput): Promise<Team[]> {
return prisma.client.team.findMany({
where,
@ -36,16 +50,15 @@ export async function createTeam(data: Prisma.TeamCreateInput, userId: string):
]);
}
export async function updateTeam(
data: Prisma.TeamUpdateInput,
where: Prisma.TeamWhereUniqueInput,
): Promise<Team> {
export async function updateTeam(teamId: string, data: Prisma.TeamUpdateInput): Promise<Team> {
return prisma.client.team.update({
where: {
id: teamId,
},
data: {
...data,
updatedAt: new Date(),
},
where,
});
}

View file

@ -53,12 +53,14 @@ export async function createTeamUser(
}
export async function updateTeamUser(
teamUserId: string,
data: Prisma.TeamUserUpdateInput,
where: Prisma.TeamUserWhereUniqueInput,
): Promise<TeamUser> {
return prisma.client.teamUser.update({
where: {
id: teamUserId,
},
data,
where,
});
}

View file

@ -22,7 +22,7 @@ export async function getTeamWebsite(
});
}
export async function getTeamWebsiteByTeamMemberId(
export async function findTeamWebsiteByUserId(
websiteId: string,
userId: string,
): Promise<TeamWebsite> {

View file

@ -5,9 +5,14 @@ import { ROLES } from 'lib/constants';
import prisma from 'lib/prisma';
import { Website, User, Role } from 'lib/types';
export async function getUser(
export interface GetUserOptions {
includePassword?: boolean;
showDeleted?: boolean;
}
async function getUser(
where: Prisma.UserWhereInput | Prisma.UserWhereUniqueInput,
options: { includePassword?: boolean; showDeleted?: boolean } = {},
options: GetUserOptions = {},
): Promise<User> {
const { includePassword = false, showDeleted = false } = options;
@ -23,6 +28,14 @@ export async function getUser(
});
}
export async function getUserById(userId: string, options: GetUserOptions = {}) {
return getUser({ id: userId }, options);
}
export async function getUserByUsername(username: string, options: GetUserOptions = {}) {
return getUser({ username }, options);
}
export async function getUsers(): Promise<User[]> {
return prisma.client.user.findMany({
take: 100,

View file

@ -2,12 +2,20 @@ import { Prisma, Website } from '@prisma/client';
import cache from 'lib/cache';
import prisma from 'lib/prisma';
export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
return prisma.client.website.findUnique({
where,
});
}
export async function getWebsiteById(id: string) {
return getWebsite({ id });
}
export async function getWebsiteByShareId(shareId: string) {
return getWebsite({ shareId });
}
export async function getWebsites(): Promise<Website[]> {
return prisma.client.website.findMany({
orderBy: {

View file

@ -2,7 +2,7 @@ import { Prisma } from '@prisma/client';
import { DATA_TYPE } from 'lib/constants';
import { uuid } from 'lib/crypto';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import { flattenJSON } from 'lib/dynamicData';
import { flattenJSON } from 'lib/data';
import kafka from 'lib/kafka';
import prisma from 'lib/prisma';
import { DynamicData } from 'lib/types';

View file

@ -2,24 +2,23 @@ import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import { WebsiteEventMetric } from 'lib/types';
import { DEFAULT_RESET_DATE, EVENT_TYPE } from 'lib/constants';
import { EVENT_TYPE } from 'lib/constants';
import { loadWebsite } from 'lib/load';
import { maxDate } from 'lib/date';
export interface GetEventMetricsCriteria {
startDate: Date;
endDate: Date;
timezone: string;
unit: string;
filters: {
url: string;
eventName: string;
};
}
export async function getEventMetrics(
...args: [
websiteId: string,
data: {
startDate: Date;
endDate: Date;
timezone: string;
unit: string;
filters: {
url: string;
eventName: string;
};
},
]
...args: [websiteId: string, criteria: GetEventMetricsCriteria]
): Promise<WebsiteEventMetric[]> {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
@ -27,25 +26,8 @@ export async function getEventMetrics(
});
}
async function relationalQuery(
websiteId: string,
{
startDate,
endDate,
timezone = 'utc',
unit = 'day',
filters,
}: {
startDate: Date;
endDate: Date;
timezone: string;
unit: string;
filters: {
url: string;
eventName: string;
};
},
) {
async function relationalQuery(websiteId: string, criteria: GetEventMetricsCriteria) {
const { startDate, endDate, timezone = 'utc', unit = 'day', filters } = criteria;
const { rawQuery, getDateQuery, getFilterQuery } = prisma;
const website = await loadWebsite(websiteId);
const filterQuery = getFilterQuery(filters);
@ -74,25 +56,8 @@ async function relationalQuery(
);
}
async function clickhouseQuery(
websiteId: string,
{
startDate,
endDate,
timezone = 'utc',
unit = 'day',
filters,
}: {
startDate: Date;
endDate: Date;
timezone: string;
unit: string;
filters: {
url: string;
eventName: string;
};
},
) {
async function clickhouseQuery(websiteId: string, criteria: GetEventMetricsCriteria) {
const { startDate, endDate, timezone = 'utc', unit = 'day', filters } = criteria;
const { rawQuery, getDateQuery, getFilterQuery } = clickhouse;
const website = await loadWebsite(websiteId);
const filterQuery = getFilterQuery(filters);

View file

@ -1,8 +1,9 @@
import { Prisma } from '@prisma/client';
import prisma from 'lib/prisma';
export async function getSession(where: Prisma.SessionWhereUniqueInput) {
export async function getSession(id: string) {
return prisma.client.session.findUnique({
where,
where: {
id,
},
});
}

View file

@ -1,6 +1,6 @@
import { DATA_TYPE } from 'lib/constants';
import { uuid } from 'lib/crypto';
import { flattenJSON } from 'lib/dynamicData';
import { flattenJSON } from 'lib/data';
import prisma from 'lib/prisma';
import { DynamicData } from 'lib/types';

View file

@ -1,7 +1,8 @@
export * from './admin/report';
export * from './admin/team';
export * from './admin/teamUser';
export * from './admin/teamWebsite';
export * from './admin/user';
export * from './admin/report';
export * from './admin/website';
export * from './analytics/events/getEventMetrics';
export * from './analytics/events/getEventUsage';