fosrl.pangolin/server/db/schema.ts

237 lines
8.3 KiB
TypeScript
Raw Normal View History

2024-09-27 21:39:03 -04:00
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";
2024-10-14 21:58:34 -04:00
import { InferSelectModel} from "drizzle-orm";
2024-09-28 11:51:21 -04:00
2024-09-28 13:31:22 -04:00
export const orgs = sqliteTable("orgs", {
2024-10-14 15:11:18 -04:00
orgId: text("orgId").primaryKey(),
2024-09-28 15:21:13 -04:00
name: text("name").notNull(),
domain: text("domain").notNull(),
2024-09-28 11:51:21 -04:00
});
export const sites = sqliteTable("sites", {
2024-09-28 15:21:13 -04:00
siteId: integer("siteId").primaryKey({ autoIncrement: true }),
2024-10-14 15:11:18 -04:00
orgId: text("orgId").references(() => orgs.orgId, {
2024-10-01 20:48:03 -04:00
onDelete: "cascade",
}),
2024-10-14 23:50:58 -04:00
niceId: text("niceId").notNull(),
2024-10-01 20:48:03 -04:00
exitNode: integer("exitNode").references(() => exitNodes.exitNodeId, {
onDelete: "set null",
}),
2024-09-28 15:21:13 -04:00
name: text("name").notNull(),
pubKey: text("pubKey"),
subnet: text("subnet"),
megabytesIn: integer("bytesIn"),
2024-10-01 20:48:03 -04:00
megabytesOut: integer("bytesOut"),
2024-09-28 11:51:21 -04:00
});
export const resources = sqliteTable("resources", {
resourceId: integer("resourceId").primaryKey({ autoIncrement: true }),
fullDomain: text("fullDomain", { length: 2048 }),
2024-10-01 20:48:03 -04:00
siteId: integer("siteId").references(() => sites.siteId, {
onDelete: "cascade",
}),
2024-10-14 15:11:18 -04:00
orgId: text("orgId").references(() => orgs.orgId, {
2024-10-02 22:05:21 -04:00
onDelete: "cascade",
}),
2024-09-28 15:21:13 -04:00
name: text("name").notNull(),
subdomain: text("subdomain"),
2024-09-28 11:51:21 -04:00
});
2024-09-28 17:10:03 -04:00
export const targets = sqliteTable("targets", {
targetId: integer("targetId").primaryKey({ autoIncrement: true }),
2024-10-26 12:15:03 -04:00
resourceId: integer("resourceId").references(() => resources.resourceId, {
2024-10-01 20:48:03 -04:00
onDelete: "cascade",
}),
2024-09-28 17:10:03 -04:00
ip: text("ip").notNull(),
2024-09-28 22:50:10 -04:00
method: text("method").notNull(),
port: integer("port").notNull(),
2024-09-28 17:10:03 -04:00
protocol: text("protocol"),
enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),
ssl: integer("ssl", { mode: "boolean" }).notNull().default(false),
2024-09-28 17:10:03 -04:00
});
2024-09-28 11:51:21 -04:00
export const exitNodes = sqliteTable("exitNodes", {
2024-09-28 15:21:13 -04:00
exitNodeId: integer("exitNodeId").primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
address: text("address").notNull(),
2024-10-26 12:02:21 -04:00
publicKey: text("pubicKey").notNull(),
2024-09-28 15:21:13 -04:00
listenPort: integer("listenPort"),
2024-09-28 11:51:21 -04:00
});
2024-09-27 21:39:03 -04:00
2024-09-28 11:51:21 -04:00
export const routes = sqliteTable("routes", {
2024-09-28 15:21:13 -04:00
routeId: integer("routeId").primaryKey({ autoIncrement: true }),
2024-10-01 20:48:03 -04:00
exitNodeId: integer("exitNodeId").references(() => exitNodes.exitNodeId, {
onDelete: "cascade",
}),
2024-09-28 15:21:13 -04:00
subnet: text("subnet").notNull(),
2024-09-28 13:31:22 -04:00
});
2024-10-01 20:48:03 -04:00
export const users = sqliteTable("user", {
2024-10-13 17:13:47 -04:00
userId: text("id").primaryKey(),
2024-10-01 20:48:03 -04:00
email: text("email").notNull().unique(),
passwordHash: text("passwordHash").notNull(),
2024-10-02 20:19:48 -04:00
twoFactorEnabled: integer("twoFactorEnabled", { mode: "boolean" })
.notNull()
.default(false),
twoFactorSecret: text("twoFactorSecret"),
2024-10-04 23:14:40 -04:00
emailVerified: integer("emailVerified", { mode: "boolean" })
.notNull()
.default(false),
2024-10-07 23:31:23 -04:00
dateCreated: text("dateCreated").notNull(),
2024-10-01 20:48:03 -04:00
});
2024-10-05 15:31:28 -04:00
export const twoFactorBackupCodes = sqliteTable("twoFactorBackupCodes", {
2024-10-13 17:13:47 -04:00
codeId: integer("id").primaryKey({ autoIncrement: true }),
2024-10-05 15:31:28 -04:00
userId: text("userId")
.notNull()
2024-10-13 17:13:47 -04:00
.references(() => users.userId, { onDelete: "cascade" }),
2024-10-05 15:31:28 -04:00
codeHash: text("codeHash").notNull(),
});
2024-10-01 20:48:03 -04:00
export const sessions = sqliteTable("session", {
2024-10-13 17:13:47 -04:00
sessionId: text("id").primaryKey(),
2024-10-01 20:48:03 -04:00
userId: text("userId")
.notNull()
2024-10-13 17:13:47 -04:00
.references(() => users.userId, { onDelete: "cascade" }),
2024-10-01 20:48:03 -04:00
expiresAt: integer("expiresAt").notNull(),
});
2024-10-03 22:31:20 -04:00
export const userOrgs = sqliteTable("userOrgs", {
userId: text("userId")
.notNull()
2024-10-13 17:13:47 -04:00
.references(() => users.userId),
2024-10-14 15:11:18 -04:00
orgId: text("orgId")
2024-10-03 22:31:20 -04:00
.notNull()
.references(() => orgs.orgId),
2024-10-07 23:31:23 -04:00
roleId: integer("roleId")
.notNull()
.references(() => roles.roleId),
2024-10-03 22:31:20 -04:00
});
2024-10-04 23:14:40 -04:00
export const emailVerificationCodes = sqliteTable("emailVerificationCodes", {
2024-10-13 17:13:47 -04:00
codeId: integer("id").primaryKey({ autoIncrement: true }),
2024-10-04 23:14:40 -04:00
userId: text("userId")
.notNull()
2024-10-13 17:13:47 -04:00
.references(() => users.userId, { onDelete: "cascade" }),
2024-10-04 23:14:40 -04:00
email: text("email").notNull(),
code: text("code").notNull(),
expiresAt: integer("expiresAt").notNull(),
});
2024-10-05 17:01:49 -04:00
export const passwordResetTokens = sqliteTable("passwordResetTokens", {
2024-10-13 17:13:47 -04:00
tokenId: integer("id").primaryKey({ autoIncrement: true }),
2024-10-05 17:01:49 -04:00
userId: text("userId")
.notNull()
2024-10-13 17:13:47 -04:00
.references(() => users.userId, { onDelete: "cascade" }),
2024-10-05 17:01:49 -04:00
tokenHash: text("tokenHash").notNull(),
expiresAt: integer("expiresAt").notNull(),
});
2024-10-05 22:45:35 -04:00
export const actions = sqliteTable("actions", {
2024-10-06 18:05:20 -04:00
actionId: text("actionId").primaryKey(),
2024-10-06 18:12:27 -04:00
name: text("name"),
2024-10-05 22:45:35 -04:00
description: text("description"),
});
export const roles = sqliteTable("roles", {
roleId: integer("roleId").primaryKey({ autoIncrement: true }),
2024-10-14 15:11:18 -04:00
orgId: text("orgId").references(() => orgs.orgId, {
2024-10-13 17:13:47 -04:00
onDelete: "cascade",
}),
2024-10-10 21:59:30 -04:00
isSuperuserRole: integer("isSuperuserRole", { mode: "boolean" }),
2024-10-05 22:45:35 -04:00
name: text("name").notNull(),
description: text("description"),
});
export const roleActions = sqliteTable("roleActions", {
roleId: integer("roleId")
.notNull()
.references(() => roles.roleId, { onDelete: "cascade" }),
2024-10-06 18:05:20 -04:00
actionId: text("actionId")
2024-10-05 22:45:35 -04:00
.notNull()
.references(() => actions.actionId, { onDelete: "cascade" }),
2024-10-14 15:11:18 -04:00
orgId: text("orgId")
.notNull()
.references(() => orgs.orgId, { onDelete: "cascade" }),
2024-10-05 22:45:35 -04:00
});
export const userActions = sqliteTable("userActions", {
userId: text("userId")
.notNull()
2024-10-13 17:13:47 -04:00
.references(() => users.userId, { onDelete: "cascade" }),
2024-10-06 18:05:20 -04:00
actionId: text("actionId")
2024-10-05 22:45:35 -04:00
.notNull()
.references(() => actions.actionId, { onDelete: "cascade" }),
2024-10-14 15:11:18 -04:00
orgId: text("orgId")
.notNull()
.references(() => orgs.orgId, { onDelete: "cascade" }),
2024-10-05 22:45:35 -04:00
});
export const roleSites = sqliteTable("roleSites", {
roleId: integer("roleId")
2024-10-05 22:45:35 -04:00
.notNull()
.references(() => roles.roleId, { onDelete: "cascade" }),
siteId: integer("siteId")
.notNull()
.references(() => sites.siteId, { onDelete: "cascade" }),
});
export const userSites = sqliteTable("userSites", {
userId: text("userId")
2024-10-05 22:45:35 -04:00
.notNull()
2024-10-13 17:13:47 -04:00
.references(() => users.userId, { onDelete: "cascade" }),
2024-10-05 22:45:35 -04:00
siteId: integer("siteId")
.notNull()
.references(() => sites.siteId, { onDelete: "cascade" }),
});
export const roleResources = sqliteTable("roleResources", {
roleId: integer("roleId")
2024-10-05 22:45:35 -04:00
.notNull()
.references(() => roles.roleId, { onDelete: "cascade" }),
2024-10-26 12:15:03 -04:00
resourceId: integer("resourceId")
2024-10-05 22:45:35 -04:00
.notNull()
.references(() => resources.resourceId, { onDelete: "cascade" }),
});
export const userResources = sqliteTable("userResources", {
userId: text("userId")
2024-10-05 22:45:35 -04:00
.notNull()
2024-10-13 17:13:47 -04:00
.references(() => users.userId, { onDelete: "cascade" }),
2024-10-26 12:15:03 -04:00
resourceId: integer("resourceId")
2024-10-05 22:45:35 -04:00
.notNull()
.references(() => resources.resourceId, { onDelete: "cascade" }),
});
2024-10-06 17:42:28 -04:00
export const limitsTable = sqliteTable("limits", {
limitId: integer("limitId").primaryKey({ autoIncrement: true }),
2024-10-14 15:11:18 -04:00
orgId: text("orgId").references(() => orgs.orgId, {
2024-10-07 23:31:23 -04:00
onDelete: "cascade",
}),
2024-10-06 17:42:28 -04:00
name: text("name").notNull(),
value: integer("value").notNull(),
description: text("description"),
});
2024-09-28 13:31:22 -04:00
export type Org = InferSelectModel<typeof orgs>;
2024-09-28 11:51:21 -04:00
export type User = InferSelectModel<typeof users>;
export type Site = InferSelectModel<typeof sites>;
export type Resource = InferSelectModel<typeof resources>;
export type ExitNode = InferSelectModel<typeof exitNodes>;
export type Route = InferSelectModel<typeof routes>;
2024-09-28 22:50:10 -04:00
export type Target = InferSelectModel<typeof targets>;
2024-10-01 20:48:03 -04:00
export type Session = InferSelectModel<typeof sessions>;
2024-10-04 23:14:40 -04:00
export type EmailVerificationCode = InferSelectModel<
typeof emailVerificationCodes
>;
2024-10-05 15:31:28 -04:00
export type TwoFactorBackupCode = InferSelectModel<typeof twoFactorBackupCodes>;
2024-10-05 17:01:49 -04:00
export type PasswordResetToken = InferSelectModel<typeof passwordResetTokens>;
2024-10-05 22:45:35 -04:00
export type Role = InferSelectModel<typeof roles>;
export type Action = InferSelectModel<typeof actions>;
export type RoleAction = InferSelectModel<typeof roleActions>;
export type UserAction = InferSelectModel<typeof userActions>;
export type RoleSite = InferSelectModel<typeof roleSites>;
export type UserSite = InferSelectModel<typeof userSites>;
export type RoleResource = InferSelectModel<typeof roleResources>;
2024-10-06 17:42:28 -04:00
export type UserResource = InferSelectModel<typeof userResources>;
2024-10-07 23:31:23 -04:00
export type Limit = InferSelectModel<typeof limitsTable>;