npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@eventsource/eventstore

v0.2.3

Published

Eventstore

Downloads

8

Readme

@eventsource/eventstore

Install

$ npm install @eventsource/eventstore

Implementations

Event store

Create events

import { event, Event, EventFromCreator } from '@eventsource/eventstore/Event'

/**
 * Create an Event creator.
 * The function `event()` returns an `IO<Event>`.
 */
const created = (name: string) => event({
    type: 'Created',
    data: {
        name
    }
})
type CreatedEvent = EventFromCreator<typeof created>

/**
 * We can even provide metadata
 */
const updated = (name: string, correlationId?: string) => event({
    type: 'Updated',
    data: {
        name
    },
    metadata: {
        correlationId
    }
})

const updatedEvent = updated('foo', 'event-3')(); // it's an `IO`, so we call it (just for the example)
// the event has four properties
updatedEvent.id        // a unique UUID
updatedEvent.type      // `updated`
updatedEvent.data      // `{ name: 'foo' }`
updatedEvent.metadata  // `{ correlationId: 'event-3' }`

Append events

import { event, Event } from '@eventsource/eventstore/Event'
import { EventStore, NO_STREAM } from '@eventsource/eventstore/EventStore'
import * as TE from 'fp-ts/TaskEither'
import * as E from 'fp-ts/Either'
import { pipe } from 'fp-ts/function';

declare const store: EventStore<Event>

/**
 * Append an event to a new stream, use `expectedRevision: NO_STREAM` to ensure
 * the stream does not exist.
 * Then retrieve the current revision.
 */
pipe(
    TE.rightIO(event({ type: 'Created' })),
    TE.chain(store.appendToStream({ stream: 'user-1', expectedRevision: NO_STREAM })),
    TE.map(({ revision }) => revision)  // retrieve the current revision
)

/**
 * We can append an event using a provided `expectedRevision` to ensure the integrity
 * of the stream log.
 * The `expectedRevision` revision can be retrieved from a previous `appendToStream()`
 * or a `readStream()` call.
 */
pipe(
    TE.rightIO(event({ type: 'Updated' })),
    TE.chain(store.appendToStream({ stream: 'user-1', expectedRevision: BigInt(1) })),
)

Read a stream

import { event, Event, EventFromCreator } from '@eventsource/eventstore/Event'
import { BACKWARDS, END, EventStore } from '@eventsource/eventstore/EventStore'
import * as TE from 'fp-ts/TaskEither'
import * as E from 'fp-ts/Either'
import { pipe, tuple } from 'fp-ts/function';

declare const store: EventStore<Event>

/**
 * Read a stream.
 * The result is an `AsyncIterable`. We can obtain an array iterating it.
 */
pipe(
    store.readStream({ stream: 'user-1' }),
    stream => TE.tryCatch(async () => {
        const events = []
        let revision = BigInt(-1)
        for await (const ev of stream) {
            events.push(ev)
            revision = ev.revision
        }

        return tuple(revision, events)
    }, E.toError),
    TE.map(([curentRevision, events]) => {
        // ...
    })
)

/**
 * Read a stream from a specific revision.
 * Events after the provided evision are returned.
 */
store.readStream({ stream: 'user-1', fromRevision: BigInt(1) })

 /**
 * Read a stream in reverse order.
 */
store.readStream({ stream: 'user-1', direction: BACKWARDS })

/**
 * Get the last stream event.
 * We can use the `maxCount` option to limit results.
 */
store.readStream({ stream: 'user-1', fromRevision: END, direction: BACKWARDS, maxCount: 1 })

Delete a stream

import type { Event } from '@eventsource/eventstore/Event'
import type { EventStore } from '@eventsource/eventstore/EventStore'

declare const store: EventStore<Event>

/**
 * We can delete a stream, deleting all stream events
 */
store.deleteStream({ stream: 'user-1' })

Read all store events

