@event-driven-io/emmett-postgresql 0.43.0-alpha.1 → 0.43.0-alpha.4

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/index.js CHANGED
@@ -1,6 +1,3 @@
1
- // src/eventStore/consumers/messageBatchProcessing/index.ts
2
- import "@event-driven-io/dumbo";
3
-
4
1
  // src/eventStore/schema/readLastMessageGlobalPosition.ts
5
2
  import { singleOrNull, SQL } from "@event-driven-io/dumbo";
6
3
 
@@ -58,6 +55,122 @@ var readLastMessageGlobalPosition = async (execute, options) => {
58
55
 
59
56
  // src/eventStore/schema/readMessagesBatch.ts
60
57
  import { mapRows, SQL as SQL2 } from "@event-driven-io/dumbo";
58
+ var readMessagesBatch = async (execute, options) => {
59
+ const from = "from" in options ? options.from : "after" in options ? options.after + 1n : 0n;
60
+ const batchSize = options && "batchSize" in options ? options.batchSize : options.to - options.from;
61
+ const fromCondition = from !== -0n ? `AND global_position >= ${from}` : "";
62
+ const toCondition = "to" in options ? `AND global_position <= ${options.to}` : "";
63
+ const limitCondition = "batchSize" in options ? `LIMIT ${options.batchSize}` : "";
64
+ const messages = await mapRows(
65
+ execute.query(
66
+ SQL2`
67
+ SELECT stream_id, stream_position, global_position, message_data, message_metadata, message_schema_version, message_type, message_id
68
+ FROM ${SQL2.identifier(messagesTable.name)}
69
+ WHERE partition = ${options?.partition ?? defaultTag} AND is_archived = FALSE AND transaction_id < pg_snapshot_xmin(pg_current_snapshot()) ${SQL2.plain(fromCondition)} ${SQL2.plain(toCondition)}
70
+ ORDER BY transaction_id, global_position
71
+ ${SQL2.plain(limitCondition)}`
72
+ ),
73
+ (row) => {
74
+ const rawEvent = {
75
+ type: row.message_type,
76
+ data: row.message_data,
77
+ metadata: row.message_metadata
78
+ };
79
+ const metadata = {
80
+ ..."metadata" in rawEvent ? rawEvent.metadata ?? {} : {},
81
+ messageId: row.message_id,
82
+ streamName: row.stream_id,
83
+ streamPosition: BigInt(row.stream_position),
84
+ globalPosition: BigInt(row.global_position)
85
+ };
86
+ return {
87
+ ...rawEvent,
88
+ kind: "Event",
89
+ metadata
90
+ };
91
+ }
92
+ );
93
+ return messages.length > 0 ? {
94
+ currentGlobalPosition: messages[messages.length - 1].metadata.globalPosition,
95
+ messages,
96
+ areMessagesLeft: messages.length === batchSize
97
+ } : {
98
+ currentGlobalPosition: "from" in options ? options.from : "after" in options ? options.after : 0n,
99
+ messages: [],
100
+ areMessagesLeft: false
101
+ };
102
+ };
103
+
104
+ // src/eventStore/consumers/messageBatchProcessing/index.ts
105
+ var DefaultPostgreSQLEventStoreProcessorBatchSize = 100;
106
+ var DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs = 50;
107
+ var postgreSQLEventStoreMessageBatchPuller = ({
108
+ executor,
109
+ batchSize,
110
+ eachBatch,
111
+ pullingFrequencyInMs,
112
+ stopWhen,
113
+ signal
114
+ }) => {
115
+ let isRunning = false;
116
+ let start;
117
+ const pullMessages = async (options) => {
118
+ const after = options.startFrom === "BEGINNING" ? 0n : options.startFrom === "END" ? (await readLastMessageGlobalPosition(executor)).currentGlobalPosition ?? 0n : options.startFrom.lastCheckpoint;
119
+ const readMessagesOptions = {
120
+ after,
121
+ batchSize
122
+ };
123
+ let waitTime = 100;
124
+ while (isRunning && !signal?.aborted) {
125
+ const { messages, currentGlobalPosition, areMessagesLeft } = await readMessagesBatch(executor, readMessagesOptions);
126
+ if (messages.length > 0) {
127
+ const result = await eachBatch(messages);
128
+ if (result && result.type === "STOP") {
129
+ isRunning = false;
130
+ break;
131
+ }
132
+ }
133
+ readMessagesOptions.after = currentGlobalPosition;
134
+ await new Promise((resolve) => setTimeout(resolve, waitTime));
135
+ if (stopWhen?.noMessagesLeft === true && !areMessagesLeft) {
136
+ isRunning = false;
137
+ break;
138
+ }
139
+ if (!areMessagesLeft) {
140
+ waitTime = Math.min(waitTime * 2, 1e3);
141
+ } else {
142
+ waitTime = pullingFrequencyInMs;
143
+ }
144
+ }
145
+ };
146
+ return {
147
+ get isRunning() {
148
+ return isRunning;
149
+ },
150
+ start: (options) => {
151
+ if (isRunning) return start;
152
+ isRunning = true;
153
+ start = (async () => {
154
+ return pullMessages(options);
155
+ })();
156
+ return start;
157
+ },
158
+ stop: async () => {
159
+ if (!isRunning) return;
160
+ isRunning = false;
161
+ await start;
162
+ }
163
+ };
164
+ };
165
+ var zipPostgreSQLEventStoreMessageBatchPullerStartFrom = (options) => {
166
+ if (options.length === 0 || options.some((o) => o === void 0 || o === "BEGINNING"))
167
+ return "BEGINNING";
168
+ if (options.every((o) => o === "END")) return "END";
169
+ return options.filter((o) => o !== void 0 && o !== "BEGINNING" && o !== "END").sort((a, b) => a > b ? 1 : -1)[0];
170
+ };
171
+
172
+ // src/eventStore/consumers/postgreSQLEventStoreConsumer.ts
173
+ import { dumbo as dumbo6 } from "@event-driven-io/dumbo";
61
174
 
62
175
  // ../emmett/dist/chunk-AZDDB5SF.js
63
176
  var isNumber = (val) => typeof val === "number" && val === val;
@@ -762,129 +875,11 @@ var projector = (options) => {
762
875
  };
763
876
  var projection = (definition) => definition;
764
877
 
765
- // src/eventStore/schema/readMessagesBatch.ts
766
- var readMessagesBatch = async (execute, options) => {
767
- const from = "from" in options ? options.from : "after" in options ? options.after + 1n : 0n;
768
- const batchSize = options && "batchSize" in options ? options.batchSize : options.to - options.from;
769
- const fromCondition = from !== -0n ? `AND global_position >= ${from}` : "";
770
- const toCondition = "to" in options ? `AND global_position <= ${options.to}` : "";
771
- const limitCondition = "batchSize" in options ? `LIMIT ${options.batchSize}` : "";
772
- const messages = await mapRows(
773
- execute.query(
774
- SQL2`
775
- SELECT stream_id, stream_position, global_position, message_data, message_metadata, message_schema_version, message_type, message_id
776
- FROM ${SQL2.identifier(messagesTable.name)}
777
- WHERE partition = ${options?.partition ?? defaultTag} AND is_archived = FALSE AND transaction_id < pg_snapshot_xmin(pg_current_snapshot()) ${SQL2.plain(fromCondition)} ${SQL2.plain(toCondition)}
778
- ORDER BY transaction_id, global_position
779
- ${SQL2.plain(limitCondition)}`
780
- ),
781
- (row) => {
782
- const rawEvent = {
783
- type: row.message_type,
784
- data: row.message_data,
785
- metadata: row.message_metadata
786
- };
787
- const metadata = {
788
- ..."metadata" in rawEvent ? rawEvent.metadata ?? {} : {},
789
- messageId: row.message_id,
790
- streamName: row.stream_id,
791
- streamPosition: BigInt(row.stream_position),
792
- globalPosition: BigInt(row.global_position)
793
- };
794
- return {
795
- ...rawEvent,
796
- kind: "Event",
797
- metadata
798
- };
799
- }
800
- );
801
- return messages.length > 0 ? {
802
- currentGlobalPosition: messages[messages.length - 1].metadata.globalPosition,
803
- messages,
804
- areMessagesLeft: messages.length === batchSize
805
- } : {
806
- currentGlobalPosition: "from" in options ? options.from : "after" in options ? options.after : 0n,
807
- messages: [],
808
- areMessagesLeft: false
809
- };
810
- };
811
-
812
- // src/eventStore/consumers/messageBatchProcessing/index.ts
813
- var DefaultPostgreSQLEventStoreProcessorBatchSize = 100;
814
- var DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs = 50;
815
- var postgreSQLEventStoreMessageBatchPuller = ({
816
- executor,
817
- batchSize,
818
- eachBatch,
819
- pullingFrequencyInMs,
820
- stopWhen,
821
- signal
822
- }) => {
823
- let isRunning = false;
824
- let start;
825
- const pullMessages = async (options) => {
826
- const after = options.startFrom === "BEGINNING" ? 0n : options.startFrom === "END" ? (await readLastMessageGlobalPosition(executor)).currentGlobalPosition ?? 0n : options.startFrom.lastCheckpoint;
827
- const readMessagesOptions = {
828
- after,
829
- batchSize
830
- };
831
- let waitTime = 100;
832
- while (isRunning && !signal?.aborted) {
833
- const { messages, currentGlobalPosition, areMessagesLeft } = await readMessagesBatch(executor, readMessagesOptions);
834
- if (messages.length > 0) {
835
- const result = await eachBatch(messages);
836
- if (result && result.type === "STOP") {
837
- isRunning = false;
838
- break;
839
- }
840
- }
841
- readMessagesOptions.after = currentGlobalPosition;
842
- await new Promise((resolve) => setTimeout(resolve, waitTime));
843
- if (stopWhen?.noMessagesLeft === true && !areMessagesLeft) {
844
- isRunning = false;
845
- break;
846
- }
847
- if (!areMessagesLeft) {
848
- waitTime = Math.min(waitTime * 2, 1e3);
849
- } else {
850
- waitTime = pullingFrequencyInMs;
851
- }
852
- }
853
- };
854
- return {
855
- get isRunning() {
856
- return isRunning;
857
- },
858
- start: (options) => {
859
- if (isRunning) return start;
860
- isRunning = true;
861
- start = (async () => {
862
- return pullMessages(options);
863
- })();
864
- return start;
865
- },
866
- stop: async () => {
867
- if (!isRunning) return;
868
- isRunning = false;
869
- await start;
870
- }
871
- };
872
- };
873
- var zipPostgreSQLEventStoreMessageBatchPullerStartFrom = (options) => {
874
- if (options.length === 0 || options.some((o) => o === void 0 || o === "BEGINNING"))
875
- return "BEGINNING";
876
- if (options.every((o) => o === "END")) return "END";
877
- return options.filter((o) => o !== void 0 && o !== "BEGINNING" && o !== "END").sort((a, b) => a > b ? 1 : -1)[0];
878
- };
879
-
880
878
  // src/eventStore/consumers/postgreSQLEventStoreConsumer.ts
881
- import { dumbo as dumbo6 } from "@event-driven-io/dumbo";
882
879
  import { v7 as uuid7 } from "uuid";
883
880
 
884
881
  // src/eventStore/consumers/postgreSQLProcessor.ts
885
882
  import { dumbo as dumbo5 } from "@event-driven-io/dumbo";
886
- import "@event-driven-io/dumbo/pg";
887
- import "pg";
888
883
 
889
884
  // src/eventStore/projections/locks/tryAcquireProcessorLock.ts
890
885
  import { single } from "@event-driven-io/dumbo";
@@ -1575,7 +1570,6 @@ var pongoSingleStreamProjection = (options) => {
1575
1570
  };
1576
1571
 
1577
1572
  // src/eventStore/projections/pongo/pongoProjectionSpec.ts
1578
- import "@event-driven-io/dumbo";
1579
1573
  import {
1580
1574
  pongoClient as pongoClient2
1581
1575
  } from "@event-driven-io/pongo";
@@ -1717,7 +1711,6 @@ var expectPongoDocuments = {
1717
1711
  import {
1718
1712
  dumbo as dumbo4
1719
1713
  } from "@event-driven-io/dumbo";
1720
- import "@event-driven-io/dumbo/pg";
1721
1714
  import { v4 as uuid6 } from "uuid";
1722
1715
 
1723
1716
  // src/eventStore/postgreSQLEventStore.ts
@@ -1727,8 +1720,6 @@ import {
1727
1720
  getFormatter,
1728
1721
  SQL as SQL20
1729
1722
  } from "@event-driven-io/dumbo";
1730
- import "@event-driven-io/dumbo/pg";
1731
- import "pg";
1732
1723
 
1733
1724
  // src/eventStore/schema/index.ts
1734
1725
  import {
@@ -1738,8 +1729,12 @@ import {
1738
1729
  } from "@event-driven-io/dumbo";
1739
1730
 
1740
1731
  // src/eventStore/schema/appendToStream.ts
1741
- import { single as single4, SQL as SQL8 } from "@event-driven-io/dumbo";
1742
- import "@event-driven-io/dumbo/pg";
1732
+ import {
1733
+ DumboError,
1734
+ single as single4,
1735
+ SQL as SQL8,
1736
+ UniqueConstraintError
1737
+ } from "@event-driven-io/dumbo";
1743
1738
  import { v4 as uuid5 } from "uuid";
1744
1739
  var appendToStreamSQL = createFunctionIfDoesNotExistSQL(
1745
1740
  "emt_append_to_stream",
@@ -1919,7 +1914,9 @@ var toExpectedVersion = (expected) => {
1919
1914
  if (expected == STREAM_EXISTS) return null;
1920
1915
  return expected;
1921
1916
  };
1922
- var isOptimisticConcurrencyError = (error) => error instanceof Error && "code" in error && error.code === "23505";
1917
+ var isOptimisticConcurrencyError = (error) => DumboError.isInstanceOf(error, {
1918
+ errorType: UniqueConstraintError.ErrorType
1919
+ });
1923
1920
  var appendEventsRaw = (execute, streamId, streamType, messages, options) => single4(
1924
1921
  execute.command(
1925
1922
  callAppendToStream({
@@ -4467,8 +4464,6 @@ var expectSQL = {
4467
4464
  };
4468
4465
 
4469
4466
  // src/eventStore/projections/postgreSQLProjection.ts
4470
- import "@event-driven-io/dumbo";
4471
- import "@event-driven-io/dumbo/pg";
4472
4467
  var transactionToPostgreSQLProjectionHandlerContext = async (connectionString, pool, transaction) => ({
4473
4468
  execute: transaction.execute,
4474
4469
  connection: {