-
Notifications
You must be signed in to change notification settings - Fork 6
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
rotorsoft
committed
Sep 28, 2024
1 parent
1b7f66a
commit be12e85
Showing
3 changed files
with
98 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
dispose, | ||
log, | ||
logAdapterCreated, | ||
logAdapterDisposed, | ||
MessageQueue, | ||
Messages | ||
} from "@rotorsoft/eventually"; | ||
import { Pool } from "pg"; | ||
import { config } from "./config"; | ||
|
||
export const PostgresMessageQueue = <M extends Messages>( | ||
table: string | ||
): MessageQueue<M> => { | ||
const pool = new Pool(config.pg); | ||
const queue: MessageQueue<M> = { | ||
name: `PostgresMessageQueue:${table}`, | ||
dispose: async () => { | ||
await pool.end(); | ||
}, | ||
|
||
// TODO: implement | ||
enqueue: async (messages) => { | ||
const sql = `TODO INSERT`.concat( | ||
messages.map((message) => message.name).join(", ") | ||
); | ||
log().green().data("sql:", sql); | ||
await pool.query(sql); | ||
}, | ||
|
||
// TODO: implement | ||
dequeue: async (callback, stream) => { | ||
const sql = `SELECT * FROM "${table}" WHERE stream = '${stream}' ORDER BY created ASC LIMIT 1`; | ||
log().green().data("sql:", sql); | ||
// const result = await pool.query(sql); | ||
// TODO: handle results with callback | ||
return Promise.resolve(); | ||
}, | ||
|
||
// TODO: implement | ||
seed: async () => { | ||
const seed = "TODO SEED"; | ||
log().green().info(`>>> Seeding message queue table: ${table}`); | ||
log().gray().info(seed); | ||
await pool.query(seed); | ||
}, | ||
|
||
drop: async (): Promise<void> => { | ||
await pool.query(`DROP TABLE IF EXISTS "${table}"`); | ||
} | ||
}; | ||
|
||
logAdapterCreated(queue.name); | ||
dispose(() => { | ||
if (queue.dispose) { | ||
logAdapterDisposed(queue.name); | ||
return queue.dispose(); | ||
} | ||
return Promise.resolve(); | ||
}); | ||
|
||
return queue; | ||
}; |
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,5 +1,6 @@ | ||
/** @module eventually-pg */ | ||
export * from "./config"; | ||
export * from "./PostgresMessageQueue"; | ||
export * from "./PostgresProjectorStore"; | ||
export * from "./PostgresStore"; | ||
export * from "./PostgresSubscriptionStore"; |
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