added visitor relations

pull/3/head
TZGyn 2 years ago
parent 5d6c8a1f2b
commit ec37dd3eef
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -19,12 +19,16 @@ export const shortener = pgTable('shortener', {
userId: integer('user_id').notNull(), userId: integer('user_id').notNull(),
}) })
export const shortenerRelations = relations(shortener, ({ one }) => ({ export const shortenerRelations = relations(
shortener,
({ one, many }) => ({
user: one(user, { user: one(user, {
fields: [shortener.userId], fields: [shortener.userId],
references: [user.id], references: [user.id],
}), }),
})) visitor: many(visitor),
}),
)
export const user = pgTable('user', { export const user = pgTable('user', {
id: serial('id').primaryKey().notNull(), id: serial('id').primaryKey().notNull(),
@ -49,6 +53,13 @@ export const visitor = pgTable('visitor', {
country: varchar('country', { length: 255 }).notNull(), country: varchar('country', { length: 255 }).notNull(),
}) })
export const visitorRelations = relations(visitor, ({ one }) => ({
shortener: one(shortener, {
fields: [visitor.shortenerId],
references: [shortener.id],
}),
}))
export const session = pgTable('session', { export const session = pgTable('session', {
token: varchar('token', { length: 255 }).notNull(), token: varchar('token', { length: 255 }).notNull(),
userId: integer('user_id').notNull(), userId: integer('user_id').notNull(),

@ -1,6 +1,4 @@
import { db } from '$lib/db' import { db } from '$lib/db'
import { shortener } from '$lib/db/schema'
import { eq } from 'drizzle-orm'
import type { PageServerLoad } from './$types' import type { PageServerLoad } from './$types'
import { redirect } from '@sveltejs/kit' import { redirect } from '@sveltejs/kit'
@ -11,10 +9,12 @@ export const load = (async (event) => {
const userId = event.locals.user const userId = event.locals.user
const shorteners = await db const shorteners = await db.query.shortener.findMany({
.select() with: {
.from(shortener) visitor: true,
.where(eq(shortener.userId, userId)) },
where: (shortener, { eq }) => eq(shortener.userId, userId),
})
return { shorteners } return { shorteners }
}) satisfies PageServerLoad }) satisfies PageServerLoad

@ -8,6 +8,7 @@
import { Input } from '$lib/components/ui/input' import { Input } from '$lib/components/ui/input'
import { Label } from '$lib/components/ui/label' import { Label } from '$lib/components/ui/label'
import { import {
BarChart,
Link2, Link2,
Loader2, Loader2,
MoreVertical, MoreVertical,
@ -86,7 +87,7 @@
{#if data.shorteners.length > 0} {#if data.shorteners.length > 0}
<div class="flex flex-col gap-4 p-4"> <div class="flex flex-col gap-4 p-4">
{#each data.shorteners as shortener} {#each data.shorteners as shortener}
<Card.Root> <Card.Root class="w-full max-w-[500px]">
<Card.Header> <Card.Header>
<Card.Title class="flex items-center gap-2"> <Card.Title class="flex items-center gap-2">
<a <a
@ -103,8 +104,16 @@
<Card.Description>{shortener.link}</Card.Description> <Card.Description>{shortener.link}</Card.Description>
</Card.Header> </Card.Header>
<Card.Content> <Card.Content>
<div class="flex justify-between"> <div class="flex items-center justify-between">
<div></div> <div class="flex gap-2">
<div
class="flex items-center justify-center gap-1 rounded bg-secondary px-3 py-1 text-sm">
<BarChart size={20} />
<div>
{shortener.visitor.length} visits
</div>
</div>
</div>
<DropdownMenu.Root> <DropdownMenu.Root>
<DropdownMenu.Trigger> <DropdownMenu.Trigger>
<MoreVertical /> <MoreVertical />

Loading…
Cancel
Save