added userid to shorteners

pull/3/head
TZGyn 2 years ago
parent c0ff2f0de8
commit 9e828d700f
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -0,0 +1 @@
ALTER TABLE "shortener" ADD COLUMN "user_id" integer NOT NULL;

@ -0,0 +1,181 @@
{
"id": "548a0489-b083-464f-800c-4dee6f1ff161",
"prevId": "9d44b46b-3e93-4782-8750-dbf39513bb0c",
"version": "5",
"dialect": "pg",
"tables": {
"session": {
"name": "session",
"schema": "",
"columns": {
"token": {
"name": "token",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"expires": {
"name": "expires",
"type": "timestamp",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"shortener": {
"name": "shortener",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"link": {
"name": "link",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"code": {
"name": "code",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"user_id": {
"name": "user_id",
"type": "integer",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"user": {
"name": "user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"uuid": {
"name": "uuid",
"type": "uuid",
"primaryKey": false,
"notNull": false,
"default": "gen_random_uuid()"
},
"email": {
"name": "email",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"username": {
"name": "username",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"password": {
"name": "password",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"user_email_unique": {
"name": "user_email_unique",
"nullsNotDistinct": false,
"columns": [
"email"
]
}
}
},
"visitor": {
"name": "visitor",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"shortener_id": {
"name": "shortener_id",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"country_code": {
"name": "country_code",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"country": {
"name": "country",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}

@ -1,13 +1,20 @@
{
"version": "5",
"dialect": "pg",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1699851315914,
"tag": "0000_nebulous_energizer",
"breakpoints": true
}
]
"version": "5",
"dialect": "pg",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1699851315914,
"tag": "0000_nebulous_energizer",
"breakpoints": true
},
{
"idx": 1,
"version": "5",
"when": 1700134783172,
"tag": "0001_regular_microchip",
"breakpoints": true
}
]
}

2
src/app.d.ts vendored

@ -4,7 +4,7 @@ declare global {
namespace App {
// interface Error {}
interface Locals {
user: number | string | null
user: number | null
}
// interface PageData {}
// interface Platform {}

@ -14,8 +14,16 @@ export const shortener = pgTable('shortener', {
link: varchar('link', { length: 255 }).notNull(),
code: varchar('code', { length: 255 }).notNull(),
createdAt: timestamp('created_at', { mode: 'string' }).defaultNow().notNull(),
userId: integer('user_id').notNull(),
})
export const shortenerRelations = relations(shortener, ({ one }) => ({
user: one(user, {
fields: [shortener.userId],
references: [user.id],
}),
}))
export const user = pgTable('user', {
id: serial('id').primaryKey().notNull(),
uuid: uuid('uuid').defaultRandom(),

@ -1,5 +1,20 @@
import { db } from '$lib/db'
import { shortener } from '$lib/db/schema'
import { eq } from 'drizzle-orm'
import type { PageServerLoad } from './$types'
import { redirect } from '@sveltejs/kit'
export const load = (async () => {
return {}
export const load = (async (event) => {
if (!event.locals.user) {
throw redirect(303, '/')
}
const userId = event.locals.user
const shorteners = await db
.select()
.from(shortener)
.where(eq(shortener.userId, userId))
return { shorteners }
}) satisfies PageServerLoad

@ -3,7 +3,16 @@
import { Separator } from '$lib/components/ui/separator'
export let data: PageData
console.log('data', data)
</script>
<div class="p-8 text-4xl font-bold">Links</div>
<Separator />
{#if data.shorteners.length > 0}
{#each data.shorteners as shortener}
{JSON.stringify(shortener)}
{/each}
{:else}
<div>No Data</div>
{/if}

Loading…
Cancel
Save