@bjnstnkvc/db 2.0.2 → 2.1.0

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/README.md CHANGED
@@ -1119,22 +1119,23 @@ DB.onQueryExecuted((event: QueryExecuted): void => {
1119
1119
  });
1120
1120
  ```
1121
1121
 
1122
- | Key | Class | Carries |
1123
- |---------------------------|-------------------------|----------------------------------------------------------------------------------------|
1124
- | `query` | `QueryExecuted` | `connection`, `table`, `plan`, `constraints`, `orders`, `limit`, `duration`, `records` |
1125
- | `transaction-beginning` | `TransactionBeginning` | `connection` |
1126
- | `transaction-committed` | `TransactionCommitted` | `connection` |
1127
- | `transaction-rolled-back` | `TransactionRolledBack` | `connection`, `reason` |
1128
- | `migrations-started` | `MigrationsStarted` | `connection`, `migrations` |
1129
- | `migration-started` | `MigrationStarted` | `migration` |
1130
- | `migration-ended` | `MigrationEnded` | `migration` |
1131
- | `migrations-ended` | `MigrationsEnded` | `connection`, `migrations` |
1132
- | `no-pending-migrations` | `NoPendingMigrations` | `connection` |
1133
- | `seeding-started` | `SeedingStarted` | `connection`, `seeders` |
1134
- | `seeder-started` | `SeederStarted` | `seeder` |
1135
- | `seeder-ended` | `SeederEnded` | `seeder` |
1136
- | `seeding-ended` | `SeedingEnded` | `connection`, `seeders` |
1137
- | `database-blocked` | `DatabaseBlocked` | `database` |
1122
+ | Key | Class | Carries |
1123
+ |----------------------------|--------------------------|----------------------------------------------------------------------------------------|
1124
+ | `query` | `QueryExecuted` | `connection`, `table`, `plan`, `constraints`, `orders`, `limit`, `duration`, `records` |
1125
+ | `transaction-beginning` | `TransactionBeginning` | `connection` |
1126
+ | `transaction-committed` | `TransactionCommitted` | `connection` |
1127
+ | `transaction-rolled-back` | `TransactionRolledBack` | `connection`, `reason` |
1128
+ | `migrations-started` | `MigrationsStarted` | `connection`, `migrations` |
1129
+ | `migration-started` | `MigrationStarted` | `migration` |
1130
+ | `migration-ended` | `MigrationEnded` | `migration` |
1131
+ | `migrations-ended` | `MigrationsEnded` | `connection`, `migrations` |
1132
+ | `no-pending-migrations` | `NoPendingMigrations` | `connection` |
1133
+ | `seeding-started` | `SeedingStarted` | `connection`, `seeders` |
1134
+ | `seeder-started` | `SeederStarted` | `seeder` |
1135
+ | `seeder-ended` | `SeederEnded` | `seeder` |
1136
+ | `seeding-ended` | `SeedingEnded` | `connection`, `seeders` |
1137
+ | `database-blocked` | `DatabaseBlocked` | `database` |
1138
+ | `database-version-changed` | `DatabaseVersionChanged` | `database`, `version` |
1138
1139
 
1139
1140
  A connection with no seeders announces nothing, so `seeding-started` firing always means at least
1140
1141
  one seeder is about to run.
@@ -1183,9 +1184,21 @@ IndexedDB is shared across tabs, which produces two situations worth handling:
1183
1184
  - Another tab holds an older version open, blocking an upgrade. The connection emits
1184
1185
  `database-blocked` and rejects with `DatabaseBlockedException`, so you can ask the user to close
1185
1186
  the other tabs.
1186
- - Another tab upgrades the database. The connection closes its own handle so it does not block that
1187
- upgrade. If the other tab is running newer code with more migrations, this tab can no longer open
1188
- the database and reports `MigrationMismatchException`, so reload the page.
1187
+ - Another tab upgrades or deletes the database. The connection closes its own handle so it does not
1188
+ block that tab, then emits `database-version-changed`. Its `version` is the version the other tab
1189
+ is opening, or `null` when it is deleting the database.
1190
+
1191
+ The other tab is usually running newer code with more migrations, after a deploy. This tab can no
1192
+ longer open the database, and its next query would reject with `MigrationMismatchException`. Reload
1193
+ the page when the event arrives, rather than querying again:
1194
+
1195
+ ```ts
1196
+ DB.onDatabaseVersionChanged((event: DatabaseVersionChanged): void => {
1197
+ if (confirm('A new version is available. Reload now?')) {
1198
+ location.reload();
1199
+ }
1200
+ });
1201
+ ```
1189
1202
 
1190
1203
  ### Connections
1191
1204
 
package/dist/main.cjs CHANGED
@@ -30,6 +30,7 @@ __export(main_exports, {
30
30
  DatabaseBlocked: () => DatabaseBlocked,
31
31
  DatabaseBlockedException: () => DatabaseBlockedException,
32
32
  DatabaseManager: () => DatabaseManager,
33
+ DatabaseVersionChanged: () => DatabaseVersionChanged,
33
34
  Grouping: () => Grouping,
34
35
  Join: () => Join,
35
36
  Migration: () => Migration,
@@ -690,6 +691,38 @@ var DatabaseBlocked = class extends Event {
690
691
  }
691
692
  };
692
693
 
694
+ // src/events/DatabaseVersionChanged.ts
695
+ var DatabaseVersionChanged = class extends Event {
696
+ /**
697
+ * The name of the database.
698
+ */
699
+ #database;
700
+ /**
701
+ * The version the other tab is opening, or null when it is deleting the database.
702
+ */
703
+ #version;
704
+ /**
705
+ * Create a new Database Version Changed event instance.
706
+ */
707
+ constructor(database, version) {
708
+ super("db:database-version-changed");
709
+ this.#database = database;
710
+ this.#version = version;
711
+ }
712
+ /**
713
+ * Get the name of the database.
714
+ */
715
+ get database() {
716
+ return this.#database;
717
+ }
718
+ /**
719
+ * Get the version the other tab is opening, or null when it is deleting the database.
720
+ */
721
+ get version() {
722
+ return this.#version;
723
+ }
724
+ };
725
+
693
726
  // src/events/MigrationEnded.ts
694
727
  var MigrationEnded = class extends Event {
695
728
  /**
@@ -3807,7 +3840,10 @@ var Connection = class {
3807
3840
  Migrator.verify(ran, Migrator.names(this.migrations));
3808
3841
  this.#schemas = new Map(registry.map((schema) => [schema.table, schema]));
3809
3842
  this.#database = database;
3810
- database.onversionchange = () => this.disconnect();
3843
+ database.onversionchange = (event) => {
3844
+ this.disconnect();
3845
+ Dispatcher.dispatch(new DatabaseVersionChanged(this.#config.database, event.newVersion));
3846
+ };
3811
3847
  return database;
3812
3848
  }
3813
3849
  };
@@ -4043,6 +4079,12 @@ var DatabaseManager = class {
4043
4079
  static onDatabaseBlocked(listener, options) {
4044
4080
  this.listen("database-blocked", listener, options);
4045
4081
  }
4082
+ /**
4083
+ * Register a listener on the "database-version-changed" event.
4084
+ */
4085
+ static onDatabaseVersionChanged(listener, options) {
4086
+ this.listen("database-version-changed", listener, options);
4087
+ }
4046
4088
  /**
4047
4089
  * Register a listener on the "seeding-started" event.
4048
4090
  */
@@ -4372,6 +4414,7 @@ var Seeder = class {
4372
4414
  DatabaseBlocked,
4373
4415
  DatabaseBlockedException,
4374
4416
  DatabaseManager,
4417
+ DatabaseVersionChanged,
4375
4418
  Grouping,
4376
4419
  Join,
4377
4420
  Migration,
package/dist/main.d.cts CHANGED
@@ -861,6 +861,22 @@ declare class DatabaseBlocked extends Event {
861
861
  get database(): string;
862
862
  }
863
863
 
864
+ declare class DatabaseVersionChanged extends Event {
865
+ #private;
866
+ /**
867
+ * Create a new Database Version Changed event instance.
868
+ */
869
+ constructor(database: string, version: number | null);
870
+ /**
871
+ * Get the name of the database.
872
+ */
873
+ get database(): string;
874
+ /**
875
+ * Get the version the other tab is opening, or null when it is deleting the database.
876
+ */
877
+ get version(): number | null;
878
+ }
879
+
864
880
  declare class MigrationEnded extends Event {
865
881
  #private;
866
882
  /**
@@ -1067,6 +1083,7 @@ declare class TransactionRolledBack extends Event {
1067
1083
 
1068
1084
  type DatabaseEvent = {
1069
1085
  'database-blocked': DatabaseBlocked;
1086
+ 'database-version-changed': DatabaseVersionChanged;
1070
1087
  'migration-ended': MigrationEnded;
1071
1088
  'migration-started': MigrationStarted;
1072
1089
  'migrations-ended': MigrationsEnded;
@@ -1205,6 +1222,10 @@ declare class DatabaseManager {
1205
1222
  * Register a listener on the "database-blocked" event.
1206
1223
  */
1207
1224
  static onDatabaseBlocked(listener: (event: DatabaseBlocked) => void, options?: ListenOptions): void;
1225
+ /**
1226
+ * Register a listener on the "database-version-changed" event.
1227
+ */
1228
+ static onDatabaseVersionChanged(listener: (event: DatabaseVersionChanged) => void, options?: ListenOptions): void;
1208
1229
  /**
1209
1230
  * Register a listener on the "seeding-started" event.
1210
1231
  */
@@ -1386,4 +1407,4 @@ declare class UniqueConstraintViolationException extends Error {
1386
1407
  constructor(table: string, index: string);
1387
1408
  }
1388
1409
 
1389
- export { type Aggregated, type Aggregation, type Aggregations, Blueprint, Builder, CheckConstraintViolationException, ColumnDefinition, type ColumnSchema, type ColumnType, type Conjunction, Connection, type ConnectionConfig, ConnectionNotConfiguredException, type Constraint, DatabaseManager as DB, DatabaseBlocked, DatabaseBlockedException, type DatabaseConfig, type DatabaseEvent, type DatabaseEventListener, DatabaseManager, type DatePart, type Direction, type Enumerable, type FreshOptions, type Grouped, Grouping, type IndexSchema, Join, type JoinClause, type JoinCondition, type JoinType, type Key, type ListenOptions, Migration, type MigrationConstructor, MigrationEnded, MigrationMismatchException, MigrationStarted, type MigrationStatus, MigrationTransactionClosedException, MigrationsEnded, MigrationsStarted, MultipleRecordsFoundException, NoPendingMigrations, NotNullConstraintViolationException, type Operator, type Order, type Paginated, type Plan, type Projection, QueryExecuted, type QueryLogEntry, QuotaExceededException, RecordsNotFoundException, ReservedTableException, Schema, SchemaException, Seeder, type SeederConstructor, SeederEnded, SeederStarted, SeedingEnded, SeedingStarted, TableNotFoundException, type TableSchema, Transaction, TransactionBeginning, TransactionCommitted, type TransactionOptions, TransactionRolledBack, UniqueConstraintViolationException };
1410
+ export { type Aggregated, type Aggregation, type Aggregations, Blueprint, Builder, CheckConstraintViolationException, ColumnDefinition, type ColumnSchema, type ColumnType, type Conjunction, Connection, type ConnectionConfig, ConnectionNotConfiguredException, type Constraint, DatabaseManager as DB, DatabaseBlocked, DatabaseBlockedException, type DatabaseConfig, type DatabaseEvent, type DatabaseEventListener, DatabaseManager, DatabaseVersionChanged, type DatePart, type Direction, type Enumerable, type FreshOptions, type Grouped, Grouping, type IndexSchema, Join, type JoinClause, type JoinCondition, type JoinType, type Key, type ListenOptions, Migration, type MigrationConstructor, MigrationEnded, MigrationMismatchException, MigrationStarted, type MigrationStatus, MigrationTransactionClosedException, MigrationsEnded, MigrationsStarted, MultipleRecordsFoundException, NoPendingMigrations, NotNullConstraintViolationException, type Operator, type Order, type Paginated, type Plan, type Projection, QueryExecuted, type QueryLogEntry, QuotaExceededException, RecordsNotFoundException, ReservedTableException, Schema, SchemaException, Seeder, type SeederConstructor, SeederEnded, SeederStarted, SeedingEnded, SeedingStarted, TableNotFoundException, type TableSchema, Transaction, TransactionBeginning, TransactionCommitted, type TransactionOptions, TransactionRolledBack, UniqueConstraintViolationException };
package/dist/main.d.ts CHANGED
@@ -861,6 +861,22 @@ declare class DatabaseBlocked extends Event {
861
861
  get database(): string;
862
862
  }
863
863
 
864
+ declare class DatabaseVersionChanged extends Event {
865
+ #private;
866
+ /**
867
+ * Create a new Database Version Changed event instance.
868
+ */
869
+ constructor(database: string, version: number | null);
870
+ /**
871
+ * Get the name of the database.
872
+ */
873
+ get database(): string;
874
+ /**
875
+ * Get the version the other tab is opening, or null when it is deleting the database.
876
+ */
877
+ get version(): number | null;
878
+ }
879
+
864
880
  declare class MigrationEnded extends Event {
865
881
  #private;
866
882
  /**
@@ -1067,6 +1083,7 @@ declare class TransactionRolledBack extends Event {
1067
1083
 
1068
1084
  type DatabaseEvent = {
1069
1085
  'database-blocked': DatabaseBlocked;
1086
+ 'database-version-changed': DatabaseVersionChanged;
1070
1087
  'migration-ended': MigrationEnded;
1071
1088
  'migration-started': MigrationStarted;
1072
1089
  'migrations-ended': MigrationsEnded;
@@ -1205,6 +1222,10 @@ declare class DatabaseManager {
1205
1222
  * Register a listener on the "database-blocked" event.
1206
1223
  */
1207
1224
  static onDatabaseBlocked(listener: (event: DatabaseBlocked) => void, options?: ListenOptions): void;
1225
+ /**
1226
+ * Register a listener on the "database-version-changed" event.
1227
+ */
1228
+ static onDatabaseVersionChanged(listener: (event: DatabaseVersionChanged) => void, options?: ListenOptions): void;
1208
1229
  /**
1209
1230
  * Register a listener on the "seeding-started" event.
1210
1231
  */
@@ -1386,4 +1407,4 @@ declare class UniqueConstraintViolationException extends Error {
1386
1407
  constructor(table: string, index: string);
1387
1408
  }
1388
1409
 
1389
- export { type Aggregated, type Aggregation, type Aggregations, Blueprint, Builder, CheckConstraintViolationException, ColumnDefinition, type ColumnSchema, type ColumnType, type Conjunction, Connection, type ConnectionConfig, ConnectionNotConfiguredException, type Constraint, DatabaseManager as DB, DatabaseBlocked, DatabaseBlockedException, type DatabaseConfig, type DatabaseEvent, type DatabaseEventListener, DatabaseManager, type DatePart, type Direction, type Enumerable, type FreshOptions, type Grouped, Grouping, type IndexSchema, Join, type JoinClause, type JoinCondition, type JoinType, type Key, type ListenOptions, Migration, type MigrationConstructor, MigrationEnded, MigrationMismatchException, MigrationStarted, type MigrationStatus, MigrationTransactionClosedException, MigrationsEnded, MigrationsStarted, MultipleRecordsFoundException, NoPendingMigrations, NotNullConstraintViolationException, type Operator, type Order, type Paginated, type Plan, type Projection, QueryExecuted, type QueryLogEntry, QuotaExceededException, RecordsNotFoundException, ReservedTableException, Schema, SchemaException, Seeder, type SeederConstructor, SeederEnded, SeederStarted, SeedingEnded, SeedingStarted, TableNotFoundException, type TableSchema, Transaction, TransactionBeginning, TransactionCommitted, type TransactionOptions, TransactionRolledBack, UniqueConstraintViolationException };
1410
+ export { type Aggregated, type Aggregation, type Aggregations, Blueprint, Builder, CheckConstraintViolationException, ColumnDefinition, type ColumnSchema, type ColumnType, type Conjunction, Connection, type ConnectionConfig, ConnectionNotConfiguredException, type Constraint, DatabaseManager as DB, DatabaseBlocked, DatabaseBlockedException, type DatabaseConfig, type DatabaseEvent, type DatabaseEventListener, DatabaseManager, DatabaseVersionChanged, type DatePart, type Direction, type Enumerable, type FreshOptions, type Grouped, Grouping, type IndexSchema, Join, type JoinClause, type JoinCondition, type JoinType, type Key, type ListenOptions, Migration, type MigrationConstructor, MigrationEnded, MigrationMismatchException, MigrationStarted, type MigrationStatus, MigrationTransactionClosedException, MigrationsEnded, MigrationsStarted, MultipleRecordsFoundException, NoPendingMigrations, NotNullConstraintViolationException, type Operator, type Order, type Paginated, type Plan, type Projection, QueryExecuted, type QueryLogEntry, QuotaExceededException, RecordsNotFoundException, ReservedTableException, Schema, SchemaException, Seeder, type SeederConstructor, SeederEnded, SeederStarted, SeedingEnded, SeedingStarted, TableNotFoundException, type TableSchema, Transaction, TransactionBeginning, TransactionCommitted, type TransactionOptions, TransactionRolledBack, UniqueConstraintViolationException };
package/dist/main.js CHANGED
@@ -626,6 +626,38 @@ var DatabaseBlocked = class extends Event {
626
626
  }
627
627
  };
628
628
 
629
+ // src/events/DatabaseVersionChanged.ts
630
+ var DatabaseVersionChanged = class extends Event {
631
+ /**
632
+ * The name of the database.
633
+ */
634
+ #database;
635
+ /**
636
+ * The version the other tab is opening, or null when it is deleting the database.
637
+ */
638
+ #version;
639
+ /**
640
+ * Create a new Database Version Changed event instance.
641
+ */
642
+ constructor(database, version) {
643
+ super("db:database-version-changed");
644
+ this.#database = database;
645
+ this.#version = version;
646
+ }
647
+ /**
648
+ * Get the name of the database.
649
+ */
650
+ get database() {
651
+ return this.#database;
652
+ }
653
+ /**
654
+ * Get the version the other tab is opening, or null when it is deleting the database.
655
+ */
656
+ get version() {
657
+ return this.#version;
658
+ }
659
+ };
660
+
629
661
  // src/events/MigrationEnded.ts
630
662
  var MigrationEnded = class extends Event {
631
663
  /**
@@ -3743,7 +3775,10 @@ var Connection = class {
3743
3775
  Migrator.verify(ran, Migrator.names(this.migrations));
3744
3776
  this.#schemas = new Map(registry.map((schema) => [schema.table, schema]));
3745
3777
  this.#database = database;
3746
- database.onversionchange = () => this.disconnect();
3778
+ database.onversionchange = (event) => {
3779
+ this.disconnect();
3780
+ Dispatcher.dispatch(new DatabaseVersionChanged(this.#config.database, event.newVersion));
3781
+ };
3747
3782
  return database;
3748
3783
  }
3749
3784
  };
@@ -3979,6 +4014,12 @@ var DatabaseManager = class {
3979
4014
  static onDatabaseBlocked(listener, options) {
3980
4015
  this.listen("database-blocked", listener, options);
3981
4016
  }
4017
+ /**
4018
+ * Register a listener on the "database-version-changed" event.
4019
+ */
4020
+ static onDatabaseVersionChanged(listener, options) {
4021
+ this.listen("database-version-changed", listener, options);
4022
+ }
3982
4023
  /**
3983
4024
  * Register a listener on the "seeding-started" event.
3984
4025
  */
@@ -4307,6 +4348,7 @@ export {
4307
4348
  DatabaseBlocked,
4308
4349
  DatabaseBlockedException,
4309
4350
  DatabaseManager,
4351
+ DatabaseVersionChanged,
4310
4352
  Grouping,
4311
4353
  Join,
4312
4354
  Migration,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bjnstnkvc/db",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
4
4
  "description": "TypeScript database layer for IndexedDB with an API modeled on Laravel.",
5
5
  "type": "module",
6
6
  "main": "./dist/main.cjs",