@pol-studios/db 1.0.48 → 1.0.50

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.
@@ -292,6 +292,13 @@ declare class AdapterAutoDetector {
292
292
  private lastResult;
293
293
  private syncStatus;
294
294
  constructor(powerSyncDb: PowerSyncDatabase | null, supabase: SupabaseClient | null, options?: AutoDetectorOptions);
295
+ /**
296
+ * Update the PowerSync database reference.
297
+ * Called when PowerSync becomes available after initial construction.
298
+ *
299
+ * @param db - PowerSync database instance or null
300
+ */
301
+ setPowerSyncDb(db: PowerSyncDatabase | null): void;
295
302
  /**
296
303
  * Update the sync status from PowerSync.
297
304
  * Called when sync status changes to re-evaluate backend recommendation.
@@ -530,9 +537,19 @@ declare class AdapterRegistry {
530
537
  */
531
538
  private adapters;
532
539
  /**
533
- * PowerSync adapter instance (set during initialization)
540
+ * PowerSync adapter instance (set during initialization or lazily via getter)
534
541
  */
535
542
  private powerSyncAdapter;
543
+ /**
544
+ * Getter function to retrieve PowerSync database instance.
545
+ * Used for lazy initialization when PowerSync becomes available after registry creation.
546
+ */
547
+ private powerSyncGetter;
548
+ /**
549
+ * Factory function to create PowerSync adapter from database instance.
550
+ * Set via setPowerSyncGetter along with the getter.
551
+ */
552
+ private powerSyncAdapterFactory;
536
553
  /**
537
554
  * Supabase adapter instance (set during initialization)
538
555
  */
@@ -592,6 +609,26 @@ declare class AdapterRegistry {
592
609
  * @param adapter - PowerSync adapter implementation
593
610
  */
594
611
  setPowerSyncAdapter(adapter: TableDataAdapter): void;
612
+ /**
613
+ * Set a getter function for lazy PowerSync adapter initialization.
614
+ *
615
+ * This allows the registry to lazily create the PowerSync adapter when
616
+ * PowerSync becomes available, rather than requiring it at initialization time.
617
+ * Solves race conditions where queries/mutations run before PowerSync is ready.
618
+ *
619
+ * @param getter - Function that returns the PowerSync database instance (or null if not ready)
620
+ * @param factory - Function that creates a TableDataAdapter from the database instance
621
+ */
622
+ setPowerSyncGetter(getter: () => unknown, factory: (db: unknown) => TableDataAdapter): void;
623
+ /**
624
+ * Ensure PowerSync adapter is initialized if available.
625
+ *
626
+ * Checks the getter (if set) and lazily creates the adapter if PowerSync
627
+ * is now available. Also updates the auto-detector with the new reference.
628
+ *
629
+ * @returns true if PowerSync adapter is available, false otherwise
630
+ */
631
+ private ensurePowerSyncAdapter;
595
632
  /**
596
633
  * Set the sync tracker for mutation tracking.
597
634
  * When set, write operations on PowerSync tables will
@@ -643,7 +680,7 @@ declare class AdapterRegistry {
643
680
  * @returns The appropriate adapter for the table
644
681
  * @throws Error if adapters are not initialized
645
682
  */
646
- getAdapter(table: string, operation?: 'read' | 'write'): TableDataAdapter;
683
+ getAdapter(table: string, _operation?: 'read' | 'write'): TableDataAdapter;
647
684
  /**
648
685
  * Get the PowerSync adapter directly
649
686
  *
@@ -4164,6 +4164,18 @@ var AdapterAutoDetector = class {
4164
4164
  listeners = /* @__PURE__ */ new Set();
4165
4165
  lastResult = null;
4166
4166
  syncStatus = null;
4167
+ /**
4168
+ * Update the PowerSync database reference.
4169
+ * Called when PowerSync becomes available after initial construction.
4170
+ *
4171
+ * @param db - PowerSync database instance or null
4172
+ */
4173
+ setPowerSyncDb(db) {
4174
+ this.powerSyncDb = db;
4175
+ if (db) {
4176
+ this.detect();
4177
+ }
4178
+ }
4167
4179
  /**
4168
4180
  * Update the sync status from PowerSync.
4169
4181
  * Called when sync status changes to re-evaluate backend recommendation.
@@ -4611,9 +4623,19 @@ var AdapterRegistry = class {
4611
4623
  */
4612
4624
  adapters = /* @__PURE__ */ new Map();
4613
4625
  /**
4614
- * PowerSync adapter instance (set during initialization)
4626
+ * PowerSync adapter instance (set during initialization or lazily via getter)
4615
4627
  */
4616
4628
  powerSyncAdapter = null;
4629
+ /**
4630
+ * Getter function to retrieve PowerSync database instance.
4631
+ * Used for lazy initialization when PowerSync becomes available after registry creation.
4632
+ */
4633
+ powerSyncGetter = null;
4634
+ /**
4635
+ * Factory function to create PowerSync adapter from database instance.
4636
+ * Set via setPowerSyncGetter along with the getter.
4637
+ */
4638
+ powerSyncAdapterFactory = null;
4617
4639
  /**
4618
4640
  * Supabase adapter instance (set during initialization)
4619
4641
  */
@@ -4682,6 +4704,46 @@ var AdapterRegistry = class {
4682
4704
  this.syncTrackingAdapter = null;
4683
4705
  }
4684
4706
  }
4707
+ /**
4708
+ * Set a getter function for lazy PowerSync adapter initialization.
4709
+ *
4710
+ * This allows the registry to lazily create the PowerSync adapter when
4711
+ * PowerSync becomes available, rather than requiring it at initialization time.
4712
+ * Solves race conditions where queries/mutations run before PowerSync is ready.
4713
+ *
4714
+ * @param getter - Function that returns the PowerSync database instance (or null if not ready)
4715
+ * @param factory - Function that creates a TableDataAdapter from the database instance
4716
+ */
4717
+ setPowerSyncGetter(getter, factory) {
4718
+ this.powerSyncGetter = getter;
4719
+ this.powerSyncAdapterFactory = factory;
4720
+ }
4721
+ /**
4722
+ * Ensure PowerSync adapter is initialized if available.
4723
+ *
4724
+ * Checks the getter (if set) and lazily creates the adapter if PowerSync
4725
+ * is now available. Also updates the auto-detector with the new reference.
4726
+ *
4727
+ * @returns true if PowerSync adapter is available, false otherwise
4728
+ */
4729
+ ensurePowerSyncAdapter() {
4730
+ if (this.powerSyncAdapter) {
4731
+ return true;
4732
+ }
4733
+ if (!this.powerSyncGetter || !this.powerSyncAdapterFactory) {
4734
+ return false;
4735
+ }
4736
+ const db = this.powerSyncGetter();
4737
+ if (!db) {
4738
+ return false;
4739
+ }
4740
+ const adapter = this.powerSyncAdapterFactory(db);
4741
+ this.setPowerSyncAdapter(adapter);
4742
+ if (this.autoDetector) {
4743
+ this.autoDetector.setPowerSyncDb(db);
4744
+ }
4745
+ return true;
4746
+ }
4685
4747
  /**
4686
4748
  * Set the sync tracker for mutation tracking.
4687
4749
  * When set, write operations on PowerSync tables will
@@ -4752,7 +4814,8 @@ var AdapterRegistry = class {
4752
4814
  * @returns The appropriate adapter for the table
4753
4815
  * @throws Error if adapters are not initialized
4754
4816
  */
4755
- getAdapter(table, operation = "read") {
4817
+ getAdapter(table, _operation = "read") {
4818
+ this.ensurePowerSyncAdapter();
4756
4819
  const tableWithoutSchema = table.includes(".") ? table.split(".")[1] : table;
4757
4820
  const strategy = this.config.tables[table] ?? this.config.tables[tableWithoutSchema];
4758
4821
  if (!strategy || strategy.strategy === "auto") {
@@ -4766,23 +4829,14 @@ var AdapterRegistry = class {
4766
4829
  throw new Error(`Table "${table}" is not configured for PowerSync sync and Supabase adapter is not available. Either add this table to the PowerSync schema or initialize the Supabase adapter.`);
4767
4830
  }
4768
4831
  }
4769
- if (operation === "write") {
4770
- if (this.powerSyncAdapter) {
4771
- return this.syncTrackingAdapter ?? this.powerSyncAdapter;
4772
- }
4773
- throw new Error(`PowerSync adapter not initialized for write operation on table "${table}". Writes to auto-strategy tables require PowerSync to be available for offline queue support.`);
4774
- }
4775
- return this.getAutoAdapter(strategy);
4832
+ return this.getAutoAdapter(strategy, table);
4776
4833
  }
4777
4834
  if (strategy.strategy === "powersync" && this.autoDetector && this.supabaseAdapter) {
4778
- if (operation === "write") {
4779
- if (this.powerSyncAdapter) {
4780
- return this.syncTrackingAdapter ?? this.powerSyncAdapter;
4781
- }
4782
- throw new Error(`PowerSync adapter not initialized for write operation on table "${table}". Writes to PowerSync tables require PowerSync to be available.`);
4783
- }
4784
4835
  const detection = this.autoDetector.detectSilent();
4785
4836
  if (detection.powerSyncStatus === "initializing" /* INITIALIZING */ && detection.isOnline) {
4837
+ if (typeof __DEV__ !== "undefined" && __DEV__) {
4838
+ console.warn(`[AdapterRegistry] Table "${table}" configured for PowerSync but using Supabase. PowerSync status: ${detection.powerSyncStatus}, Online: ${detection.isOnline}`);
4839
+ }
4786
4840
  return this.supabaseAdapter;
4787
4841
  }
4788
4842
  }
@@ -4884,11 +4938,14 @@ var AdapterRegistry = class {
4884
4938
  * @param strategy - Optional auto strategy configuration
4885
4939
  * @returns The automatically selected adapter
4886
4940
  */
4887
- getAutoAdapter(_strategy) {
4941
+ getAutoAdapter(_strategy, table) {
4888
4942
  if (!this.autoDetector) {
4889
4943
  if (!this.supabaseAdapter) {
4890
4944
  throw new Error("No auto-detector configured and Supabase adapter not available. Either initialize auto-detection or set adapters explicitly.");
4891
4945
  }
4946
+ if (typeof __DEV__ !== "undefined" && __DEV__) {
4947
+ console.warn(`[AdapterRegistry] No auto-detector configured${table ? ` for table "${table}"` : ""}. Using Supabase fallback.`);
4948
+ }
4892
4949
  return this.supabaseAdapter;
4893
4950
  }
4894
4951
  const detection = this.autoDetector.detectSilent();
@@ -4898,6 +4955,9 @@ var AdapterRegistry = class {
4898
4955
  if (!this.supabaseAdapter) {
4899
4956
  throw new Error("Neither PowerSync nor Supabase adapters are available.");
4900
4957
  }
4958
+ if (typeof __DEV__ !== "undefined" && __DEV__) {
4959
+ console.warn(`[AdapterRegistry] PowerSync recommended${table ? ` for table "${table}"` : ""} but adapter not available. Falling back to Supabase. Reason: ${detection.reason}`);
4960
+ }
4901
4961
  return this.supabaseAdapter;
4902
4962
  }
4903
4963
  return this.powerSyncAdapter;
@@ -7936,4 +7996,4 @@ moment/moment.js:
7936
7996
  (*! license : MIT *)
7937
7997
  (*! momentjs.com *)
7938
7998
  */
7939
- //# sourceMappingURL=chunk-GEU5ED7L.js.map
7999
+ //# sourceMappingURL=chunk-7CLN4ORU.js.map