feat: add peerDependencies

This commit is contained in:
2025-07-07 13:11:16 +02:00
parent 2093dd7097
commit dd1570d52e
36 changed files with 481 additions and 274 deletions

View File

@@ -1,4 +1,4 @@
import z from "zod";
import z from "zod/v4";
import type { CollectionRegistrar } from "../types.ts";

View File

@@ -3,8 +3,4 @@ import { registrar as events } from "./events.ts";
import { registrar as relations } from "./relations.ts";
import { registrar as snapshots } from "./snapshots.ts";
export const registrars: CollectionRegistrar[] = [
events,
relations,
snapshots,
];
export const registrars: CollectionRegistrar[] = [events, relations, snapshots];

View File

@@ -1,4 +1,4 @@
import z from "zod";
import z from "zod/v4";
import type { CollectionRegistrar } from "../types.ts";

View File

@@ -1,4 +1,4 @@
import z from "zod";
import z from "zod/v4";
import type { CollectionRegistrar } from "../types.ts";

View File

@@ -70,7 +70,10 @@ export class MongoEventsProvider implements EventsProvider {
* @param options - Read options for modifying the result.
*/
async getByStreams(streams: string[], options: EventReadOptions = {}): Promise<EventRecord[]> {
return (await this.#withReadOptions(this.collection.find({ stream: { $in: streams }, ...this.#withFilters(options) }), options)
return (await this.#withReadOptions(
this.collection.find({ stream: { $in: streams }, ...this.#withFilters(options) }),
options,
)
.sort({ created: 1 })
.toArray()
.then(toParsedRecords(schema))) as EventRecord[];

View File

@@ -1,5 +1,5 @@
import type { Db, WithId } from "mongodb";
import type { z, ZodObject } from "zod";
import type { z, ZodObject } from "zod/v4";
/**
* Take a list of records and run it through the given zod parser. This
@@ -9,7 +9,9 @@ import type { z, ZodObject } from "zod";
*
* @param parser - Zod parser to run the documents through.
*/
export function toParsedRecords<TSchema extends ZodObject>(parser: TSchema): (documents: WithId<object>[]) => z.infer<TSchema>[] {
export function toParsedRecords<TSchema extends ZodObject>(
parser: TSchema,
): (documents: WithId<object>[]) => z.infer<TSchema>[] {
return parser.array().parse;
}
@@ -21,7 +23,9 @@ export function toParsedRecords<TSchema extends ZodObject>(parser: TSchema): (do
*
* @param parser - Zod parser to run the document through.
*/
export function toParsedRecord<TSchema extends ZodObject>(parser: TSchema): (document: WithId<object> | null) => z.infer<TSchema> | undefined {
export function toParsedRecord<TSchema extends ZodObject>(
parser: TSchema,
): (document: WithId<object> | null) => z.infer<TSchema> | undefined {
return function (document) {
if (document === null) {
return undefined;

View File

@@ -1,6 +1,10 @@
import type { Options, Sql } from "postgres";
export type PostgresConnection = [PostgresConnectionUrl, Options<any>?] | [Options<any>] | Sql | PostgresConnectionFactory;
export type PostgresConnection =
| [PostgresConnectionUrl, Options<any>?]
| [Options<any>]
| Sql
| PostgresConnectionFactory;
type PostgresConnectionUrl = `postgres://${string}:${string}@${string}:${number}/${string}`;

View File

@@ -76,7 +76,10 @@ export class PostgresEventsProvider implements EventsProvider {
* @param stream - Stream to fetch events for.
* @param options - Read options for modifying the result.
*/
async getByStream(stream: string, { filter, cursor, direction, limit }: EventReadOptions = {}): Promise<EventRecord[]> {
async getByStream(
stream: string,
{ filter, cursor, direction, limit }: EventReadOptions = {},
): Promise<EventRecord[]> {
return this.db.sql<PGEventRecord[]>`
SELECT * FROM ${this.table}
WHERE
@@ -94,7 +97,10 @@ export class PostgresEventsProvider implements EventsProvider {
* @param streams - Stream to get events for.
* @param options - Read options for modifying the result.
*/
async getByStreams(streams: string[], { filter, cursor, direction, limit }: EventReadOptions = {}): Promise<EventRecord[]> {
async getByStreams(
streams: string[],
{ filter, cursor, direction, limit }: EventReadOptions = {},
): Promise<EventRecord[]> {
return this.db.sql<PGEventRecord[]>`
SELECT * FROM ${this.table}
WHERE
@@ -112,7 +118,9 @@ export class PostgresEventsProvider implements EventsProvider {
* @param id - Event id.
*/
async getById(id: string): Promise<EventRecord | undefined> {
return this.db.sql<PGEventRecord[]>`SELECT * FROM ${this.table} WHERE id = ${id}`.then(this.#fromDriver).then(([record]) => record);
return this.db.sql<PGEventRecord[]>`SELECT * FROM ${this.table} WHERE id = ${id}`
.then(this.#fromDriver)
.then(([record]) => record);
}
/**

View File

@@ -35,9 +35,11 @@ export class PostgresRelationsProvider implements RelationsProvider {
* @param stream - Stream to add to the key.
*/
async insert(key: string, stream: string): Promise<void> {
await this.db.sql`INSERT INTO ${this.table} (key, stream) VALUES (${key}, ${stream}) ON CONFLICT DO NOTHING`.catch((error) => {
throw new Error(`EventStore > 'relations.insert' failed with postgres error: ${error.message}`);
});
await this.db.sql`INSERT INTO ${this.table} (key, stream) VALUES (${key}, ${stream}) ON CONFLICT DO NOTHING`.catch(
(error) => {
throw new Error(`EventStore > 'relations.insert' failed with postgres error: ${error.message}`);
},
);
}
/**
@@ -107,7 +109,9 @@ export class PostgresRelationsProvider implements RelationsProvider {
await this.db.sql
.begin(async (sql) => {
for (let i = 0; i < relations.length; i += batchSize) {
const conditions = relations.slice(i, i + batchSize).map(({ key, stream }) => `(key = '${key}' AND stream = '${stream}')`);
const conditions = relations
.slice(i, i + batchSize)
.map(({ key, stream }) => `(key = '${key}' AND stream = '${stream}')`);
await sql`DELETE FROM ${this.table} WHERE ${this.db.sql.unsafe(conditions.join(" OR "))}`;
}
})