{"success":true,"name":"never-return-raw-db-documents-pick-the-fields-every-response","description":"An API endpoint responds with the object that came straight from the database","category":"ai-brimind","path":"ai-brimind/never-return-raw-db-documents-pick-the-fields-every-response/SKILL.md","file":"SKILL.md","content":"---\nname: never-return-raw-db-documents-pick-the-fields-every-response\ndescription: \"An API endpoint responds with the object that came straight from the database\"\n---\n\n# Never Return Raw DB Documents: Pick the Fields Every Response Sends On Purpose\n\n## When to use\nYou are writing an endpoint that fetches a record and sends it to the client as-is — res.json(user) with the whole Mongoose document — without an explicit decision about which fields the caller should actually receive.\n\n## Example request\nres.json(user) with the full document → the password hash, reset tokens, and internal flags ride along into the browser's network tab; a projection or a whitelist of fields is the missing gate. The admin panel needed 'internalNotes' so it was added to the user schema → every PUBLIC endpoint returning users now leaks it automatically; raw-document responses make every schema addition an accidental API change. Frontend only needs id, name, and avatar for a list of 200 members → shipping full documents makes the payload ten times heavier for nothing; select('name avatar') at the query is both the privacy and the performance fix. A field must NEVER leave the server (password hash) → belt and suspenders: select:false in the schema hides it by default, and explicit response shaping catches the rest; defense in one layer only is one mistake away from a leak.\n\n## Instructions\nAct as a backend developer who treats every API response as a package crossing a border — nothing gets in the box because it happened to be lying nearby; every field is either declared on purpose or it stays home — because res.json(document) is not 'returning the user', it is 'returning whatever the schema contains today and will contain after every future edit, forever'. For each endpoint response, do this: (1) Write the response shape first, from the CONSUMER's needs: what does this screen actually require — id, name, status? That short list IS the contract; everything else is contraband. If you cannot list the fields, look at the frontend code that will consume it — the answer is there. (2) Enforce the shape with the mechanisms the stack already has, as close to the query as possible: Mongoose select('name email status') or projection objects so unneeded fields never even leave the database (privacy AND payload size in one move), plus schema-level select:false on radioactive fields (password hash, tokens) so forgetting a projection somewhere does not leak them — that default is your safety net, not your only defense. (3) For shapes that repeat across endpoints, build them once: a small toPublicUser(doc)-style mapper function per entity, used everywhere that entity goes out — one place to audit, one place to change, instead of twelve endpoints each hand-picking slightly different fields (which is how 'why does THIS route leak email?' happens). (4) Distinguish audiences explicitly when they exist: the owner of a profile may receive more fields than a stranger viewing it, and an admin more still — name these variants (publicView, ownerView) rather than one blurry response that always sends the maximum. (5) Verify from the outside, where it counts: open the network tab or curl the endpoint and READ the actual JSON — every field visible there is public information now, whatever the UI displays. Only mark the endpoint done once you can point at the explicit field selection between the database and res.json, and answer for any schema field: 'does this leave the server, on which endpoints, and who decided that?' — if the honest answer is 'whatever find() returned', the API is not exposing data, the schema is, and nobody is steering.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #221\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}