@firtoz/drizzle-indexeddb 0.3.0 → 0.4.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.
@@ -0,0 +1,41 @@
1
+ import type {
2
+ IDBDatabaseLike,
3
+ IDBCreator,
4
+ IDBOpenOptions,
5
+ IDBDeleter,
6
+ } from "./idb-types";
7
+ import { defaultIDBCreator } from "./native-idb-database";
8
+
9
+ /**
10
+ * Opens an IndexedDB database using the provided creator or the default native implementation.
11
+ */
12
+ export async function openIndexedDb(
13
+ name: string,
14
+ dbCreator?: IDBCreator,
15
+ options?: IDBOpenOptions,
16
+ ): Promise<IDBDatabaseLike> {
17
+ const dbCreatorToUse = dbCreator ?? defaultIDBCreator;
18
+ return dbCreatorToUse(name, options);
19
+ }
20
+
21
+ /**
22
+ * Default IDB deleter that uses the native IndexedDB API.
23
+ */
24
+ const defaultIDBDeleter: IDBDeleter = (name: string): Promise<void> => {
25
+ return new Promise((resolve, reject) => {
26
+ const request = indexedDB.deleteDatabase(name);
27
+ request.onerror = () => reject(request.error);
28
+ request.onsuccess = () => resolve();
29
+ });
30
+ };
31
+
32
+ /**
33
+ * Deletes an IndexedDB database (useful for testing)
34
+ */
35
+ export async function deleteIndexedDB(
36
+ dbName: string,
37
+ dbDeleter?: IDBDeleter,
38
+ ): Promise<void> {
39
+ const dbDeleterToUse = dbDeleter ?? defaultIDBDeleter;
40
+ return dbDeleterToUse(dbName);
41
+ }
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Index information returned by getStoreIndexes
3
+ */
4
+ export interface IndexInfo {
5
+ name: string;
6
+ keyPath: string | string[];
7
+ }
8
+
9
+ /**
10
+ * Options for creating an object store
11
+ */
12
+ export interface CreateStoreOptions {
13
+ keyPath?: string;
14
+ autoIncrement?: boolean;
15
+ }
16
+
17
+ /**
18
+ * Options for creating an index
19
+ */
20
+ export interface CreateIndexOptions {
21
+ unique?: boolean;
22
+ }
23
+
24
+ /**
25
+ * Key range specification for index queries
26
+ */
27
+ export interface KeyRangeSpec {
28
+ type: "only" | "lowerBound" | "upperBound" | "bound";
29
+ value?: unknown;
30
+ lower?: unknown;
31
+ upper?: unknown;
32
+ lowerOpen?: boolean;
33
+ upperOpen?: boolean;
34
+ }
35
+
36
+ /**
37
+ * Minimal database interface with high-level async operations.
38
+ * This is the interface that custom implementations (mocks, Chrome extension proxies, etc.) need to implement.
39
+ *
40
+ * All operations are simple async functions - no transactions, requests, or callbacks to deal with.
41
+ */
42
+ export interface IDBDatabaseLike {
43
+ /** Database version number */
44
+ readonly version: number;
45
+
46
+ // =========================================================================
47
+ // Schema Operations (for migrations)
48
+ // =========================================================================
49
+
50
+ /** Check if a store exists */
51
+ hasStore(storeName: string): boolean;
52
+
53
+ /** Get list of all store names */
54
+ getStoreNames(): string[];
55
+
56
+ /** Create an object store (only valid during migrations) */
57
+ createStore(storeName: string, options?: CreateStoreOptions): void;
58
+
59
+ /** Delete an object store (only valid during migrations) */
60
+ deleteStore(storeName: string): void;
61
+
62
+ /** Create an index on a store (only valid during migrations) */
63
+ createIndex(
64
+ storeName: string,
65
+ indexName: string,
66
+ keyPath: string | string[],
67
+ options?: CreateIndexOptions,
68
+ ): void;
69
+
70
+ /** Delete an index from a store (only valid during migrations) */
71
+ deleteIndex(storeName: string, indexName: string): void;
72
+
73
+ /** Get all indexes for a store (for index discovery) */
74
+ getStoreIndexes(storeName: string): IndexInfo[];
75
+
76
+ // =========================================================================
77
+ // Data Operations (all async, handle transactions internally)
78
+ // =========================================================================
79
+
80
+ /** Get all items from a store */
81
+ getAll<T = unknown>(storeName: string): Promise<T[]>;
82
+
83
+ /** Get items from a store using an index with optional key range */
84
+ getAllByIndex<T = unknown>(
85
+ storeName: string,
86
+ indexName: string,
87
+ keyRange?: KeyRangeSpec,
88
+ ): Promise<T[]>;
89
+
90
+ /** Get a single item by key */
91
+ get<T = unknown>(storeName: string, key: IDBValidKey): Promise<T | undefined>;
92
+
93
+ /** Add items to a store (batch operation) */
94
+ add(storeName: string, items: unknown[]): Promise<void>;
95
+
96
+ /** Update items in a store (batch operation, uses put) */
97
+ put(storeName: string, items: unknown[]): Promise<void>;
98
+
99
+ /** Delete items from a store by keys (batch operation) */
100
+ delete(storeName: string, keys: IDBValidKey[]): Promise<void>;
101
+
102
+ /** Clear all items from a store */
103
+ clear(storeName: string): Promise<void>;
104
+
105
+ // =========================================================================
106
+ // Lifecycle
107
+ // =========================================================================
108
+
109
+ /** Close the database connection */
110
+ close(): void;
111
+ }
112
+
113
+ /**
114
+ * Options for opening a database with version upgrade support.
115
+ */
116
+ export interface IDBOpenOptions {
117
+ /** Target version for the database. If higher than current, triggers upgrade. */
118
+ version?: number;
119
+ /** Called during version upgrade - this is where schema changes (createStore, createIndex) are allowed. */
120
+ onUpgrade?: (db: IDBDatabaseLike) => void;
121
+ }
122
+
123
+ /**
124
+ * Function type for creating/opening an IndexedDB-like database.
125
+ * Custom implementations can use this to provide proxy/mock/alternative backends.
126
+ */
127
+ export type IDBCreator = (
128
+ name: string,
129
+ options?: IDBOpenOptions,
130
+ ) => Promise<IDBDatabaseLike>;
131
+
132
+ /**
133
+ * Function type for deleting an IndexedDB database.
134
+ */
135
+ export type IDBDeleter = (name: string) => Promise<void>;
package/src/index.ts CHANGED
@@ -8,23 +8,35 @@ export {
8
8
  type DeleteIndexOperation,
9
9
  } from "./function-migrator";
