mpb-localkit 1.4.4 → 1.4.6

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.
@@ -1,11 +1,11 @@
1
1
  import { ZodRawShape, ZodObject } from 'zod';
2
2
  export { z } from 'zod';
3
- import { e as CollectionOptions, b as CollectionDescriptor, f as StorageAdapter, D as Doc, F as Filter, g as Change, d as SyncEngine, c as SyncConfig, h as SyncTransport, P as PushPayload, i as PullPayload, A as AuthAdapter, a as Credentials, U as User, j as SocialAuthAdapter, k as SocialProvider, O as ObservableCollection } from '../collection-CBG-_MiM.js';
4
- export { C as Collection, l as OfflineSession, Q as QueryOptions, m as SyncResult, S as SyncStatus, T as TransportType, n as WhereClause, W as WithMeta } from '../collection-CBG-_MiM.js';
5
- import { E as ErrorConfig, a as ErrorEntry, b as ErrorType, c as ErrorContext, S as SyncAPI, d as ErrorTrackingConfig, A as AuthConfig } from '../types-BlqxG_-6.js';
6
- export { e as AppConfig, f as ErrorSnapshot } from '../types-BlqxG_-6.js';
7
- import { A as AuthStore, S as SyncStore } from '../events-hGI5qde5.js';
8
- export { c as collectionQueryOptions, l as localkitKeys, s as subscribeToCollection } from '../query-D2jbktTG.js';
3
+ import { f as CollectionOptions, b as CollectionDescriptor, e as StorageAdapter, D as Doc, F as Filter, g as Change, d as SyncEngine, c as SyncConfig, h as SyncTransport, P as PushPayload, i as PullPayload, A as AuthAdapter, a as Credentials, U as User, j as SocialAuthAdapter, k as SocialProvider, O as ObservableCollection } from '../collection-Bfdtksek.js';
4
+ export { C as Collection, l as OfflineSession, Q as QueryOptions, m as SyncResult, S as SyncStatus, T as TransportType, n as WhereClause, W as WithMeta } from '../collection-Bfdtksek.js';
5
+ import { E as ErrorConfig, a as ErrorEntry, b as ErrorType, c as ErrorContext, S as SyncAPI, d as ErrorTrackingConfig, A as AuthConfig } from '../types-BDV8co-l.js';
6
+ export { e as AppConfig, f as ErrorSnapshot } from '../types-BDV8co-l.js';
7
+ import { A as AuthStore, S as SyncStore } from '../events-E14LI52R.js';
8
+ export { c as collectionQueryOptions, l as localkitKeys, s as subscribeToCollection } from '../query-DEV1NGRT.js';
9
9
 
10
10
  /**
11
11
  * Wraps a Zod schema to create a typed collection descriptor.
@@ -17,6 +17,31 @@ export { c as collectionQueryOptions, l as localkitKeys, s as subscribeToCollect
17
17
  */
18
18
  declare function collection<TShape extends ZodRawShape>(schema: ZodObject<TShape>, options?: CollectionOptions): CollectionDescriptor<TShape>;
19
19
 
20
+ declare class IndexedDBAdapter implements StorageAdapter {
21
+ private dbName;
22
+ private db;
23
+ private _ensureLock;
24
+ constructor(appName: string);
25
+ private getDb;
26
+ private ensureStore;
27
+ get(collection: string, id: string): Promise<Doc | null>;
28
+ getMany(collection: string, filter?: Filter): Promise<Doc[]>;
29
+ put(collection: string, id: string, doc: Doc): Promise<void>;
30
+ delete(collection: string, id: string): Promise<void>;
31
+ getChangesSince(timestamp: number): Promise<Change[]>;
32
+ }
33
+
34
+ declare class MemoryAdapter implements StorageAdapter {
35
+ private store;
36
+ private getCollection;
37
+ get(collection: string, id: string): Promise<Doc | null>;
38
+ getRaw(collection: string, id: string): Promise<Doc | null>;
39
+ getMany(collection: string, filter?: Filter): Promise<Doc[]>;
40
+ put(collection: string, id: string, doc: Doc): Promise<void>;
41
+ delete(collection: string, id: string): Promise<void>;
42
+ getChangesSince(timestamp: number): Promise<Change[]>;
43
+ }
44
+
20
45
  declare function generateId(): string;
