UNPKG

database-backup

Version:

A NestJS dynamic module for backing up PostgreSQL databases using pg_dump.

108 lines (72 loc) β€’ 2.65 kB
# πŸ“¦ database-backup A **NestJS dynamic module** for backing up PostgreSQL databases using the native [`pg_dump`](https://www.postgresql.org/docs/current/app-pgdump.html) utility. This package allows you to easily integrate PostgreSQL backup functionality into your NestJS applications. --- ## Installation ```bash npm install database-backup ``` > ⚠️ Make sure you have `pg_dump` installed and available in your system’s PATH. > It is included by default with PostgreSQL installations. --- ## πŸ“– Usage ### 1. Import the Module In your `AppModule` (or any feature module): ```typescript import { Module } from '@nestjs/common'; import { BackupModule } from 'database-backup'; @Module({ imports: [ BackupModule.register({ host: 'localhost', port: 5432, databaseUsername: 'postgres', databasePassword: 'your_password', // Optional: pgDumpPath: '/path/to/pg_dump' }), ], }) export class AppModule {} ``` --- ### 2. Use the Service Inject the `BackupService` anywhere you need to trigger a backup: ```typescript import { Injectable } from '@nestjs/common'; import { BackupService } from 'database-backup'; @Injectable() export class AppService { constructor(private readonly backupService: BackupService) {} async runBackup() { await this.backupService.backupSingleDatabase('my_database'); } } ``` --- ## βš™οΈ Configuration Options | Option | Type | Required | Description | | ------------------ | ------ | -------- | --------------------------------------------------------- | | `host` | string | βœ… | Database host (e.g., `localhost`) | | `port` | number | βœ… | Database port (default: `5432`) | | `databaseUsername` | string | βœ… | PostgreSQL username | | `databasePassword` | string | βœ… | PostgreSQL password | | `pgDumpPath` | string | ❌ | Optional path to `pg_dump` binary (if not in system PATH) | --- ## πŸ“‚ Backup Output Backups are saved in the current working directory as: ``` <database-name>-backup.sql ``` Example: ``` my_database-backup.sql ``` --- ## πŸ”‘ Peer Dependencies This package requires the following peer dependencies: * `@nestjs/core` (v11+) * `@nestjs/common` (v11+) Ensure they are installed in your NestJS project. --- ## πŸ“ License MIT Β© 2025 \Shaheel Abbasi