Cursor rule
.cursor/rules/database-patterns.mdcDatabase design patterns and conventions for PostgreSQL in POS System
Cursor rules
Quality
62/100
Scores the file, not the repository.Length
888 words
36 headings · 11 code blocksRepository
118
— · pushed 339 days agoLast changed
3 days ago
First indexed 3 days ago.123456# Database Development Guidelines (PostgreSQL)78## Schema Design910### Primary Schema11The main database schema is defined in [database/init/01_schema.sql](mdc:database/init/01_schema.sql):1213### Core Tables Structure:141. **users** - Staff and user management with role-based access152. **categories** - Product categorization with sorting and colors163. **products** - Menu items with pricing and availability174. **dining_tables** - Table and seating management185. **orders** - Order lifecycle with status tracking196. **order_items** - Individual items within orders207. **payments** - Payment processing and history218. **inventory** - Stock management and tracking229. **order_status_history** - Audit trail for order changes2324## Naming Conventions2526### Table Names27- Use plural snake_case: `order_items`, `dining_tables`28- Descriptive names that clearly indicate content29- Avoid abbreviations unless universally understood3031### Column Names32- Use snake_case: `created_at`, `order_number`33- Boolean fields start with `is_`: `is_active`, `is_occupied`34- Timestamps end with `_at`: `created_at`, `processed_at`35- Foreign keys follow pattern: `{table_name}_id`3637### Constraint Names38- Primary keys: `{table}_pkey`39- Foreign keys: `fk_{table}_{referenced_table}`40- Unique constraints: `unique_{table}_{column}`41- Check constraints: `check_{table}_{column}_{description}`4243## Data Types & Standards4445### UUID Primary Keys46Use UUID v4 for all primary keys:47```sql48CREATE TABLE example_table (49 id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),50 -- other columns51);52```5354### Timestamps55Always use `TIMESTAMP WITH TIME ZONE` for time tracking:56```sql57created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,58updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP59```6061### Money Values62Use `DECIMAL(10,2)` for all monetary values:63```sql64price DECIMAL(10,2) NOT NULL,65total_amount DECIMAL(10,2) NOT NULL DEFAULT 066```6768### String Fields69- Use appropriate VARCHAR lengths based on expected content70- Use TEXT for long content without length restrictions71- Apply NOT NULL constraints where appropriate7273## Indexing Strategy7475### Performance Indexes76Critical indexes defined in [database/init/01_schema.sql](mdc:database/init/01_schema.sql):7778```sql79-- Query optimization indexes80CREATE INDEX idx_orders_status ON orders(status);81CREATE INDEX idx_orders_created_at ON orders(created_at);82CREATE INDEX idx_orders_table_id ON orders(table_id);83CREATE INDEX idx_order_items_order_id ON order_items(order_id);84CREATE INDEX idx_products_category_id ON products(category_id);85CREATE INDEX idx_products_is_available ON products(is_available);86```8788### Index Guidelines89- Index foreign key columns for JOIN performance90- Index columns used in WHERE clauses frequently91- Index columns used for sorting (ORDER BY)92- Avoid over-indexing (impacts INSERT/UPDATE performance)93- Consider composite indexes for multi-column queries9495## Relationships & Constraints9697### Foreign Key Relationships98Maintain referential integrity with proper CASCADE rules:99```sql100-- Orders reference tables and users101table_id UUID REFERENCES dining_tables(id) ON DELETE SET NULL,102user_id UUID REFERENCES users(id) ON DELETE SET NULL,103104-- Order items reference orders and products105order_id UUID REFERENCES orders(id) ON DELETE CASCADE,106product_id UUID REFERENCES products(id) ON DELETE CASCADE107```108109### Check Constraints110Use CHECK constraints for data validation:111```sql112-- Enum-like constraints113role VARCHAR(20) NOT NULL CHECK (role IN ('admin', 'manager', 'cashier', 'kitchen')),114status VARCHAR(20) NOT NULL CHECK (status IN ('pending', 'confirmed', 'preparing', 'ready', 'served', 'completed', 'cancelled')),115116-- Business rule constraints117quantity INTEGER NOT NULL DEFAULT 1 CHECK (quantity > 0),118price DECIMAL(10,2) NOT NULL CHECK (price >= 0)119```120121## Triggers & Automation122123### Updated Timestamp Trigger124Automatic updated_at timestamp management:125```sql126CREATE OR REPLACE FUNCTION update_updated_at_column()127RETURNS TRIGGER AS $$128BEGIN129 NEW.updated_at = CURRENT_TIMESTAMP;130 RETURN NEW;131END;132$$ language 'plpgsql';133134-- Apply to relevant tables135CREATE TRIGGER update_orders_updated_at136 BEFORE UPDATE ON orders137 FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();138```139140## Data Migration Patterns141142### Schema Changes143For schema modifications:1441. Create migration scripts with version numbers1452. Always provide both UP and DOWN migrations1463. Test migrations on sample data before production1474. Use transactions for multiple related changes148149### Example Migration Pattern:150```sql151-- Migration: 001_add_customer_phone.sql152BEGIN;153154ALTER TABLE orders ADD COLUMN customer_phone VARCHAR(20);155CREATE INDEX idx_orders_customer_phone ON orders(customer_phone)156 WHERE customer_phone IS NOT NULL;157158-- Rollback plan documented159-- ALTER TABLE orders DROP COLUMN customer_phone;160161COMMIT;162```163164## Query Performance Patterns165166### Efficient JOINs167Use appropriate JOIN types and index foreign keys:168```sql169-- Good: Index-optimized JOIN170SELECT o.*, t.table_number, u.username171FROM orders o172LEFT JOIN dining_tables t ON o.table_id = t.id173LEFT JOIN users u ON o.user_id = u.id174WHERE o.status = 'pending';175```176177### Pagination178Always use LIMIT/OFFSET for large result sets:179```sql180SELECT * FROM orders181ORDER BY created_at DESC182LIMIT $1 OFFSET $2;183```184185### Parameterized Queries186Always use parameter placeholders to prevent SQL injection:187```sql188-- Good: Parameterized query189SELECT * FROM products WHERE category_id = $1 AND is_available = $2;190191-- Bad: String concatenation (vulnerable to injection)192-- SELECT * FROM products WHERE category_id = '" + categoryId + "'193```194195## Backup & Recovery196197### Data Protection198- Regular automated backups of critical data199- Point-in-time recovery capability200- Test restore procedures regularly201- Document recovery time objectives (RTO)202203## Security Considerations204205### Access Control206- Use least-privilege principles for database users207- Separate users for application vs administrative access208- Regular password rotation for database accounts209- Network-level access restrictions210211### Data Encryption212- Encrypt sensitive data at rest213- Use SSL/TLS for database connections214- Consider column-level encryption for PII215- Audit access to sensitive tables216217## Monitoring & Maintenance218219### Performance Monitoring220- Monitor slow queries and optimization opportunities221- Track connection pool usage and limits222- Monitor disk space and growth trends223- Set up alerting for critical metrics224225### Regular Maintenance226- Analyze and vacuum tables regularly227- Update table statistics for query optimization228- Monitor and manage database locks229- Review and optimize indexes based on usage patterns
Also in madebyaris/poinf-of-sales
Diff this repo’s formatsOne repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| madebyaris/poinf-of-sales.cursor/rules/admin-interface-patterns.mdc · 118 | Cursor rules | stylearchsecurityapi+2 | 62/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/api-patterns.mdc · 118 | Cursor rules | lint-formatstylesecuritydatabase+3 | 62/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/authentication-and-security-patterns.mdc · 118 | Cursor rules | setupteststylesecurity+4 | 81/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/backend-golang.mdc · 118 | Cursor rules | testlint-formatstylearch+5 | 69/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/business-logic-patterns.mdc · 118 | Cursor rules | teststyledatabaseperformance+1 | 50/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/development-workflow.mdc · 118 | Cursor rules | setupbuildteststyle+3 | 86/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/docker-deployment.mdc · 118 | Cursor rules | setupbuildteststyle+8 | 77/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/frontend-react.mdc · 118 | Cursor rules | buildtestlint-formatstyle+4 | 69/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/makefile-scripting.mdc · 118 | Cursor rules | setuplint-formatstylearch+2 | 81/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/performance-optimization-patterns.mdc · 118 | Cursor rules | buildteststyledatabase+3 | 66/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/project-architecture.mdc · 118 | Cursor rules | setupteststylearch+6 | 78/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/react-native-mobile-patterns.mdc · 118 | Cursor rules | buildstylearchui+2 | 74/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/role-based-access-patterns.mdc · 118 | Cursor rules | styletypessecuritydatabase+2 | 58/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/tech-debt-prevention.mdc · 118 | Cursor rules | styletesting-strategyui | 50/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/testing-patterns.mdc · 118 | Cursor rules | setupteststylearch+4 | 74/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/user-journey-optimization.mdc · 118 | Cursor rules | styleperformanceagent-behaviour | 50/100 | 3 days ago |
Diff against .cursor/rules/admin-interface-patterns.mdc Diff against .cursor/rules/api-patterns.mdc Diff against .cursor/rules/authentication-and-security-patterns.mdc Diff against .cursor/rules/backend-golang.mdc Diff against .cursor/rules/business-logic-patterns.mdc Diff against .cursor/rules/development-workflow.mdc Diff against .cursor/rules/docker-deployment.mdc Diff against .cursor/rules/frontend-react.mdc Diff against .cursor/rules/makefile-scripting.mdc Diff against .cursor/rules/performance-optimization-patterns.mdc Diff against .cursor/rules/project-architecture.mdc Diff against .cursor/rules/react-native-mobile-patterns.mdc Diff against .cursor/rules/role-based-access-patterns.mdc Diff against .cursor/rules/tech-debt-prevention.mdc Diff against .cursor/rules/testing-patterns.mdc Diff against .cursor/rules/user-journey-optimization.mdc
Similar configs
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| hiromaily/go-crypto-wallet.cursor/rules/typescript.mdc · 126 | Cursor rules | setupbuildtestlint-format+6 | 100/100 | 3 days ago | |
| TechSquidTV/Hermes.cursor/rules/10-hermes-api.mdc · 45 | Cursor rules | testlint-formatstylearch+5 | 100/100 | 3 days ago | |
| markstev/mark-starter.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+6 | 99/100 | 3 days ago | |
| Allymahmoud/case-intake-platform.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| dodgecfr/combatfilms-webapp.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| deifos/clipmira-subtitles.cursor/rules/frontend.mdc · 1 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| langflow-ai/langflow.cursor/rules/docs_development.mdc · 153k | Cursor rules | setupbuildtestlint-format+7 | 97/100 | 3 days ago | |
| TechSquidTV/Hermes.cursor/rules/20-hermes-api-tests.mdc · 45 | Cursor rules | teststyletesting-strategysecurity+3 | 97/100 | 3 days ago |
