@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.d.cts CHANGED
@@ -141,6 +141,7 @@ type PongoProjectionOptions<EventType extends Event> = {
141
141
  handle: (events: ReadEvent<EventType, PostgresReadEventMetadata>[], context: PongoProjectionHandlerContext) => Promise<void>;
142
142
  canHandle: CanHandle<EventType>;
143
143
  truncate?: TruncateProjection<PongoProjectionHandlerContext>;
144
+ init?: (context: PongoProjectionHandlerContext) => void | Promise<void>;
144
145
  };
145
146
  declare const pongoProjection: <EventType extends Event>({ truncate, handle, canHandle, }: PongoProjectionOptions<EventType>) => PostgreSQLProjectionDefinition<EventType>;
146
147
  type PongoMultiStreamProjectionOptions<Document extends PongoDocument, EventType extends Event, EventMetaDataType extends PostgresReadEventMetadata = PostgresReadEventMetadata> = {
@@ -252,8 +253,20 @@ type PostgreSQLProjectionHandlerOptions<EventType extends Event = Event> = {
252
253
  };
253
254
  declare const handleProjections: <EventType extends Event = Event>(options: PostgreSQLProjectionHandlerOptions<EventType>) => Promise<void>;
254
255
  declare const postgreSQLProjection: <EventType extends Event>(definition: PostgreSQLProjectionDefinition<EventType>) => PostgreSQLProjectionDefinition<EventType>;
255
- declare const postgreSQLRawBatchSQLProjection: <EventType extends Event>(handle: (events: EventType[], context: PostgreSQLProjectionHandlerContext) => Promise<SQL[]> | SQL[], ...canHandle: CanHandle<EventType>) => PostgreSQLProjectionDefinition<EventType>;
256
- declare const postgreSQLRawSQLProjection: <EventType extends Event>(handle: (event: EventType, context: PostgreSQLProjectionHandlerContext) => Promise<SQL> | SQL, ...canHandle: CanHandle<EventType>) => PostgreSQLProjectionDefinition<EventType>;
256
+ type PostgreSQLRawBatchSQLProjection<EventType extends Event> = {
257
+ evolve: (events: EventType[], context: PostgreSQLProjectionHandlerContext) => Promise<SQL[]> | SQL[];
258
+ canHandle: CanHandle<EventType>;
259
+ initSQL?: SQL;
260
+ init?: (context: PostgreSQLProjectionHandlerContext) => void | Promise<void>;
261
+ };
262
+ declare const postgreSQLRawBatchSQLProjection: <EventType extends Event>(options: PostgreSQLRawBatchSQLProjection<EventType>) => PostgreSQLProjectionDefinition<EventType>;
263
+ type PostgreSQLRawSQLProjection<EventType extends Event> = {
264
+ evolve: (events: EventType, context: PostgreSQLProjectionHandlerContext) => Promise<SQL[]> | SQL[] | Promise<SQL> | SQL;
265
+ canHandle: CanHandle<EventType>;
266
+ initSQL?: SQL;
267
+ init?: (context: PostgreSQLProjectionHandlerContext) => void | Promise<void>;
268
+ };
269
+ declare const postgreSQLRawSQLProjection: <EventType extends Event>(options: PostgreSQLRawSQLProjection<EventType>) => PostgreSQLProjectionDefinition<EventType>;
257
270
 
258
271
  declare const rebuildPostgreSQLProjections: <EventType extends AnyEvent = AnyEvent>(options: Omit<PostgreSQLEventStoreConsumerOptions<EventType>, "stopWhen" | "processors"> & ({
259
272
  projections: (ProjectorOptions<EventType, ReadEventMetadataWithGlobalPosition, PostgreSQLProcessorHandlerContext> | PostgreSQLProjectionDefinition<EventType>)[];
@@ -329,6 +342,10 @@ type PostgresEventStoreOptions = {
329
342
  };
330
343
  connectionOptions?: PostgresEventStoreConnectionOptions;
331
344
  hooks?: {
345
+ /**
346
+ * This hook will be called **BEFORE** event store schema is created
347
+ */
348
+ onBeforeSchemaCreated?: (context: PostgreSQLProjectionHandlerContext) => Promise<void> | void;
332
349
  /**
333
350
  * This hook will be called **AFTER** event store schema was created
334
351
  */
@@ -462,6 +479,6 @@ declare const subscriptionsTable: {
462
479
  };
463
480
 
464
481
  declare const schemaSQL: SQL[];
465
- declare const createEventStoreSchema: (pool: NodePostgresPool) => Promise<void>;
482
+ declare const createEventStoreSchema: (connectionString: string, pool: NodePostgresPool, hooks?: PostgresEventStoreOptions["hooks"]) => Promise<void>;
466
483
 
467
- export { type AppendToStreamBeforeCommitHook, DefaultPostgreSQLEventStoreProcessorBatchSize, DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs, type ExtendableContext, type PongoAssertOptions, type PongoDocumentEvolve, type PongoMultiStreamProjectionOptions, type PongoProjectionHandlerContext, type PongoProjectionOptions, type PongoSingleStreamProjectionOptions, type PongoWithNotNullDocumentEvolve, type PongoWithNullableDocumentEvolve, type PostgreSQLCheckpointer, type PostgreSQLConsumerContext, type PostgreSQLEventStoreConsumer, type PostgreSQLEventStoreConsumerConfig, type PostgreSQLEventStoreConsumerOptions, PostgreSQLEventStoreDefaultStreamVersion, type PostgreSQLEventStoreMessageBatchPuller, type PostgreSQLEventStoreMessageBatchPullerOptions, type PostgreSQLEventStoreMessageBatchPullerStartFrom, type PostgreSQLEventStoreMessageBatchPullerStartOptions, type PostgreSQLEventStoreMessagesBatchHandlerResult, type PostgreSQLProcessor, type PostgreSQLProcessorConnectionOptions, type PostgreSQLProcessorEachBatchHandler, type PostgreSQLProcessorEachMessageHandler, type PostgreSQLProcessorHandlerContext, type PostgreSQLProcessorOptions, type PostgreSQLProcessorStartFrom, type PostgreSQLProjectionAssert, type PostgreSQLProjectionDefinition, type PostgreSQLProjectionHandler, type PostgreSQLProjectionHandlerContext, type PostgreSQLProjectionHandlerOptions, PostgreSQLProjectionSpec, type PostgreSQLProjectionSpecEvent, type PostgreSQLProjectionSpecOptions, type PostgreSQLProjectionSpecWhenOptions, type PostgreSQLProjectorOptions, type PostgreSQLReactorOptions, type PostgresEventStore, type PostgresEventStoreConnectionOptions, type PostgresEventStoreOptions, type PostgresReadEvent, type PostgresReadEventMetadata, type ReadLastMessageGlobalPositionResult, type ReadMessagesBatchOptions, type ReadMessagesBatchResult, type ReadProcessorCheckpointResult, type StoreLastProcessedProcessorPositionResult, addDefaultPartitionSQL, addModuleForAllTenantsSQL, addModuleSQL, addPartitionSQL, addTablePartitions, addTenantForAllModulesSQL, addTenantSQL, appendToStream, appendToStreamSQL, assertSQLQueryResultMatches, createEventStoreSchema, defaultPostgreSQLOptions, defaultTag, documentDoesNotExist, documentExists, documentMatchingExists, documentsAreTheSame, documentsMatchingHaveCount, dropFutureConceptModuleAndTenantFunctions, dropOldAppendToSQLWithoutGlobalPositions, emmettPrefix, eventInStream, eventsInStream, expectPongoDocuments, expectSQL, getPostgreSQLEventStore, globalNames, globalTag, handleProjections, messagesTable, messagesTableSQL, migrationFromEventsToMessagesSQL, newEventsInStream, pongoMultiStreamProjection, pongoProjection, pongoSingleStreamProjection, postgreSQLCheckpointer, postgreSQLEventStoreConsumer, postgreSQLEventStoreMessageBatchPuller, postgreSQLMessageProcessor, postgreSQLProjection, postgreSQLProjector, postgreSQLRawBatchSQLProjection, postgreSQLRawSQLProjection, postgreSQLReactor, readLastMessageGlobalPosition, readMessagesBatch, readProcessorCheckpoint, readStream, rebuildPostgreSQLProjections, sanitizeNameSQL, schemaSQL, storeProcessorCheckpoint, storeSubscriptionCheckpointSQL, streamsTable, streamsTableSQL, subscriptionsTable, subscriptionsTableSQL, zipPostgreSQLEventStoreMessageBatchPullerStartFrom };
484
+ export { type AppendToStreamBeforeCommitHook, DefaultPostgreSQLEventStoreProcessorBatchSize, DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs, type ExtendableContext, type PongoAssertOptions, type PongoDocumentEvolve, type PongoMultiStreamProjectionOptions, type PongoProjectionHandlerContext, type PongoProjectionOptions, type PongoSingleStreamProjectionOptions, type PongoWithNotNullDocumentEvolve, type PongoWithNullableDocumentEvolve, type PostgreSQLCheckpointer, type PostgreSQLConsumerContext, type PostgreSQLEventStoreConsumer, type PostgreSQLEventStoreConsumerConfig, type PostgreSQLEventStoreConsumerOptions, PostgreSQLEventStoreDefaultStreamVersion, type PostgreSQLEventStoreMessageBatchPuller, type PostgreSQLEventStoreMessageBatchPullerOptions, type PostgreSQLEventStoreMessageBatchPullerStartFrom, type PostgreSQLEventStoreMessageBatchPullerStartOptions, type PostgreSQLEventStoreMessagesBatchHandlerResult, type PostgreSQLProcessor, type PostgreSQLProcessorConnectionOptions, type PostgreSQLProcessorEachBatchHandler, type PostgreSQLProcessorEachMessageHandler, type PostgreSQLProcessorHandlerContext, type PostgreSQLProcessorOptions, type PostgreSQLProcessorStartFrom, type PostgreSQLProjectionAssert, type PostgreSQLProjectionDefinition, type PostgreSQLProjectionHandler, type PostgreSQLProjectionHandlerContext, type PostgreSQLProjectionHandlerOptions, PostgreSQLProjectionSpec, type PostgreSQLProjectionSpecEvent, type PostgreSQLProjectionSpecOptions, type PostgreSQLProjectionSpecWhenOptions, type PostgreSQLProjectorOptions, type PostgreSQLRawBatchSQLProjection, type PostgreSQLRawSQLProjection, type PostgreSQLReactorOptions, type PostgresEventStore, type PostgresEventStoreConnectionOptions, type PostgresEventStoreOptions, type PostgresReadEvent, type PostgresReadEventMetadata, type ReadLastMessageGlobalPositionResult, type ReadMessagesBatchOptions, type ReadMessagesBatchResult, type ReadProcessorCheckpointResult, type StoreLastProcessedProcessorPositionResult, addDefaultPartitionSQL, addModuleForAllTenantsSQL, addModuleSQL, addPartitionSQL, addTablePartitions, addTenantForAllModulesSQL, addTenantSQL, appendToStream, appendToStreamSQL, assertSQLQueryResultMatches, createEventStoreSchema, defaultPostgreSQLOptions, defaultTag, documentDoesNotExist, documentExists, documentMatchingExists, documentsAreTheSame, documentsMatchingHaveCount, dropFutureConceptModuleAndTenantFunctions, dropOldAppendToSQLWithoutGlobalPositions, emmettPrefix, eventInStream, eventsInStream, expectPongoDocuments, expectSQL, getPostgreSQLEventStore, globalNames, globalTag, handleProjections, messagesTable, messagesTableSQL, migrationFromEventsToMessagesSQL, newEventsInStream, pongoMultiStreamProjection, pongoProjection, pongoSingleStreamProjection, postgreSQLCheckpointer, postgreSQLEventStoreConsumer, postgreSQLEventStoreMessageBatchPuller, postgreSQLMessageProcessor, postgreSQLProjection, postgreSQLProjector, postgreSQLRawBatchSQLProjection, postgreSQLRawSQLProjection, postgreSQLReactor, readLastMessageGlobalPosition, readMessagesBatch, readProcessorCheckpoint, readStream, rebuildPostgreSQLProjections, sanitizeNameSQL, schemaSQL, storeProcessorCheckpoint, storeSubscriptionCheckpointSQL, streamsTable, streamsTableSQL, subscriptionsTable, subscriptionsTableSQL, zipPostgreSQLEventStoreMessageBatchPullerStartFrom };
package/dist/index.d.ts CHANGED
@@ -141,6 +141,7 @@ type PongoProjectionOptions<EventType extends Event> = {
141
141
  handle: (events: ReadEvent<EventType, PostgresReadEventMetadata>[], context: PongoProjectionHandlerContext) => Promise<void>;
142
142
  canHandle: CanHandle<EventType>;
143
143
  truncate?: TruncateProjection<PongoProjectionHandlerContext>;
144
+ init?: (context: PongoProjectionHandlerContext) => void | Promise<void>;
144
145
  };
145
146
  declare const pongoProjection: <EventType extends Event>({ truncate, handle, canHandle, }: PongoProjectionOptions<EventType>) => PostgreSQLProjectionDefinition<EventType>;
146
147
  type PongoMultiStreamProjectionOptions<Document extends PongoDocument, EventType extends Event, EventMetaDataType extends PostgresReadEventMetadata = PostgresReadEventMetadata> = {
@@ -252,8 +253,20 @@ type PostgreSQLProjectionHandlerOptions<EventType extends Event = Event> = {
252
253
  };
253
254
  declare const handleProjections: <EventType extends Event = Event>(options: PostgreSQLProjectionHandlerOptions<EventType>) => Promise<void>;
254
255
  declare const postgreSQLProjection: <EventType extends Event>(definition: PostgreSQLProjectionDefinition<EventType>) => PostgreSQLProjectionDefinition<EventType>;
255
- declare const postgreSQLRawBatchSQLProjection: <EventType extends Event>(handle: (events: EventType[], context: PostgreSQLProjectionHandlerContext) => Promise<SQL[]> | SQL[], ...canHandle: CanHandle<EventType>) => PostgreSQLProjectionDefinition<EventType>;
256
- declare const postgreSQLRawSQLProjection: <EventType extends Event>(handle: (event: EventType, context: PostgreSQLProjectionHandlerContext) => Promise<SQL> | SQL, ...canHandle: CanHandle<EventType>) => PostgreSQLProjectionDefinition<EventType>;
256
+ type PostgreSQLRawBatchSQLProjection<EventType extends Event> = {
257
+ evolve: (events: EventType[], context: PostgreSQLProjectionHandlerContext) => Promise<SQL[]> | SQL[];
258
+ canHandle: CanHandle<EventType>;
259
+ initSQL?: SQL;
260
+ init?: (context: PostgreSQLProjectionHandlerContext) => void | Promise<void>;
261
+ };
262
+ declare const postgreSQLRawBatchSQLProjection: <EventType extends Event>(options: PostgreSQLRawBatchSQLProjection<EventType>) => PostgreSQLProjectionDefinition<EventType>;
263
+ type PostgreSQLRawSQLProjection<EventType extends Event> = {
264
+ evolve: (events: EventType, context: PostgreSQLProjectionHandlerContext) => Promise<SQL[]> | SQL[] | Promise<SQL> | SQL;
265
+ canHandle: CanHandle<EventType>;
266
+ initSQL?: SQL;
267
+ init?: (context: PostgreSQLProjectionHandlerContext) => void | Promise<void>;
268
+ };
269
+ declare const postgreSQLRawSQLProjection: <EventType extends Event>(options: PostgreSQLRawSQLProjection<EventType>) => PostgreSQLProjectionDefinition<EventType>;
257
270
 
258
271
  declare const rebuildPostgreSQLProjections: <EventType extends AnyEvent = AnyEvent>(options: Omit<PostgreSQLEventStoreConsumerOptions<EventType>, "stopWhen" | "processors"> & ({
259
272
  projections: (ProjectorOptions<EventType, ReadEventMetadataWithGlobalPosition, PostgreSQLProcessorHandlerContext> | PostgreSQLProjectionDefinition<EventType>)[];
@@ -329,6 +342,10 @@ type PostgresEventStoreOptions = {
329
342
  };
330
343
  connectionOptions?: PostgresEventStoreConnectionOptions;
331
344
  hooks?: {
345
+ /**
346
+ * This hook will be called **BEFORE** event store schema is created
347
+ */
348
+ onBeforeSchemaCreated?: (context: PostgreSQLProjectionHandlerContext) => Promise<void> | void;
332
349
  /**
333
350
  * This hook will be called **AFTER** event store schema was created
334
351
  */
@@ -462,6 +479,6 @@ declare const subscriptionsTable: {
462
479
  };
463
480
 
464
481
  declare const schemaSQL: SQL[];
465
- declare const createEventStoreSchema: (pool: NodePostgresPool) => Promise<void>;
482
+ declare const createEventStoreSchema: (connectionString: string, pool: NodePostgresPool, hooks?: PostgresEventStoreOptions["hooks"]) => Promise<void>;
466
483
 
467
- export { type AppendToStreamBeforeCommitHook, DefaultPostgreSQLEventStoreProcessorBatchSize, DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs, type ExtendableContext, type PongoAssertOptions, type PongoDocumentEvolve, type PongoMultiStreamProjectionOptions, type PongoProjectionHandlerContext, type PongoProjectionOptions, type PongoSingleStreamProjectionOptions, type PongoWithNotNullDocumentEvolve, type PongoWithNullableDocumentEvolve, type PostgreSQLCheckpointer, type PostgreSQLConsumerContext, type PostgreSQLEventStoreConsumer, type PostgreSQLEventStoreConsumerConfig, type PostgreSQLEventStoreConsumerOptions, PostgreSQLEventStoreDefaultStreamVersion, type PostgreSQLEventStoreMessageBatchPuller, type PostgreSQLEventStoreMessageBatchPullerOptions, type PostgreSQLEventStoreMessageBatchPullerStartFrom, type PostgreSQLEventStoreMessageBatchPullerStartOptions, type PostgreSQLEventStoreMessagesBatchHandlerResult, type PostgreSQLProcessor, type PostgreSQLProcessorConnectionOptions, type PostgreSQLProcessorEachBatchHandler, type PostgreSQLProcessorEachMessageHandler, type PostgreSQLProcessorHandlerContext, type PostgreSQLProcessorOptions, type PostgreSQLProcessorStartFrom, type PostgreSQLProjectionAssert, type PostgreSQLProjectionDefinition, type PostgreSQLProjectionHandler, type PostgreSQLProjectionHandlerContext, type PostgreSQLProjectionHandlerOptions, PostgreSQLProjectionSpec, type PostgreSQLProjectionSpecEvent, type PostgreSQLProjectionSpecOptions, type PostgreSQLProjectionSpecWhenOptions, type PostgreSQLProjectorOptions, type PostgreSQLReactorOptions, type PostgresEventStore, type PostgresEventStoreConnectionOptions, type PostgresEventStoreOptions, type PostgresReadEvent, type PostgresReadEventMetadata, type ReadLastMessageGlobalPositionResult, type ReadMessagesBatchOptions, type ReadMessagesBatchResult, type ReadProcessorCheckpointResult, type StoreLastProcessedProcessorPositionResult, addDefaultPartitionSQL, addModuleForAllTenantsSQL, addModuleSQL, addPartitionSQL, addTablePartitions, addTenantForAllModulesSQL, addTenantSQL, appendToStream, appendToStreamSQL, assertSQLQueryResultMatches, createEventStoreSchema, defaultPostgreSQLOptions, defaultTag, documentDoesNotExist, documentExists, documentMatchingExists, documentsAreTheSame, documentsMatchingHaveCount, dropFutureConceptModuleAndTenantFunctions, dropOldAppendToSQLWithoutGlobalPositions, emmettPrefix, eventInStream, eventsInStream, expectPongoDocuments, expectSQL, getPostgreSQLEventStore, globalNames, globalTag, handleProjections, messagesTable, messagesTableSQL, migrationFromEventsToMessagesSQL, newEventsInStream, pongoMultiStreamProjection, pongoProjection, pongoSingleStreamProjection, postgreSQLCheckpointer, postgreSQLEventStoreConsumer, postgreSQLEventStoreMessageBatchPuller, postgreSQLMessageProcessor, postgreSQLProjection, postgreSQLProjector, postgreSQLRawBatchSQLProjection, postgreSQLRawSQLProjection, postgreSQLReactor, readLastMessageGlobalPosition, readMessagesBatch, readProcessorCheckpoint, readStream, rebuildPostgreSQLProjections, sanitizeNameSQL, schemaSQL, storeProcessorCheckpoint, storeSubscriptionCheckpointSQL, streamsTable, streamsTableSQL, subscriptionsTable, subscriptionsTableSQL, zipPostgreSQLEventStoreMessageBatchPullerStartFrom };
484
+ export { type AppendToStreamBeforeCommitHook, DefaultPostgreSQLEventStoreProcessorBatchSize, DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs, type ExtendableContext, type PongoAssertOptions, type PongoDocumentEvolve, type PongoMultiStreamProjectionOptions, type PongoProjectionHandlerContext, type PongoProjectionOptions, type PongoSingleStreamProjectionOptions, type PongoWithNotNullDocumentEvolve, type PongoWithNullableDocumentEvolve, type PostgreSQLCheckpointer, type PostgreSQLConsumerContext, type PostgreSQLEventStoreConsumer, type PostgreSQLEventStoreConsumerConfig, type PostgreSQLEventStoreConsumerOptions, PostgreSQLEventStoreDefaultStreamVersion, type PostgreSQLEventStoreMessageBatchPuller, type PostgreSQLEventStoreMessageBatchPullerOptions, type PostgreSQLEventStoreMessageBatchPullerStartFrom, type PostgreSQLEventStoreMessageBatchPullerStartOptions, type PostgreSQLEventStoreMessagesBatchHandlerResult, type PostgreSQLProcessor, type PostgreSQLProcessorConnectionOptions, type PostgreSQLProcessorEachBatchHandler, type PostgreSQLProcessorEachMessageHandler, type PostgreSQLProcessorHandlerContext, type PostgreSQLProcessorOptions, type PostgreSQLProcessorStartFrom, type PostgreSQLProjectionAssert, type PostgreSQLProjectionDefinition, type PostgreSQLProjectionHandler, type PostgreSQLProjectionHandlerContext, type PostgreSQLProjectionHandlerOptions, PostgreSQLProjectionSpec, type PostgreSQLProjectionSpecEvent, type PostgreSQLProjectionSpecOptions, type PostgreSQLProjectionSpecWhenOptions, type PostgreSQLProjectorOptions, type PostgreSQLRawBatchSQLProjection, type PostgreSQLRawSQLProjection, type PostgreSQLReactorOptions, type PostgresEventStore, type PostgresEventStoreConnectionOptions, type PostgresEventStoreOptions, type PostgresReadEvent, type PostgresReadEventMetadata, type ReadLastMessageGlobalPositionResult, type ReadMessagesBatchOptions, type ReadMessagesBatchResult, type ReadProcessorCheckpointResult, type StoreLastProcessedProcessorPositionResult, addDefaultPartitionSQL, addModuleForAllTenantsSQL, addModuleSQL, addPartitionSQL, addTablePartitions, addTenantForAllModulesSQL, addTenantSQL, appendToStream, appendToStreamSQL, assertSQLQueryResultMatches, createEventStoreSchema, defaultPostgreSQLOptions, defaultTag, documentDoesNotExist, documentExists, documentMatchingExists, documentsAreTheSame, documentsMatchingHaveCount, dropFutureConceptModuleAndTenantFunctions, dropOldAppendToSQLWithoutGlobalPositions, emmettPrefix, eventInStream, eventsInStream, expectPongoDocuments, expectSQL, getPostgreSQLEventStore, globalNames, globalTag, handleProjections, messagesTable, messagesTableSQL, migrationFromEventsToMessagesSQL, newEventsInStream, pongoMultiStreamProjection, pongoProjection, pongoSingleStreamProjection, postgreSQLCheckpointer, postgreSQLEventStoreConsumer, postgreSQLEventStoreMessageBatchPuller, postgreSQLMessageProcessor, postgreSQLProjection, postgreSQLProjector, postgreSQLRawBatchSQLProjection, postgreSQLRawSQLProjection, postgreSQLReactor, readLastMessageGlobalPosition, readMessagesBatch, readProcessorCheckpoint, readStream, rebuildPostgreSQLProjections, sanitizeNameSQL, schemaSQL, storeProcessorCheckpoint, storeSubscriptionCheckpointSQL, streamsTable, streamsTableSQL, subscriptionsTable, subscriptionsTableSQL, zipPostgreSQLEventStoreMessageBatchPullerStartFrom };
package/dist/index.js CHANGED
@@ -1487,8 +1487,26 @@ var schemaSQL = [
1487
1487
  addDefaultPartitionSQL,
1488
1488
  storeSubscriptionCheckpointSQL
1489
1489
  ];
1490
- var createEventStoreSchema = async (pool) => {
1491
- await pool.withTransaction(({ execute }) => execute.batchCommand(schemaSQL));
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 (hooks?.onBeforeSchemaCreated) {
1503
+ await hooks.onBeforeSchemaCreated(context);
1504
+ }
1505
+ await context.execute.batchCommand(schemaSQL);
1506
+ });
1507
+ if (hooks?.onAfterSchemaCreated) {
1508
+ await hooks.onAfterSchemaCreated();
1509
+ }
1492
1510
  };
1493
1511
 
1494
1512
  // src/eventStore/consumers/postgreSQLProcessor.ts
@@ -1558,7 +1576,12 @@ var getProcessorPool = (options) => {
1558
1576
  var postgreSQLProjector = (options) => {
1559
1577
  const { pool, connectionString, close } = getProcessorPool(options);
1560
1578
  const hooks = {
1561
- onStart: options.hooks?.onStart,
1579
+ onStart: async (context) => {
1580
+ if (options.hooks?.onStart) await options.hooks.onStart(context);
1581
+ if (options.projection.init) {
1582
+ await options.projection.init(context);
1583
+ }
1584
+ },
1562
1585
  onClose: options.hooks?.onClose || close ? async () => {
1563
1586
  if (options.hooks?.onClose) await options.hooks?.onClose();
1564
1587
  if (close) await close();
@@ -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 = pongoClient(connectionString, {
1831
+ connectionOptions: { client, pool }
1832
+ });
1833
+ await pongo.db().collection(collectionName).schema.migrate();
1802
1834
  }
1803
1835
  });
1804
1836
  };
@@ -2094,23 +2126,39 @@ var handleProjections = async (options) => {
2094
2126
  }
2095
2127
  };
2096
2128
  var postgreSQLProjection = (definition) => projection(definition);
2097
- var postgreSQLRawBatchSQLProjection = (handle, ...canHandle) => postgreSQLProjection({
2098
- canHandle,
2129
+ var postgreSQLRawBatchSQLProjection = (options) => postgreSQLProjection({
2130
+ canHandle: options.canHandle,
2099
2131
  handle: async (events, context) => {
2100
- const sqls = await handle(events, context);
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 = (handle, ...canHandle) => postgreSQLRawBatchSQLProjection(
2105
- async (events, context) => {
2106
- const sqls = [];
2107
- for (const event of events) {
2108
- sqls.push(await handle(event, context));
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
- return sqls;
2111
- },
2112
- ...canHandle
2113
- );
2160
+ });
2161
+ };
2114
2162
 
2115
2163
  // src/eventStore/postgreSQLEventStore.ts
2116
2164
  var defaultPostgreSQLOptions = {
@@ -2126,18 +2174,29 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
2126
2174
  const pool = "dumbo" in poolOptions ? poolOptions.dumbo : dumbo4(poolOptions);
2127
2175
  let migrateSchema = void 0;
2128
2176
  const autoGenerateSchema = options.schema?.autoMigration === void 0 || options.schema?.autoMigration !== "None";
2129
- const ensureSchemaExists = () => {
2130
- if (!autoGenerateSchema) return Promise.resolve();
2177
+ const inlineProjections = (options.projections ?? []).filter(({ type }) => type === "inline").map(({ projection: projection2 }) => projection2);
2178
+ const migrate = async () => {
2131
2179
  if (!migrateSchema) {
2132
- migrateSchema = createEventStoreSchema(pool).then(async () => {
2133
- if (options.hooks?.onAfterSchemaCreated) {
2134
- await options.hooks.onAfterSchemaCreated();
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 (options.hooks?.onBeforeSchemaCreated) {
2188
+ await options.hooks.onBeforeSchemaCreated(context);
2189
+ }
2190
+ },
2191
+ onAfterSchemaCreated: options.hooks?.onAfterSchemaCreated
2136
2192
  });
2137
2193
  }
2138
2194
  return migrateSchema;
2139
2195
  };
2140
- const inlineProjections = (options.projections ?? []).filter(({ type }) => type === "inline").map(({ projection: projection2 }) => projection2);
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,9 +2212,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
2153
2212
  schema: {
2154
2213
  sql: () => schemaSQL.join(""),
2155
2214
  print: () => console.log(schemaSQL.join("")),
2156
- migrate: async () => {
2157
- await (migrateSchema = createEventStoreSchema(pool));
2158
- }
2215
+ migrate
2159
2216
  },
2160
2217
  async aggregateStream(streamName, options2) {
2161
2218
  const { evolve, initialState, read } = options2;