21
46
 
22
47
  /** A raw CryptoKey or a password+salt pair for PBKDF2 / AES-KW derivation. */
@@ -391,6 +416,8 @@ type App<C extends Record<string, CollectionDescriptor<ZodRawShape>>> = Observab
391
416
  errors: ErrorManager;
392
417
  syncStore: SyncStore;
393
418
  sync?: SyncAPI;
419
+ /** @internal Upgrade storage adapter (used by LocalKitProvider for SSR→client transition) */
420
+ _upgradeStorage: (adapter: StorageAdapter) => void;
394
421
  };
395
422
  interface CreateAppConfig<C extends Record<string, CollectionDescriptor<ZodRawShape>>> {
396
423
  collections: C;
@@ -401,4 +428,4 @@ interface CreateAppConfig<C extends Record<string, CollectionDescriptor<ZodRawSh
401
428
  }
402
429
  declare function createApp<C extends Record<string, CollectionDescriptor<ZodRawShape>>>(config: CreateAppConfig<C>): App<C>;
403
430
 
404
- export { type App, AuthAdapter, AuthConfig, AutoTransport, type AutoTransportConfig, BetterAuthAdapter, type BetterAuthAdapterConfig, type BetterAuthClient, Change, CollectionDescriptor, type CreateAppConfig, Credentials, Doc, EmailPasswordAuth, type EmailPasswordAuthConfig, EncryptedStorageAdapter, type EncryptionConfig, ErrorContext, ErrorEntry, ErrorTrackingConfig, ErrorType, Filter, HttpTransport, type KeySpec, type KeychainConfig, type MigrationResult, OfflineSessionAdapter, PullPayload, PushPayload, type ReEncryptResult, SocialAuthAdapter, SocialProvider, StorageAdapter, SyncConfig, SyncEngine, SyncTransport, User, WebSocketTransport, type WebSocketTransportConfig, type WrapConfig, collection, createApp, encrypted, generateId, hashPassword, migrateToEncrypted, reEncryptAll, resolveConflict, rewrapKey, setupFocusTrigger, setupIntervalTrigger, setupReconnectTrigger };
431
+ export { type App, AuthAdapter, AuthConfig, AutoTransport, type AutoTransportConfig, BetterAuthAdapter, type BetterAuthAdapterConfig, type BetterAuthClient, Change, CollectionDescriptor, type CreateAppConfig, Credentials, Doc, EmailPasswordAuth, type EmailPasswordAuthConfig, EncryptedStorageAdapter, type EncryptionConfig, ErrorContext, ErrorEntry, ErrorTrackingConfig, ErrorType, Filter, HttpTransport, IndexedDBAdapter, type KeySpec, type KeychainConfig, MemoryAdapter, type MigrationResult, OfflineSessionAdapter, PullPayload, PushPayload, type ReEncryptResult, SocialAuthAdapter, SocialProvider, StorageAdapter, SyncConfig, SyncEngine, SyncTransport, User, WebSocketTransport, type WebSocketTransportConfig, type WrapConfig, collection, createApp, encrypted, generateId, hashPassword, migrateToEncrypted, reEncryptAll, resolveConflict, rewrapKey, setupFocusTrigger, setupIntervalTrigger, setupReconnectTrigger };
@@ -1,122 +1,8 @@
1
- import { openDB } from 'idb';
2
1
  export { z } from 'zod';
2
+ import { openDB } from 'idb';
3
3
  import { v7 } from 'uuid';
4
4
 
