forked from undb-io/undb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
117 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,29 @@ | ||
import { singleton } from "@undb/di" | ||
import type { Topic } from "../reply/topic" | ||
|
||
export interface Message { | ||
topic: string | ||
message: string | ||
export interface Message<T> { | ||
topic: Topic | ||
message: T | ||
} | ||
|
||
export interface PubSub { | ||
publish(topic: string, message: string): void | ||
subscribe(topic: string): AsyncIterable<string> | ||
export interface PubSub<T> { | ||
publish(topic: Topic, message: T): void | ||
subscribe(topic: Topic): AsyncIterable<T> | ||
} | ||
|
||
@singleton() | ||
export class PubSubContext { | ||
private pubSub!: PubSub | ||
export class PubSubContext<T> { | ||
private pubSub!: PubSub<T> | ||
|
||
setPubSub(pubSub: PubSub): void { | ||
setPubSub(pubSub: PubSub<T>): void { | ||
this.pubSub = pubSub | ||
} | ||
|
||
publish(topic: string, message: string): void { | ||
publish(topic: Topic, message: T): void { | ||
this.pubSub.publish(topic, message) | ||
} | ||
|
||
subscribe(topic: string): AsyncIterable<string> { | ||
subscribe(topic: Topic): AsyncIterable<T> { | ||
return this.pubSub.subscribe(topic) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { BaseEvent, Option, IEventJSON } from "@undb/domain" | ||
import type { Outbox } from "@undb/persistence" | ||
import { RecordEventFactory } from "@undb/table" | ||
|
||
export class ReplyEventFactory { | ||
static from(outbox: Outbox): Option<BaseEvent> { | ||
// TODO: just use date time timestamp | ||
return RecordEventFactory.fromJSON({ ...outbox, timestamp: outbox.timestamp.toISOString() } as IEventJSON) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Option, type BaseEvent } from "@undb/domain" | ||
import { RecordCreatedEvent, RecordDeletedEvent } from "@undb/table" | ||
import { match, P } from "ts-pattern" | ||
|
||
export type Topic = `tenant.${string}.${string}` | ||
|
||
export const getTopic = (event: BaseEvent): Option<Topic> => { | ||
const topic = match(event) | ||
.returnType<Topic | null>() | ||
.with(P.instanceOf(RecordCreatedEvent), (e) => `tenant.${e.payload.tableId}.record.created`) | ||
.with(P.instanceOf(RecordDeletedEvent), (e) => `tenant.${e.payload.tableId}.record.deleted`) | ||
.otherwise(() => null) | ||
|
||
return Option(topic) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/table/src/modules/records/events/record-event.factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Option, type IEventJSON } from "@undb/domain" | ||
import { | ||
RecordCreatedEvent, | ||
RecordDeletedEvent, | ||
type IRecordCreatedEvent, | ||
type IRecordDeletedEvent, | ||
type IRecordEvent, | ||
} from "." | ||
import { match } from "ts-pattern" | ||
|
||
export class RecordEventFactory { | ||
static fromJSON(event: IEventJSON): Option<IRecordEvent> { | ||
const evt = match(event) | ||
.returnType<IRecordEvent | null>() | ||
.with( | ||
{ name: "record.created" }, | ||
(event) => | ||
new RecordCreatedEvent(event.payload as IRecordCreatedEvent, event.meta, event.id, new Date(event.timestamp)), | ||
) | ||
.with( | ||
{ name: "record.deleted" }, | ||
(event) => | ||
new RecordDeletedEvent(event.payload as IRecordDeletedEvent, event.meta, event.id, new Date(event.timestamp)), | ||
) | ||
.otherwise(() => null) | ||
|
||
return Option(evt) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters