@rotorsoft/act-pg 1.17.1 → 1.19.0
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/dist/.tsbuildinfo +1 -1
- package/dist/@types/postgres-store.d.ts +6 -6
- package/dist/@types/postgres-store.d.ts.map +1 -1
- package/dist/index.cjs +72 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +72 -14
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BlockedLease, Committed, EventMeta, Lease, Message, NotifyDisposer, Query, QueryStatsOptions, QueryStreams, QueryStreamsResult, Schema, Schemas, Store, StoreNotification, StreamFilter, StreamPosition, StreamStats, SubscribeInput } from "@rotorsoft/act";
|
|
1
|
+
import type { BlockedLease, Committed, EventMeta, Lease, Message, NotifyDisposer, Query, QueryStatsOptions, QueryStreams, QueryStreamsResult, Schema, Schemas, Store, StoreNotification, StreamFilter, StreamPosition, StreamStats, SubscribeInput, SubscribeResult } from "@rotorsoft/act";
|
|
2
2
|
import { type Encryption } from "@rotorsoft/act-crypto";
|
|
3
3
|
import pg from "pg";
|
|
4
4
|
type Config = Readonly<{
|
|
@@ -326,11 +326,11 @@ export declare class PostgresStore implements Store {
|
|
|
326
326
|
* @param streams - Streams to register with optional source.
|
|
327
327
|
* @returns subscribed count and current max watermark.
|
|
328
328
|
*/
|
|
329
|
-
subscribe(streams: SubscribeInput[], correlated_at?: number
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}>;
|
|
329
|
+
subscribe(streams: SubscribeInput[], correlated_at?: number, correlator?: {
|
|
330
|
+
key: string;
|
|
331
|
+
by: string;
|
|
332
|
+
millis: number;
|
|
333
|
+
}): Promise<SubscribeResult>;
|
|
334
334
|
/**
|
|
335
335
|
* Acknowledge and release leases after processing, updating stream positions.
|
|
336
336
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgres-store.d.ts","sourceRoot":"","sources":["../../src/postgres-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,SAAS,EACT,SAAS,EACT,KAAK,EAEL,OAAO,EACP,cAAc,EACd,KAAK,EACL,iBAAiB,EACjB,YAAY,EACZ,kBAAkB,EAClB,MAAM,EACN,OAAO,EACP,KAAK,EACL,iBAAiB,EACjB,YAAY,EACZ,cAAc,EACd,WAAW,EACX,cAAc,
|
|
1
|
+
{"version":3,"file":"postgres-store.d.ts","sourceRoot":"","sources":["../../src/postgres-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,SAAS,EACT,SAAS,EACT,KAAK,EAEL,OAAO,EACP,cAAc,EACd,KAAK,EACL,iBAAiB,EACjB,YAAY,EACZ,kBAAkB,EAClB,MAAM,EACN,OAAO,EACP,KAAK,EACL,iBAAiB,EACjB,YAAY,EACZ,cAAc,EACd,WAAW,EACX,cAAc,EACd,eAAe,EAChB,MAAM,gBAAgB,CAAC;AAUxB,OAAO,EAEL,KAAK,UAAU,EAGhB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,MAAM,IAAI,CAAC;AA0BpB,KAAK,MAAM,GAAG,QAAQ,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,cAAc,CAAC,EAAE,UAAU,CAAC;CAC7B,CAAC,GACA,EAAE,CAAC,UAAU,CAAC;AAqGhB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2GG;AACH,qBAAa,aAAc,YAAW,KAAK;IACzC,OAAO,CAAC,KAAK,CAAC;IACd,QAAQ,CAA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N,QAAQ,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,OAAO,EAAE,MAAM,OAAO,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,KACpE,OAAO,CAAC,IAAI,CAAC,GACjB,OAAO,CAAC,IAAI,CAAC,CA4Cf;IAED;;;;;;;;;;;;;;;OAeG;IACG,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAMhD;CACF"}
|
package/dist/index.cjs
CHANGED
|
@@ -335,13 +335,36 @@ var PostgresStore = class {
|
|
|
335
335
|
);
|
|
336
336
|
await client.query(
|
|
337
337
|
`CREATE TABLE IF NOT EXISTS ${this._fqc} (
|
|
338
|
-
|
|
338
|
+
key text PRIMARY KEY,
|
|
339
339
|
at int NOT NULL DEFAULT -1,
|
|
340
|
-
|
|
340
|
+
leased_by text,
|
|
341
|
+
leased_until timestamptz
|
|
341
342
|
) TABLESPACE pg_default;`
|
|
342
343
|
);
|
|
344
|
+
await client.query(`
|
|
345
|
+
DO $$
|
|
346
|
+
BEGIN
|
|
347
|
+
IF EXISTS (
|
|
348
|
+
SELECT 1 FROM information_schema.columns
|
|
349
|
+
WHERE table_schema = '${this.config.schema}'
|
|
350
|
+
AND table_name = '${this.config.table}_correlated'
|
|
351
|
+
AND column_name = 'id'
|
|
352
|
+
) THEN
|
|
353
|
+
ALTER TABLE ${this._fqc} ADD COLUMN IF NOT EXISTS key text;
|
|
354
|
+
ALTER TABLE ${this._fqc} ADD COLUMN IF NOT EXISTS leased_by text;
|
|
355
|
+
ALTER TABLE ${this._fqc} ADD COLUMN IF NOT EXISTS leased_until timestamptz;
|
|
356
|
+
UPDATE ${this._fqc} SET key = '' WHERE key IS NULL;
|
|
357
|
+
ALTER TABLE ${this._fqc}
|
|
358
|
+
DROP CONSTRAINT IF EXISTS ${this.config.table}_correlated_singleton;
|
|
359
|
+
ALTER TABLE ${this._fqc}
|
|
360
|
+
DROP CONSTRAINT IF EXISTS ${this.config.table}_correlated_pkey;
|
|
361
|
+
ALTER TABLE ${this._fqc} DROP COLUMN IF EXISTS id;
|
|
362
|
+
ALTER TABLE ${this._fqc} ALTER COLUMN key SET NOT NULL;
|
|
363
|
+
ALTER TABLE ${this._fqc} ADD PRIMARY KEY (key);
|
|
364
|
+
END IF;
|
|
365
|
+
END $$;`);
|
|
343
366
|
await client.query(
|
|
344
|
-
`INSERT INTO ${this._fqc} (
|
|
367
|
+
`INSERT INTO ${this._fqc} (key) VALUES ('') ON CONFLICT (key) DO NOTHING;`
|
|
345
368
|
);
|
|
346
369
|
await client.query(
|
|
347
370
|
`ALTER TABLE ${this._fqs}
|
|
@@ -806,7 +829,7 @@ var PostgresStore = class {
|
|
|
806
829
|
* @param streams - Streams to register with optional source.
|
|
807
830
|
* @returns subscribed count and current max watermark.
|
|
808
831
|
*/
|
|
809
|
-
async subscribe(streams, correlated_at) {
|
|
832
|
+
async subscribe(streams, correlated_at, correlator) {
|
|
810
833
|
const client = await this._client("subscribe");
|
|
811
834
|
try {
|
|
812
835
|
await client.query("BEGIN");
|
|
@@ -843,20 +866,61 @@ var PostgresStore = class {
|
|
|
843
866
|
[JSON.stringify(streams)]
|
|
844
867
|
);
|
|
845
868
|
}
|
|
846
|
-
|
|
869
|
+
let correlating;
|
|
870
|
+
if (correlator) {
|
|
871
|
+
const { rowCount } = await client.query(
|
|
872
|
+
`INSERT INTO ${this._fqc} (key, at, leased_by, leased_until)
|
|
873
|
+
SELECT $1,
|
|
874
|
+
GREATEST(COALESCE($2::int, -1),
|
|
875
|
+
COALESCE((SELECT at FROM ${this._fqc} WHERE key = ''), -1)),
|
|
876
|
+
$3,
|
|
877
|
+
-- A non-positive millis releases outright rather than
|
|
878
|
+
-- setting an expiry a hair in the future, which would still
|
|
879
|
+
-- refuse a successor asking in the same instant.
|
|
880
|
+
CASE WHEN $4::int > 0
|
|
881
|
+
THEN now() + ($4::int * interval '1 millisecond')
|
|
882
|
+
ELSE NULL END
|
|
883
|
+
ON CONFLICT (key) DO UPDATE
|
|
884
|
+
SET at = GREATEST(${this._fqc}.at, COALESCE($2::int, -1)),
|
|
885
|
+
leased_by = CASE WHEN $4::int > 0 THEN EXCLUDED.leased_by END,
|
|
886
|
+
leased_until = EXCLUDED.leased_until
|
|
887
|
+
WHERE ${this._fqc}.leased_until IS NULL
|
|
888
|
+
OR ${this._fqc}.leased_until < now()
|
|
889
|
+
OR ${this._fqc}.leased_by = EXCLUDED.leased_by`,
|
|
890
|
+
[
|
|
891
|
+
correlator.key,
|
|
892
|
+
correlated_at ?? null,
|
|
893
|
+
correlator.by,
|
|
894
|
+
correlator.millis
|
|
895
|
+
]
|
|
896
|
+
);
|
|
897
|
+
correlating = (rowCount ?? 0) > 0;
|
|
898
|
+
if (!correlating && correlated_at !== void 0)
|
|
899
|
+
await client.query(
|
|
900
|
+
`UPDATE ${this._fqc} SET at = GREATEST(at, $1::int) WHERE key = $2`,
|
|
901
|
+
[correlated_at, correlator.key]
|
|
902
|
+
);
|
|
903
|
+
if (correlated_at !== void 0)
|
|
904
|
+
await client.query(
|
|
905
|
+
`UPDATE ${this._fqc} SET at = GREATEST(at, $1::int) WHERE key = ''`,
|
|
906
|
+
[correlated_at]
|
|
907
|
+
);
|
|
908
|
+
} else if (correlated_at !== void 0)
|
|
847
909
|
await client.query(
|
|
848
|
-
`UPDATE ${this._fqc} SET at = GREATEST(at, $1::int) WHERE
|
|
910
|
+
`UPDATE ${this._fqc} SET at = GREATEST(at, $1::int) WHERE key = ''`,
|
|
849
911
|
[correlated_at]
|
|
850
912
|
);
|
|
851
913
|
const { rows } = await client.query(
|
|
852
914
|
`SELECT (SELECT COALESCE(MAX(at), -1) FROM ${this._fqs}) AS max,
|
|
853
|
-
(SELECT at FROM ${this._fqc} WHERE
|
|
915
|
+
(SELECT at FROM ${this._fqc} WHERE key = $1) AS correlated_at`,
|
|
916
|
+
[correlator?.key ?? ""]
|
|
854
917
|
);
|
|
855
918
|
await client.query("COMMIT");
|
|
856
919
|
return {
|
|
857
920
|
subscribed,
|
|
858
921
|
watermark: rows[0]?.max ?? -1,
|
|
859
|
-
correlated_at: Number(rows[0]?.correlated_at ?? -1)
|
|
922
|
+
correlated_at: Number(rows[0]?.correlated_at ?? -1),
|
|
923
|
+
...correlating === void 0 ? {} : { correlating }
|
|
860
924
|
};
|
|
861
925
|
} catch (error) {
|
|
862
926
|
await client.query("ROLLBACK").catch(() => {
|
|
@@ -1529,12 +1593,6 @@ var PostgresStore = class {
|
|
|
1529
1593
|
this._fqt
|
|
1530
1594
|
]);
|
|
1531
1595
|
const result = /* @__PURE__ */ new Map();
|
|
1532
|
-
const retired = full.filter((t) => t.snapshot === void 0).map((t) => t.stream);
|
|
1533
|
-
if (retired.length) {
|
|
1534
|
-
await client.query(`DELETE FROM ${this._fqs} WHERE stream = ANY($1)`, [
|
|
1535
|
-
retired
|
|
1536
|
-
]);
|
|
1537
|
-
}
|
|
1538
1596
|
for (const { stream, snapshot, meta } of full) {
|
|
1539
1597
|
const { rowCount } = await client.query(
|
|
1540
1598
|
`DELETE FROM ${this._fqt} WHERE stream = $1`,
|