5
- var __defProp = Object.defineProperty;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __esm = (fn, res) => function __init() {
8
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
9
- };
10
- var __export = (target, all) => {
11
- for (var name in all)
12
- __defProp(target, name, { get: all[name], enumerable: true });
13
- };
14
-
15
- // src/core/storage/indexeddb.ts
16
- var indexeddb_exports = {};
17
- __export(indexeddb_exports, {
18
- IndexedDBAdapter: () => IndexedDBAdapter
19
- });
20
- var CHANGES_INDEX, IndexedDBAdapter;
21
- var init_indexeddb = __esm({
22
- "src/core/storage/indexeddb.ts"() {
23
- CHANGES_INDEX = "_updatedAt";
24
- IndexedDBAdapter = class {
25
- dbName;
26
- db = null;
27
- _ensureLock = Promise.resolve();
28
- constructor(appName) {
29
- this.dbName = `offlinekit-${appName}`;
30
- }
31
- async getDb() {
32
- if (this.db) return this.db;
33
- this.db = await openDB(this.dbName, 1, {
34
- upgrade(_db) {
35
- }
36
- });
37
- return this.db;
38
- }
39
- async ensureStore(collection2) {
40
- const prev = this._ensureLock;
41
- let release;
42
- this._ensureLock = new Promise((r) => {
43
- release = r;
44
- });
45
- await prev;
46
- try {
47
- let db = await this.getDb();
48
- if (db.objectStoreNames.contains(collection2)) return db;
49
- const version = db.version + 1;
50
- db.close();
51
- this.db = null;
52
- db = await openDB(this.dbName, version, {
53
- upgrade(db2) {
54
- if (!db2.objectStoreNames.contains(collection2)) {
55
- const store = db2.createObjectStore(collection2, { keyPath: "_id" });
56
- store.createIndex(CHANGES_INDEX, "_updatedAt");
57
- }
58
- }
59
- });
60
- this.db = db;
61
- return db;
62
- } finally {
63
- release();
64
- }
65
- }
66
- async get(collection2, id) {
67
- const db = await this.ensureStore(collection2);
68
- const doc = await db.get(collection2, id);
69
- if (!doc || doc._deleted) return null;
70
- return doc;
71
- }
72
- async getMany(collection2, filter) {
73
- const db = await this.ensureStore(collection2);
74
- const all = await db.getAll(collection2);
75
- return all.filter((doc) => {
76
- if (doc._deleted) return false;
77
- if (!filter) return true;
78
- return Object.entries(filter).every(([k, v]) => Reflect.get(doc, k) === v);
79
- });
80
- }
81
- async put(collection2, id, doc) {
82
- const db = await this.ensureStore(collection2);
83
- await db.put(collection2, { ...doc, _id: id, _collection: collection2 });
84
- }
85
- async delete(collection2, id) {
86
- const db = await this.ensureStore(collection2);
87
- const existing = await db.get(collection2, id);
88
- if (!existing) return;
89
- await db.put(collection2, {
90
- ...existing,
91
- _deleted: true,
92
- _updatedAt: Date.now()
93
- });
94
- }
95
- async getChangesSince(timestamp) {
96
- const db = await this.getDb();
97
- const results = [];
98
- for (const storeName of Array.from(db.objectStoreNames)) {
99
- if (!db.objectStoreNames.contains(storeName)) continue;
100
- const tx = db.transaction(storeName, "readonly");
101
- const store = tx.objectStore(storeName);
102
- const index = store.index(CHANGES_INDEX);
103
- const range = IDBKeyRange.lowerBound(timestamp, true);
104
- const docs = await index.getAll(range);
105
- for (const doc of docs) {
106
- results.push({
107
- collection: storeName,
108
- id: doc._id,
109
- doc,
110
- updatedAt: doc._updatedAt,
111
- deleted: doc._deleted
112
- });
113
- }
114
- }
115
- return results;
116
- }
117
- };
118
- }
119
- });
5
+ // src/core/schema/index.ts
120
6
 
121
7
  // src/core/schema/collection.ts
122
8
  function collection(schema, options) {
@@ -127,6 +13,101 @@ function collection(schema, options) {
127
13
  migrate: options?.migrate
128
14
  };
129
15
  }
