DEV Community

Mohammad Waseem
Mohammad Waseem

Posted on

Streamlining Production Databases: A TypeScript-Based Open Source Solution for Clutter Management

Managing cluttered production databases is a common challenge that can significantly impact application performance and data integrity. In this post, we'll explore how a security-focused approach, combined with TypeScript and open source tools, can provide an effective strategy for identifying and cleaning unnecessary or outdated data in your production environment.

The Challenge of Cluttering Production Databases

Over time, production databases tend to accumulate obsolete records, redundant data, or poorly indexed tables, leading to slow queries, increased storage costs, and potential security risks. Traditional methods of cleanup often require manual scripting, risking downtime or data loss, especially if not well-automated. Our goal is to develop a robust, maintainable solution leveraging TypeScript — a strongly-typed superset of JavaScript — which offers safety and productivity benefits.

Architectural Approach

The core idea revolves around writing an automated, configurable tool that scans the database, identifies candidate clutter based on customizable rules, and safely archives or deletes such data. To do this efficiently, we use open source tools like typeorm for database interaction, ts-node for running TypeScript scripts, and typescript itself.

Setting Up the Environment

First, let's initialize our project with TypeScript and the necessary dependencies:

npm init -y
npm install typeorm reflect-metadata pg
npm install typescript ts-node --save-dev
Enter fullscreen mode Exit fullscreen mode

Create a tsconfig.json for TypeScript configuration:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "outDir": "dist"
  },
  "include": ["src/**/*.ts"]
}
Enter fullscreen mode Exit fullscreen mode

Connecting to the Database

Define a connection setup in src/database.ts:

import { createConnection } from "typeorm";

export const connect = async () => {
  await createConnection({
    type: "postgres",
    host: "localhost",
    port: 5432,
    username: "user",
    password: "pass",
    database: "prod_db",
    entities: ["src/entity/**/*.ts"],
    synchronize: false,
  });
};
Enter fullscreen mode Exit fullscreen mode

Identifying Cluttered Data

Create an entity model, for example for logs or audit trails, which are typical clutter:

import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class Log {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  message: string;

  @Column()
  createdAt: Date;
}
Enter fullscreen mode Exit fullscreen mode

Next, write a script (src/cleanup.ts) to find and process old logs:

import { getRepository } from "typeorm";
import { Log } from "./entity/Log";

const CLEANUP_THRESHOLD_DAYS = 30;

export const cleanupLogs = async () => {
  const repo = getRepository(Log);
  const cutoffDate = new Date();
  cutoffDate.setDate(cutoffDate.getDate() - CLEANUP_THRESHOLD_DAYS);

  const oldLogs = await repo.find({ where: { createdAt: LessThan(cutoffDate) } });

  // Safely archive or delete
  for (const log of oldLogs) {
    // Implement archiving logic here, e.g., move to cold storage
    await repo.delete(log.id);
  }
  console.log(`Deleted ${oldLogs.length} old logs`);
};
Enter fullscreen mode Exit fullscreen mode

Maintaining Security Integrity

Since security is paramount, incorporate validation, access control, and offline backups before deletions. Use open-source tools like node-helmet or encryption libraries to secure data at rest and in transit.

Running and Automating the Tool

Create a CLI script to run the cleanup:

import { connect } from './database';
import { cleanupLogs } from './cleanup';

const main = async () => {
  await connect();
  await cleanupLogs();
};

main().catch(error => {
  console.error('Error during cleanup:', error);
  process.exit(1);
});
Enter fullscreen mode Exit fullscreen mode

Schedule this script with cron or CI/CD pipelines for regular maintenance, ensuring your production database stays lean, performant, and secure.

Conclusion

By leveraging TypeScript and open source tools, security-conscious developers can build automated, reliable systems to manage database clutter effectively. This approach not only preserves data integrity and security but also reduces manual intervention, ultimately leading to safer and more efficient production environments.


🛠️ QA Tip

To test this safely without using real user data, I use TempoMail USA.

Top comments (0)