import type { Event } from '@eventsource/eventstore/Event'
import { BACKWARDS, EventStore } from '@eventsource/eventstore/EventStore'
import * as TE from 'fp-ts/TaskEither'
import * as E from 'fp-ts/Either'
import { pipe, tuple } from 'fp-ts/function';

declare const store: EventStore<Event>

/**
 * Read all the events from the eventstore
 * Get
 */
pipe(
    store.readAll(),
    stream => TE.tryCatch(async () => {
        const events = []
        let position = undefined
        for await (const ev of stream) {
            events.push(ev)
            position = ev.position
        }

        return tuple(position, events)
    }, E.toError),
    TE.map(([curentPosition, events]) => {
        // ...
    })
);

/**
 * Read all the events from the eventstore starting from a specific position.
 * The position can be retrieved from a previous `readAll()` call.
 */
store.readAll({ fromPosition: 'position' })

/**
 * We can use `direction` and `maxCount` like the `readStream()` method
 */
store.readAll({
    fromPosition: 'position',
    direction: BACKWARDS,
    maxCount: 1
})

Subscribable Event Store

Some event store implements the SubscribableEventStore interface and can subscribe in orrder to receive live events.

Subscribe to stream

import type { Event } from '@eventsource/eventstore/Event'
import { START, SubscribableEventStore } from '@eventsource/eventstore/EventStore'

declare const subscribableStore: SubscribableEventStore<Event>

/**
 * Live subscription to stream
 */
pipe(
    subscribableStore.subscribe({ stream: 'user-1' }),
    async (stream) => {
        for await (const ev of stream) {
            // do something with the event
            // await process(ev)
        }
    }
)

/**
 * Catch-Up subscription to stream from start.
 * We can open a catch-up subscription, reading all the stream events and opening a subscription
 */
subscribableStore.subscribe({ stream: 'user-1', fromRevision: START })

/**
 * Catch-Up subscription to stream from revision.
 */
subscribableStore.subscribe({ stream: 'user-1', fromRevision: BigInt(2) })

Subscibe to all events

import type { Event } from '@eventsource/eventstore/Event'
import { START, SubscribableEventStore } from '@eventsource/eventstore/EventStore'
import { pipe } from 'fp-ts/function';

declare const subscribableStore: SubscribableEventStore<Event>

/**
 * Live subscription to all streams.
 * We can save the position of the last processed event in order to start
 * another subscription from that position lateer (see below).
 */
 pipe(
    subscribableStore.subscribeToAll(),
    async (stream) => {
        let lastPosition
        for await (const ev of stream) {
            // do something with the event
            // await process(ev)
            lastPosition = ev.position
        }
    }
)

/**
 * Catch-Up subscription to all streams from start.
 * We can open a catch-up subscription, reading all the stream events and opening a subscription
 */
subscribableStore.subscribeToAll({ fromPosition: START })

/**
 * Catch-Up subscription to all streams from position.
 */
subscribableStore.subscribeToAll({ fromPosition: 'last-position' })

Reading and subscribing

To handle backpressure on large streams, read and subscribe methods return an AsyncIterable object.

ix is the suggested library to work with AsyncIterable objects.

Aborting

To abort read or subscribe operations, all methods supports the AbortSignal.

import { event, Event, EventFromCreator } from '@eventsource/eventstore/Event'
import { AbortError } from '@eventsource/eventstore/errors'
import { BACKWARDS, END, EventStore } from '@eventsource/eventstore/EventStore'
import * as TE from 'fp-ts/TaskEither'
import * as E from 'fp-ts/Either'
import { pipe } from 'fp-ts/function';
import * as IXO from 'ix/asynciterable/operators'

declare const store: EventStore<Event>

const controller = new AbortController()

pipe(
    store.subscribe({ stream: 'user-1', signal: controller.signal }),
    IXO.tap(console.log),
    IXO.finalize(() => controller.abort()) // abort subscribe
)