10
10
 
11
- export {
12
- deleteIndexedDB,
13
- type IDBCreator,
14
- type IDBOpenOptions,
15
- type IDBDatabaseLike,
16
- type IndexInfo,
17
- type CreateStoreOptions,
18
- type CreateIndexOptions,
19
- type KeyRangeSpec,
20
- } from "./utils";
11
+ // IDB Types
12
+ export type {
13
+ IDBCreator,
14
+ IDBOpenOptions,
15
+ IDBDatabaseLike,
16
+ IDBDeleter,
17
+ IndexInfo,
18
+ CreateStoreOptions,
19
+ CreateIndexOptions,
20
+ KeyRangeSpec,
21
+ } from "./idb-types";
22
+
23
+ // IDB Interceptor (for testing/debugging)
24
+ export type { IDBInterceptor, IDBOperation } from "./idb-interceptor";
25
+
26
+ // IDB Operations
27
+ export { openIndexedDb, deleteIndexedDB } from "./idb-operations";
28
+
29
+ // Native IDB Implementation
30
+ export { defaultIDBCreator } from "./native-idb-database";
21
31
 
32
+ // Instrumented IDB (for testing)
33
+ export { createInstrumentedDbCreator } from "./instrumented-idb-database";
34
+
35
+ // Collection
22
36
  export {
23
37
  indexedDBCollectionOptions,
24
38
  type IndexedDBCollectionConfig,
25
39
  type IndexedDBSyncItem,
26
- type IDBInterceptor,
27
- type IDBOperation,
28
40
  } from "./collections/indexeddb-collection";
