-
Notifications
You must be signed in to change notification settings - Fork 1
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
366 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,46 @@ | ||
import type { Pg } from './pg.ts' | ||
import type { Adapter, Options, OutboxMessage, StartStop } from './types.ts' | ||
import Cursor from 'pg-cursor' | ||
|
||
export class Es implements StartStop { | ||
private lastEventId = '0' | ||
|
||
constructor(private readonly pg: Pg, private readonly adapter: Adapter, private readonly options: Options) {} | ||
|
||
async start() { | ||
await this.initSync() | ||
} | ||
|
||
async stop() {} | ||
|
||
setLastEventId(index: OutboxMessage['id']) { | ||
this.lastEventId = index | ||
} | ||
|
||
getLastEventId() { | ||
return this.lastEventId | ||
} | ||
|
||
private async initSync() { | ||
const client = await this.pg.getClient() | ||
const cursor = client.query( | ||
new Cursor( | ||
` | ||
select * from pg_trx_outbox | ||
where is_event and id > $1 | ||
order by id | ||
`, | ||
[this.getLastEventId()] | ||
) | ||
) | ||
while (true) { | ||
const messages: OutboxMessage[] = await cursor.read(this.options.eventSourcingOptions?.initSyncBatchSize ?? 100) | ||
if (!messages.length) { | ||
break | ||
} | ||
await this.adapter.send(messages) | ||
this.setLastEventId(messages.at(-1)!.id) | ||
} | ||
client.release() | ||
} | ||
} |
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
Oops, something went wrong.