@event-driven-io/emmett-postgresql 0.40.1 → 0.41.0-alpha.2
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 +102 -45
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -4
- package/dist/index.d.ts +21 -4
- package/dist/index.js +82 -25
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1487,15 +1487,33 @@ var schemaSQL = [
|
|
|
1487
1487
|
addDefaultPartitionSQL,
|
|
1488
1488
|
storeSubscriptionCheckpointSQL
|
|
1489
1489
|
];
|
|
1490
|
-
var createEventStoreSchema = async (pool) => {
|
|
1491
|
-
await pool.withTransaction((
|
|
1490
|
+
var createEventStoreSchema = async (connectionString, pool, hooks) => {
|
|
1491
|
+
await pool.withTransaction(async (tx) => {
|
|
1492
|
+
const client = await tx.connection.open();
|
|
1493
|
+
const context = {
|
|
1494
|
+
execute: tx.execute,
|
|
1495
|
+
connection: {
|
|
1496
|
+
connectionString,
|
|
1497
|
+
client,
|
|
1498
|
+
transaction: tx,
|
|
1499
|
+
pool
|
|
1500
|
+
}
|
|
1501
|
+
};
|
|
1502
|
+
if (_optionalChain([hooks, 'optionalAccess', _44 => _44.onBeforeSchemaCreated])) {
|
|
1503
|
+
await hooks.onBeforeSchemaCreated(context);
|
|
1504
|
+
}
|
|
1505
|
+
await context.execute.batchCommand(schemaSQL);
|
|
1506
|
+
});
|
|
1507
|
+
if (_optionalChain([hooks, 'optionalAccess', _45 => _45.onAfterSchemaCreated])) {
|
|
1508
|
+
await hooks.onAfterSchemaCreated();
|
|
1509
|
+
}
|
|
1492
1510
|
};
|
|
1493
1511
|
|
|
1494
1512
|
// src/eventStore/consumers/postgreSQLProcessor.ts
|
|
1495
1513
|
var postgreSQLCheckpointer = () => ({
|
|
1496
1514
|
read: async (options, context) => {
|
|
1497
1515
|
const result = await readProcessorCheckpoint(context.execute, options);
|
|
1498
|
-
return { lastCheckpoint: _optionalChain([result, 'optionalAccess',
|
|
1516
|
+
return { lastCheckpoint: _optionalChain([result, 'optionalAccess', _46 => _46.lastProcessedPosition]) };
|
|
1499
1517
|
},
|
|
1500
1518
|
store: async (options, context) => {
|
|
1501
1519
|
const newPosition = getCheckpoint(options.message);
|
|
@@ -1513,13 +1531,13 @@ var postgreSQLProcessingScope = (options) => {
|
|
|
1513
1531
|
const processorConnectionString = options.connectionString;
|
|
1514
1532
|
const processorPool = options.pool;
|
|
1515
1533
|
const processingScope = async (handler, partialContext) => {
|
|
1516
|
-
const connection = _optionalChain([partialContext, 'optionalAccess',
|
|
1517
|
-
const connectionString = _nullishCoalesce(processorConnectionString, () => ( _optionalChain([connection, 'optionalAccess',
|
|
1534
|
+
const connection = _optionalChain([partialContext, 'optionalAccess', _47 => _47.connection]);
|
|
1535
|
+
const connectionString = _nullishCoalesce(processorConnectionString, () => ( _optionalChain([connection, 'optionalAccess', _48 => _48.connectionString])));
|
|
1518
1536
|
if (!connectionString)
|
|
1519
1537
|
throw new EmmettError(
|
|
1520
1538
|
`PostgreSQL processor '${options.processorId}' is missing connection string. Ensure that you passed it through options`
|
|
1521
1539
|
);
|
|
1522
|
-
const pool = _nullishCoalesce((!processorConnectionString || connectionString == processorConnectionString ? _optionalChain([connection, 'optionalAccess',
|
|
1540
|
+
const pool = _nullishCoalesce((!processorConnectionString || connectionString == processorConnectionString ? _optionalChain([connection, 'optionalAccess', _49 => _49.pool]) : processorPool), () => ( processorPool));
|
|
1523
1541
|
if (!pool)
|
|
1524
1542
|
throw new EmmettError(
|
|
1525
1543
|
`PostgreSQL processor '${options.processorId}' is missing connection string. Ensure that you passed it through options`
|
|
@@ -1558,9 +1576,14 @@ var getProcessorPool = (options) => {
|
|
|
1558
1576
|
var postgreSQLProjector = (options) => {
|
|
1559
1577
|
const { pool, connectionString, close } = getProcessorPool(options);
|
|
1560
1578
|
const hooks = {
|
|
1561
|
-
onStart:
|
|
1562
|
-
|
|
1563
|
-
if (
|
|
1579
|
+
onStart: async (context) => {
|
|
1580
|
+
if (_optionalChain([options, 'access', _50 => _50.hooks, 'optionalAccess', _51 => _51.onStart])) await options.hooks.onStart(context);
|
|
1581
|
+
if (options.projection.init) {
|
|
1582
|
+
await options.projection.init(context);
|
|
1583
|
+
}
|
|
1584
|
+
},
|
|
1585
|
+
onClose: _optionalChain([options, 'access', _52 => _52.hooks, 'optionalAccess', _53 => _53.onClose]) || close ? async () => {
|
|
1586
|
+
if (_optionalChain([options, 'access', _54 => _54.hooks, 'optionalAccess', _55 => _55.onClose])) await _optionalChain([options, 'access', _56 => _56.hooks, 'optionalAccess', _57 => _57.onClose, 'call', _58 => _58()]);
|
|
1564
1587
|
if (close) await close();
|
|
1565
1588
|
} : void 0
|
|
1566
1589
|
};
|
|
@@ -1578,9 +1601,9 @@ var postgreSQLProjector = (options) => {
|
|
|
1578
1601
|
var postgreSQLReactor = (options) => {
|
|
1579
1602
|
const { pool, connectionString, close } = getProcessorPool(options);
|
|
1580
1603
|
const hooks = {
|
|
1581
|
-
onStart: _optionalChain([options, 'access',
|
|
1582
|
-
onClose: _optionalChain([options, 'access',
|
|
1583
|
-
if (_optionalChain([options, 'access',
|
|
1604
|
+
onStart: _optionalChain([options, 'access', _59 => _59.hooks, 'optionalAccess', _60 => _60.onStart]),
|
|
1605
|
+
onClose: _optionalChain([options, 'access', _61 => _61.hooks, 'optionalAccess', _62 => _62.onClose]) || close ? async () => {
|
|
1606
|
+
if (_optionalChain([options, 'access', _63 => _63.hooks, 'optionalAccess', _64 => _64.onClose])) await _optionalChain([options, 'access', _65 => _65.hooks, 'optionalAccess', _66 => _66.onClose, 'call', _67 => _67()]);
|
|
1584
1607
|
if (close) await close();
|
|
1585
1608
|
} : void 0
|
|
1586
1609
|
};
|
|
@@ -1630,7 +1653,7 @@ var postgreSQLEventStoreConsumer = (options) => {
|
|
|
1630
1653
|
})
|
|
1631
1654
|
);
|
|
1632
1655
|
return result.some(
|
|
1633
|
-
(r) => r.status === "fulfilled" && _optionalChain([r, 'access',
|
|
1656
|
+
(r) => r.status === "fulfilled" && _optionalChain([r, 'access', _68 => _68.value, 'optionalAccess', _69 => _69.type]) !== "STOP"
|
|
1634
1657
|
) ? void 0 : {
|
|
1635
1658
|
type: "STOP"
|
|
1636
1659
|
};
|
|
@@ -1639,8 +1662,8 @@ var postgreSQLEventStoreConsumer = (options) => {
|
|
|
1639
1662
|
stopWhen: options.stopWhen,
|
|
1640
1663
|
executor: pool.execute,
|
|
1641
1664
|
eachBatch,
|
|
1642
|
-
batchSize: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess',
|
|
1643
|
-
pullingFrequencyInMs: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess',
|
|
1665
|
+
batchSize: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _70 => _70.batchSize]), () => ( DefaultPostgreSQLEventStoreProcessorBatchSize)),
|
|
1666
|
+
pullingFrequencyInMs: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _71 => _71.pullingFrequencyInMs]), () => ( DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs))
|
|
1644
1667
|
});
|
|
1645
1668
|
const stop = async () => {
|
|
1646
1669
|
if (!isRunning) return;
|
|
@@ -1799,6 +1822,15 @@ var pongoMultiStreamProjection = (options) => {
|
|
|
1799
1822
|
connectionOptions: { client, pool }
|
|
1800
1823
|
});
|
|
1801
1824
|
await pongo.db().collection(collectionName).deleteMany();
|
|
1825
|
+
},
|
|
1826
|
+
init: async (context) => {
|
|
1827
|
+
const {
|
|
1828
|
+
connection: { connectionString, client, pool }
|
|
1829
|
+
} = context;
|
|
1830
|
+
const pongo = _pongo.pongoClient.call(void 0, connectionString, {
|
|
1831
|
+
connectionOptions: { client, pool }
|
|
1832
|
+
});
|
|
1833
|
+
await pongo.db().collection(collectionName).schema.migrate();
|
|
1802
1834
|
}
|
|
1803
1835
|
});
|
|
1804
1836
|
};
|
|
@@ -1961,7 +1993,7 @@ var PostgreSQLProjectionSpec = {
|
|
|
1961
1993
|
const allEvents = [];
|
|
1962
1994
|
const run = async (pool) => {
|
|
1963
1995
|
let globalPosition = 0n;
|
|
1964
|
-
const numberOfTimes = _nullishCoalesce(_optionalChain([options2, 'optionalAccess',
|
|
1996
|
+
const numberOfTimes = _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _72 => _72.numberOfTimes]), () => ( 1));
|
|
1965
1997
|
for (const event of [
|
|
1966
1998
|
...givenEvents,
|
|
1967
1999
|
...Array.from({ length: numberOfTimes }).flatMap(() => events)
|
|
@@ -2018,18 +2050,18 @@ var PostgreSQLProjectionSpec = {
|
|
|
2018
2050
|
if (!isErrorConstructor(args[0])) {
|
|
2019
2051
|
assertTrue(
|
|
2020
2052
|
args[0](error),
|
|
2021
|
-
`Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess',
|
|
2053
|
+
`Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _73 => _73.toString, 'call', _74 => _74()])}`
|
|
2022
2054
|
);
|
|
2023
2055
|
return;
|
|
2024
2056
|
}
|
|
2025
2057
|
assertTrue(
|
|
2026
2058
|
error instanceof args[0],
|
|
2027
|
-
`Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess',
|
|
2059
|
+
`Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _75 => _75.toString, 'call', _76 => _76()])}`
|
|
2028
2060
|
);
|
|
2029
2061
|
if (args[1]) {
|
|
2030
2062
|
assertTrue(
|
|
2031
2063
|
args[1](error),
|
|
2032
|
-
`Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess',
|
|
2064
|
+
`Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _77 => _77.toString, 'call', _78 => _78()])}`
|
|
2033
2065
|
);
|
|
2034
2066
|
}
|
|
2035
2067
|
} finally {
|
|
@@ -2048,7 +2080,7 @@ var eventInStream = (streamName, event) => {
|
|
|
2048
2080
|
...event,
|
|
2049
2081
|
metadata: {
|
|
2050
2082
|
..._nullishCoalesce(event.metadata, () => ( {})),
|
|
2051
|
-
streamName: _nullishCoalesce(_optionalChain([event, 'access',
|
|
2083
|
+
streamName: _nullishCoalesce(_optionalChain([event, 'access', _79 => _79.metadata, 'optionalAccess', _80 => _80.streamName]), () => ( streamName))
|
|
2052
2084
|
}
|
|
2053
2085
|
};
|
|
2054
2086
|
};
|
|
@@ -2094,23 +2126,39 @@ var handleProjections = async (options) => {
|
|
|
2094
2126
|
}
|
|
2095
2127
|
};
|
|
2096
2128
|
var postgreSQLProjection = (definition) => projection(definition);
|
|
2097
|
-
var postgreSQLRawBatchSQLProjection = (
|
|
2098
|
-
canHandle,
|
|
2129
|
+
var postgreSQLRawBatchSQLProjection = (options) => postgreSQLProjection({
|
|
2130
|
+
canHandle: options.canHandle,
|
|
2099
2131
|
handle: async (events, context) => {
|
|
2100
|
-
const sqls = await
|
|
2132
|
+
const sqls = await options.evolve(events, context);
|
|
2101
2133
|
await context.execute.batchCommand(sqls);
|
|
2134
|
+
},
|
|
2135
|
+
init: async (context) => {
|
|
2136
|
+
if (options.init) {
|
|
2137
|
+
await options.init(context);
|
|
2138
|
+
}
|
|
2139
|
+
if (options.initSQL) {
|
|
2140
|
+
await context.execute.command(options.initSQL);
|
|
2141
|
+
}
|
|
2102
2142
|
}
|
|
2103
2143
|
});
|
|
2104
|
-
var postgreSQLRawSQLProjection = (
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2144
|
+
var postgreSQLRawSQLProjection = (options) => {
|
|
2145
|
+
const { evolve, ...rest } = options;
|
|
2146
|
+
return postgreSQLRawBatchSQLProjection({
|
|
2147
|
+
...rest,
|
|
2148
|
+
evolve: async (events, context) => {
|
|
2149
|
+
const sqls = [];
|
|
2150
|
+
for (const event of events) {
|
|
2151
|
+
const pendingSqls = await evolve(event, context);
|
|
2152
|
+
if (Array.isArray(pendingSqls)) {
|
|
2153
|
+
sqls.push(...pendingSqls);
|
|
2154
|
+
} else {
|
|
2155
|
+
sqls.push(pendingSqls);
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
return sqls;
|
|
2109
2159
|
}
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
...canHandle
|
|
2113
|
-
);
|
|
2160
|
+
});
|
|
2161
|
+
};
|
|
2114
2162
|
|
|
2115
2163
|
// src/eventStore/postgreSQLEventStore.ts
|
|
2116
2164
|
var defaultPostgreSQLOptions = {
|
|
@@ -2125,19 +2173,30 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
2125
2173
|
};
|
|
2126
2174
|
const pool = "dumbo" in poolOptions ? poolOptions.dumbo : _dumbo.dumbo.call(void 0, poolOptions);
|
|
2127
2175
|
let migrateSchema = void 0;
|
|
2128
|
-
const autoGenerateSchema = _optionalChain([options, 'access',
|
|
2129
|
-
const
|
|
2130
|
-
|
|
2176
|
+
const autoGenerateSchema = _optionalChain([options, 'access', _81 => _81.schema, 'optionalAccess', _82 => _82.autoMigration]) === void 0 || _optionalChain([options, 'access', _83 => _83.schema, 'optionalAccess', _84 => _84.autoMigration]) !== "None";
|
|
2177
|
+
const inlineProjections = (_nullishCoalesce(options.projections, () => ( []))).filter(({ type }) => type === "inline").map(({ projection: projection2 }) => projection2);
|
|
2178
|
+
const migrate = async () => {
|
|
2131
2179
|
if (!migrateSchema) {
|
|
2132
|
-
migrateSchema = createEventStoreSchema(pool
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2180
|
+
migrateSchema = createEventStoreSchema(connectionString, pool, {
|
|
2181
|
+
onBeforeSchemaCreated: async (context) => {
|
|
2182
|
+
for (const projection2 of inlineProjections) {
|
|
2183
|
+
if (projection2.init) {
|
|
2184
|
+
await projection2.init(context);
|
|
2185
|
+
}
|
|
2186
|
+
}
|
|
2187
|
+
if (_optionalChain([options, 'access', _85 => _85.hooks, 'optionalAccess', _86 => _86.onBeforeSchemaCreated])) {
|
|
2188
|
+
await options.hooks.onBeforeSchemaCreated(context);
|
|
2189
|
+
}
|
|
2190
|
+
},
|
|
2191
|
+
onAfterSchemaCreated: _optionalChain([options, 'access', _87 => _87.hooks, 'optionalAccess', _88 => _88.onAfterSchemaCreated])
|
|
2136
2192
|
});
|
|
2137
2193
|
}
|
|
2138
2194
|
return migrateSchema;
|
|
2139
2195
|
};
|
|
2140
|
-
const
|
|
2196
|
+
const ensureSchemaExists = () => {
|
|
2197
|
+
if (!autoGenerateSchema) return Promise.resolve();
|
|
2198
|
+
return migrate();
|
|
2199
|
+
};
|
|
2141
2200
|
const beforeCommitHook = inlineProjections.length > 0 ? (events, { transaction }) => handleProjections({
|
|
2142
2201
|
projections: inlineProjections,
|
|
2143
2202
|
connection: {
|
|
@@ -2153,13 +2212,11 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
2153
2212
|
schema: {
|
|
2154
2213
|
sql: () => schemaSQL.join(""),
|
|
2155
2214
|
print: () => console.log(schemaSQL.join("")),
|
|
2156
|
-
migrate
|
|
2157
|
-
await (migrateSchema = createEventStoreSchema(pool));
|
|
2158
|
-
}
|
|
2215
|
+
migrate
|
|
2159
2216
|
},
|
|
2160
2217
|
async aggregateStream(streamName, options2) {
|
|
2161
2218
|
const { evolve, initialState, read } = options2;
|
|
2162
|
-
const expectedStreamVersion = _optionalChain([read, 'optionalAccess',
|
|
2219
|
+
const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _89 => _89.expectedStreamVersion]);
|
|
2163
2220
|
let state = initialState();
|
|
2164
2221
|
const result = await this.readStream(streamName, options2.read);
|
|
2165
2222
|
const currentStreamVersion = result.currentStreamVersion;
|
|
@@ -2200,7 +2257,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
2200
2257
|
throw new ExpectedVersionConflictError(
|
|
2201
2258
|
-1n,
|
|
2202
2259
|
//TODO: Return actual version in case of error
|
|
2203
|
-
_nullishCoalesce(_optionalChain([options2, 'optionalAccess',
|
|
2260
|
+
_nullishCoalesce(_optionalChain([options2, 'optionalAccess', _90 => _90.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
|
|
2204
2261
|
);
|
|
2205
2262
|
return {
|
|
2206
2263
|
nextExpectedStreamVersion: appendResult.nextStreamPosition,
|