29
41
 
30
42
  // IndexedDB Provider
@@ -39,3 +51,30 @@ export {
39
51
  useDrizzleIndexedDB,
40
52
  type UseDrizzleIndexedDBContextReturn,
41
53
  } from "./context/useDrizzleIndexedDB";
54
+
55
+ // IDB Proxy (for Chrome extension, messaging-based IDB access)
56
+ export {
57
+ // Types
58
+ type IDBProxyRequest,
59
+ type IDBProxyRequestBody,
60
+ type IDBProxyResponse,
61
+ type IDBProxySyncMessage,
62
+ generateRequestId,
63
+ generateClientId,
64
+ // Transport
65
+ type IDBProxyClientTransport,
66
+ type IDBProxyServerTransport,
67
+ createInMemoryTransport,
68
+ createMultiClientTransport,
69
+ // Client
70
+ IDBProxyClient,
71
+ createProxyDbCreator,
72
+ type SyncHandler,
73
+ // Server
74
+ IDBProxyServer,
75
+ createProxyServer,
76
+ type IDBProxyServerOptions,
77
+ // Sync adapter (connects proxy sync to collection)
78
+ createCollectionSyncHandler,
79
+ combineSyncHandlers,
80
+ } from "./proxy";
@@ -0,0 +1,188 @@
1
+ import type {
2
+ IDBDatabaseLike,
3
+ IDBCreator,
4
+ IDBOpenOptions,
5
+ IndexInfo,
6
+ CreateStoreOptions,
7
+ CreateIndexOptions,
8
+ KeyRangeSpec,
9
+ } from "./idb-types";
10
+ import type { IDBInterceptor } from "./idb-interceptor";
11
+ import { defaultIDBCreator } from "./native-idb-database";
12
+
13
+ /**
14
+ * A database wrapper that intercepts operations and reports them to an interceptor.
15
+ * Useful for testing to verify what IndexedDB operations are actually performed.
16
+ */
17
+ class InstrumentedIDBDatabase implements IDBDatabaseLike {
18
+ constructor(
19
+ private db: IDBDatabaseLike,
20
+ private interceptor: IDBInterceptor,
21
+ ) {}
22
+
23
+ get version(): number {
24
+ return this.db.version;
25
+ }
26
+
27
+ // Schema operations (pass through without interception)
28
+ hasStore(storeName: string): boolean {
29
+ return this.db.hasStore(storeName);
30
+ }
31
+
32
+ getStoreNames(): string[] {
33
+ return this.db.getStoreNames();
34
+ }
35
+
36
+ createStore(storeName: string, options?: CreateStoreOptions): void {
37
+ this.db.createStore(storeName, options);
38
+ }
39
+
40
+ deleteStore(storeName: string): void {
41
+ this.db.deleteStore(storeName);
42
+ }
43
+
44
+ createIndex(
45
+ storeName: string,
46
+ indexName: string,
47
+ keyPath: string | string[],
48
+ options?: CreateIndexOptions,
49
+ ): void {
50
+ this.db.createIndex(storeName, indexName, keyPath, options);
51
+ }
52
+
53
+ deleteIndex(storeName: string, indexName: string): void {
54
+ this.db.deleteIndex(storeName, indexName);
55
+ }
56
+
57
+ getStoreIndexes(storeName: string): IndexInfo[] {
58
+ return this.db.getStoreIndexes(storeName);
59
+ }
60
+
61
+ // Data operations (intercepted)
62
+ async getAll<T = unknown>(storeName: string): Promise<T[]> {
63
+ const items = await this.db.getAll<T>(storeName);
64
+
65
+ this.interceptor.onOperation?.({
66
+ type: "getAll",
67
+ storeName,
68
+ itemsReturned: items,
69
+ itemCount: items.length,
70
+ timestamp: Date.now(),
71
+ });
72
+
73
+ return items;
74
+ }
75
+
76
+ async getAllByIndex<T = unknown>(
77
+ storeName: string,
78
+ indexName: string,
79
+ keyRange?: KeyRangeSpec,
80
+ ): Promise<T[]> {
81
+ const items = await this.db.getAllByIndex<T>(
82
+ storeName,
83
+ indexName,
84
+ keyRange,
85
+ );
86
+
87
+ this.interceptor.onOperation?.({
88
+ type: "index-getAll",
89
+ storeName,
90
+ indexName,
91
+ keyRange: keyRange as unknown as IDBKeyRange | undefined,
92
+ itemsReturned: items,
93
+ itemCount: items.length,
94
+ timestamp: Date.now(),
95
+ });
96
+
97
+ return items;
98
+ }
99
+
100
+ async get<T = unknown>(
101
+ storeName: string,
102
+ key: IDBValidKey,
103
+ ): Promise<T | undefined> {
104
+ const item = await this.db.get<T>(storeName, key);
105
+
106
+ this.interceptor.onOperation?.({
107
+ type: "get",
108
+ storeName,
109
+ key,
110
+ itemReturned: item,
111
+ timestamp: Date.now(),
112
+ });
113
+
114
+ return item;
115
+ }
116
+
117
+ async add(storeName: string, items: unknown[]): Promise<void> {
118
+ await this.db.add(storeName, items);
119
+
120
+ this.interceptor.onOperation?.({
121
+ type: "add",
122
+ storeName,
123
+ items,
124
+ itemCount: items.length,
125
+ timestamp: Date.now(),
126
+ });
127
+ }
128
+
129
+ async put(storeName: string, items: unknown[]): Promise<void> {
130
+ await this.db.put(storeName, items);
131
+
132
+ this.interceptor.onOperation?.({
133
+ type: "put",
134
+ storeName,
135
+ items,
136
+ itemCount: items.length,
137
+ timestamp: Date.now(),
138
+ });
139
+ }
140
+
141
+ async delete(storeName: string, keys: IDBValidKey[]): Promise<void> {
142
+ await this.db.delete(storeName, keys);
143
+
144
+ this.interceptor.onOperation?.({
145
+ type: "delete",
146
+ storeName,
147
+ keys,
148
+ keyCount: keys.length,
149
+ timestamp: Date.now(),
150
+ });
151
+ }
152
+
153
+ async clear(storeName: string): Promise<void> {
154
+ await this.db.clear(storeName);
155
+
156
+ this.interceptor.onOperation?.({
157
+ type: "clear",
158
+ storeName,
159
+ timestamp: Date.now(),
160
+ });
161
+ }
162
+
163
+ close(): void {
164
+ this.db.close();
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Creates an instrumented database creator that wraps operations with interception.
170
+ * Use this for testing to verify what IndexedDB operations are performed.
171
+ *
172
+ * @example
173
+ * const interceptor = { onOperation: (op) => console.log(op) };
174
+ * const dbCreator = createInstrumentedDbCreator(interceptor);
175
+ *
176
+ * <DrizzleIndexedDBProvider dbCreator={dbCreator} ... />
177
+ */
178
+ export function createInstrumentedDbCreator(
179
+ interceptor: IDBInterceptor,
180
+ baseCreator?: IDBCreator,
181
+ ): IDBCreator {
182
+ const creator = baseCreator ?? defaultIDBCreator;
183
+
184
+ return async (name: string, options?: IDBOpenOptions) => {
185
+ const db = await creator(name, options);
186
+ return new InstrumentedIDBDatabase(db, interceptor);
187
+ };
188
+ }