This application leverages powerful PostgreSQL features to provide robust data management, consistency, and auditability.
The application uses PostgreSQL triggers for automatic actions on data changes:
-
update_timestamp: Automatically updates the
updated_attimestamp column whenever a record is updated- Applied to:
users,my_trips,user_settings,flight_bookings,hotel_bookings,reviews
- Applied to:
-
log_activity: Logs changes to important tables in the
analyticsandadmin_logstables- Applied to:
users,my_trips,destinations,flight_bookings,hotel_bookings,notifications,reviews,booking_approvals
- Applied to:
The application uses stored procedures for encapsulating and centralizing business logic:
- create_trip: Creates a new trip with initial data and logs the activity
- register_user: Registers a new user with default settings and logs the activity
- process_booking_approval: Processes a booking approval workflow and sends notifications
- get_trip_statistics: Generates statistics about trips in the system
The application uses transactions to ensure data consistency for multi-step operations:
- Enhanced transaction helper: Provides detailed logging and automatic error handling
- Isolation level support: Allows specifying READ COMMITTED, REPEATABLE READ, or SERIALIZABLE isolation
- Error mapping: Maps PostgreSQL error codes to user-friendly error messages
To apply the PostgreSQL triggers and stored procedures, run:
npx tsx scripts/run-migrations.tsTo set up all advanced PostgreSQL features (triggers, stored procedures, etc.), run:
npx tsx scripts/setup-postgresql-features.tsTo verify that all the PostgreSQL features are working correctly, run:
npx tsx scripts/test-postgresql-features.tsimport { transaction } from '../server/db';
// Simple transaction
await transaction(async (client) => {
await client.query('INSERT INTO users (username) VALUES ($1)', ['user1']);
await client.query('INSERT INTO user_settings (user_id) VALUES ($1)', [1]);
});
// Transaction with custom options
await transaction(
async (client) => {
// Transaction operations here
},
{
name: 'important_operation',
isolation: 'SERIALIZABLE'
}
);import { query } from '../server/db';
// Register a new user
await query(
`CALL register_user($1, $2, $3, $4, $5, $6, $7, $8)`,
[
'username',
'email@example.com',
'hashed_password',
'First',
'Last',
null,
'Bio text',
'+1234567890'
]
);
// Process a booking approval
await query(
`CALL process_booking_approval($1, $2, $3, $4, $5)`,
['flight', 123, 1, 'approved', 'Booking approved by admin']
);- Automatic timestamps: All tables with
updated_atcolumns automatically get their values updated on change - Activity logging: All changes to important tables are logged in
analyticsandadmin_logstables - Transactional integrity: Complex operations are wrapped in transactions to ensure consistency