Hermes MySQL
    Preparing search index...

    Function createEventStore

    • Creates an event store on MySQL 8.0+ / Aurora MySQL 3. Run migrate(pool, { eventStore: true }) first.

      An append locks the row of its stream in hermes_streams until the end of its transaction, checks the expected version, stores the events with the next positions and updates the inline projections, all in one transaction: its own, in READ COMMITTED, or the one of the application (tx). In the transaction of the application, the append runs inside a savepoint, so a rejected append (e.g. a VersionConflictError) leaves nothing behind and the transaction stays usable. Appends to different streams do not wait for each other, with one exception: an inline projection whose document is fed by many streams (ofId) locks that document in every append that feeds it, until the end of its transaction.

      Every event gets a new eventId (a UUID); an id given by the caller is not used. For idempotent appends, use the expected version.

      Every projection has its own table, named after it (projectionTable) and created when it is missing (see MySqlEventStore.init). It keeps the documents, and in a single-stream projection also the version of its stream.

      Its own transaction is tried again when MySQL picks it as the victim of a deadlock (e.g. when many instances create the same stream at once). A deadlock in the transaction of the application rolls back the whole transaction, so it is thrown to the application as it is.

      Reads and appends use the pool, so point it at the writer of Aurora, not at a reader: the readers lag behind.

      Parameters

      • pool: Pool

        The mysql2 pool of the application

      • Optionaloptions: {
            projections?: Readonly<
                {
                    "~types"?: { event: any; state: any };
                    eventTypes: readonly any[];
                    isSingleStream: boolean;
                    kind: "document";
                    mode: ProjectionMode;
                    name: string;
                    canHandle(event: { type: string }): boolean;
                    evolve(state: any, event: RecordedEvent<E>): any;
                    getDocumentId(event: RecordedEvent<E>): string;
                    initialState(): any;
                },
            >[];
        }
        • Optionalprojections?: Readonly<
              {
                  "~types"?: { event: any; state: any };
                  eventTypes: readonly any[];
                  isSingleStream: boolean;
                  kind: "document";
                  mode: ProjectionMode;
                  name: string;
                  canHandle(event: { type: string }): boolean;
                  evolve(state: any, event: RecordedEvent<E>): any;
                  getDocumentId(event: RecordedEvent<E>): string;
                  initialState(): any;
              },
          >[]

          The projections: the inline ones are updated in the transaction of every append, and init() creates the tables of all of them

      Returns MySqlEventStore