@haathie/pgmb 0.2.8 → 0.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/lib/abortable-async-iterator.d.ts +14 -0
- package/lib/abortable-async-iterator.js +5 -12
- package/lib/batcher.d.ts +12 -0
- package/lib/batcher.js +1 -5
- package/lib/client.d.ts +77 -0
- package/lib/client.js +34 -41
- package/lib/consts.d.ts +1 -0
- package/lib/consts.js +1 -4
- package/lib/index.d.ts +6 -0
- package/lib/index.js +4 -20
- package/lib/queries.d.ts +498 -0
- package/lib/queries.js +19 -22
- package/lib/query-types.d.ts +17 -0
- package/lib/query-types.js +1 -2
- package/lib/retry-handler.d.ts +11 -0
- package/lib/retry-handler.js +7 -11
- package/lib/sse.d.ts +4 -0
- package/lib/sse.js +16 -52
- package/lib/types.d.ts +234 -0
- package/lib/types.js +1 -2
- package/lib/utils.d.ts +19 -0
- package/lib/utils.js +6 -15
- package/lib/webhook-handler.d.ts +6 -0
- package/lib/webhook-handler.js +7 -13
- package/package.json +3 -9
- package/sql/pgmb-0.2.0-0.2.8.sql +0 -1
- package/src/abortable-async-iterator.ts +0 -98
- package/src/batcher.ts +0 -90
- package/src/client.ts +0 -704
- package/src/consts.ts +0 -1
- package/src/index.ts +0 -6
- package/src/queries.ts +0 -630
- package/src/query-types.ts +0 -21
- package/src/retry-handler.ts +0 -125
- package/src/sse.ts +0 -148
- package/src/types.ts +0 -267
- package/src/utils.ts +0 -71
- package/src/webhook-handler.ts +0 -91
package/src/batcher.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import type { IEventData, PGMBEventBatcherOpts } from './types.ts'
|
|
2
|
-
|
|
3
|
-
type Batch<T> = {
|
|
4
|
-
messages: T[]
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export class PGMBEventBatcher<T extends IEventData> {
|
|
8
|
-
|
|
9
|
-
#publish: PGMBEventBatcherOpts<T>['publish']
|
|
10
|
-
#flushIntervalMs: number | undefined
|
|
11
|
-
#maxBatchSize: number
|
|
12
|
-
#currentBatch: Batch<T> = { messages: [] }
|
|
13
|
-
#flushTimeout: NodeJS.Timeout | undefined
|
|
14
|
-
#flushTask: Promise<void> | undefined
|
|
15
|
-
#logger: PGMBEventBatcherOpts<T>['logger']
|
|
16
|
-
#shouldLog?: PGMBEventBatcherOpts<T>['shouldLog']
|
|
17
|
-
#batch = 0
|
|
18
|
-
|
|
19
|
-
constructor({
|
|
20
|
-
shouldLog,
|
|
21
|
-
publish,
|
|
22
|
-
flushIntervalMs,
|
|
23
|
-
maxBatchSize = 2500,
|
|
24
|
-
logger
|
|
25
|
-
}: PGMBEventBatcherOpts<T>) {
|
|
26
|
-
this.#publish = publish
|
|
27
|
-
this.#flushIntervalMs = flushIntervalMs
|
|
28
|
-
this.#maxBatchSize = maxBatchSize
|
|
29
|
-
this.#logger = logger
|
|
30
|
-
this.#shouldLog = shouldLog
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async end() {
|
|
34
|
-
clearTimeout(this.#flushTimeout)
|
|
35
|
-
await this.#flushTask
|
|
36
|
-
await this.flush()
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Enqueue a message to be published, will be flushed to the database
|
|
41
|
-
* when flush() is called (either manually or via interval)
|
|
42
|
-
*/
|
|
43
|
-
enqueue(msg: T) {
|
|
44
|
-
this.#currentBatch.messages.push(msg)
|
|
45
|
-
if(this.#currentBatch.messages.length >= this.#maxBatchSize) {
|
|
46
|
-
this.flush()
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if(this.#flushTimeout || !this.#flushIntervalMs) {
|
|
51
|
-
return
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
this.#flushTimeout = setTimeout(() => this.flush(), this.#flushIntervalMs)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async flush() {
|
|
58
|
-
if(!this.#currentBatch.messages.length) {
|
|
59
|
-
return
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const batch = this.#currentBatch
|
|
63
|
-
this.#currentBatch = { messages: [] }
|
|
64
|
-
clearTimeout(this.#flushTimeout)
|
|
65
|
-
this.#flushTimeout = undefined
|
|
66
|
-
|
|
67
|
-
await this.#flushTask
|
|
68
|
-
|
|
69
|
-
this.#flushTask = this.#publishBatch(batch)
|
|
70
|
-
return this.#flushTask
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async #publishBatch({ messages }: Batch<T>) {
|
|
74
|
-
const batch = ++this.#batch
|
|
75
|
-
try {
|
|
76
|
-
const ids = await this.#publish(...messages)
|
|
77
|
-
for(const [i, { id }] of ids.entries()) {
|
|
78
|
-
if(this.#shouldLog && !this.#shouldLog(messages[i])) {
|
|
79
|
-
continue
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
this.#logger
|
|
83
|
-
?.info({ batch, id, message: messages[i] }, 'published message')
|
|
84
|
-
}
|
|
85
|
-
} catch(err) {
|
|
86
|
-
this.#logger
|
|
87
|
-
?.error({ batch, err, msgs: messages }, 'failed to publish messages')
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|