Skip to main content

Push Notification Service - Database Seeder

This package provides tools to generate realistic sample data for the Push Notification Service database. It supports multiple approaches including direct ORM-based generation, SQL file generation, and Docker-based setup.

Overview​

The database seeder creates the following sample data:

  • Users: People who receive notifications
  • Devices: Mobile, web, or other devices registered to users
  • Notifications: Messages sent to users
  • User Preferences: User-specific notification preferences
  • Sent Notifications: Records of notifications that have been delivered
  • Notification Logs: Detailed tracking of notification lifecycle

Prerequisites​

  • Node.js 14+ and npm
  • PostgreSQL 12+ (if using direct database insertion)
  • Docker and Docker Compose (if using the Docker approach)

Installation​

Option 1: Automated Setup​

Run the installation script:

chmod +x setup-seed-tools.sh
./setup-seed-tools.sh

Option 2: Manual Setup​

  1. Install dependencies:
npm install sequelize pg pg-hstore faker uuid dotenv
  1. Create a .env file with your database configuration:
DB_HOST=localhost
DB_PORT=5432
DB_NAME=notification_db
DB_USER=admin
DB_PASSWORD=admin123

# Data generation options
USER_COUNT=100
NOTIFICATION_COUNT=500
START_DATE=2023-01-01
TRUNCATE_FIRST=true

# Mode: 'sequelize' or 'sql'
MODE=sequelize
EXECUTE_SQL=false

Usage​

Basic Usage​

To generate data using the Sequelize ORM (directly inserts into database):

node data-generator.js

Advanced Usage​

The enhanced data generator supports multiple modes and options:

Generate with Sequelize ORM​

MODE=sequelize node enhanced-data-generator.js

Generate SQL File Only​

MODE=sql EXECUTE_SQL=false node enhanced-data-generator.js

Generated SQL will be saved to ./generated-data/seed-data.sql.

Generate and Execute SQL​

MODE=sql EXECUTE_SQL=true node enhanced-data-generator.js

Customize Data Volume​

USER_COUNT=500 NOTIFICATION_COUNT=2000 node enhanced-data-generator.js

Docker-based Approach​

Using Docker eliminates the need to install Node.js and dependencies locally:

  1. Ensure Docker and Docker Compose are installed
  2. Create necessary files (data-generator.js, docker-compose.yml)
  3. Run:
docker-compose up

This will:

  • Start a PostgreSQL container
  • Initialize the database schema
  • Run the data generator script

Make Commands​

If you've set up the Makefile, you can use these simplified commands:

# Install dependencies
make install

# Generate data using Sequelize ORM
make seed

# Generate SQL file only
make sql

# Generate and execute SQL
make execute-sql

# Run using Docker
make docker-seed

# Clean generated files
make clean

# Reset the database (drop and recreate)
make reset-db

Configuration​

The following environment variables can be set:

VariableDescriptionDefault
MODEGeneration mode: 'sequelize' or 'sql''sequelize'
USER_COUNTNumber of users to generate100
NOTIFICATION_COUNTNumber of notifications to generate500
START_DATEStart date for random dates'2023-01-01'
TRUNCATE_FIRSTWhether to truncate tables before insertingfalse
EXECUTE_SQLExecute generated SQL filefalse
OUTPUT_DIRDirectory for SQL output files'./generated-data'
DB_HOSTDatabase host'localhost'
DB_PORTDatabase port5432
DB_NAMEDatabase name'notification_db'
DB_USERDatabase user'admin'
DB_PASSWORDDatabase password'admin123'

Generated Data​

The data generator creates:

  • 100 users (configurable)
  • 1-3 devices per user
  • 500 notifications (configurable)
  • 1-4 notification preferences per user
  • ~80% of notifications marked as sent
  • 2-5 log entries per notification

Types of data included:

  • Device types: android, iOS, web
  • Notification types: message, friend_request, payment, security, marketing, system
  • Log types: created, processed, sent, delivered, failed, clicked

Data Model​

Troubleshooting​

Database Connection Issues​

If you encounter connection errors:

  1. Verify your database credentials in .env
  2. Ensure PostgreSQL is running
  3. Check if the database exists and is accessible

Example troubleshooting:

# Check PostgreSQL status
sudo service postgresql status

# Connect to database manually
psql -h localhost -U admin -d notification_db

# Check if tables exist
\dt

Duplicate Key Errors​

If you run the script multiple times, you may get unique constraint violations:

  • Set TRUNCATE_FIRST=true to clear tables before inserting
  • Or run make reset-db to completely reset the database

Performance Issues​

For large volumes of data:

  • Use the SQL generation mode (MODE=sql) which is more efficient
  • Increase Node.js memory if needed: NODE_OPTIONS=--max-old-space-size=4096