16
+ var CHANGES_INDEX = "_updatedAt";
17
+ var IndexedDBAdapter = class {
18
+ dbName;
19
+ db = null;
20
+ _ensureLock = Promise.resolve();
21
+ constructor(appName) {
22
+ this.dbName = `offlinekit-${appName}`;
23
+ }
24
+ async getDb() {
25
+ if (this.db) return this.db;
26
+ this.db = await openDB(this.dbName, 1, {
27
+ upgrade(_db) {
28
+ }
29
+ });
30
+ return this.db;
31
+ }
32
+ async ensureStore(collection2) {
33
+ const prev = this._ensureLock;
34
+ let release;
35
+ this._ensureLock = new Promise((r) => {
36
+ release = r;
37
+ });
38
+ await prev;
39
+ try {
40
+ let db = await this.getDb();
41
+ if (db.objectStoreNames.contains(collection2)) return db;
42
+ const version = db.version + 1;
43
+ db.close();
44
+ this.db = null;
45
+ db = await openDB(this.dbName, version, {
46
+ upgrade(db2) {
47
+ if (!db2.objectStoreNames.contains(collection2)) {
48
+ const store = db2.createObjectStore(collection2, { keyPath: "_id" });
49
+ store.createIndex(CHANGES_INDEX, "_updatedAt");
50
+ }
51
+ }
52
+ });
53
+ this.db = db;
54
+ return db;
55
+ } finally {
56
+ release();
57
+ }
58
+ }
59
+ async get(collection2, id) {
60
+ const db = await this.ensureStore(collection2);
61
+ const doc = await db.get(collection2, id);
62
+ if (!doc || doc._deleted) return null;
63
+ return doc;
64
+ }
65
+ async getMany(collection2, filter) {
66
+ const db = await this.ensureStore(collection2);
67
+ const all = await db.getAll(collection2);
68
+ return all.filter((doc) => {
69
+ if (doc._deleted) return false;
70
+ if (!filter) return true;
71
+ return Object.entries(filter).every(([k, v]) => Reflect.get(doc, k) === v);
72
+ });
73
+ }
74
+ async put(collection2, id, doc) {
75
+ const db = await this.ensureStore(collection2);
76
+ await db.put(collection2, { ...doc, _id: id, _collection: collection2 });
77
+ }
78
+ async delete(collection2, id) {
79
+ const db = await this.ensureStore(collection2);
80
+ const existing = await db.get(collection2, id);
81
+ if (!existing) return;
82
+ await db.put(collection2, {
83
+ ...existing,
84
+ _deleted: true,
85
+ _updatedAt: Date.now()
86
+ });
87
+ }
88
+ async getChangesSince(timestamp) {
89
+ const db = await this.getDb();
90
+ const results = [];
91
+ for (const storeName of Array.from(db.objectStoreNames)) {
92
+ if (!db.objectStoreNames.contains(storeName)) continue;
93
+ const tx = db.transaction(storeName, "readonly");
94
+ const store = tx.objectStore(storeName);
95
+ const index = store.index(CHANGES_INDEX);
96
+ const range = IDBKeyRange.lowerBound(timestamp, true);
97
+ const docs = await index.getAll(range);
98
+ for (const doc of docs) {
99
+ results.push({
100
+ collection: storeName,
101
+ id: doc._id,
102
+ doc,
103
+ updatedAt: doc._updatedAt,
104
+ deleted: doc._deleted
105
+ });
106
+ }
107
+ }
108
+ return results;
109
+ }
110
+ };
130
111
 
131
112
  // src/core/storage/memory.ts
132
113
  var MemoryAdapter = class {
@@ -1795,48 +1776,38 @@ function resolveAuth(config) {
1795
1776
  }
1796
1777
  return adapter;
1797
1778
  }
