@event-driven-io/emmett-sqlite 0.42.0-beta.2 → 0.42.0-beta.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.cjs CHANGED
@@ -205,6 +205,10 @@ var _uuid = require('uuid');
205
205
 
206
206
  var _asyncretry = require('async-retry'); var _asyncretry2 = _interopRequireDefault(_asyncretry);
207
207
 
208
+
209
+ var emmettPrefix = "emt";
210
+ var defaultTag = `${emmettPrefix}:default`;
211
+ var unknownTag = `${emmettPrefix}:unknown`;
208
212
  var STREAM_EXISTS = "STREAM_EXISTS";
209
213
  var STREAM_DOES_NOT_EXIST = "STREAM_DOES_NOT_EXIST";
210
214
  var NO_CONCURRENCY_CHECK = "NO_CONCURRENCY_CHECK";
@@ -451,6 +455,7 @@ var JSONParser = {
451
455
  return _optionalChain([options, 'optionalAccess', _19 => _19.map]) ? options.map(parsed) : parsed;
452
456
  }
453
457
  };
458
+ var textEncoder = new TextEncoder();
454
459
  var AssertionError = class extends Error {
455
460
  constructor(message2) {
456
461
  super(message2);
@@ -589,13 +594,14 @@ var sqliteRawBatchSQLProjection = (options) => sqliteProjection({
589
594
  const sqls = await options.evolve(events, context);
590
595
  for (const sql2 of sqls) await context.connection.command(sql2);
591
596
  },
592
- init: async (context) => {
597
+ init: async (initOptions) => {
593
598
  if (options.init) {
594
- await options.init(context);
599
+ await options.init(initOptions);
595
600
  }
596
601
  if (options.initSQL) {
597
602
  const initSQLs = Array.isArray(options.initSQL) ? options.initSQL : [options.initSQL];
598
- for (const sql2 of initSQLs) await context.connection.command(sql2);
603
+ for (const sql2 of initSQLs)
604
+ await initOptions.context.connection.command(sql2);
599
605
  }
600
606
  }
601
607
  });
@@ -658,11 +664,12 @@ var schema_0_41_0 = [
658
664
  ];
659
665
 
660
666
  // src/eventStore/schema/typing.ts
661
- var emmettPrefix = "emt";
667
+ var emmettPrefix2 = "emt";
662
668
  var globalTag = "global";
663
- var defaultTag = "emt:default";
669
+ var defaultTag2 = `${emmettPrefix2}:default`;
670
+ var unknownTag2 = `${emmettPrefix2}:unknown`;
664
671
  var globalNames = {
665
- module: `${emmettPrefix}:module:${globalTag}`
672
+ module: `${emmettPrefix2}:module:${globalTag}`
666
673
  };
667
674
  var columns = {
668
675
  partition: {
@@ -671,24 +678,24 @@ var columns = {
671
678
  isArchived: { name: "is_archived" }
672
679
  };
673
680
  var streamsTable = {
674
- name: `${emmettPrefix}_streams`,
681
+ name: `${emmettPrefix2}_streams`,
675
682
  columns: {
676
683
  partition: columns.partition,
677
684
  isArchived: columns.isArchived
678
685
  }
679
686
  };
680
687
  var messagesTable = {
681
- name: `${emmettPrefix}_messages`,
688
+ name: `${emmettPrefix2}_messages`,
682
689
  columns: {
683
690
  partition: columns.partition,
684
691
  isArchived: columns.isArchived
685
692
  }
686
693
  };
687
694
  var processorsTable = {
688
- name: `${emmettPrefix}_processors`
695
+ name: `${emmettPrefix2}_processors`
689
696
  };
690
697
  var projectionsTable = {
691
- name: `${emmettPrefix}_projections`
698
+ name: `${emmettPrefix2}_projections`
692
699
  };
693
700
 
694
701
  // src/eventStore/schema/migrations/0_42_0/0_42_0.migration.ts
@@ -824,7 +831,7 @@ var processorsTableSQL = sql(
824
831
  partition TEXT NOT NULL DEFAULT '${globalTag}',
825
832
  status TEXT NOT NULL DEFAULT 'stopped',
826
833
  last_processed_checkpoint TEXT NOT NULL,
827
- processor_instance_id TEXT DEFAULT 'emt:unknown',
834
+ processor_instance_id TEXT DEFAULT '${unknownTag2}',
828
835
  PRIMARY KEY (processor_id, partition, version)
829
836
  );
830
837
  `
@@ -880,7 +887,7 @@ var readLastMessageGlobalPosition = async (db, options) => {
880
887
  ORDER BY global_position
881
888
  LIMIT 1`
882
889
  ),
883
- [_nullishCoalesce(_optionalChain([options, 'optionalAccess', _22 => _22.partition]), () => ( defaultTag))]
890
+ [_nullishCoalesce(_optionalChain([options, 'optionalAccess', _22 => _22.partition]), () => ( defaultTag2))]
884
891
  )
885
892
  );
886
893
  return {
@@ -903,7 +910,7 @@ var readMessagesBatch = async (db, options) => {
903
910
  ORDER BY global_position
904
911
  ${limitCondition}`
905
912
  ),
906
- [_nullishCoalesce(_optionalChain([options, 'optionalAccess', _23 => _23.partition]), () => ( defaultTag))]
913
+ [_nullishCoalesce(_optionalChain([options, 'optionalAccess', _23 => _23.partition]), () => ( defaultTag2))]
907
914
  )).map((row) => {
908
915
  const rawEvent = {
909
916
  type: row.message_type,
@@ -1108,7 +1115,7 @@ var appendToStreamRaw = async (connection, streamId, streamType, messages, optio
1108
1115
  messages,
1109
1116
  expectedStreamVersion,
1110
1117
  streamId,
1111
- _nullishCoalesce(_optionalChain([options, 'optionalAccess', _29 => _29.partition, 'optionalAccess', _30 => _30.toString, 'call', _31 => _31()]), () => ( defaultTag))
1118
+ _nullishCoalesce(_optionalChain([options, 'optionalAccess', _29 => _29.partition, 'optionalAccess', _30 => _30.toString, 'call', _31 => _31()]), () => ( defaultTag2))
1112
1119
  );
1113
1120
  const returningIds = await connection.query(sqlString, values);
1114
1121
  if (returningIds.length === 0 || !_optionalChain([returningIds, 'access', _32 => _32[returningIds.length - 1], 'optionalAccess', _33 => _33.global_position])) {
@@ -1157,7 +1164,7 @@ var buildMessageInsertQuery = (messages, expectedStreamVersion, streamId, partit
1157
1164
  queryBuilder.values.push(
1158
1165
  streamId,
1159
1166
  _nullishCoalesce(streamPosition.toString(), () => ( 0)),
1160
- _nullishCoalesce(partition, () => ( defaultTag)),
1167
+ _nullishCoalesce(partition, () => ( defaultTag2)),
1161
1168
  message.kind === "Event" ? "E" : "C",
1162
1169
  JSONParser.stringify(message.data),
1163
1170
  JSONParser.stringify(message.metadata),
@@ -1203,7 +1210,7 @@ var readProcessorCheckpoint = async (db, options) => {
1203
1210
  WHERE partition = ? AND processor_id = ?
1204
1211
  LIMIT 1`
1205
1212
  ),
1206
- [_nullishCoalesce(_optionalChain([options, 'optionalAccess', _40 => _40.partition]), () => ( defaultTag)), options.processorId]
1213
+ [_nullishCoalesce(_optionalChain([options, 'optionalAccess', _40 => _40.partition]), () => ( defaultTag2)), options.processorId]
1207
1214
  )
1208
1215
  );
