fosrl.pangolin/server/db/schema.ts

235 lines
8.2 KiB
TypeScript
Raw Normal View History

2024-09-27 21:39:03 -04:00
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";
2024-09-28 11:51:21 -04:00
import { InferSelectModel } from "drizzle-orm";
2024-09-28 13:31:22 -04:00
// Orgs table
export const orgs = sqliteTable("orgs", {
2024-09-28 15:21:13 -04:00
orgId: integer("orgId").primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
domain: text("domain").notNull(),
2024-09-28 11:51:21 -04:00
});
// Sites table
export const sites = sqliteTable("sites", {
2024-09-28 15:21:13 -04:00
siteId: integer("siteId").primaryKey({ autoIncrement: true }),
2024-10-01 20:48:03 -04:00
orgId: integer("orgId").references(() => orgs.orgId, {
onDelete: "cascade",
}),
exitNode: integer("exitNode").references(() => exitNodes.exitNodeId, {
onDelete: "set null",
}),
2024-09-28 15:21:13 -04:00
name: text("name").notNull(),
subdomain: text("subdomain"),
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
});
// Resources table
export const resources = sqliteTable("resources", {
2024-09-28 17:10:03 -04:00
resourceId: text("resourceId", { length: 2048 }).primaryKey(),
2024-10-01 20:48:03 -04:00
siteId: integer("siteId").references(() => sites.siteId, {
onDelete: "cascade",
}),
2024-10-02 22:05:21 -04:00
orgId: integer("orgId").references(() => orgs.orgId, {
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
// Targets table
export const targets = sqliteTable("targets", {
targetId: integer("targetId").primaryKey({ autoIncrement: true }),
2024-10-01 20:48:03 -04:00
resourceId: text("resourceId").references(() => resources.resourceId, {
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),
2024-09-28 17:10:03 -04:00
});
2024-09-28 11:51:21 -04:00
// Exit Nodes table
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(),
privateKey: text("privateKey"),
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
// Routes table
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
// Users table
export const users = sqliteTable("user", {
id: text("id").primaryKey(), // has to be id not userId for lucia
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-01 20:48:03 -04:00
});
2024-10-05 15:31:28 -04:00
export const twoFactorBackupCodes = sqliteTable("twoFactorBackupCodes", {
id: integer("id").primaryKey({ autoIncrement: true }),
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
codeHash: text("codeHash").notNull(),
});
2024-10-01 20:48:03 -04:00
// Sessions table
export const sessions = sqliteTable("session", {
id: text("id").primaryKey(), // has to be id not sessionId for lucia
userId: text("userId")
.notNull()
2024-10-04 23:14:40 -04:00
.references(() => users.id, { 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()
.references(() => users.id),
orgId: integer("orgId")
.notNull()
.references(() => orgs.orgId),
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", {
id: integer("id").primaryKey({ autoIncrement: true }),
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
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", {
id: integer("id").primaryKey({ autoIncrement: true }),
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
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 }),
orgId: integer("orgId").references(() => orgs.orgId, { onDelete: "cascade" }),
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" }),
orgId: integer("orgId")
.notNull()
.references(() => orgs.orgId, { onDelete: "cascade" }),
2024-10-05 22:45:35 -04:00
});
export const userActions = sqliteTable("userActions", {
userId: text("userId")
.notNull()
.references(() => users.id, { 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" }),
orgId: integer("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()
.references(() => users.id, { onDelete: "cascade" }),
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" }),
resourceId: text("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()
.references(() => users.id, { onDelete: "cascade" }),
resourceId: text("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 }),
orgId: integer("orgId").references(() => orgs.orgId, { onDelete: "cascade" }),
name: text("name").notNull(),
value: integer("value").notNull(),
description: text("description"),
});
2024-09-28 11:51:21 -04:00
// Define the model types for type inference
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>;
export type Limit = InferSelectModel<typeof limitsTable>;