1798
- var LazyStorageAdapter = class {
1799
- _inner = null;
1800
- _initPromise = null;
1801
- resolve() {
1802
- if (this._inner) return Promise.resolve(this._inner);
1803
- if (this._initPromise) return this._initPromise;
1804
- this._initPromise = (async () => {
1805
- if (typeof window !== "undefined" && typeof indexedDB !== "undefined") {
1806
- const { IndexedDBAdapter: IndexedDBAdapter2 } = await Promise.resolve().then(() => (init_indexeddb(), indexeddb_exports));
1807
- this._inner = new IndexedDBAdapter2("localkit");
1808
- } else {
1809
- this._inner = new MemoryAdapter();
1810
- }
1811
- return this._inner;
1812
- })();
1813
- return this._initPromise;
1779
+ var StorageProxy = class {
1780
+ constructor(_target) {
1781
+ this._target = _target;
1814
1782
  }
1815
- async get(collection2, id) {
1816
- return (await this.resolve()).get(collection2, id);
1783
+ get target() {
1784
+ return this._target;
1817
1785
  }
1818
- async getRaw(collection2, id) {
1819
- const s = await this.resolve();
1820
- return s.getRaw?.(collection2, id) ?? s.get(collection2, id);
1786
+ setTarget(adapter) {
1787
+ this._target = adapter;
1821
1788
  }
1822
- async getMany(collection2, filter) {
1823
- return (await this.resolve()).getMany(collection2, filter);
1789
+ get(collection2, id) {
1790
+ return this._target.get(collection2, id);
1824
1791
  }
1825
- async put(collection2, id, doc) {
1826
- return (await this.resolve()).put(collection2, id, doc);
1792
+ getRaw(collection2, id) {
1793
+ return this._target.getRaw?.(collection2, id) ?? this._target.get(collection2, id);
1827
1794
  }
1828
- async delete(collection2, id) {
1829
- return (await this.resolve()).delete(collection2, id);
1795
+ getMany(collection2, filter) {
1796
+ return this._target.getMany(collection2, filter);
1830
1797
  }
1831
- async getChangesSince(since) {
1832
- return (await this.resolve()).getChangesSince(since);
1798
+ put(collection2, id, doc) {
1799
+ return this._target.put(collection2, id, doc);
1800
+ }
1801
+ delete(collection2, id) {
1802
+ return this._target.delete(collection2, id);
1803
+ }
1804
+ getChangesSince(since) {
1805
+ return this._target.getChangesSince(since);
1833
1806
  }
1834
1807
  };
1835
- function defaultStorage() {
1836
- return new LazyStorageAdapter();
1837
- }
1838
1808
  function createApp(config) {
1839
- const storage = config.storage ?? defaultStorage();
1809
+ const storageProxy = new StorageProxy(config.storage ?? new MemoryAdapter());
1810
+ const storage = storageProxy;
1840
1811
  const RESERVED_KEYS = ["auth", "errors", "syncStore", "sync"];
1841
1812
  for (const name of Object.keys(config.collections)) {
1842
1813
  if (RESERVED_KEYS.includes(name)) {
@@ -1901,7 +1872,10 @@ function createApp(config) {
1901
1872
  auth: authStore,
1902
1873
  errors,
1903
1874
  syncStore,
1904
- ...syncAPI !== void 0 ? { sync: syncAPI } : {}
1875
+ ...syncAPI !== void 0 ? { sync: syncAPI } : {},
1876
+ _upgradeStorage(adapter) {
1877
+ storageProxy.setTarget(adapter);
1878
+ }
1905
1879
  };
1906
1880
  }
1907
1881
 
@@ -1930,6 +1904,6 @@ function subscribeToCollection(collection2, queryClient) {
1930
1904
  });
1931
1905
  }
1932
1906
 
1933
- export { AutoTransport, BetterAuthAdapter, EmailPasswordAuth, EncryptedStorageAdapter, HttpTransport, OfflineSessionAdapter, SyncEngine, WebSocketTransport, collection, collectionQueryOptions, createApp, encrypted, generateId, hashPassword, localkitKeys, migrateToEncrypted, reEncryptAll, resolveConflict, rewrapKey, setupFocusTrigger, setupIntervalTrigger, setupReconnectTrigger, subscribeToCollection };
1907
+ export { AutoTransport, BetterAuthAdapter, EmailPasswordAuth, EncryptedStorageAdapter, HttpTransport, IndexedDBAdapter, MemoryAdapter, OfflineSessionAdapter, SyncEngine, WebSocketTransport, collection, collectionQueryOptions, createApp, encrypted, generateId, hashPassword, localkitKeys, migrateToEncrypted, reEncryptAll, resolveConflict, rewrapKey, setupFocusTrigger, setupIntervalTrigger, setupReconnectTrigger, subscribeToCollection };
1934
1908
  //# sourceMappingURL=index.js.map
1935
1909
  //# sourceMappingURL=index.js.map