Openstatus
www.openstatus.dev
1import { relations, sql } from "drizzle-orm";
2import { index, integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
3
4import { user } from "../users";
5import { workspace } from "../workspaces";
6
7export const apiKey = sqliteTable(
8 "api_key",
9 {
10 id: integer("id").primaryKey({ autoIncrement: true }),
11 name: text("name").notNull(),
12 description: text("description"),
13 prefix: text("prefix").notNull().unique(),
14 hashedToken: text("hashed_token").notNull().unique(),
15 workspaceId: integer("workspace_id")
16 .notNull()
17 .references(() => workspace.id, { onDelete: "cascade" }),
18 createdById: integer("created_by_id")
19 .notNull()
20 .references(() => user.id, { onDelete: "cascade" }),
21 createdAt: integer("created_at", { mode: "timestamp" }).default(
22 sql`(strftime('%s', 'now'))`,
23 ),
24 expiresAt: integer("expires_at", { mode: "timestamp" }),
25 lastUsedAt: integer("last_used_at", { mode: "timestamp" }),
26 },
27 (table) => ({
28 prefixIdx: index("api_key_prefix_idx").on(table.prefix),
29 }),
30);
31
32export const apiKeyRelations = relations(apiKey, ({ one }) => ({
33 workspace: one(workspace, {
34 fields: [apiKey.workspaceId],
35 references: [workspace.id],
36 }),
37 createdBy: one(user, {
38 fields: [apiKey.createdById],
39 references: [user.id],
40 }),
41}));