mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
Database refactoring.
This commit is contained in:
parent
bb184dc2cc
commit
467c7f289f
37 changed files with 566 additions and 591 deletions
|
|
@ -1,9 +1,7 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createAccount(data) {
|
||||
return runQuery(
|
||||
prisma.account.create({
|
||||
data,
|
||||
}),
|
||||
);
|
||||
return prisma.client.account.create({
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +1,28 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function deleteAccount(user_id) {
|
||||
return runQuery(
|
||||
prisma.$transaction([
|
||||
prisma.pageview.deleteMany({
|
||||
where: { session: { website: { user_id } } },
|
||||
}),
|
||||
prisma.event_data.deleteMany({
|
||||
where: { event: { session: { website: { user_id } } } },
|
||||
}),
|
||||
prisma.event.deleteMany({
|
||||
where: { session: { website: { user_id } } },
|
||||
}),
|
||||
prisma.session.deleteMany({
|
||||
where: { website: { user_id } },
|
||||
}),
|
||||
prisma.website.deleteMany({
|
||||
where: { user_id },
|
||||
}),
|
||||
prisma.account.delete({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
}),
|
||||
]),
|
||||
);
|
||||
const { client } = prisma;
|
||||
|
||||
return client.$transaction([
|
||||
client.pageview.deleteMany({
|
||||
where: { session: { website: { user_id } } },
|
||||
}),
|
||||
client.event_data.deleteMany({
|
||||
where: { event: { session: { website: { user_id } } } },
|
||||
}),
|
||||
client.event.deleteMany({
|
||||
where: { session: { website: { user_id } } },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { website: { user_id } },
|
||||
}),
|
||||
client.website.deleteMany({
|
||||
where: { user_id },
|
||||
}),
|
||||
client.account.delete({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getAccountById(user_id) {
|
||||
return runQuery(
|
||||
prisma.account.findUnique({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.account.findUnique({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getAccountByUsername(username) {
|
||||
return runQuery(
|
||||
prisma.account.findUnique({
|
||||
where: {
|
||||
username,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.account.findUnique({
|
||||
where: {
|
||||
username,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,19 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getAccounts() {
|
||||
return runQuery(
|
||||
prisma.account.findMany({
|
||||
orderBy: [
|
||||
{ is_admin: 'desc' },
|
||||
{
|
||||
username: 'asc',
|
||||
},
|
||||
],
|
||||
select: {
|
||||
user_id: true,
|
||||
username: true,
|
||||
is_admin: true,
|
||||
created_at: true,
|
||||
updated_at: true,
|
||||
return prisma.client.account.findMany({
|
||||
orderBy: [
|
||||
{ is_admin: 'desc' },
|
||||
{
|
||||
username: 'asc',
|
||||
},
|
||||
}),
|
||||
);
|
||||
],
|
||||
select: {
|
||||
user_id: true,
|
||||
username: true,
|
||||
is_admin: true,
|
||||
created_at: true,
|
||||
updated_at: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function updateAccount(user_id, data) {
|
||||
return runQuery(
|
||||
prisma.account.update({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
data,
|
||||
}),
|
||||
);
|
||||
return prisma.client.account.update({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
|
||||
export async function createWebsite(user_id, data) {
|
||||
return runQuery(
|
||||
prisma.website.create({
|
||||
return prisma.client.website
|
||||
.create({
|
||||
data: {
|
||||
account: {
|
||||
connect: {
|
||||
|
|
@ -12,12 +12,12 @@ export async function createWebsite(user_id, data) {
|
|||
},
|
||||
...data,
|
||||
},
|
||||
}),
|
||||
).then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.set(`website:${res.website_uuid}`, Number(res.website_id));
|
||||
}
|
||||
})
|
||||
.then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.set(`website:${res.website_uuid}`, Number(res.website_id));
|
||||
}
|
||||
|
||||
return res;
|
||||
});
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,28 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
|
||||
export async function deleteWebsite(website_id) {
|
||||
return runQuery(
|
||||
prisma
|
||||
.$transaction([
|
||||
prisma.pageview.deleteMany({
|
||||
where: { session: { website: { website_id } } },
|
||||
}),
|
||||
prisma.event_data.deleteMany({
|
||||
where: { event: { session: { website: { website_id } } } },
|
||||
}),
|
||||
prisma.event.deleteMany({
|
||||
where: { session: { website: { website_id } } },
|
||||
}),
|
||||
prisma.session.deleteMany({
|
||||
where: { website: { website_id } },
|
||||
}),
|
||||
prisma.website.delete({
|
||||
where: { website_id },
|
||||
}),
|
||||
])
|
||||
.then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.del(`website:${res.website_uuid}`);
|
||||
}
|
||||
}),
|
||||
);
|
||||
const { client, multiQuery } = prisma;
|
||||
|
||||
return multiQuery([
|
||||
client.pageview.deleteMany({
|
||||
where: { session: { website: { website_id } } },
|
||||
}),
|
||||
client.event_data.deleteMany({
|
||||
where: { event: { session: { website: { website_id } } } },
|
||||
}),
|
||||
client.event.deleteMany({
|
||||
where: { session: { website: { website_id } } },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { website: { website_id } },
|
||||
}),
|
||||
client.website.delete({
|
||||
where: { website_id },
|
||||
}),
|
||||
]).then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.del(`website:${res.website_uuid}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,23 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getAllWebsites() {
|
||||
let data = await runQuery(
|
||||
prisma.website.findMany({
|
||||
orderBy: [
|
||||
{
|
||||
user_id: 'asc',
|
||||
},
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
include: {
|
||||
account: {
|
||||
select: {
|
||||
username: true,
|
||||
},
|
||||
let data = await prisma.client.website.findMany({
|
||||
orderBy: [
|
||||
{
|
||||
user_id: 'asc',
|
||||
},
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
include: {
|
||||
account: {
|
||||
select: {
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
return data.map(i => ({ ...i, account: i.account.username }));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getUserWebsites(user_id) {
|
||||
return runQuery(
|
||||
prisma.website.findMany({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
user_id,
|
||||
},
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getWebsiteById(website_id) {
|
||||
return runQuery(
|
||||
prisma.website.findUnique({
|
||||
where: {
|
||||
website_id,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.website.findUnique({
|
||||
where: {
|
||||
website_id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getWebsiteByShareId(share_id) {
|
||||
return runQuery(
|
||||
prisma.website.findUnique({
|
||||
where: {
|
||||
share_id,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.website.findUnique({
|
||||
where: {
|
||||
share_id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getWebsiteByUuid(website_uuid) {
|
||||
return runQuery(
|
||||
prisma.website.findUnique({
|
||||
where: {
|
||||
website_uuid,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return prisma.client.website.findUnique({
|
||||
where: {
|
||||
website_uuid,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,25 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
|
||||
export async function resetWebsite(website_id) {
|
||||
return runQuery(prisma.$queryRaw`delete from session where website_id=${website_id}`);
|
||||
const { client, multiQuery } = prisma;
|
||||
|
||||
return multiQuery([
|
||||
client.pageview.deleteMany({
|
||||
where: { session: { website: { website_id } } },
|
||||
}),
|
||||
client.event_data.deleteMany({
|
||||
where: { event: { session: { website: { website_id } } } },
|
||||
}),
|
||||
client.event.deleteMany({
|
||||
where: { session: { website: { website_id } } },
|
||||
}),
|
||||
client.session.deleteMany({
|
||||
where: { website: { website_id } },
|
||||
}),
|
||||
]).then(async res => {
|
||||
if (process.env.REDIS_URL) {
|
||||
await redis.del(`website:${res.website_uuid}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
import { prisma, runQuery } from 'lib/relational';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function updateWebsite(website_id, data) {
|
||||
return runQuery(
|
||||
prisma.website.update({
|
||||
where: {
|
||||
website_id,
|
||||
},
|
||||
data,
|
||||
}),
|
||||
);
|
||||
return prisma.client.website.update({
|
||||
where: {
|
||||
website_id,
|
||||
},
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue