-
Notifications
You must be signed in to change notification settings - Fork 68
feat: add unique field constraints support with validation and model … #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yash-pouranik
merged 8 commits into
geturbackend:main
from
Special7ka:feat/unique-field-constraints-v2
Apr 2, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
32ca4a6
feat: add unique field constraints support with validation and model …
Special7ka d02432e
fix(unique): address review feedback for unique constraints flow
Special7ka 0b91b28
Merge remote-tracking branch 'upstream/main' into feat/unique-field-c…
Special7ka 15b4106
chore(pr): remove unrelated lockfile and dashboard build changes
Special7ka a6e285c
fix(unique): address review feedback for schema creation and index flow
Special7ka bd5fdfd
fix(indexes): normalize unique field keys and remove unsafe index rol…
Special7ka e7b59df
fix(schema): reject nested unique fields and normalize nested keys
Special7ka 136fcf8
Merge branch 'main' into feat/unique-field-constraints-v2
yash-pouranik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2,008 changes: 1,051 additions & 957 deletions
2,008
apps/dashboard-api/src/controllers/project.controller.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,180 +1,250 @@ | ||
| const { sanitize } = require("@urbackend/common"); | ||
| const mongoose = require('mongoose'); | ||
| const mongoose = require("mongoose"); | ||
| const { Project } = require("@urbackend/common"); | ||
| const { getConnection } = require("@urbackend/common"); | ||
| const { getCompiledModel } = require("@urbackend/common"); | ||
| const {QueryEngine} = require("@urbackend/common"); | ||
| const { QueryEngine } = require("@urbackend/common"); | ||
| const { validateData, validateUpdateData } = require("@urbackend/common"); | ||
|
|
||
| // Validate MongoDB ObjectId | ||
| const isValidId = (id) => mongoose.Types.ObjectId.isValid(id); | ||
|
|
||
| const isDuplicateKeyError = (err) => { | ||
| return err && err.code === 11000; | ||
| }; | ||
|
|
||
| // INSERT DATA | ||
| module.exports.insertData = async (req, res) => { | ||
| try { | ||
| console.time("insert data") | ||
| const { collectionName } = req.params; | ||
| const project = req.project; | ||
|
|
||
| const collectionConfig = project.collections.find(c => c.name === collectionName); | ||
| if (!collectionConfig) return res.status(404).json({ error: "Collection not found" }); | ||
|
|
||
| const schemaRules = collectionConfig.model; | ||
| const incomingData = req.body; | ||
|
|
||
| // Recursive validation for all field types | ||
| const { error, cleanData } = validateData(incomingData, schemaRules); | ||
| if (error) return res.status(400).json({ error }); | ||
|
|
||
| const safeData = sanitize(cleanData); | ||
|
|
||
| let docSize = 0; | ||
| if (!project.resources.db.isExternal) { | ||
| docSize = Buffer.byteLength(JSON.stringify(safeData)); | ||
| if ((project.databaseUsed || 0) + docSize > project.databaseLimit) { | ||
| return res.status(403).json({ error: "Database limit exceeded." }); | ||
| } | ||
| } | ||
|
|
||
| const connection = await getConnection(project._id); | ||
| const Model = getCompiledModel(connection, collectionConfig, project._id, project.resources.db.isExternal); | ||
|
|
||
| const result = await Model.create(safeData); | ||
|
|
||
| if (!project.resources.db.isExternal) { | ||
| await Project.updateOne( | ||
| { _id: project._id }, | ||
| { $inc: { databaseUsed: docSize } } | ||
| ); | ||
| } | ||
|
|
||
| console.timeEnd("insert data") | ||
| res.status(201).json(result); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| try { | ||
| console.time("insert data"); | ||
| const { collectionName } = req.params; | ||
| const project = req.project; | ||
|
|
||
| const collectionConfig = project.collections.find( | ||
| (c) => c.name === collectionName, | ||
| ); | ||
| if (!collectionConfig) | ||
| return res.status(404).json({ error: "Collection not found" }); | ||
|
|
||
| const schemaRules = collectionConfig.model; | ||
| const incomingData = req.body; | ||
|
|
||
| // Recursive validation for all field types | ||
| const { error, cleanData } = validateData(incomingData, schemaRules); | ||
| if (error) return res.status(400).json({ error }); | ||
|
|
||
| const safeData = sanitize(cleanData); | ||
|
|
||
| let docSize = 0; | ||
| if (!project.resources.db.isExternal) { | ||
| docSize = Buffer.byteLength(JSON.stringify(safeData)); | ||
| if ((project.databaseUsed || 0) + docSize > project.databaseLimit) { | ||
| return res.status(403).json({ error: "Database limit exceeded." }); | ||
| } | ||
| } | ||
|
|
||
| const connection = await getConnection(project._id); | ||
| const Model = getCompiledModel( | ||
| connection, | ||
| collectionConfig, | ||
| project._id, | ||
| project.resources.db.isExternal, | ||
| ); | ||
|
|
||
| const result = await Model.create(safeData); | ||
|
|
||
| if (!project.resources.db.isExternal) { | ||
| await Project.updateOne( | ||
| { _id: project._id }, | ||
| { $inc: { databaseUsed: docSize } }, | ||
| ); | ||
| } | ||
|
|
||
| console.timeEnd("insert data"); | ||
| res.status(201).json(result); | ||
| } catch (err) { | ||
| console.error(err); | ||
|
|
||
| if (isDuplicateKeyError(err)) { | ||
| return res.status(409).json({ | ||
| error: "Duplicate value violates unique constraint.", | ||
| details: err.message, | ||
| }); | ||
| } | ||
|
|
||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| }; | ||
|
|
||
| // GET ALL DATA | ||
| module.exports.getAllData = async (req, res) => { | ||
| try { | ||
| console.time("getall") | ||
| const { collectionName } = req.params; | ||
| const project = req.project; | ||
|
|
||
| const collectionConfig = project.collections.find(c => c.name === collectionName); | ||
| if (!collectionConfig) return res.status(404).json({ error: "Collection not found" }); | ||
|
|
||
| const connection = await getConnection(project._id); | ||
| const Model = getCompiledModel(connection, collectionConfig, project._id, project.resources.db.isExternal); | ||
|
|
||
| const features = new QueryEngine(Model.find(), req.query) | ||
| .filter() | ||
| .sort() | ||
| .paginate(); | ||
|
|
||
| const data = await features.query.lean(); | ||
| console.timeEnd("getall") | ||
| res.json(data); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| try { | ||
| console.time("getall"); | ||
| const { collectionName } = req.params; | ||
| const project = req.project; | ||
|
|
||
| const collectionConfig = project.collections.find( | ||
| (c) => c.name === collectionName, | ||
| ); | ||
| if (!collectionConfig) | ||
| return res.status(404).json({ error: "Collection not found" }); | ||
|
|
||
| const connection = await getConnection(project._id); | ||
| const Model = getCompiledModel( | ||
| connection, | ||
| collectionConfig, | ||
| project._id, | ||
| project.resources.db.isExternal, | ||
| ); | ||
|
|
||
| const features = new QueryEngine(Model.find(), req.query) | ||
| .filter() | ||
| .sort() | ||
| .paginate(); | ||
|
|
||
| const data = await features.query.lean(); | ||
| console.timeEnd("getall"); | ||
| res.json(data); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| }; | ||
|
|
||
| // GET SINGLE DOC | ||
| module.exports.getSingleDoc = async (req, res) => { | ||
| try { | ||
| const { collectionName, id } = req.params; | ||
| const project = req.project; | ||
|
|
||
| // ensure valid mongose objct id | ||
| if (!isValidId(id)) return res.status(400).json({ error: "Invalid ID format." }); | ||
|
|
||
| const collectionConfig = project.collections.find(c => c.name === collectionName); | ||
| if (!collectionConfig) return res.status(404).json({ error: "Collection not found" }); | ||
|
|
||
| const connection = await getConnection(project._id); | ||
| const Model = getCompiledModel(connection, collectionConfig, project._id, project.resources.db.isExternal); | ||
|
|
||
| const doc = await Model.findById(id).lean(); | ||
| if (!doc) return res.status(404).json({ error: "Document not found." }); | ||
|
|
||
| res.json(doc); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| try { | ||
| const { collectionName, id } = req.params; | ||
| const project = req.project; | ||
|
|
||
| // ensure valid mongose objct id | ||
| if (!isValidId(id)) | ||
| return res.status(400).json({ error: "Invalid ID format." }); | ||
|
|
||
| const collectionConfig = project.collections.find( | ||
| (c) => c.name === collectionName, | ||
| ); | ||
| if (!collectionConfig) | ||
| return res.status(404).json({ error: "Collection not found" }); | ||
|
|
||
| const connection = await getConnection(project._id); | ||
| const Model = getCompiledModel( | ||
| connection, | ||
| collectionConfig, | ||
| project._id, | ||
| project.resources.db.isExternal, | ||
| ); | ||
|
|
||
| const doc = await Model.findById(id).lean(); | ||
| if (!doc) return res.status(404).json({ error: "Document not found." }); | ||
|
|
||
| res.json(doc); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| }; | ||
|
|
||
| // UPDATE DATA | ||
| module.exports.updateSingleData = async (req, res) => { | ||
| try { | ||
| const { collectionName, id } = req.params; | ||
| const project = req.project; | ||
| const incomingData = req.body; | ||
|
|
||
| if (!isValidId(id)) return res.status(400).json({ error: "Invalid ID format." }); | ||
|
|
||
| const collectionConfig = project.collections.find(c => c.name === collectionName); | ||
| if (!collectionConfig) return res.status(404).json({ error: "Collection not found" }); | ||
|
|
||
| const connection = await getConnection(project._id); | ||
| const Model = getCompiledModel(connection, collectionConfig, project._id, project.resources.db.isExternal); | ||
|
|
||
| // Recursive validation for all field types | ||
| const schemaRules = collectionConfig.model; | ||
| const { error: validationError, updateData } = validateUpdateData(incomingData, schemaRules); | ||
| if (validationError) return res.status(400).json({ error: validationError }); | ||
|
|
||
| const sanitizedData = sanitize(updateData); | ||
|
|
||
| const result = await Model.findByIdAndUpdate(id, { $set: sanitizedData }, { new: true }).lean(); | ||
| if (!result) return res.status(404).json({ error: "Document not found." }); | ||
|
|
||
| res.json({ message: "Updated", data: result }); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| try { | ||
| const { collectionName, id } = req.params; | ||
| const project = req.project; | ||
| const incomingData = req.body; | ||
|
|
||
| if (!isValidId(id)) | ||
| return res.status(400).json({ error: "Invalid ID format." }); | ||
|
|
||
| const collectionConfig = project.collections.find( | ||
| (c) => c.name === collectionName, | ||
| ); | ||
| if (!collectionConfig) | ||
| return res.status(404).json({ error: "Collection not found" }); | ||
|
|
||
| const connection = await getConnection(project._id); | ||
| const Model = getCompiledModel( | ||
| connection, | ||
| collectionConfig, | ||
| project._id, | ||
| project.resources.db.isExternal, | ||
| ); | ||
|
|
||
| // Recursive validation for all field types | ||
| const schemaRules = collectionConfig.model; | ||
| const { error: validationError, updateData } = validateUpdateData( | ||
| incomingData, | ||
| schemaRules, | ||
| ); | ||
| if (validationError) | ||
| return res.status(400).json({ error: validationError }); | ||
|
|
||
| const sanitizedData = sanitize(updateData); | ||
|
|
||
| const result = await Model.findByIdAndUpdate( | ||
| id, | ||
| { $set: sanitizedData }, | ||
| { new: true, runValidators: true }, | ||
| ).lean(); | ||
|
|
||
| if (!result) return res.status(404).json({ error: "Document not found." }); | ||
|
|
||
| res.json({ message: "Updated", data: result }); | ||
| } catch (err) { | ||
| console.error(err); | ||
|
|
||
| if (isDuplicateKeyError(err)) { | ||
| return res.status(409).json({ | ||
| error: "Duplicate value violates unique constraint.", | ||
| details: err.message, | ||
| }); | ||
| } | ||
|
|
||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| }; | ||
|
|
||
| // DELETE DATA | ||
| module.exports.deleteSingleDoc = async (req, res) => { | ||
| try { | ||
| const { collectionName, id } = req.params; | ||
| const project = req.project; | ||
|
|
||
| if (!isValidId(id)) return res.status(400).json({ error: "Invalid ID format." }); | ||
|
|
||
| const collectionConfig = project.collections.find(c => c.name === collectionName); | ||
| if (!collectionConfig) return res.status(404).json({ error: "Collection not found" }); | ||
|
|
||
| const connection = await getConnection(project._id); | ||
| const Model = getCompiledModel(connection, collectionConfig, project._id, project.resources.db.isExternal); | ||
|
|
||
| const docToDelete = await Model.findById(id); | ||
| if (!docToDelete) return res.status(404).json({ error: "Document not found." }); | ||
|
|
||
| let docSize = 0; | ||
| if (!project.resources.db.isExternal) { | ||
| docSize = Buffer.byteLength(JSON.stringify(docToDelete)); | ||
| } | ||
|
|
||
| await Model.deleteOne({ _id: id }); | ||
| try { | ||
| const { collectionName, id } = req.params; | ||
| const project = req.project; | ||
|
|
||
| if (!isValidId(id)) | ||
| return res.status(400).json({ error: "Invalid ID format." }); | ||
|
|
||
| const collectionConfig = project.collections.find( | ||
| (c) => c.name === collectionName, | ||
| ); | ||
| if (!collectionConfig) | ||
| return res.status(404).json({ error: "Collection not found" }); | ||
|
|
||
| const connection = await getConnection(project._id); | ||
| const Model = getCompiledModel( | ||
| connection, | ||
| collectionConfig, | ||
| project._id, | ||
| project.resources.db.isExternal, | ||
| ); | ||
|
|
||
| const docToDelete = await Model.findById(id); | ||
| if (!docToDelete) | ||
| return res.status(404).json({ error: "Document not found." }); | ||
|
|
||
| let docSize = 0; | ||
| if (!project.resources.db.isExternal) { | ||
| docSize = Buffer.byteLength(JSON.stringify(docToDelete)); | ||
| } | ||
|
|
||
| if (!project.resources.db.isExternal) { | ||
| let databaseUsed = Math.max(0, (project.databaseUsed || 0) - docSize); | ||
| await Project.updateOne( | ||
| { _id: project._id }, | ||
| { $set: { databaseUsed } } | ||
| ); | ||
| } | ||
| await Model.deleteOne({ _id: id }); | ||
|
|
||
| res.json({ message: "Document deleted", id }); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| if (!project.resources.db.isExternal) { | ||
| let databaseUsed = Math.max(0, (project.databaseUsed || 0) - docSize); | ||
| await Project.updateOne({ _id: project._id }, { $set: { databaseUsed } }); | ||
| } | ||
| }; | ||
|
|
||
| res.json({ message: "Document deleted", id }); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.