-
Notifications
You must be signed in to change notification settings - Fork 68
chore: added console.errro #30
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,6 @@ module.exports.insertData = async (req, res) => { | |
| } | ||
| } | ||
|
|
||
| // Prevnt NoSQL injection | ||
| const safeData = sanitize(cleanData); | ||
|
|
||
| let docSize = 0; | ||
|
|
@@ -66,6 +65,7 @@ module.exports.insertData = async (req, res) => { | |
| console.timeEnd("insert data") | ||
| res.status(201).json(result); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| }; | ||
|
|
@@ -92,6 +92,7 @@ module.exports.getAllData = async (req, res) => { | |
| console.timeEnd("getall") | ||
| res.json(data); | ||
| } catch (err) { | ||
| console.error(err); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| }; | ||
|
|
@@ -116,6 +117,7 @@ module.exports.getSingleDoc = async (req, res) => { | |
|
|
||
| res.json(doc); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| }; | ||
|
|
@@ -135,12 +137,12 @@ module.exports.updateSingleData = async (req, res) => { | |
| const connection = await getConnection(project._id); | ||
| const Model = getCompiledModel(connection, collectionConfig, project._id, project.resources.db.isExternal); | ||
|
|
||
| // Strict Schema Validation | ||
| // Schma Validation | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix typo in validation comment. Line 140 says 🤖 Prompt for AI Agents |
||
| const schemaRules = collectionConfig.model; | ||
| const updateData = {}; | ||
| for (const key in incomingData) { | ||
| const fieldRule = schemaRules.find(f => f.key === key); | ||
| if (!fieldRule) continue; // Unknown fields ko ignore karo | ||
| if (!fieldRule) continue; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| const value = incomingData[key]; | ||
| if (fieldRule.type === 'Number' && typeof value !== 'number') return res.status(400).json({ error: `Field '${key}' must be a Number.` }); | ||
|
|
@@ -157,6 +159,7 @@ module.exports.updateSingleData = async (req, res) => { | |
|
|
||
| res.json({ message: "Updated", data: result }); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| }; | ||
|
|
@@ -195,6 +198,7 @@ module.exports.deleteSingleDoc = async (req, res) => { | |
|
|
||
| res.json({ message: "Document deleted", id }); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,17 +19,16 @@ module.exports.checkSchema = async (req, res) => { | |
|
|
||
| res.status(200).json({ message: "Schema exists", collection: collectionConfig }); | ||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| }; | ||
|
|
||
| module.exports.createSchema = async (req, res) => { | ||
| try { | ||
| const { name, fields } = createSchemaApiKeySchema.parse(req.body); | ||
| let project = req.project; // From verifyApiKey | ||
| let project = req.project; | ||
|
|
||
| // Need the full document to call .save(), req.project from cache might be a lean object. | ||
| // It's safer to fetch the mongoose model instance to modify it. | ||
| const projectId = project._id; | ||
| const fullProject = await Project.findById(projectId); | ||
|
|
||
|
|
@@ -40,9 +39,7 @@ module.exports.createSchema = async (req, res) => { | |
|
|
||
| if (!fullProject.jwtSecret) fullProject.jwtSecret = uuidv4(); | ||
|
|
||
| // Convert the incoming API payload (name, type, required) into the UrBackend schema format (key, type, required) | ||
| const transformedFields = (fields || []).map(f => { | ||
| // Capitalize first letter of type mapping "string" -> "String" requirement | ||
| const mappedType = f.type.charAt(0).toUpperCase() + f.type.slice(1).toLowerCase(); | ||
| return { | ||
| key: f.name, | ||
|
|
@@ -57,8 +54,7 @@ module.exports.createSchema = async (req, res) => { | |
| // Clear redis cache | ||
| await deleteProjectById(projectId.toString()); | ||
| await setProjectById(projectId.toString(), fullProject); | ||
| await deleteProjectByApiKeyCache(fullProject.apiKey); // In case it was cached by hashed key | ||
| // NOTE: req.hashedApiKey is available from middleware if we strictly want that. | ||
| await deleteProjectByApiKeyCache(fullProject.apiKey); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "== Check Project model for apiKey requirement/default =="
fd 'Project\.(js|ts)$' | while read -r f; do
echo "--- $f ---"
rg -n -C3 'apiKey|required|default' "$f" || true
done
echo
echo "== Inspect deleteProjectByApiKeyCache null/falsy handling =="
fd 'redisCaching\.(js|ts)$' | while read -r f; do
echo "--- $f ---"
rg -n -C4 'deleteProjectByApiKeyCache|apiKey|toString\(' "$f" || true
doneRepository: yash-pouranik/urBackend Length of output: 2373 🏁 Script executed: # Find schema.controller.js and read context around line 57
fd 'schema\.controller\.js$' | head -1 | xargs cat -n | sed -n '40,75p'Repository: yash-pouranik/urBackend Length of output: 1535 Add a guard before invalidating API-key cache. Line 57 passes 🤖 Prompt for AI Agents |
||
| if (req.hashedApiKey) { | ||
| await deleteProjectByApiKeyCache(req.hashedApiKey); | ||
| } | ||
|
|
@@ -70,6 +66,7 @@ module.exports.createSchema = async (req, res) => { | |
| res.status(201).json({ message: "Schema created successfully", project: projectObj }); | ||
| } catch (err) { | ||
| if (err instanceof z.ZodError) return res.status(400).json({ error: err.errors }); | ||
| console.error(err); | ||
| res.status(500).json({ error: err.message }); | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While
console.erroris useful, for production environments it's recommended to use a structured logger (e.g., Winston, Pino). Structured logging provides more context with log levels, can output in machine-readable formats like JSON for easier analysis with log management tools, and offers more flexible configuration.