feat: create route to store FCM token
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { createClient } from "@libsql/client";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { drizzle } from "drizzle-orm/libsql";
|
||||
|
||||
import type { Env } from "~/types/env";
|
||||
|
||||
@@ -6,11 +6,11 @@ import {
|
||||
text,
|
||||
} from "drizzle-orm/sqlite-core";
|
||||
|
||||
export const tokenTable = sqliteTable("token", {
|
||||
export const deviceTokensTable = sqliteTable("device_tokens", {
|
||||
deviceId: text("device_id").primaryKey(),
|
||||
token: text("token").notNull(),
|
||||
token: text("token").notNull().unique(),
|
||||
username: text("username"),
|
||||
/** Used to determine if a device hasn't been used in a while. Should start to ignore tokens where the device hasn't connected in about a month */
|
||||
/** Used to determine if a device hasn't been used in a while. Should start to ignore tokens where the device hasn't connected in about a month. */
|
||||
lastConnectedAt: text("last_connected_at").default(sql`(CURRENT_TIMESTAMP)`),
|
||||
});
|
||||
|
||||
@@ -19,7 +19,7 @@ export const watchStatusTable = sqliteTable(
|
||||
{
|
||||
deviceId: text("device_id")
|
||||
.notNull()
|
||||
.references(() => tokenTable.deviceId),
|
||||
.references(() => deviceTokensTable.deviceId),
|
||||
titleId: integer("title_id").notNull(),
|
||||
},
|
||||
(table) => ({
|
||||
@@ -27,4 +27,4 @@ export const watchStatusTable = sqliteTable(
|
||||
}),
|
||||
);
|
||||
|
||||
export const tables = [watchStatusTable, tokenTable];
|
||||
export const tables = [watchStatusTable, deviceTokensTable];
|
||||
|
||||
@@ -3,7 +3,7 @@ import { eq, sql } from "drizzle-orm";
|
||||
import type { Env } from "~/types/env";
|
||||
|
||||
import { getDb } from "./db";
|
||||
import { tokenTable } from "./schema";
|
||||
import { deviceTokensTable } from "./schema";
|
||||
|
||||
export function saveToken(
|
||||
env: Env,
|
||||
@@ -12,15 +12,15 @@ export function saveToken(
|
||||
username: string | null,
|
||||
) {
|
||||
return getDb(env)
|
||||
.insert(tokenTable)
|
||||
.insert(deviceTokensTable)
|
||||
.values({ deviceId, token, username })
|
||||
.run();
|
||||
}
|
||||
|
||||
export function updateDeviceLastConnectedAt(env: Env, deviceId: string) {
|
||||
return getDb(env)
|
||||
.update(tokenTable)
|
||||
.update(deviceTokensTable)
|
||||
.set({ lastConnectedAt: sql`(CURRENT_TIMESTAMP)` })
|
||||
.where(eq(tokenTable.deviceId, deviceId))
|
||||
.where(eq(deviceTokensTable.deviceId, deviceId))
|
||||
.run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user