You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, when users create collection schemas in urBackend, there's no way to mark fields as unique. This means users can't enforce critical constraints like:
Unique email addresses
Unique usernames
Unique product SKUs
Unique order numbers
This leads to data integrity issues where duplicate values can be inserted, breaking business logic.
Example Problem:
// User creates "users" collection with "email" field// Current behavior: Can insert duplicate emails ❌{email: "john@example.com"}{email: "john@example.com"}// This should be rejected but isn't!
This feature will add unique field constraints to the schema builder, with MongoDB unique indexes enforced at the database level.
🎯 Problem Statement
Current Behavior:
Schema builder has field types (String, Number, Boolean, Date, Object, Array)
No validation for unique values
Users can insert duplicate data without errors ❌
User Story:
"As a developer building a user authentication system, I want to ensure email addresses are unique so that each user has a distinct account."
Desired Behavior:
User creates "email" field in schema
Marks it as "unique" via checkbox ✅
MongoDB creates unique index on that field
If duplicate value is inserted, API returns 409 error with clear message
Existing data is validated when adding unique constraint
💡 Proposed Solution
Implement unique field constraints with:
Schema definition updates - add unique: Boolean property to field schema
MongoDB unique indexes - automatically create indexes when schema is saved
Validation on insert/update - return clear errors for duplicates (409 Conflict)
Frontend UI - add "Unique" checkbox in schema builder
Migration handling - check existing data before adding unique constraint
Composite unique constraints (bonus) - multiple fields together unique
✅ Acceptance Criteria
Backend Requirements
Schema Model Update
Add unique property to field definitions in schema
Return user-friendly 409 error with field name and value
Step 5: Frontend - Schema Builder
Add checkbox for "Unique" next to each field
Optionally add "Case insensitive" toggle for String fields
Step 6: Frontend - Error Handling
Display duplicate errors clearly when insert/update fails
Show which field and value caused the duplicate
💡 Implementation Hints
Backend Hints
Creating Unique Index:
// MongoDB collection method to create indexModel.collection.createIndex({fieldName: 1},{unique: true,sparse: true});
Detecting Duplicates (Aggregation):
// Use $group to find duplicate valuesModel.aggregate([{$group: {_id: '$fieldName',count: {$sum: 1}}},{$match: {count: {$gt: 1}}}]);
Catching MongoDB Duplicate Error:
// MongoDB throws error with code 11000 for duplicatesif(error.code===11000){// Extract field from error.keyPattern// Extract value from error.keyValue}
Case Insensitive Unique:
// Use collation with strength: 2{collation: {locale: 'en',strength: 2}}
Frontend Hints
Schema Builder UI:
Add checkbox similar to "Required" checkbox
Show/hide case-insensitive option based on field type (String only)
Displaying Unique Fields:
Add 🔑 icon or badge next to field name
Show in field list/table view
Error Handling:
Check response status code (409 for duplicates)
Extract field name and value from error response
Show toast/alert with clear message
📂 Files You'll Need to Modify
Backend
backend/models/Schema.js - Add unique field to schema
backend/utils/indexManager.js - NEW FILE (create/drop indexes)
backend/controllers/schema.controller.js - Index creation logic
backend/controllers/data.controller.js - Error handling for duplicates
📋 Description
Currently, when users create collection schemas in urBackend, there's no way to mark fields as unique. This means users can't enforce critical constraints like:
This leads to data integrity issues where duplicate values can be inserted, breaking business logic.
Example Problem:
This feature will add unique field constraints to the schema builder, with MongoDB unique indexes enforced at the database level.
🎯 Problem Statement
Current Behavior:
User Story:
Desired Behavior:
💡 Proposed Solution
Implement unique field constraints with:
unique: Booleanproperty to field schema✅ Acceptance Criteria
Backend Requirements
Schema Model Update
uniqueproperty to field definitions in schemaUnique Index Creation
Model.collection.createIndex({ fieldName: 1 }, { unique: true })Index Removal
Model.collection.dropIndex('fieldName_1')Duplicate Value Detection
{ "success": false, "error": "Cannot add unique constraint: 3 duplicate values found for field 'email'" }Insert/Update Validation
{ "success": false, "error": "Value 'john@example.com' already exists for field 'email'", "code": "DUPLICATE_VALUE" }Case Sensitivity Handling
{ locale: 'en', strength: 2 }Composite Unique Constraints (Optional)
firstName + lastNameunique togetherFrontend Requirements
Schema Builder UI Updates
Visual Indicators
Validation Feedback
Case Sensitivity Toggle (Optional)
Error Display
Database & Migration
Index Management
Existing Schema Migration
Testing
Unit Tests
Integration Tests
🛠️ Implementation Guide
High-Level Approach
Step 1: Backend - Schema Model
unique: Booleanfield to your schema definitionuniqueOptionsfor advanced features (case sensitivity, etc.)Step 2: Backend - Index Management
Step 3: Backend - Schema Controller
createIndex()Step 4: Backend - Data Controller
11000)Step 5: Frontend - Schema Builder
Step 6: Frontend - Error Handling
💡 Implementation Hints
Backend Hints
Creating Unique Index:
Detecting Duplicates (Aggregation):
Catching MongoDB Duplicate Error:
Case Insensitive Unique:
Frontend Hints
Schema Builder UI:
Displaying Unique Fields:
Error Handling:
📂 Files You'll Need to Modify
Backend
backend/models/Schema.js- Add unique field to schemabackend/utils/indexManager.js- NEW FILE (create/drop indexes)backend/controllers/schema.controller.js- Index creation logicbackend/controllers/data.controller.js- Error handling for duplicatesFrontend
frontend/src/components/SchemaBuilder.jsx- Add unique checkboxfrontend/src/components/SchemaViewer.jsx- Display unique badgefrontend/src/pages/Database.jsx- Handle duplicate errors🔍 Key Concepts to Research
fieldName_1📊 Testing Checklist
Manual Testing
Create Unique Field
{ email: "test@example.com" }Case Insensitive Unique
{ email: "Test@Example.com" }{ email: "test@example.com" }→ should failAdd Unique to Existing Schema
Remove Unique Constraint
Null/Empty Values
Update with Duplicate
{ email: "a@example.com" }{ email: "b@example.com" }{ email: "a@example.com" }→ should failAutomated Tests
🎓 Learning Outcomes
By completing this issue, you will learn:
🏷️ Labels
enhancementintermediate✅backendfrontenddatabaseschemahelp wanted📚 Resources
Existing Code to Reference:
backend/models/Schema.jsbackend/controllers/schema.controller.jsbackend/controllers/data.controller.jsbackend/utils/injectModel.jsfrontend/src/components/SchemaBuilder.jsxMongoDB Documentation:
Similar Patterns:
💡 Edge Cases to Handle
address.zipCode)🚀 Estimated Time
Time to complete: 4-5 hours (for intermediate developer)
Breakdown:
👥 Assignee
Looking for an intermediate contributor who has:
Difficulty Level: ⭐⭐⭐ Intermediate (requires database knowledge + full-stack)
💬 Implementation Tips
🚧 Common Pitfalls to Avoid
Ready to add data integrity? 🔑 Drop a comment and let's make schemas powerful!