mirror of
https://github.com/freedbygrace/DynamoDNS.git
synced 2026-08-27 18:57:28 +00:00
Improve DNS record data handling by optimizing database interactions and fixing inconsistencies.
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/41d1fd85-f35a-4c48-9de4-e4c0438d7331.jpg
This commit is contained in:
+116
-46
@@ -386,23 +386,39 @@ export class DatabaseStorage implements IStorage {
|
|||||||
|
|
||||||
// DNS Record management
|
// DNS Record management
|
||||||
async getDnsRecord(id: string): Promise<DnsRecord | undefined> {
|
async getDnsRecord(id: string): Promise<DnsRecord | undefined> {
|
||||||
const [record] = await db.select({
|
// Use raw SQL to select only columns that actually exist in the database
|
||||||
id: dnsRecords.id,
|
const query = `
|
||||||
domainId: dnsRecords.domainId,
|
SELECT
|
||||||
name: dnsRecords.name,
|
id, domain_id, name, type, content, ttl,
|
||||||
type: dnsRecords.type,
|
is_active, auto_update, notes, last_updated, created_at
|
||||||
content: dnsRecords.content,
|
FROM dns_records
|
||||||
ttl: dnsRecords.ttl,
|
WHERE id = $1
|
||||||
isActive: dnsRecords.isActive,
|
`;
|
||||||
isAutoIP: dnsRecords.isAutoIP,
|
|
||||||
notes: dnsRecords.notes,
|
const result = await db.execute(query, [id]);
|
||||||
providerId: dnsRecords.providerId,
|
|
||||||
lastUpdated: dnsRecords.lastUpdated,
|
if (!result || result.length === 0) {
|
||||||
createdAt: dnsRecords.createdAt
|
return undefined;
|
||||||
})
|
}
|
||||||
.from(dnsRecords)
|
|
||||||
.where(eq(dnsRecords.id, id));
|
const record = result[0];
|
||||||
return record;
|
|
||||||
|
// Map the result to match our schema expectations
|
||||||
|
return {
|
||||||
|
id: record.id,
|
||||||
|
domainId: record.domain_id,
|
||||||
|
name: record.name,
|
||||||
|
type: record.type,
|
||||||
|
content: record.content,
|
||||||
|
ttl: record.ttl,
|
||||||
|
isActive: record.is_active,
|
||||||
|
isAutoIP: record.auto_update, // Map auto_update to isAutoIP
|
||||||
|
notes: record.notes,
|
||||||
|
lastUpdated: record.last_updated,
|
||||||
|
createdAt: record.created_at,
|
||||||
|
proxied: null, // Add missing fields with null values
|
||||||
|
providerId: null
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDnsRecordsByDomain(domainId: string): Promise<DnsRecord[]> {
|
async getDnsRecordsByDomain(domainId: string): Promise<DnsRecord[]> {
|
||||||
@@ -491,43 +507,97 @@ export class DatabaseStorage implements IStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async updateDnsRecord(id: string, recordData: Partial<InsertDnsRecord>): Promise<DnsRecord | undefined> {
|
async updateDnsRecord(id: string, recordData: Partial<InsertDnsRecord>): Promise<DnsRecord | undefined> {
|
||||||
// Filter out the proxied field if it exists and map isAutoIP to autoUpdate
|
// Filter out fields that don't exist in the database
|
||||||
const { proxied, isAutoIP, ...filteredDataRaw } = recordData as any;
|
const { proxied, isAutoIP, providerId, ...validFields } = recordData as any;
|
||||||
|
|
||||||
// Create a proper data object with the correct field names
|
// Build the SET clause parts and parameters for the SQL query
|
||||||
const updateData: any = {
|
const setClauses = [];
|
||||||
...filteredDataRaw,
|
const params = [id]; // First parameter is the id for the WHERE clause
|
||||||
lastUpdated: new Date()
|
let paramIndex = 2; // Start from $2 since $1 is the id
|
||||||
};
|
|
||||||
|
|
||||||
// Map isAutoIP to autoUpdate if it exists
|
// Add each field to the SET clauses
|
||||||
|
Object.entries(validFields).forEach(([key, value]) => {
|
||||||
|
// Convert camelCase to snake_case for database
|
||||||
|
const dbField = key.replace(/([A-Z])/g, '_$1').toLowerCase();
|
||||||
|
setClauses.push(`${dbField} = $${paramIndex}`);
|
||||||
|
params.push(value);
|
||||||
|
paramIndex++;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Handle isAutoIP separately (maps to auto_update in database)
|
||||||
if (isAutoIP !== undefined) {
|
if (isAutoIP !== undefined) {
|
||||||
updateData.autoUpdate = isAutoIP;
|
setClauses.push(`auto_update = $${paramIndex}`);
|
||||||
|
params.push(isAutoIP);
|
||||||
|
paramIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [updatedRecord] = await db.update(dnsRecords)
|
// Always update last_updated
|
||||||
.set(updateData)
|
setClauses.push(`last_updated = $${paramIndex}`);
|
||||||
.where(eq(dnsRecords.id, id))
|
params.push(new Date());
|
||||||
.returning({
|
|
||||||
id: dnsRecords.id,
|
|
||||||
domainId: dnsRecords.domainId,
|
|
||||||
name: dnsRecords.name,
|
|
||||||
type: dnsRecords.type,
|
|
||||||
content: dnsRecords.content,
|
|
||||||
ttl: dnsRecords.ttl,
|
|
||||||
isActive: dnsRecords.isActive,
|
|
||||||
autoUpdate: dnsRecords.autoUpdate,
|
|
||||||
notes: dnsRecords.notes,
|
|
||||||
lastUpdated: dnsRecords.lastUpdated,
|
|
||||||
createdAt: dnsRecords.createdAt
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!updatedRecord) return undefined;
|
// If no fields to update other than last_updated, return the existing record
|
||||||
|
if (setClauses.length === 1) {
|
||||||
|
// Get the record first to return it
|
||||||
|
const existingRecord = await this.getDnsRecord(id);
|
||||||
|
if (!existingRecord) return undefined;
|
||||||
|
|
||||||
|
// Update just the last_updated field
|
||||||
|
const query = `
|
||||||
|
UPDATE dns_records
|
||||||
|
SET last_updated = $2
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING id, domain_id, name, type, content, ttl,
|
||||||
|
is_active, auto_update, notes, last_updated, created_at
|
||||||
|
`;
|
||||||
|
|
||||||
|
const result = await db.execute(query, [id, new Date()]);
|
||||||
|
if (!result || result.length === 0) return undefined;
|
||||||
|
|
||||||
|
// Map result to expected format
|
||||||
|
const updatedRecord = result[0];
|
||||||
|
return {
|
||||||
|
id: updatedRecord.id,
|
||||||
|
domainId: updatedRecord.domain_id,
|
||||||
|
name: updatedRecord.name,
|
||||||
|
type: updatedRecord.type,
|
||||||
|
content: updatedRecord.content,
|
||||||
|
ttl: updatedRecord.ttl,
|
||||||
|
isActive: updatedRecord.is_active,
|
||||||
|
isAutoIP: updatedRecord.auto_update,
|
||||||
|
notes: updatedRecord.notes,
|
||||||
|
lastUpdated: updatedRecord.last_updated,
|
||||||
|
createdAt: updatedRecord.created_at,
|
||||||
|
proxied: null,
|
||||||
|
providerId: null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Return the record with frontend-expected field names
|
// Build and execute the query with all fields
|
||||||
|
const query = `
|
||||||
|
UPDATE dns_records
|
||||||
|
SET ${setClauses.join(', ')}
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING id, domain_id, name, type, content, ttl,
|
||||||
|
is_active, auto_update, notes, last_updated, created_at
|
||||||
|
`;
|
||||||
|
|
||||||
|
const result = await db.execute(query, params);
|
||||||
|
if (!result || result.length === 0) return undefined;
|
||||||
|
|
||||||
|
// Map result to expected format
|
||||||
|
const updatedRecord = result[0];
|
||||||
return {
|
return {
|
||||||
...updatedRecord,
|
id: updatedRecord.id,
|
||||||
isAutoIP: updatedRecord.autoUpdate,
|
domainId: updatedRecord.domain_id,
|
||||||
|
name: updatedRecord.name,
|
||||||
|
type: updatedRecord.type,
|
||||||
|
content: updatedRecord.content,
|
||||||
|
ttl: updatedRecord.ttl,
|
||||||
|
isActive: updatedRecord.is_active,
|
||||||
|
isAutoIP: updatedRecord.auto_update,
|
||||||
|
notes: updatedRecord.notes,
|
||||||
|
lastUpdated: updatedRecord.last_updated,
|
||||||
|
createdAt: updatedRecord.created_at,
|
||||||
proxied: null,
|
proxied: null,
|
||||||
providerId: null
|
providerId: null
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user