aboutsummaryrefslogtreecommitdiff
path: root/packages/idb-bridge/src/index.ts
blob: 825d41f5e0e0e65201be0525f7f601d749114a30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {
  DatabaseTransaction,
  RecordGetResponse,
  RecordGetRequest,
  Schema,
  Backend,
  RecordStoreRequest,
  RecordStoreResponse,
  DatabaseConnection,
  ObjectStoreProperties,
  StoreLevel,
  ResultLevel,
  IndexProperties,
} from "./backend-interface";
import { Listener } from "./util/FakeEventTarget";
import {
  DatabaseDump,
  ObjectStoreDump,
  IndexRecord,
  ObjectStoreRecord,
  MemoryBackendDump,
} from "./MemoryBackend";
import { Event, IDBKeyRange } from "./idbtypes";
import {
  BridgeIDBCursor,
  BridgeIDBDatabase,
  BridgeIDBFactory,
  BridgeIDBIndex,
  BridgeIDBKeyRange,
  BridgeIDBObjectStore,
  BridgeIDBOpenDBRequest,
  BridgeIDBRequest,
  BridgeIDBTransaction,
  BridgeIDBVersionChangeEvent,
  DatabaseList,
  RequestObj,
} from "./bridge-idb";

export {
  BridgeIDBCursor,
  BridgeIDBDatabase,
  BridgeIDBFactory,
  BridgeIDBIndex,
  BridgeIDBKeyRange,
  BridgeIDBObjectStore,
  BridgeIDBOpenDBRequest,
  BridgeIDBRequest,
  BridgeIDBTransaction,
  StoreLevel,
  ResultLevel,
};
export type {
  DatabaseTransaction,
  RecordGetRequest,
  RecordGetResponse,
  Schema,
  Backend,
  DatabaseList,
  RecordStoreRequest,
  RecordStoreResponse,
  DatabaseConnection,
  ObjectStoreProperties,
  RequestObj,
  DatabaseDump,
  ObjectStoreDump,
  IndexRecord,
  ObjectStoreRecord,
  IndexProperties,
  MemoryBackendDump,
  Event,
  Listener,
};

export { MemoryBackend } from "./MemoryBackend";
export type { AccessStats } from "./MemoryBackend";

// globalThis polyfill, see https://mathiasbynens.be/notes/globalthis
(function () {
  if (typeof globalThis === "object") return;
  Object.defineProperty(Object.prototype, "__magic__", {
    get: function () {
      return this;
    },
    configurable: true, // This makes it possible to `delete` the getter later.
  });
  // @ts-ignore: polyfill magic
  __magic__.globalThis = __magic__; // lolwat
  // @ts-ignore: polyfill magic
  delete Object.prototype.__magic__;
})();

/**
 * Global indexeddb objects, either from the native or bridge-idb
 * implementation, depending on what is availabe in
 * the global environment.
 */
export const GlobalIDB: {
  KeyRange: typeof BridgeIDBKeyRange;
} = {
  KeyRange: (globalThis as any).IDBKeyRange ?? BridgeIDBKeyRange,
};

/**
 * Populate the global name space such that the given IndexedDB factory is made
 * available globally.
 *
 * @public
 */
export function shimIndexedDB(factory: BridgeIDBFactory): void {
  // @ts-ignore: shimming
  const g = globalThis as any;

  g.indexedDB = factory;
  g.IDBCursor = BridgeIDBCursor;
  g.IDBKeyRange = BridgeIDBKeyRange;
  g.IDBDatabase = BridgeIDBDatabase;
  g.IDBFactory = BridgeIDBFactory;
  g.IDBIndex = BridgeIDBIndex;
  g.IDBKeyRange = BridgeIDBKeyRange;
  g.IDBObjectStore = BridgeIDBObjectStore;
  g.IDBOpenDBRequest = BridgeIDBOpenDBRequest;
  g.IDBRequest = BridgeIDBRequest;
  g.IDBTransaction = BridgeIDBTransaction;
  g.IDBVersionChangeEvent = BridgeIDBVersionChangeEvent;
}

export * from "./idbtypes";

export * from "./util/structuredClone";