Skip to Content
gentlemans-stack
ReferenceDomains

todo-domain

Mongoose persistence boundary for todo.

API

  • createTodoDomainRepository: the todo repository (create, read, update, delete).
  • getTodoModel: the Mongoose model for todos.
  • Types: TodoDomainRepository, TodoDocument, TodoMongooseModel, TodoPersistence.
FileExports
todo.schema.tsgetTodoModel(connection), TodoDocument, TodoMongooseModel
todo.repository.tscreateTodoDomainRepository(connection, logger), TodoDomainRepository, Todo

The persistence type derives done, ownerId, title and workspaceId from the portable Todo model, replacing its serialized ID/date representation with Mongoose's _id and native timestamps. The Mongoose schema supplies persistence rules such as title being required and done defaulting to false. Model initialization is idempotent and reuses connection.models.Todo when already registered.

Todos are scoped to a workspace, with a { workspaceId, createdAt } index. The repository exposes list(workspaceId), create({ ownerId, title, workspaceId }), findById({ id, workspaceId }) (returns undefined when absent), complete({ id, workspaceId }), and delete({ id, workspaceId }). Every filter includes workspaceId, so a todo id from another workspace behaves like a missing one. complete/delete throw an AppError("RESOURCE_NOT_FOUND", { resource: "todo" }) when no document matches. todo-service's composition root lets that propagate as the contract's declared not-found error.

Tests

The source-adjacent Vitest suite starts one MongoMemoryServer, uses a dedicated Mongoose connection, clears collections after each test, and closes the connection before stopping the server. It covers every repository method, including the not-found paths.

Commands

pnpm nx run todo-domain:typecheck   # type-check the package
pnpm nx run todo-domain:test        # run the Vitest suite

On this page