55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import { sql } from "drizzle-orm";
|
|
import {
|
|
integer,
|
|
primaryKey,
|
|
sqliteTable,
|
|
text,
|
|
} from "drizzle-orm/sqlite-core";
|
|
|
|
export const deviceTokensTable = sqliteTable("device_tokens", {
|
|
deviceId: text("device_id").primaryKey(),
|
|
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. */
|
|
lastConnectedAt: text("last_connected_at")
|
|
.default(sql`(CURRENT_TIMESTAMP)`)
|
|
.$onUpdate(() => sql`(CURRENT_TIMESTAMP)`),
|
|
});
|
|
|
|
export const watchStatusTable = sqliteTable(
|
|
"watch_status",
|
|
{
|
|
deviceId: text("device_id")
|
|
.notNull()
|
|
.references(() => deviceTokensTable.deviceId),
|
|
titleId: integer("title_id").notNull(),
|
|
},
|
|
(table) => ({
|
|
pk: primaryKey({ columns: [table.deviceId, table.titleId] }),
|
|
}),
|
|
);
|
|
|
|
export const keyValueTable = sqliteTable("key_value", {
|
|
key: text("key", { enum: ["schedule_last_checked_at"] }).primaryKey(),
|
|
value: text("value").notNull(),
|
|
});
|
|
|
|
export const titleMessagesTable = sqliteTable("title_messages", {
|
|
titleId: integer("title_id").notNull().primaryKey(),
|
|
messageId: text("message_id").notNull(),
|
|
});
|
|
|
|
/** Used to keep track of titles that haven't been released yet and the time when the first episode will be released is unknown */
|
|
export const unreleasedTitlesTable = sqliteTable("unreleased_titles", {
|
|
titleId: integer("title_id").notNull().primaryKey(),
|
|
});
|
|
|
|
export const tables = [
|
|
// must be first to avoid foreign key constraint errors
|
|
watchStatusTable,
|
|
deviceTokensTable,
|
|
keyValueTable,
|
|
titleMessagesTable,
|
|
unreleasedTitlesTable,
|
|
];
|