database-backup
Version:
A NestJS dynamic module for backing up PostgreSQL databases using pg_dump.
108 lines (72 loc) β’ 2.65 kB
Markdown
# π¦ 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