mirror of
https://github.com/freedbygrace/DynamoDNS.git
synced 2026-08-09 17:59:24 +00:00
Migrate storage from in-memory to PostgreSQL database.
Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9111ef36-26c8-4085-84ca-a35dc1fec1b5 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7083d608-d6d3-4a6a-9a27-6286c5109627/37883948-9924-4429-8878-f2530cf6fe2b.jpg
This commit is contained in:
+91
-28
@@ -1,19 +1,9 @@
|
||||
import { pgTable, text, serial, integer, boolean, timestamp, jsonb } from "drizzle-orm/pg-core";
|
||||
import { pgTable, text, serial, integer, boolean, timestamp, jsonb, foreignKey, unique } from "drizzle-orm/pg-core";
|
||||
import { createInsertSchema } from "drizzle-zod";
|
||||
import { z } from "zod";
|
||||
import { relations } from "drizzle-orm";
|
||||
|
||||
// Users & Authentication
|
||||
export const users = pgTable("users", {
|
||||
id: serial("id").primaryKey(),
|
||||
username: text("username").notNull().unique(),
|
||||
password: text("password").notNull(),
|
||||
email: text("email").notNull().unique(),
|
||||
fullName: text("full_name"),
|
||||
role: text("role").default("user").notNull(),
|
||||
organizationId: integer("organization_id"),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const organizations = pgTable("organizations", {
|
||||
id: serial("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
@@ -21,12 +11,41 @@ export const organizations = pgTable("organizations", {
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const users = pgTable("users", {
|
||||
id: serial("id").primaryKey(),
|
||||
username: text("username").notNull().unique(),
|
||||
password: text("password").notNull(),
|
||||
email: text("email").notNull().unique(),
|
||||
fullName: text("full_name"),
|
||||
role: text("role").default("user").notNull(),
|
||||
organizationId: integer("organization_id").references(() => organizations.id, { onDelete: "set null" }),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const usersRelations = relations(users, ({ one }) => ({
|
||||
organization: one(organizations, {
|
||||
fields: [users.organizationId],
|
||||
references: [organizations.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
// We'll define this after all tables are declared
|
||||
|
||||
// DNS Management
|
||||
export const providers = pgTable("providers", {
|
||||
id: serial("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
type: text("type").notNull(),
|
||||
credentials: jsonb("credentials"),
|
||||
isActive: boolean("is_active").default(true).notNull(),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const domains = pgTable("domains", {
|
||||
id: serial("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
organizationId: integer("organization_id").notNull(),
|
||||
providerId: integer("provider_id").notNull(),
|
||||
organizationId: integer("organization_id").notNull().references(() => organizations.id, { onDelete: "cascade" }),
|
||||
providerId: integer("provider_id").notNull().references(() => providers.id, { onDelete: "cascade" }),
|
||||
isActive: boolean("is_active").default(true).notNull(),
|
||||
lastUpdated: timestamp("last_updated"),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
@@ -34,7 +53,7 @@ export const domains = pgTable("domains", {
|
||||
|
||||
export const dnsRecords = pgTable("dns_records", {
|
||||
id: serial("id").primaryKey(),
|
||||
domainId: integer("domain_id").notNull(),
|
||||
domainId: integer("domain_id").notNull().references(() => domains.id, { onDelete: "cascade" }),
|
||||
name: text("name").notNull(),
|
||||
type: text("type").notNull(),
|
||||
content: text("content").notNull(),
|
||||
@@ -45,22 +64,13 @@ export const dnsRecords = pgTable("dns_records", {
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const providers = pgTable("providers", {
|
||||
id: serial("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
type: text("type").notNull(),
|
||||
credentials: jsonb("credentials"),
|
||||
isActive: boolean("is_active").default(true).notNull(),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export const dnsHistory = pgTable("dns_history", {
|
||||
id: serial("id").primaryKey(),
|
||||
recordId: integer("record_id").notNull(),
|
||||
recordId: integer("record_id").notNull().references(() => dnsRecords.id, { onDelete: "cascade" }),
|
||||
action: text("action").notNull(),
|
||||
previousValue: text("previous_value"),
|
||||
newValue: text("new_value"),
|
||||
userId: integer("user_id"),
|
||||
userId: integer("user_id").references(() => users.id, { onDelete: "set null" }),
|
||||
timestamp: timestamp("timestamp").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
@@ -69,9 +79,9 @@ export const apiTokens = pgTable("api_tokens", {
|
||||
id: serial("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
token: text("token").notNull().unique(),
|
||||
organizationId: integer("organization_id").notNull(),
|
||||
organizationId: integer("organization_id").notNull().references(() => organizations.id, { onDelete: "cascade" }),
|
||||
permissions: text("permissions").array(),
|
||||
createdBy: integer("created_by").notNull(),
|
||||
createdBy: integer("created_by").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
isActive: boolean("is_active").default(true).notNull(),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
expiresAt: timestamp("expires_at"),
|
||||
@@ -158,3 +168,56 @@ export type ProviderType = typeof providerTypes[number];
|
||||
// DNS Record Types
|
||||
export const recordTypes = ['A', 'AAAA', 'CNAME', 'MX', 'TXT', 'SRV', 'NS', 'CAA', 'PTR'] as const;
|
||||
export type RecordType = typeof recordTypes[number];
|
||||
|
||||
// Define table relations
|
||||
export const organizationsRelations = relations(organizations, ({ many }) => ({
|
||||
users: many(users),
|
||||
domains: many(domains),
|
||||
apiTokens: many(apiTokens),
|
||||
}));
|
||||
|
||||
export const domainsRelations = relations(domains, ({ one, many }) => ({
|
||||
organization: one(organizations, {
|
||||
fields: [domains.organizationId],
|
||||
references: [organizations.id],
|
||||
}),
|
||||
provider: one(providers, {
|
||||
fields: [domains.providerId],
|
||||
references: [providers.id],
|
||||
}),
|
||||
dnsRecords: many(dnsRecords),
|
||||
}));
|
||||
|
||||
export const dnsRecordsRelations = relations(dnsRecords, ({ one, many }) => ({
|
||||
domain: one(domains, {
|
||||
fields: [dnsRecords.domainId],
|
||||
references: [domains.id],
|
||||
}),
|
||||
history: many(dnsHistory),
|
||||
}));
|
||||
|
||||
export const dnsHistoryRelations = relations(dnsHistory, ({ one }) => ({
|
||||
record: one(dnsRecords, {
|
||||
fields: [dnsHistory.recordId],
|
||||
references: [dnsRecords.id],
|
||||
}),
|
||||
user: one(users, {
|
||||
fields: [dnsHistory.userId],
|
||||
references: [users.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
export const providersRelations = relations(providers, ({ many }) => ({
|
||||
domains: many(domains),
|
||||
}));
|
||||
|
||||
export const apiTokensRelations = relations(apiTokens, ({ one }) => ({
|
||||
organization: one(organizations, {
|
||||
fields: [apiTokens.organizationId],
|
||||
references: [organizations.id],
|
||||
}),
|
||||
creator: one(users, {
|
||||
fields: [apiTokens.createdBy],
|
||||
references: [users.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user