2.4.2 • Published 1 year ago

@castore/event-storage-adapter-postgres v2.4.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Postgres Event Storage Adapter

DRY Castore EventStorageAdapter implementation using a Postgres database.

📥 Installation

# npm
npm install @castore/event-storage-adapter-postgres

# yarn
yarn add @castore/event-storage-adapter-postgres

This package has @castore/core as peer dependency, so you will have to install it as well:

# npm
npm install @castore/core

# yarn
yarn add @castore/core

👩‍💻 Usage

import { PostgresEventStorageAdapter } from '@castore/event-storage-adapter-postgres';

const pokemonsEventStorageAdapter = new PostgresEventStorageAdapter({
  // 👇 You can specify a connection string for your Postgres database
  connectionString: 'postgresql://postgres:postgres@localhost:5432/postgres',
});

const pokemonsEventStore = new EventStore({
  ...
  eventStorageAdapter: pokemonsEventStorageAdapter,
});

🤔 How it works

This adapter simply persists events in a Postgres database.

Database Table Structure and Customization

Below is an overview of the underlying table structure used by the adapter and the available customization options when creating the event table.

Table Structure Overview

ColumnData Type(s)Default CustomizationDescription
idBIGSERIAL (or SERIAL if specified via idType)BIGSERIALPrimary key; auto-incrementing identifier
aggregate_nameVARCHAR(aggregateNameLength)VARCHAR(32)Aggregate name (identifies the event store)
aggregate_idUUID, ULID, or VARCHAR(aggregateId.length if type is VARCHAR)UUIDIdentifier for the aggregate; if using VARCHAR, a length must be specified
versionBIGINT (or INTEGER if specified via versionType)BIGINTEvent version number
typeVARCHAR(typeLength)VARCHAR(64)Name of the event type
dataJSONBn/aEvent payload stored in JSONB
metadataJSONBn/aAdditional event metadata
timestampTIMESTAMPTZ with DEFAULT CURRENT_TIMESTAMP(3)CURRENT_TIMESTAMP(3)Timestamp of the event (set automatically at insertion)
UNIQUE(aggregate_name, aggregate_id, version)n/aEnforces that each combination of aggregate_name, aggregate_id, and version is unique

Customizable Options in createEventTable

OptionDefault ValueAllowed Values / Description
tableNameeventName of the table to be created
idTypeBIGSERIALDatabase type for the id column; allowed values: BIGSERIAL or SERIAL
aggregateNameLength32Length for the aggregate_name VARCHAR column
aggregateId{ type: "UUID" }Defines the aggregate_id column type; can be UUID, ULID, or VARCHAR (if using VARCHAR, a length must be provided)
versionTypeBIGINTDatabase type for the version column; allowed values: BIGINT or INTEGER
typeLength64Length for the type VARCHAR column

Additionally, indexes are created on aggregate_name, version, and a composite index on (aggregate_name, aggregate_id) to enhance query performance.

2.4.2

1 year ago

2.4.1

1 year ago

2.4.0

1 year ago