1209
1216
  return {
@@ -1223,7 +1230,7 @@ var readStream = async (db, streamId, options) => {
1223
1230
  FROM ${messagesTable.name}
1224
1231
  WHERE stream_id = ? AND partition = ? AND is_archived = FALSE ${fromCondition} ${toCondition}
1225
1232
  ORDER BY stream_position ASC`,
1226
- [streamId, _nullishCoalesce(_optionalChain([options, 'optionalAccess', _41 => _41.partition]), () => ( defaultTag))]
1233
+ [streamId, _nullishCoalesce(_optionalChain([options, 'optionalAccess', _41 => _41.partition]), () => ( defaultTag2))]
1227
1234
  );
1228
1235
  const messages = results.map((row) => {
1229
1236
  const rawEvent = {
@@ -1257,7 +1264,7 @@ var readStream = async (db, streamId, options) => {
1257
1264
 
1258
1265
  // src/eventStore/schema/storeProcessorCheckpoint.ts
1259
1266
  async function storeSubscriptionCheckpointSQLite(db, processorId, version, position, checkPosition, partition, processorInstanceId) {
1260
- processorInstanceId ??= "emt:unknown";
1267
+ processorInstanceId ??= unknownTag2;
1261
1268
  if (checkPosition !== null) {
1262
1269
  const updateResult = await db.command(
1263
1270
  sql(`
@@ -1342,7 +1349,7 @@ async function storeProcessorCheckpoint(db, options) {
1342
1349
  _nullishCoalesce(options.version, () => ( 1)),
1343
1350
  options.newPosition,
1344
1351
  options.lastProcessedPosition,
1345
- _nullishCoalesce(options.partition, () => ( defaultTag))
1352
+ _nullishCoalesce(options.partition, () => ( defaultTag2))
1346
1353
  );
1347
1354
  return result === 1 ? { success: true, newPosition: options.newPosition } : { success: false, reason: result === 0 ? "IGNORED" : "MISMATCH" };
1348
1355
  } catch (error) {
@@ -1521,6 +1528,19 @@ var sqliteEventStoreConsumer = (options) => {
1521
1528
  };
1522
1529
  };
1523
1530
 
1531
+ // src/eventStore/schema/streamExists.ts
1532
+ var streamExists = async (db, streamId, options) => {
1533
+ const queryResult = await db.query(
1534
+ `SELECT EXISTS (
1535
+ SELECT 1
1536
+ from ${streamsTable.name}
1537
+ WHERE stream_id = ? AND partition = ? AND is_archived = FALSE)
1538
+ `,
1539
+ [streamId, _nullishCoalesce(_optionalChain([options, 'optionalAccess', _52 => _52.partition]), () => ( defaultTag2))]
1540
+ );
1541
+ return _nullishCoalesce(_optionalChain([queryResult, 'access', _53 => _53[0], 'optionalAccess', _54 => _54.exists]), () => ( false));
1542
+ };
1543
+
1524
1544
  // src/eventStore/SQLiteEventStore.ts
1525
1545
  var SQLiteEventStoreDefaultStreamVersion = 0n;
1526
1546
  var getSQLiteEventStore = (options) => {
@@ -1529,13 +1549,13 @@ var getSQLiteEventStore = (options) => {
1529
1549
  const pool = _nullishCoalesce(options.pool, () => ( SQLiteConnectionPool(options)));
1530
1550
  let migrateSchema = void 0;
1531
1551
  const inlineProjections = (_nullishCoalesce(options.projections, () => ( []))).filter(({ type }) => type === "inline").map(({ projection: projection2 }) => projection2);
1532
- const onBeforeCommitHook = _optionalChain([options, 'access', _52 => _52.hooks, 'optionalAccess', _53 => _53.onBeforeCommit]);
1552
+ const onBeforeCommitHook = _optionalChain([options, 'access', _55 => _55.hooks, 'optionalAccess', _56 => _56.onBeforeCommit]);
1533
1553
  const withConnection = async (handler) => pool.withConnection(async (database) => {
1534
1554
  await ensureSchemaExists(database);
1535
1555
  return await handler(database);
1536
1556
  });
1537
1557
  if (options) {
1538
- autoGenerateSchema = _optionalChain([options, 'access', _54 => _54.schema, 'optionalAccess', _55 => _55.autoMigration]) === void 0 || _optionalChain([options, 'access', _56 => _56.schema, 'optionalAccess', _57 => _57.autoMigration]) !== "None";
1558
+ autoGenerateSchema = _optionalChain([options, 'access', _57 => _57.schema, 'optionalAccess', _58 => _58.autoMigration]) === void 0 || _optionalChain([options, 'access', _59 => _59.schema, 'optionalAccess', _60 => _60.autoMigration]) !== "None";
1539
1559
  }
1540
1560
  const migrate = (connection) => {
1541
1561
  if (!migrateSchema) {
@@ -1543,14 +1563,19 @@ var getSQLiteEventStore = (options) => {
1543
1563
  onBeforeSchemaCreated: async (context) => {
1544
1564
  for (const projection2 of inlineProjections) {
1545
1565
  if (projection2.init) {
1546
- await projection2.init(context);
1566
+ await projection2.init({
1567
+ version: _nullishCoalesce(projection2.version, () => ( 1)),
1568
+ registrationType: "async",
1569
+ status: "active",
1570
+ context
1571
+ });
1547
1572
  }
1548
1573
  }
1549
- if (_optionalChain([options, 'access', _58 => _58.hooks, 'optionalAccess', _59 => _59.onBeforeSchemaCreated])) {
1574
+ if (_optionalChain([options, 'access', _61 => _61.hooks, 'optionalAccess', _62 => _62.onBeforeSchemaCreated])) {
1550
1575
  await options.hooks.onBeforeSchemaCreated(context);
1551
1576
  }
1552
1577
  },
1553
- onAfterSchemaCreated: _optionalChain([options, 'access', _60 => _60.hooks, 'optionalAccess', _61 => _61.onAfterSchemaCreated])
1578
+ onAfterSchemaCreated: _optionalChain([options, 'access', _63 => _63.hooks, 'optionalAccess', _64 => _64.onAfterSchemaCreated])
1554
1579
  });
1555
1580
  }
1556
1581
  return migrateSchema;
@@ -1562,7 +1587,7 @@ var getSQLiteEventStore = (options) => {
1562
1587
  return {
1563
1588
  async aggregateStream(streamName, options2) {
1564
1589
  const { evolve, initialState, read } = options2;
1565
- const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _62 => _62.expectedStreamVersion]);
1590
+ const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _65 => _65.expectedStreamVersion]);
1566
1591
  let state = initialState();
1567
1592
  if (typeof streamName !== "string") {
1568
1593
  throw new Error("Stream name is not string");
@@ -1591,7 +1616,7 @@ var getSQLiteEventStore = (options) => {
1591
1616
  ),
1592
1617
  appendToStream: async (streamName, events, options2) => {
1593
1618
  const [firstPart, ...rest] = streamName.split("-");
1594
- const streamType = firstPart && rest.length > 0 ? firstPart : "emt:unknown";
1619
+ const streamType = firstPart && rest.length > 0 ? firstPart : unknownTag2;
1595
1620
  const appendResult = await withConnection(
1596
1621
  (connection) => appendToStream(connection, streamName, streamType, events, {
1597
1622
  ...options2,
@@ -1610,7 +1635,7 @@ var getSQLiteEventStore = (options) => {
1610
1635
  throw new ExpectedVersionConflictError(
1611
1636
  -1n,
1612
1637
  //TODO: Return actual version in case of error
1613
- _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _63 => _63.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
1638
+ _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _66 => _66.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
1614
1639
  );
1615
1640
  return {
1616
1641
  nextExpectedStreamVersion: appendResult.nextStreamPosition,
@@ -1618,6 +1643,11 @@ var getSQLiteEventStore = (options) => {
1618
1643
  createdNewStream: appendResult.nextStreamPosition >= BigInt(events.length)
1619
1644
  };
1620
1645
  },
1646
+ streamExists(streamName, options2) {
1647
+ return withConnection(
1648
+ (connection) => streamExists(connection, streamName, options2)
1649
+ );
1650
+ },
1621
1651
  consumer: (options2) => sqliteEventStoreConsumer({
1622
1652
  ..._nullishCoalesce(options2, () => ( {})),
1623
1653
  fileName,
@@ -1646,7 +1676,7 @@ var SQLiteProjectionSpec = {
1646
1676
  const allEvents = [];
1647
1677
  const run = async (connection2) => {
1648
1678
  let globalPosition = 0n;
1649
- const numberOfTimes = _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _64 => _64.numberOfTimes]), () => ( 1));
1679
+ const numberOfTimes = _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _67 => _67.numberOfTimes]), () => ( 1));
1650
1680
  for (const event of [
1651
1681
  ...givenEvents,
1652
1682
  ...Array.from({ length: numberOfTimes }).flatMap(() => events)
@@ -1667,7 +1697,12 @@ var SQLiteProjectionSpec = {
1667
1697
  });
1668
1698
  }
1669
1699
  if (!wasInitialized && projection2.init) {
1670
- await projection2.init({ connection: connection2 });
1700
+ await projection2.init({
1701
+ registrationType: "async",
1702
+ status: "active",
1703
+ context: { connection: connection2 },
1704
+ version: _nullishCoalesce(projection2.version, () => ( 1))
1705
+ });
1671
1706
  wasInitialized = true;
1672
1707
  }
1673
1708
  await connection2.withTransaction(
@@ -1703,18 +1738,18 @@ var SQLiteProjectionSpec = {
1703
1738
  if (!isErrorConstructor(args[0])) {
1704
1739
  assertTrue(
1705
1740
  args[0](error),
1706
- `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _65 => _65.toString, 'call', _66 => _66()])}`
1741
+ `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _68 => _68.toString, 'call', _69 => _69()])}`
1707
1742
  );
1708
1743
  return;
1709
1744
  }
1710
1745
  assertTrue(
1711
1746
  error instanceof args[0],
1712
- `Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _67 => _67.toString, 'call', _68 => _68()])}`
1747
+ `Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _70 => _70.toString, 'call', _71 => _71()])}`
1713
1748
  );
1714
1749
  if (args[1]) {
1715
1750
  assertTrue(
1716
1751
  args[1](error),
1717
- `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _69 => _69.toString, 'call', _70 => _70()])}`
1752
+ `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _72 => _72.toString, 'call', _73 => _73()])}`
1718
1753
  );
1719
1754
  }
1720
1755
  } finally {
@@ -1733,7 +1768,7 @@ var eventInStream = (streamName, event) => {
1733
1768
  ...event,
1734
1769
  metadata: {
1735
1770
  ..._nullishCoalesce(event.metadata, () => ( {})),
1736
- streamName: _nullishCoalesce(_optionalChain([event, 'access', _71 => _71.metadata, 'optionalAccess', _72 => _72.streamName]), () => ( streamName))
1771
+ streamName: _nullishCoalesce(_optionalChain([event, 'access', _74 => _74.metadata, 'optionalAccess', _75 => _75.streamName]), () => ( streamName))
1737
1772
  }
1738
1773
  };
1739
1774
  };
@@ -1795,5 +1830,6 @@ var expectSQL = {
1795
1830
 
1796
1831
 
1797
1832
 
1798
- exports.InMemorySQLiteDatabase = InMemorySQLiteDatabase; exports.InMemorySharedCacheSQLiteDatabase = InMemorySharedCacheSQLiteDatabase; exports.SQLiteConnectionPool = SQLiteConnectionPool; exports.SQLiteEventStoreDefaultStreamVersion = SQLiteEventStoreDefaultStreamVersion; exports.SQLiteProjectionSpec = SQLiteProjectionSpec; exports.appendToStream = appendToStream; exports.assertSQLQueryResultMatches = assertSQLQueryResultMatches; exports.createEventStoreSchema = createEventStoreSchema; exports.defaultTag = defaultTag; exports.emmettPrefix = emmettPrefix; exports.eventInStream = eventInStream; exports.eventsInStream = eventsInStream; exports.expectSQL = expectSQL; exports.getSQLiteEventStore = getSQLiteEventStore; exports.globalNames = globalNames; exports.globalTag = globalTag; exports.handleProjections = handleProjections; exports.isSQLiteError = isSQLiteError; exports.messagesTable = messagesTable; exports.messagesTableSQL = messagesTableSQL; exports.migration_0_42_0_FromSubscriptionsToProcessors = migration_0_42_0_FromSubscriptionsToProcessors; exports.migration_0_42_0_SQLs = migration_0_42_0_SQLs; exports.newEventsInStream = newEventsInStream; exports.processorsTable = processorsTable; exports.processorsTableSQL = processorsTableSQL; exports.projectionsTable = projectionsTable; exports.projectionsTableSQL = projectionsTableSQL; exports.readLastMessageGlobalPosition = readLastMessageGlobalPosition; exports.readMessagesBatch = readMessagesBatch; exports.readProcessorCheckpoint = readProcessorCheckpoint; exports.readStream = readStream; exports.schemaSQL = schemaSQL; exports.schema_0_41_0 = schema_0_41_0; exports.schema_0_42_0 = schema_0_42_0; exports.sql = sql; exports.sqliteConnection = sqliteConnection; exports.sqliteProjection = sqliteProjection; exports.sqliteRawBatchSQLProjection = sqliteRawBatchSQLProjection; exports.sqliteRawSQLProjection = sqliteRawSQLProjection; exports.storeProcessorCheckpoint = storeProcessorCheckpoint; exports.streamsTable = streamsTable; exports.streamsTableSQL = streamsTableSQL;
1833
+
1834
+ exports.InMemorySQLiteDatabase = InMemorySQLiteDatabase; exports.InMemorySharedCacheSQLiteDatabase = InMemorySharedCacheSQLiteDatabase; exports.SQLiteConnectionPool = SQLiteConnectionPool; exports.SQLiteEventStoreDefaultStreamVersion = SQLiteEventStoreDefaultStreamVersion; exports.SQLiteProjectionSpec = SQLiteProjectionSpec; exports.appendToStream = appendToStream; exports.assertSQLQueryResultMatches = assertSQLQueryResultMatches; exports.createEventStoreSchema = createEventStoreSchema; exports.defaultTag = defaultTag2; exports.emmettPrefix = emmettPrefix2; exports.eventInStream = eventInStream; exports.eventsInStream = eventsInStream; exports.expectSQL = expectSQL; exports.getSQLiteEventStore = getSQLiteEventStore; exports.globalNames = globalNames; exports.globalTag = globalTag; exports.handleProjections = handleProjections; exports.isSQLiteError = isSQLiteError; exports.messagesTable = messagesTable; exports.messagesTableSQL = messagesTableSQL; exports.migration_0_42_0_FromSubscriptionsToProcessors = migration_0_42_0_FromSubscriptionsToProcessors; exports.migration_0_42_0_SQLs = migration_0_42_0_SQLs; exports.newEventsInStream = newEventsInStream; exports.processorsTable = processorsTable; exports.processorsTableSQL = processorsTableSQL; exports.projectionsTable = projectionsTable; exports.projectionsTableSQL = projectionsTableSQL; exports.readLastMessageGlobalPosition = readLastMessageGlobalPosition; exports.readMessagesBatch = readMessagesBatch; exports.readProcessorCheckpoint = readProcessorCheckpoint; exports.readStream = readStream; exports.schemaSQL = schemaSQL; exports.schema_0_41_0 = schema_0_41_0; exports.schema_0_42_0 = schema_0_42_0; exports.sql = sql; exports.sqliteConnection = sqliteConnection; exports.sqliteProjection = sqliteProjection; exports.sqliteRawBatchSQLProjection = sqliteRawBatchSQLProjection; exports.sqliteRawSQLProjection = sqliteRawSQLProjection; exports.storeProcessorCheckpoint = storeProcessorCheckpoint; exports.streamsTable = streamsTable; exports.streamsTableSQL = streamsTableSQL; exports.unknownTag = unknownTag2;
1799
1835
  //# sourceMappingURL=index.cjs.map