@event-driven-io/emmett-postgresql 0.38.3 → 0.38.5
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.cjs +218 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +216 -24
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -84,24 +84,117 @@ var ExpectedVersionConflictError = class _ExpectedVersionConflictError extends C
|
|
|
84
84
|
Object.setPrototypeOf(this, _ExpectedVersionConflictError.prototype);
|
|
85
85
|
}
|
|
86
86
|
};
|
|
87
|
-
var
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
var isPrimitive = (value) => {
|
|
88
|
+
const type = typeof value;
|
|
89
|
+
return value === null || value === void 0 || type === "boolean" || type === "number" || type === "string" || type === "symbol" || type === "bigint";
|
|
90
|
+
};
|
|
91
|
+
var compareArrays = (left, right) => {
|
|
92
|
+
if (left.length !== right.length) {
|
|
93
|
+
return false;
|
|
90
94
|
}
|
|
91
|
-
|
|
92
|
-
|
|
95
|
+
for (let i = 0; i < left.length; i++) {
|
|
96
|
+
const leftHas = i in left;
|
|
97
|
+
const rightHas = i in right;
|
|
98
|
+
if (leftHas !== rightHas) return false;
|
|
99
|
+
if (leftHas && !deepEquals(left[i], right[i])) return false;
|
|
93
100
|
}
|
|
94
|
-
|
|
95
|
-
|
|
101
|
+
return true;
|
|
102
|
+
};
|
|
103
|
+
var compareDates = (left, right) => {
|
|
104
|
+
return left.getTime() === right.getTime();
|
|
105
|
+
};
|
|
106
|
+
var compareRegExps = (left, right) => {
|
|
107
|
+
return left.toString() === right.toString();
|
|
108
|
+
};
|
|
109
|
+
var compareErrors = (left, right) => {
|
|
110
|
+
if (left.message !== right.message || left.name !== right.name) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
const leftKeys = Object.keys(left);
|
|
114
|
+
const rightKeys = Object.keys(right);
|
|
115
|
+
if (leftKeys.length !== rightKeys.length) return false;
|
|
116
|
+
const rightKeySet = new Set(rightKeys);
|
|
117
|
+
for (const key of leftKeys) {
|
|
118
|
+
if (!rightKeySet.has(key)) return false;
|
|
119
|
+
if (!deepEquals(left[key], right[key])) return false;
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
};
|
|
123
|
+
var compareMaps = (left, right) => {
|
|
124
|
+
if (left.size !== right.size) return false;
|
|
125
|
+
for (const [key, value] of left) {
|
|
126
|
+
if (isPrimitive(key)) {
|
|
127
|
+
if (!right.has(key) || !deepEquals(value, right.get(key))) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
} else {
|
|
131
|
+
let found = false;
|
|
132
|
+
for (const [rightKey, rightValue] of right) {
|
|
133
|
+
if (deepEquals(key, rightKey) && deepEquals(value, rightValue)) {
|
|
134
|
+
found = true;
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (!found) return false;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return true;
|
|
142
|
+
};
|
|
143
|
+
var compareSets = (left, right) => {
|
|
144
|
+
if (left.size !== right.size) return false;
|
|
145
|
+
for (const leftItem of left) {
|
|
146
|
+
if (isPrimitive(leftItem)) {
|
|
147
|
+
if (!right.has(leftItem)) return false;
|
|
148
|
+
} else {
|
|
149
|
+
let found = false;
|
|
150
|
+
for (const rightItem of right) {
|
|
151
|
+
if (deepEquals(leftItem, rightItem)) {
|
|
152
|
+
found = true;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (!found) return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
};
|
|
161
|
+
var compareArrayBuffers = (left, right) => {
|
|
162
|
+
if (left.byteLength !== right.byteLength) return false;
|
|
163
|
+
const leftView = new Uint8Array(left);
|
|
164
|
+
const rightView = new Uint8Array(right);
|
|
165
|
+
for (let i = 0; i < leftView.length; i++) {
|
|
166
|
+
if (leftView[i] !== rightView[i]) return false;
|
|
96
167
|
}
|
|
97
|
-
|
|
168
|
+
return true;
|
|
169
|
+
};
|
|
170
|
+
var compareTypedArrays = (left, right) => {
|
|
171
|
+
if (left.constructor !== right.constructor) return false;
|
|
172
|
+
if (left.byteLength !== right.byteLength) return false;
|
|
173
|
+
const leftArray = new Uint8Array(
|
|
174
|
+
left.buffer,
|
|
175
|
+
left.byteOffset,
|
|
176
|
+
left.byteLength
|
|
177
|
+
);
|
|
178
|
+
const rightArray = new Uint8Array(
|
|
179
|
+
right.buffer,
|
|
180
|
+
right.byteOffset,
|
|
181
|
+
right.byteLength
|
|
182
|
+
);
|
|
183
|
+
for (let i = 0; i < leftArray.length; i++) {
|
|
184
|
+
if (leftArray[i] !== rightArray[i]) return false;
|
|
185
|
+
}
|
|
186
|
+
return true;
|
|
187
|
+
};
|
|
188
|
+
var compareObjects = (left, right) => {
|
|
98
189
|
const keys1 = Object.keys(left);
|
|
99
190
|
const keys2 = Object.keys(right);
|
|
100
|
-
if (keys1.length !== keys2.length
|
|
191
|
+
if (keys1.length !== keys2.length) {
|
|
101
192
|
return false;
|
|
102
|
-
|
|
103
|
-
|
|
193
|
+
}
|
|
194
|
+
for (const key of keys1) {
|
|
195
|
+
if (left[key] instanceof Function && right[key] instanceof Function) {
|
|
104
196
|
continue;
|
|
197
|
+
}
|
|
105
198
|
const isEqual = deepEquals(left[key], right[key]);
|
|
106
199
|
if (!isEqual) {
|
|
107
200
|
return false;
|
|
@@ -109,8 +202,88 @@ var deepEquals = (left, right) => {
|
|
|
109
202
|
}
|
|
110
203
|
return true;
|
|
111
204
|
};
|
|
205
|
+
var getType = (value) => {
|
|
206
|
+
if (value === null) return "null";
|
|
207
|
+
if (value === void 0) return "undefined";
|
|
208
|
+
const primitiveType = typeof value;
|
|
209
|
+
if (primitiveType !== "object") return primitiveType;
|
|
210
|
+
if (Array.isArray(value)) return "array";
|
|
211
|
+
if (value instanceof Boolean) return "boxed-boolean";
|
|
212
|
+
if (value instanceof Number) return "boxed-number";
|
|
213
|
+
if (value instanceof String) return "boxed-string";
|
|
214
|
+
if (value instanceof Date) return "date";
|
|
215
|
+
if (value instanceof RegExp) return "regexp";
|
|
216
|
+
if (value instanceof Error) return "error";
|
|
217
|
+
if (value instanceof Map) return "map";
|
|
218
|
+
if (value instanceof Set) return "set";
|
|
219
|
+
if (value instanceof ArrayBuffer) return "arraybuffer";
|
|
220
|
+
if (value instanceof DataView) return "dataview";
|
|
221
|
+
if (value instanceof WeakMap) return "weakmap";
|
|
222
|
+
if (value instanceof WeakSet) return "weakset";
|
|
223
|
+
if (ArrayBuffer.isView(value)) return "typedarray";
|
|
224
|
+
return "object";
|
|
225
|
+
};
|
|
226
|
+
var deepEquals = (left, right) => {
|
|
227
|
+
if (left === right) return true;
|
|
228
|
+
if (isEquatable(left)) {
|
|
229
|
+
return left.equals(right);
|
|
230
|
+
}
|
|
231
|
+
const leftType = getType(left);
|
|
232
|
+
const rightType = getType(right);
|
|
233
|
+
if (leftType !== rightType) return false;
|
|
234
|
+
switch (leftType) {
|
|
235
|
+
case "null":
|
|
236
|
+
case "undefined":
|
|
237
|
+
case "boolean":
|
|
238
|
+
case "number":
|
|
239
|
+
case "bigint":
|
|
240
|
+
case "string":
|
|
241
|
+
case "symbol":
|
|
242
|
+
case "function":
|
|
243
|
+
return left === right;
|
|
244
|
+
case "array":
|
|
245
|
+
return compareArrays(left, right);
|
|
246
|
+
case "date":
|
|
247
|
+
return compareDates(left, right);
|
|
248
|
+
case "regexp":
|
|
249
|
+
return compareRegExps(left, right);
|
|
250
|
+
case "error":
|
|
251
|
+
return compareErrors(left, right);
|
|
252
|
+
case "map":
|
|
253
|
+
return compareMaps(
|
|
254
|
+
left,
|
|
255
|
+
right
|
|
256
|
+
);
|
|
257
|
+
case "set":
|
|
258
|
+
return compareSets(left, right);
|
|
259
|
+
case "arraybuffer":
|
|
260
|
+
return compareArrayBuffers(left, right);
|
|
261
|
+
case "dataview":
|
|
262
|
+
case "weakmap":
|
|
263
|
+
case "weakset":
|
|
264
|
+
return false;
|
|
265
|
+
case "typedarray":
|
|
266
|
+
return compareTypedArrays(
|
|
267
|
+
left,
|
|
268
|
+
right
|
|
269
|
+
);
|
|
270
|
+
case "boxed-boolean":
|
|
271
|
+
return left.valueOf() === right.valueOf();
|
|
272
|
+
case "boxed-number":
|
|
273
|
+
return left.valueOf() === right.valueOf();
|
|
274
|
+
case "boxed-string":
|
|
275
|
+
return left.valueOf() === right.valueOf();
|
|
276
|
+
case "object":
|
|
277
|
+
return compareObjects(
|
|
278
|
+
left,
|
|
279
|
+
right
|
|
280
|
+
);
|
|
281
|
+
default:
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
};
|
|
112
285
|
var isEquatable = (left) => {
|
|
113
|
-
return left && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
|
|
286
|
+
return left !== null && left !== void 0 && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
|
|
114
287
|
};
|
|
115
288
|
var ParseError = class extends Error {
|
|
116
289
|
constructor(text) {
|
|
@@ -804,9 +977,15 @@ var appendToStreamSQL = _dumbo.rawSql.call(void 0,
|
|
|
804
977
|
v_transaction_id := pg_current_xact_id();
|
|
805
978
|
|
|
806
979
|
IF v_expected_stream_position IS NULL THEN
|
|
807
|
-
SELECT COALESCE(
|
|
808
|
-
|
|
809
|
-
|
|
980
|
+
SELECT COALESCE(
|
|
981
|
+
(SELECT stream_position
|
|
982
|
+
FROM ${streamsTable.name}
|
|
983
|
+
WHERE stream_id = v_stream_id
|
|
984
|
+
AND partition = v_partition
|
|
985
|
+
AND is_archived = FALSE
|
|
986
|
+
LIMIT 1),
|
|
987
|
+
0
|
|
988
|
+
) INTO v_expected_stream_position;
|
|
810
989
|
END IF;
|
|
811
990
|
|
|
812
991
|
v_next_stream_position := v_expected_stream_position + array_upper(v_messages_data, 1);
|
|
@@ -1060,9 +1239,12 @@ var streamsTableSQL = _dumbo.rawSql.call(void 0,
|
|
|
1060
1239
|
stream_type TEXT NOT NULL,
|
|
1061
1240
|
stream_metadata JSONB NOT NULL,
|
|
1062
1241
|
is_archived BOOLEAN NOT NULL DEFAULT FALSE,
|
|
1063
|
-
PRIMARY KEY (stream_id,
|
|
1064
|
-
|
|
1065
|
-
|
|
1242
|
+
PRIMARY KEY (stream_id, partition, is_archived)
|
|
1243
|
+
) PARTITION BY LIST (partition);
|
|
1244
|
+
|
|
1245
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_streams_unique
|
|
1246
|
+
ON ${streamsTable.name}(stream_id, partition, is_archived)
|
|
1247
|
+
INCLUDE (stream_position);`
|
|
1066
1248
|
);
|
|
1067
1249
|
var messagesTableSQL = _dumbo.rawSql.call(void 0,
|
|
1068
1250
|
`
|
|
@@ -2120,12 +2302,16 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
2120
2302
|
...options.connectionOptions ? options.connectionOptions : {}
|
|
2121
2303
|
};
|
|
2122
2304
|
const pool = "dumbo" in poolOptions ? poolOptions.dumbo : _dumbo.dumbo.call(void 0, poolOptions);
|
|
2123
|
-
let migrateSchema;
|
|
2305
|
+
let migrateSchema = void 0;
|
|
2124
2306
|
const autoGenerateSchema = _optionalChain([options, 'access', _83 => _83.schema, 'optionalAccess', _84 => _84.autoMigration]) === void 0 || _optionalChain([options, 'access', _85 => _85.schema, 'optionalAccess', _86 => _86.autoMigration]) !== "None";
|
|
2125
2307
|
const ensureSchemaExists = () => {
|
|
2126
2308
|
if (!autoGenerateSchema) return Promise.resolve();
|
|
2127
2309
|
if (!migrateSchema) {
|
|
2128
|
-
migrateSchema = createEventStoreSchema(pool)
|
|
2310
|
+
migrateSchema = createEventStoreSchema(pool).then(async () => {
|
|
2311
|
+
if (_optionalChain([options, 'access', _87 => _87.hooks, 'optionalAccess', _88 => _88.onAfterSchemaCreated])) {
|
|
2312
|
+
await options.hooks.onAfterSchemaCreated();
|
|
2313
|
+
}
|
|
2314
|
+
});
|
|
2129
2315
|
}
|
|
2130
2316
|
return migrateSchema;
|
|
2131
2317
|
};
|
|
@@ -2151,7 +2337,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
2151
2337
|
},
|
|
2152
2338
|
async aggregateStream(streamName, options2) {
|
|
2153
2339
|
const { evolve, initialState, read } = options2;
|
|
2154
|
-
const expectedStreamVersion = _optionalChain([read, 'optionalAccess',
|
|
2340
|
+
const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _89 => _89.expectedStreamVersion]);
|
|
2155
2341
|
let state = initialState();
|
|
2156
2342
|
const result = await this.readStream(streamName, options2.read);
|
|
2157
2343
|
const currentStreamVersion = result.currentStreamVersion;
|
|
@@ -2192,7 +2378,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
2192
2378
|
throw new ExpectedVersionConflictError(
|
|
2193
2379
|
-1n,
|
|
2194
2380
|
//TODO: Return actual version in case of error
|
|
2195
|
-
_nullishCoalesce(_optionalChain([options2, 'optionalAccess',
|
|
2381
|
+
_nullishCoalesce(_optionalChain([options2, 'optionalAccess', _90 => _90.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
|
|
2196
2382
|
);
|
|
2197
2383
|
return {
|
|
2198
2384
|
nextExpectedStreamVersion: appendResult.nextStreamPosition,
|
|
@@ -2212,16 +2398,22 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
2212
2398
|
...options,
|
|
2213
2399
|
connectionOptions: {
|
|
2214
2400
|
connection
|
|
2401
|
+
},
|
|
2402
|
+
schema: {
|
|
2403
|
+
..._nullishCoalesce(options.schema, () => ( {})),
|
|
2404
|
+
autoMigration: "None"
|
|
2215
2405
|
}
|
|
2216
2406
|
};
|
|
2217
2407
|
const eventStore = getPostgreSQLEventStore(
|
|
2218
2408
|
connectionString,
|
|
2219
2409
|
storeOptions
|
|
2220
2410
|
);
|
|
2221
|
-
return
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2411
|
+
return ensureSchemaExists().then(
|
|
2412
|
+
() => callback({
|
|
2413
|
+
eventStore,
|
|
2414
|
+
close: () => Promise.resolve()
|
|
2415
|
+
})
|
|
2416
|
+
);
|
|
2225
2417
|
});
|
|
2226
2418
|
}
|
|
2227
2419
|
};
|