agents-templated 2.2.18 → 2.2.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/templates/agents/commands/arch-check.md +58 -58
- package/templates/agents/commands/audit.md +58 -58
- package/templates/agents/commands/debug-track.md +58 -58
- package/templates/agents/commands/docs.md +58 -58
- package/templates/agents/commands/fix.md +58 -58
- package/templates/agents/commands/learn-loop.md +58 -58
- package/templates/agents/commands/perf.md +58 -58
- package/templates/agents/commands/plan.md +58 -58
- package/templates/agents/commands/pr.md +58 -58
- package/templates/agents/commands/problem-map.md +58 -58
- package/templates/agents/commands/release-ready.md +58 -58
- package/templates/agents/commands/release.md +58 -58
- package/templates/agents/commands/risk-review.md +58 -58
- package/templates/agents/commands/scope-shape.md +58 -58
- package/templates/agents/commands/task.md +58 -58
- package/templates/agents/commands/test.md +58 -58
- package/templates/agents/commands/ux-bar.md +58 -58
- package/templates/agents/rules/core.md +173 -173
- package/templates/agents/rules/database.md +305 -305
- package/templates/agents/rules/frontend.md +217 -217
- package/templates/agents/rules/security.md +278 -278
- package/templates/agents/rules/style.md +344 -344
- package/templates/agents/rules/testing.md +371 -371
- package/templates/agents/rules/workflows.md +56 -56
|
@@ -1,305 +1,305 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: "Database and Data Layer Guidelines"
|
|
3
|
-
description: "Apply when designing schema, building data access layers, optimizing queries, or managing database operations"
|
|
4
|
-
alwaysApply: true
|
|
5
|
-
version: "3.0.0"
|
|
6
|
-
tags: ["database", "backend", "data", "orm", "schema"]
|
|
7
|
-
globs:
|
|
8
|
-
- "src/models/**/*"
|
|
9
|
-
- "src/data/**/*"
|
|
10
|
-
---
|
|
11
|
-
|
|
12
|
-
# Database and Data Layer Guidelines
|
|
13
|
-
|
|
14
|
-
Technology-agnostic patterns for secure, maintainable data access.
|
|
15
|
-
|
|
16
|
-
## Core Principles
|
|
17
|
-
|
|
18
|
-
### Design First
|
|
19
|
-
- **Schema design** - Think through relationships and constraints
|
|
20
|
-
- **Normalization** - Minimize data duplication and anomalies
|
|
21
|
-
- **Scalability** - Design for growth and performance
|
|
22
|
-
- **Consistency** - ACID transactions when needed (SQL) or eventual consistency patterns (NoSQL)
|
|
23
|
-
- **Security** - Access controls at database and application level
|
|
24
|
-
|
|
25
|
-
### Access Patterns
|
|
26
|
-
- **ORM/ODM only** - Use abstraction layer, never raw SQL/queries
|
|
27
|
-
- **Parameterized queries** - Prevent injection attacks
|
|
28
|
-
- **Query optimization** - Avoid N+1 problems and slow queries
|
|
29
|
-
- **Connection pooling** - Manage resources efficiently
|
|
30
|
-
- **Error handling** - Graceful degradation when database unavailable
|
|
31
|
-
|
|
32
|
-
### Migration Strategy
|
|
33
|
-
- **Version control** - All schema changes tracked
|
|
34
|
-
- **Idempotent migrations** - Can run multiple times safely
|
|
35
|
-
- **Reversible migrations** - Can rollback if needed
|
|
36
|
-
- **Testing** - Test migrations in all environments
|
|
37
|
-
- **Documentation** - Document significant schema decisions
|
|
38
|
-
|
|
39
|
-
## Database Strategy Options
|
|
40
|
-
|
|
41
|
-
### SQL Databases (PostgreSQL, MySQL, SQLite, SQL Server)
|
|
42
|
-
|
|
43
|
-
Characteristics:
|
|
44
|
-
- Structured schema with defined tables and relationships
|
|
45
|
-
- ACID transactions for data consistency
|
|
46
|
-
- Powerful querying with SQL
|
|
47
|
-
- Good for relational data
|
|
48
|
-
- Mature tooling and libraries
|
|
49
|
-
|
|
50
|
-
Tools:
|
|
51
|
-
- ORM: Sequelize (Node.js), SQLAlchemy (Python), Hibernate (Java), Entity Framework (.NET)
|
|
52
|
-
- Query Builder: Knex.js (Node.js), SqlAlchemy Core (Python), QueryDSL (Java)
|
|
53
|
-
- Migration tools: Prisma Migrate, Flyway, Liquibase, Alembic
|
|
54
|
-
|
|
55
|
-
Best for:
|
|
56
|
-
- Complex relationships between entities
|
|
57
|
-
- Strong consistency requirements
|
|
58
|
-
- Transactional integrity
|
|
59
|
-
- Complex queries
|
|
60
|
-
- Team comfortable with SQL
|
|
61
|
-
|
|
62
|
-
### NoSQL Databases (MongoDB, DynamoDB, CouchDB, Firestore)
|
|
63
|
-
|
|
64
|
-
Characteristics:
|
|
65
|
-
- Flexible/schemaless documents
|
|
66
|
-
- Eventual consistency
|
|
67
|
-
- Good horizontal scaling
|
|
68
|
-
- Fast for simple lookups
|
|
69
|
-
- Dev-friendly for rapid iteration
|
|
70
|
-
|
|
71
|
-
Tools:
|
|
72
|
-
- ODM: Mongoose (MongoDB), AWS SDK (DynamoDB), Firebase SDK (Firestore)
|
|
73
|
-
- Schema validation: JSONSchema, Zod, Joi
|
|
74
|
-
|
|
75
|
-
Best for:
|
|
76
|
-
- Flexible/evolving data structures
|
|
77
|
-
- Horizontal scaling
|
|
78
|
-
- Fast document lookups
|
|
79
|
-
- Real-time updates
|
|
80
|
-
- Prototyping and MVPs
|
|
81
|
-
|
|
82
|
-
### Managed Database Services (Supabase, Firebase, AWS RDS)
|
|
83
|
-
|
|
84
|
-
Characteristics:
|
|
85
|
-
- Hosted by cloud provider
|
|
86
|
-
- Automatic backups and scaling
|
|
87
|
-
- Built-in authentication
|
|
88
|
-
- Real-time subscriptions (some)
|
|
89
|
-
- Reduced infrastructure complexity
|
|
90
|
-
|
|
91
|
-
Best for:
|
|
92
|
-
- Rapid prototyping
|
|
93
|
-
- Teams without DevOps expertise
|
|
94
|
-
- Projects needing built-in features
|
|
95
|
-
- Compliance-heavy requirements
|
|
96
|
-
- Teams preferring managed services
|
|
97
|
-
|
|
98
|
-
## Schema Design Patterns
|
|
99
|
-
|
|
100
|
-
### Core Tables/Collections
|
|
101
|
-
|
|
102
|
-
Base entities:
|
|
103
|
-
- **Users**: Authentication and identity data
|
|
104
|
-
- **Roles**: Permission groups
|
|
105
|
-
- **Audit logs**: Track changes for compliance
|
|
106
|
-
|
|
107
|
-
Related data:
|
|
108
|
-
- Define relationships clearly
|
|
109
|
-
- Use appropriate constraints (foreign keys, unique constraints)
|
|
110
|
-
- Add indexes for common queries
|
|
111
|
-
- Document connection between tables
|
|
112
|
-
|
|
113
|
-
### Timestamps
|
|
114
|
-
|
|
115
|
-
Include timestamps:
|
|
116
|
-
- **Created at**: When record created (immutable)
|
|
117
|
-
- **Updated at**: When record last modified
|
|
118
|
-
- **Deleted at**: For soft deletes (when applicable)
|
|
119
|
-
|
|
120
|
-
### IDs and Keys
|
|
121
|
-
|
|
122
|
-
ID strategy:
|
|
123
|
-
- **Primary key**: Uniquely identifies record
|
|
124
|
-
- **Foreign key**: References another table (relational)
|
|
125
|
-
- **Unique constraints**: Prevent duplicates
|
|
126
|
-
- **Non-sequential IDs**: Use UUID or similar for security
|
|
127
|
-
|
|
128
|
-
### Data Validation
|
|
129
|
-
|
|
130
|
-
At database level:
|
|
131
|
-
- Required fields (NOT NULL)
|
|
132
|
-
- Data type constraints
|
|
133
|
-
- Length constraints (for strings)
|
|
134
|
-
- Enum constraints (for limited values)
|
|
135
|
-
- Range constraints (for numbers)
|
|
136
|
-
|
|
137
|
-
At application level:
|
|
138
|
-
- Validate before insert/update
|
|
139
|
-
- Validate format (email, URL, etc.)
|
|
140
|
-
- Validate business rules
|
|
141
|
-
- Return clear error messages
|
|
142
|
-
|
|
143
|
-
## Query Patterns
|
|
144
|
-
|
|
145
|
-
### Efficient Queries
|
|
146
|
-
|
|
147
|
-
Do:
|
|
148
|
-
- Use WHERE clauses to filter
|
|
149
|
-
- Use indexes on filtered fields
|
|
150
|
-
- Use SELECT specific columns
|
|
151
|
-
- Use LIMIT for pagination
|
|
152
|
-
- Use JOIN for related data
|
|
153
|
-
- Use aggregation functions
|
|
154
|
-
- Batch operations when possible
|
|
155
|
-
|
|
156
|
-
Don't:
|
|
157
|
-
- SELECT * (select all columns)
|
|
158
|
-
- N+1 queries (query in loop)
|
|
159
|
-
- Unindexed large scans
|
|
160
|
-
- Complex JOINs without indexes
|
|
161
|
-
- Missing WHERE clause
|
|
162
|
-
- Fetching unused data
|
|
163
|
-
|
|
164
|
-
### Pagination
|
|
165
|
-
|
|
166
|
-
Pagination pattern:
|
|
167
|
-
1. Accept limit and offset parameters
|
|
168
|
-
2. Validate reasonable limit (max 100-1000)
|
|
169
|
-
3. Query with LIMIT and OFFSET
|
|
170
|
-
4. Return total count optionally
|
|
171
|
-
5. Include nextPage/previousPage links
|
|
172
|
-
|
|
173
|
-
### Relationships
|
|
174
|
-
|
|
175
|
-
Query related data:
|
|
176
|
-
- One-to-many: JOIN or separate queries
|
|
177
|
-
- Many-to-many: Junction table with JOINs
|
|
178
|
-
- One-to-one: JOIN or embed in document
|
|
179
|
-
- Hierarchical: Recursive queries or denormalization
|
|
180
|
-
|
|
181
|
-
## Security
|
|
182
|
-
|
|
183
|
-
### Access Control
|
|
184
|
-
|
|
185
|
-
Implement at database:
|
|
186
|
-
- Row-level security (RLS in PostgreSQL, Firestore)
|
|
187
|
-
- Column-level access restrictions
|
|
188
|
-
- Database-level user permissions
|
|
189
|
-
|
|
190
|
-
Implement at application:
|
|
191
|
-
- Verify user owns resource before returning
|
|
192
|
-
- Check permissions before allowing modifications
|
|
193
|
-
- Log access to sensitive data
|
|
194
|
-
- Rate limit database operations
|
|
195
|
-
|
|
196
|
-
### Sensitive Data
|
|
197
|
-
|
|
198
|
-
Protect sensitive data:
|
|
199
|
-
- Hash passwords (never store plaintext)
|
|
200
|
-
- Encrypt PII at rest
|
|
201
|
-
- Never log sensitive data
|
|
202
|
-
- Mask sensitive data in backups
|
|
203
|
-
- Use parameterized queries to prevent injection
|
|
204
|
-
|
|
205
|
-
### Backup & Recovery
|
|
206
|
-
|
|
207
|
-
Backup strategy:
|
|
208
|
-
- Automated daily backups
|
|
209
|
-
- Test recovery procedures
|
|
210
|
-
- Retain backups for compliance period
|
|
211
|
-
- Encrypt backups at rest
|
|
212
|
-
- Store backups separately from primary database
|
|
213
|
-
|
|
214
|
-
## Performance
|
|
215
|
-
|
|
216
|
-
### Indexing
|
|
217
|
-
|
|
218
|
-
Index columns that are:
|
|
219
|
-
- Frequently filtered (WHERE clause)
|
|
220
|
-
- Used in JOINs
|
|
221
|
-
- Used in ORDER BY
|
|
222
|
-
- Used in DISTINCT
|
|
223
|
-
|
|
224
|
-
Don't over-index:
|
|
225
|
-
- Too many indexes slow down writes
|
|
226
|
-
- Indexes require storage space
|
|
227
|
-
- Maintain only used indexes
|
|
228
|
-
|
|
229
|
-
### Connection Management
|
|
230
|
-
|
|
231
|
-
Connection pooling:
|
|
232
|
-
- Reuse connections across requests
|
|
233
|
-
- Configure reasonable pool size (10-20 connections)
|
|
234
|
-
- Set idle timeout
|
|
235
|
-
- Monitor connection usage
|
|
236
|
-
- Alert on connection exhaustion
|
|
237
|
-
|
|
238
|
-
### Query Optimization
|
|
239
|
-
|
|
240
|
-
Optimize slow queries:
|
|
241
|
-
1. Profile query execution time
|
|
242
|
-
2. Check execution plan
|
|
243
|
-
3. Add missing indexes
|
|
244
|
-
4. Rewrite queries if needed
|
|
245
|
-
5. Consider denormalization if needed
|
|
246
|
-
6. Verify schema design
|
|
247
|
-
|
|
248
|
-
## Monitoring & Maintenance
|
|
249
|
-
|
|
250
|
-
### Health Checks
|
|
251
|
-
|
|
252
|
-
Monitor:
|
|
253
|
-
- Database connectivity
|
|
254
|
-
- Query performance (slow query logs)
|
|
255
|
-
- Connection pool status
|
|
256
|
-
- Storage space usage
|
|
257
|
-
- Backup completion
|
|
258
|
-
- Replication lag (if applicable)
|
|
259
|
-
|
|
260
|
-
### Database Maintenance
|
|
261
|
-
|
|
262
|
-
Regular tasks:
|
|
263
|
-
- Update statistics/analyze
|
|
264
|
-
- Vacuum/compact (depending on DB)
|
|
265
|
-
- Rebuild indexes if needed
|
|
266
|
-
- Clean up old audit logs
|
|
267
|
-
- Review and optimize slow queries
|
|
268
|
-
|
|
269
|
-
## Testing
|
|
270
|
-
|
|
271
|
-
### Test Data
|
|
272
|
-
|
|
273
|
-
Setup:
|
|
274
|
-
- Use separate test database
|
|
275
|
-
- Reset data before each test
|
|
276
|
-
- Use factories for consistent test data
|
|
277
|
-
- Seed with realistic data
|
|
278
|
-
|
|
279
|
-
Testing patterns:
|
|
280
|
-
- Test CRUD operations
|
|
281
|
-
- Test relationships
|
|
282
|
-
- Test constraints
|
|
283
|
-
- Test error cases
|
|
284
|
-
- Test performance
|
|
285
|
-
|
|
286
|
-
### Test Examples
|
|
287
|
-
|
|
288
|
-
Test: Create and retrieve user
|
|
289
|
-
1. Create user with valid data
|
|
290
|
-
2. Retrieve by ID
|
|
291
|
-
3. Verify data matches
|
|
292
|
-
|
|
293
|
-
Test: Enforce unique constraint
|
|
294
|
-
1. Create user with email
|
|
295
|
-
2. Attempt to create another with same email
|
|
296
|
-
3. Verify error is returned
|
|
297
|
-
|
|
298
|
-
Test: Cascade delete
|
|
299
|
-
1. Create parent and child records
|
|
300
|
-
2. Delete parent
|
|
301
|
-
3. Verify child is deleted
|
|
302
|
-
|
|
303
|
-
---
|
|
304
|
-
|
|
305
|
-
Remember: Good database design prevents bugs, improves performance, and makes your application maintainable.
|
|
1
|
+
---
|
|
2
|
+
title: "Database and Data Layer Guidelines"
|
|
3
|
+
description: "Apply when designing schema, building data access layers, optimizing queries, or managing database operations"
|
|
4
|
+
alwaysApply: true
|
|
5
|
+
version: "3.0.0"
|
|
6
|
+
tags: ["database", "backend", "data", "orm", "schema"]
|
|
7
|
+
globs:
|
|
8
|
+
- "src/models/**/*"
|
|
9
|
+
- "src/data/**/*"
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# Database and Data Layer Guidelines
|
|
13
|
+
|
|
14
|
+
Technology-agnostic patterns for secure, maintainable data access.
|
|
15
|
+
|
|
16
|
+
## Core Principles
|
|
17
|
+
|
|
18
|
+
### Design First
|
|
19
|
+
- **Schema design** - Think through relationships and constraints
|
|
20
|
+
- **Normalization** - Minimize data duplication and anomalies
|
|
21
|
+
- **Scalability** - Design for growth and performance
|
|
22
|
+
- **Consistency** - ACID transactions when needed (SQL) or eventual consistency patterns (NoSQL)
|
|
23
|
+
- **Security** - Access controls at database and application level
|
|
24
|
+
|
|
25
|
+
### Access Patterns
|
|
26
|
+
- **ORM/ODM only** - Use abstraction layer, never raw SQL/queries
|
|
27
|
+
- **Parameterized queries** - Prevent injection attacks
|
|
28
|
+
- **Query optimization** - Avoid N+1 problems and slow queries
|
|
29
|
+
- **Connection pooling** - Manage resources efficiently
|
|
30
|
+
- **Error handling** - Graceful degradation when database unavailable
|
|
31
|
+
|
|
32
|
+
### Migration Strategy
|
|
33
|
+
- **Version control** - All schema changes tracked
|
|
34
|
+
- **Idempotent migrations** - Can run multiple times safely
|
|
35
|
+
- **Reversible migrations** - Can rollback if needed
|
|
36
|
+
- **Testing** - Test migrations in all environments
|
|
37
|
+
- **Documentation** - Document significant schema decisions
|
|
38
|
+
|
|
39
|
+
## Database Strategy Options
|
|
40
|
+
|
|
41
|
+
### SQL Databases (PostgreSQL, MySQL, SQLite, SQL Server)
|
|
42
|
+
|
|
43
|
+
Characteristics:
|
|
44
|
+
- Structured schema with defined tables and relationships
|
|
45
|
+
- ACID transactions for data consistency
|
|
46
|
+
- Powerful querying with SQL
|
|
47
|
+
- Good for relational data
|
|
48
|
+
- Mature tooling and libraries
|
|
49
|
+
|
|
50
|
+
Tools:
|
|
51
|
+
- ORM: Sequelize (Node.js), SQLAlchemy (Python), Hibernate (Java), Entity Framework (.NET)
|
|
52
|
+
- Query Builder: Knex.js (Node.js), SqlAlchemy Core (Python), QueryDSL (Java)
|
|
53
|
+
- Migration tools: Prisma Migrate, Flyway, Liquibase, Alembic
|
|
54
|
+
|
|
55
|
+
Best for:
|
|
56
|
+
- Complex relationships between entities
|
|
57
|
+
- Strong consistency requirements
|
|
58
|
+
- Transactional integrity
|
|
59
|
+
- Complex queries
|
|
60
|
+
- Team comfortable with SQL
|
|
61
|
+
|
|
62
|
+
### NoSQL Databases (MongoDB, DynamoDB, CouchDB, Firestore)
|
|
63
|
+
|
|
64
|
+
Characteristics:
|
|
65
|
+
- Flexible/schemaless documents
|
|
66
|
+
- Eventual consistency
|
|
67
|
+
- Good horizontal scaling
|
|
68
|
+
- Fast for simple lookups
|
|
69
|
+
- Dev-friendly for rapid iteration
|
|
70
|
+
|
|
71
|
+
Tools:
|
|
72
|
+
- ODM: Mongoose (MongoDB), AWS SDK (DynamoDB), Firebase SDK (Firestore)
|
|
73
|
+
- Schema validation: JSONSchema, Zod, Joi
|
|
74
|
+
|
|
75
|
+
Best for:
|
|
76
|
+
- Flexible/evolving data structures
|
|
77
|
+
- Horizontal scaling
|
|
78
|
+
- Fast document lookups
|
|
79
|
+
- Real-time updates
|
|
80
|
+
- Prototyping and MVPs
|
|
81
|
+
|
|
82
|
+
### Managed Database Services (Supabase, Firebase, AWS RDS)
|
|
83
|
+
|
|
84
|
+
Characteristics:
|
|
85
|
+
- Hosted by cloud provider
|
|
86
|
+
- Automatic backups and scaling
|
|
87
|
+
- Built-in authentication
|
|
88
|
+
- Real-time subscriptions (some)
|
|
89
|
+
- Reduced infrastructure complexity
|
|
90
|
+
|
|
91
|
+
Best for:
|
|
92
|
+
- Rapid prototyping
|
|
93
|
+
- Teams without DevOps expertise
|
|
94
|
+
- Projects needing built-in features
|
|
95
|
+
- Compliance-heavy requirements
|
|
96
|
+
- Teams preferring managed services
|
|
97
|
+
|
|
98
|
+
## Schema Design Patterns
|
|
99
|
+
|
|
100
|
+
### Core Tables/Collections
|
|
101
|
+
|
|
102
|
+
Base entities:
|
|
103
|
+
- **Users**: Authentication and identity data
|
|
104
|
+
- **Roles**: Permission groups
|
|
105
|
+
- **Audit logs**: Track changes for compliance
|
|
106
|
+
|
|
107
|
+
Related data:
|
|
108
|
+
- Define relationships clearly
|
|
109
|
+
- Use appropriate constraints (foreign keys, unique constraints)
|
|
110
|
+
- Add indexes for common queries
|
|
111
|
+
- Document connection between tables
|
|
112
|
+
|
|
113
|
+
### Timestamps
|
|
114
|
+
|
|
115
|
+
Include timestamps:
|
|
116
|
+
- **Created at**: When record created (immutable)
|
|
117
|
+
- **Updated at**: When record last modified
|
|
118
|
+
- **Deleted at**: For soft deletes (when applicable)
|
|
119
|
+
|
|
120
|
+
### IDs and Keys
|
|
121
|
+
|
|
122
|
+
ID strategy:
|
|
123
|
+
- **Primary key**: Uniquely identifies record
|
|
124
|
+
- **Foreign key**: References another table (relational)
|
|
125
|
+
- **Unique constraints**: Prevent duplicates
|
|
126
|
+
- **Non-sequential IDs**: Use UUID or similar for security
|
|
127
|
+
|
|
128
|
+
### Data Validation
|
|
129
|
+
|
|
130
|
+
At database level:
|
|
131
|
+
- Required fields (NOT NULL)
|
|
132
|
+
- Data type constraints
|
|
133
|
+
- Length constraints (for strings)
|
|
134
|
+
- Enum constraints (for limited values)
|
|
135
|
+
- Range constraints (for numbers)
|
|
136
|
+
|
|
137
|
+
At application level:
|
|
138
|
+
- Validate before insert/update
|
|
139
|
+
- Validate format (email, URL, etc.)
|
|
140
|
+
- Validate business rules
|
|
141
|
+
- Return clear error messages
|
|
142
|
+
|
|
143
|
+
## Query Patterns
|
|
144
|
+
|
|
145
|
+
### Efficient Queries
|
|
146
|
+
|
|
147
|
+
Do:
|
|
148
|
+
- Use WHERE clauses to filter
|
|
149
|
+
- Use indexes on filtered fields
|
|
150
|
+
- Use SELECT specific columns
|
|
151
|
+
- Use LIMIT for pagination
|
|
152
|
+
- Use JOIN for related data
|
|
153
|
+
- Use aggregation functions
|
|
154
|
+
- Batch operations when possible
|
|
155
|
+
|
|
156
|
+
Don't:
|
|
157
|
+
- SELECT * (select all columns)
|
|
158
|
+
- N+1 queries (query in loop)
|
|
159
|
+
- Unindexed large scans
|
|
160
|
+
- Complex JOINs without indexes
|
|
161
|
+
- Missing WHERE clause
|
|
162
|
+
- Fetching unused data
|
|
163
|
+
|
|
164
|
+
### Pagination
|
|
165
|
+
|
|
166
|
+
Pagination pattern:
|
|
167
|
+
1. Accept limit and offset parameters
|
|
168
|
+
2. Validate reasonable limit (max 100-1000)
|
|
169
|
+
3. Query with LIMIT and OFFSET
|
|
170
|
+
4. Return total count optionally
|
|
171
|
+
5. Include nextPage/previousPage links
|
|
172
|
+
|
|
173
|
+
### Relationships
|
|
174
|
+
|
|
175
|
+
Query related data:
|
|
176
|
+
- One-to-many: JOIN or separate queries
|
|
177
|
+
- Many-to-many: Junction table with JOINs
|
|
178
|
+
- One-to-one: JOIN or embed in document
|
|
179
|
+
- Hierarchical: Recursive queries or denormalization
|
|
180
|
+
|
|
181
|
+
## Security
|
|
182
|
+
|
|
183
|
+
### Access Control
|
|
184
|
+
|
|
185
|
+
Implement at database:
|
|
186
|
+
- Row-level security (RLS in PostgreSQL, Firestore)
|
|
187
|
+
- Column-level access restrictions
|
|
188
|
+
- Database-level user permissions
|
|
189
|
+
|
|
190
|
+
Implement at application:
|
|
191
|
+
- Verify user owns resource before returning
|
|
192
|
+
- Check permissions before allowing modifications
|
|
193
|
+
- Log access to sensitive data
|
|
194
|
+
- Rate limit database operations
|
|
195
|
+
|
|
196
|
+
### Sensitive Data
|
|
197
|
+
|
|
198
|
+
Protect sensitive data:
|
|
199
|
+
- Hash passwords (never store plaintext)
|
|
200
|
+
- Encrypt PII at rest
|
|
201
|
+
- Never log sensitive data
|
|
202
|
+
- Mask sensitive data in backups
|
|
203
|
+
- Use parameterized queries to prevent injection
|
|
204
|
+
|
|
205
|
+
### Backup & Recovery
|
|
206
|
+
|
|
207
|
+
Backup strategy:
|
|
208
|
+
- Automated daily backups
|
|
209
|
+
- Test recovery procedures
|
|
210
|
+
- Retain backups for compliance period
|
|
211
|
+
- Encrypt backups at rest
|
|
212
|
+
- Store backups separately from primary database
|
|
213
|
+
|
|
214
|
+
## Performance
|
|
215
|
+
|
|
216
|
+
### Indexing
|
|
217
|
+
|
|
218
|
+
Index columns that are:
|
|
219
|
+
- Frequently filtered (WHERE clause)
|
|
220
|
+
- Used in JOINs
|
|
221
|
+
- Used in ORDER BY
|
|
222
|
+
- Used in DISTINCT
|
|
223
|
+
|
|
224
|
+
Don't over-index:
|
|
225
|
+
- Too many indexes slow down writes
|
|
226
|
+
- Indexes require storage space
|
|
227
|
+
- Maintain only used indexes
|
|
228
|
+
|
|
229
|
+
### Connection Management
|
|
230
|
+
|
|
231
|
+
Connection pooling:
|
|
232
|
+
- Reuse connections across requests
|
|
233
|
+
- Configure reasonable pool size (10-20 connections)
|
|
234
|
+
- Set idle timeout
|
|
235
|
+
- Monitor connection usage
|
|
236
|
+
- Alert on connection exhaustion
|
|
237
|
+
|
|
238
|
+
### Query Optimization
|
|
239
|
+
|
|
240
|
+
Optimize slow queries:
|
|
241
|
+
1. Profile query execution time
|
|
242
|
+
2. Check execution plan
|
|
243
|
+
3. Add missing indexes
|
|
244
|
+
4. Rewrite queries if needed
|
|
245
|
+
5. Consider denormalization if needed
|
|
246
|
+
6. Verify schema design
|
|
247
|
+
|
|
248
|
+
## Monitoring & Maintenance
|
|
249
|
+
|
|
250
|
+
### Health Checks
|
|
251
|
+
|
|
252
|
+
Monitor:
|
|
253
|
+
- Database connectivity
|
|
254
|
+
- Query performance (slow query logs)
|
|
255
|
+
- Connection pool status
|
|
256
|
+
- Storage space usage
|
|
257
|
+
- Backup completion
|
|
258
|
+
- Replication lag (if applicable)
|
|
259
|
+
|
|
260
|
+
### Database Maintenance
|
|
261
|
+
|
|
262
|
+
Regular tasks:
|
|
263
|
+
- Update statistics/analyze
|
|
264
|
+
- Vacuum/compact (depending on DB)
|
|
265
|
+
- Rebuild indexes if needed
|
|
266
|
+
- Clean up old audit logs
|
|
267
|
+
- Review and optimize slow queries
|
|
268
|
+
|
|
269
|
+
## Testing
|
|
270
|
+
|
|
271
|
+
### Test Data
|
|
272
|
+
|
|
273
|
+
Setup:
|
|
274
|
+
- Use separate test database
|
|
275
|
+
- Reset data before each test
|
|
276
|
+
- Use factories for consistent test data
|
|
277
|
+
- Seed with realistic data
|
|
278
|
+
|
|
279
|
+
Testing patterns:
|
|
280
|
+
- Test CRUD operations
|
|
281
|
+
- Test relationships
|
|
282
|
+
- Test constraints
|
|
283
|
+
- Test error cases
|
|
284
|
+
- Test performance
|
|
285
|
+
|
|
286
|
+
### Test Examples
|
|
287
|
+
|
|
288
|
+
Test: Create and retrieve user
|
|
289
|
+
1. Create user with valid data
|
|
290
|
+
2. Retrieve by ID
|
|
291
|
+
3. Verify data matches
|
|
292
|
+
|
|
293
|
+
Test: Enforce unique constraint
|
|
294
|
+
1. Create user with email
|
|
295
|
+
2. Attempt to create another with same email
|
|
296
|
+
3. Verify error is returned
|
|
297
|
+
|
|
298
|
+
Test: Cascade delete
|
|
299
|
+
1. Create parent and child records
|
|
300
|
+
2. Delete parent
|
|
301
|
+
3. Verify child is deleted
|
|
302
|
+
|
|
303
|
+
---
|
|
304
|
+
|
|
305
|
+
Remember: Good database design prevents bugs, improves performance, and makes your application maintainable.
|