49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
import {
|
|
Generated,
|
|
Insertable,
|
|
JSONColumnType,
|
|
Selectable,
|
|
Updateable,
|
|
} from "kysely"
|
|
import { db } from "./database"
|
|
|
|
export async function createSubscription(
|
|
subscription: PushSubscriptionJSON
|
|
): Promise<Subscription> {
|
|
const inserted = await db
|
|
.insertInto("subscription")
|
|
.values({ subscription: JSON.stringify(subscription) })
|
|
.returningAll()
|
|
.executeTakeFirst()
|
|
|
|
return inserted!
|
|
}
|
|
|
|
export async function updateSubscription(
|
|
subscription: PushSubscriptionJSON,
|
|
id: number
|
|
): Promise<void> {
|
|
await db
|
|
.updateTable("subscription")
|
|
.set({ subscription: JSON.stringify(subscription) })
|
|
.where("id", "=", id)
|
|
.executeTakeFirst()
|
|
}
|
|
|
|
export async function getSubscription(id: number) {
|
|
return await db
|
|
.selectFrom("subscription")
|
|
.selectAll()
|
|
.where("id", "=", id)
|
|
.executeTakeFirst()
|
|
}
|
|
|
|
export interface SubscriptionTable {
|
|
id: Generated<number>
|
|
subscription: JSONColumnType<PushSubscriptionJSON>
|
|
}
|
|
|
|
export type Subscription = Selectable<SubscriptionTable>
|
|
export type NewSubscription = Insertable<SubscriptionTable>
|
|
export type SubscriptionUpdate = Updateable<SubscriptionTable>
|