{"id":25566512,"url":"https://github.com/pdmlab/mongo-eventstore","last_synced_at":"2025-04-12T10:51:28.137Z","repository":{"id":42622107,"uuid":"401370530","full_name":"PDMLab/mongo-eventstore","owner":"PDMLab","description":"A simple MongoDB event store","archived":false,"fork":false,"pushed_at":"2024-02-21T01:09:14.000Z","size":765,"stargazers_count":8,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-26T05:41:54.595Z","etag":null,"topics":["eventsourcing","eventstore","mongodb","mongodb-eventstore","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/mongo-eventstore","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PDMLab.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-30T14:19:55.000Z","updated_at":"2024-07-12T02:31:03.000Z","dependencies_parsed_at":"2023-02-04T06:46:50.513Z","dependency_job_id":"97255373-06d7-463d-880c-6d869e2b44cd","html_url":"https://github.com/PDMLab/mongo-eventstore","commit_stats":{"total_commits":22,"total_committers":2,"mean_commits":11.0,"dds":0.2727272727272727,"last_synced_commit":"6c90f615cf335cc8740a9a109e6a1dbe27d45a09"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":"PDMLab/ts-node-prettier-vscode-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fmongo-eventstore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fmongo-eventstore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fmongo-eventstore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fmongo-eventstore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PDMLab","download_url":"https://codeload.github.com/PDMLab/mongo-eventstore/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248557844,"owners_count":21124165,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eventsourcing","eventstore","mongodb","mongodb-eventstore","typescript"],"created_at":"2025-02-20T22:32:58.178Z","updated_at":"2025-04-12T10:51:28.110Z","avatar_url":"https://github.com/PDMLab.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) [![Join the chat at https://gitter.im/pdmlab/community](https://badges.gitter.im/pdmlab/community.svg)](https://gitter.im/pdmlab/community)\n\n# mongo-eventstore - an Eventstore for Node.js build on top of MongoDB\n\nThis library provides an eventstore based on (but not affiliated with) MongoDB implemented in TypeScript.\n\nIt is based on personal expierence with Event Sourcing and also inspired by this [EventSourcing.NodeJS](https://github.com/oskardudycz/EventSourcing.NodeJS) samples.\n\nA sample can be found [here](https://github.com/AlexZeitler/mongo-eventstore-sample).\n\n## Installation\n\n```bash\nnpm install mongo-eventstore mongodb\n```\n\nor\n\n```bash\nyarn add mongo-eventstore mongodb\n```\n\n## Usage\n\n`mongo-eventstore` currently supports these operations:\n\n```typescript\ntype EventStore = {\n  readStream: (streamId: string) =\u003e Promise\u003cEvent[]\u003e\n  readAllEvents: (options?: { batchSize?: number }) =\u003e Promise\u003cEvent[]\u003e\n  appendToStream: (\n    streamId: string,\n    eventData: Event | Event[]\n  ) =\u003e Promise\u003cvoid\u003e\n  aggregateAll: () =\u003e Promise\u003cProjection[]\u003e\n  aggregateStream\u003cProjection, SomeEvent extends Event\u003e(\n    events: SomeEvent[],\n    project: (\n      currentState: Partial\u003cProjection\u003e,\n      event: SomeEvent\n    ) =\u003e Partial\u003cProjection\u003e\n  ): Projection\n}\n```\n\n### Creating an Event Store\n\n```typescript\nimport { MongoClient } from 'mongodb'\nimport MongoDbEventStore from 'mongo-eventstore'\n\nconst client = await MongoClient.connect('mongodb://localhost:27017')\nconst db = client.db('TestDb')\nconst eventstore = await MongoDbEventStore(db, 'customer')\n```\n\n### Appending events to a stream\n\n```typescript\nimport { Event } from 'mongo-eventstore'\nimport { v4 } from 'uuid'\n\ntype Created = Event\u003c\n  `Created`,\n  { firstName: string; lastName: string; level: number }\n\u003e\n\nconst event: Created = {\n  type: 'Created',\n  firstName: 'John',\n  lastName: 'Doe',\n  level: '0'\n}\nconst streamId = v4()\nawait eventstore.appendToStream(streamId, [event])\n```\n\nThe event will be persisted into the collection `customer.events` in database `TestDb` and look like this:\n\n```json\n{\n  \"type\": \"Created\",\n  \"data\": {\n    \"firstName\": \"John\",\n    \"lastName\": \"Doe\",\n    \"level\": 0\n  },\n  \"timestamp\": 1631174064135,\n  \"streamId\": \"TestEvents\",\n  \"version\": 1\n}\n```\n\n### Reading events from a stream\n\n```typescript\nconst events = await eventstore.readStream('TestEvents')\n```\n\n### Building in-memory projections for a particular stream\n\n```typescript\nimport { MongoClient } from 'mongodb'\nimport { Projection } from 'mongo-eventstore'\n\nfunction projectCustomerNames(\n  currentState: Partial\u003cCustomerNamesProjection\u003e,\n  event: CustomerEvent\n): Partial\u003cCustomerNamesProjection\u003e {\n  switch (event.type) {\n    case 'Created': {\n      const projection: CustomerNamesProjection = {\n        id: event.streamId,\n        type: 'CustomerNamesProjection',\n        firstName: event.data.firstName,\n        lastName: event.data.lastName\n      }\n      return projection\n    }\n    default:\n      return currentState\n  }\n}\n\nconst events = (await eventstore.readAllEvents()) as CustomerEvent[]\nconst projections = eventstore.aggregateStream(events, projectCustomerNames)\n```\n\nThe projections based on the event above will look like this:\n\n```json\n[\n  {\n    \"type\": \"CustomerNamesProjection\",\n    \"firstName\": \"John\",\n    \"lastName\": \"Doe\"\n  }\n]\n```\n\n### Building in-memory projections for all streams\n\n```typescript\nconst eventstore = await MongoDbEventStore(db, StreamName, [\n  {\n    projectionType: 'CustomerNamesProjection',\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    project: projectCustomerNames as any\n  }\n])\n\nconst projections = await eventstore.aggregateAll()\n```\n\n## Want to help?\n\nThis project is just getting off the ground and could use some help with cleaning things up and refactoring.\n\nIf you want to contribute - we'd love it! Just open an issue to work against so you get full credit for your fork. You can open the issue first so we can discuss and you can work your fork as we go along.\n\nIf you see a bug, please be so kind as to show how it's failing, and we'll do our best to get it fixed quickly.\n\nBefore sending a PR, please [create an issue](issues/new) to introduce your idea and have a reference for your PR.\n\nWe're using [conventional commits](https://www.conventionalcommits.org), so please use it for your commits as well.\n\nAlso please add tests and make sure to run `npm run lint-ts` or `yarn lint-ts`.\n\n## License\n\nMIT License\n\nCopyright (c) 2020 - 2021 PDMLab\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdmlab%2Fmongo-eventstore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpdmlab%2Fmongo-eventstore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdmlab%2Fmongo-eventstore/lists"}