Athena — roomy-mobile/archive/changes/2026-01-05-add-roomy-firestore-mcp/tasks/005-firestore-crud-layer.md


status: done

Task: Firestore CRUD helpers

End Goal

A CRUD layer with single-doc operations that MCP tools will call.

Currently

Firestore env works, datamodel entities exist, serialization works.

Should

  • Build entity collection paths from templates
  • Single doc operations: list, get, create, update, delete, upsert
  • Handle doc ID generation and timestamp fields

Constraints

  • [ ] Throw if doc exists on create
  • [ ] Throw if doc missing on update (unless upsert)
  • [ ] Auto-ensure createdAt/updatedAt on creates
  • [ ] Auto-ensure updatedAt on updates

Acceptance Criteria

  • [ ] CRUD functions compile and behave correctly with emulator
  • [ ] createDoc throws if document already exists
  • [ ] updateDoc throws if document missing (without upsert)
  • [ ] deleteDoc returns { id, deleted: false } if doc missing

Implementation Checklist

  • [x] 5.1 Create src/firestore/crud.ts
  • [x] 5.2 Implement buildEntityCollectionPath(entity, pathParams)
  • [x] 5.3 Implement buildEntityDocRef({ firestore, entity, pathParams, id })
  • [x] 5.4 Implement listDocs({ firestore, entity, pathParams, limit? }) → array of docs
  • [x] 5.5 Implement getDoc({ firestore, entity, pathParams, id }) → doc or null
  • [x] 5.6 Implement createDoc({ firestore, entity, pathParams, id?, data })
  • [x] 5.7 Write payload including id: ref.id and { [docIdParamName]: ref.id } if different
  • [x] 5.8 Implement updateDoc({ firestore, entity, pathParams, id, data })
  • [x] 5.9 Implement deleteDoc({ firestore, entity, pathParams, id }){ id, deleted }
  • [x] 5.10 Implement upsertDoc({ firestore, entity, pathParams, id, data, merge? })
  • [x] 5.11 Ensure createdAt on new docs, updatedAt always

Notes

  • Use fromJsonSafeToFirestore before writing
  • Use toJsonSafe after reading
  • Default merge: true for upsert

Reacties

Nog geen reacties