locality-idb 1.3.0 → 1.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.
- package/README.md +110 -1
- package/dist/index.cjs +41 -16
- package/dist/index.d.cts +9 -1
- package/dist/index.d.mts +9 -1
- package/dist/index.iife.js +41 -16
- package/dist/index.mjs +41 -16
- package/package.json +1 -1
- package/dist/index.umd.js +0 -1566
package/README.md
CHANGED
|
@@ -911,6 +911,58 @@ const db = new Locality({
|
|
|
911
911
|
});
|
|
912
912
|
```
|
|
913
913
|
|
|
914
|
+
#### Properties
|
|
915
|
+
|
|
916
|
+
##### `version: number` (getter)
|
|
917
|
+
|
|
918
|
+
Gets the current database version.
|
|
919
|
+
|
|
920
|
+
**Returns:** The database version number
|
|
921
|
+
|
|
922
|
+
**Example:**
|
|
923
|
+
|
|
924
|
+
```typescript
|
|
925
|
+
const db = new Locality({
|
|
926
|
+
dbName: 'my-database',
|
|
927
|
+
version: 2,
|
|
928
|
+
schema: mySchema,
|
|
929
|
+
});
|
|
930
|
+
|
|
931
|
+
await db.ready(); // (optional) for extra safety
|
|
932
|
+
console.log(db.version); // 2
|
|
933
|
+
```
|
|
934
|
+
|
|
935
|
+
##### `tableList: string[]` (getter)
|
|
936
|
+
|
|
937
|
+
Gets all table (store) names in the current database.
|
|
938
|
+
|
|
939
|
+
**Returns:** Array of table names
|
|
940
|
+
|
|
941
|
+
**Example:**
|
|
942
|
+
|
|
943
|
+
```typescript
|
|
944
|
+
const tables = db.tableList;
|
|
945
|
+
console.log(tables); // ['users', 'posts', 'comments']
|
|
946
|
+
```
|
|
947
|
+
|
|
948
|
+
##### `dbList: Promise<IDBDatabaseInfo[]>` (getter)
|
|
949
|
+
|
|
950
|
+
Gets the list of all existing IndexedDB databases in the current origin.
|
|
951
|
+
|
|
952
|
+
**Returns:** Array of database information objects containing name and version
|
|
953
|
+
|
|
954
|
+
**Example:**
|
|
955
|
+
|
|
956
|
+
```typescript
|
|
957
|
+
const databases = await db.dbList;
|
|
958
|
+
console.log(databases);
|
|
959
|
+
// [{ name: 'my-database', version: 1 }, { name: 'other-db', version: 2 }]
|
|
960
|
+
```
|
|
961
|
+
|
|
962
|
+
> This is an instance method that calls the static [`Locality.getDatabaseList()`](#localitygetdatabaselist-promiseidbdatabaseinfo) internally.
|
|
963
|
+
|
|
964
|
+
---
|
|
965
|
+
|
|
914
966
|
#### Methods
|
|
915
967
|
|
|
916
968
|
##### `ready(): Promise<void>`
|
|
@@ -1123,6 +1175,63 @@ await db.export({
|
|
|
1123
1175
|
|
|
1124
1176
|
---
|
|
1125
1177
|
|
|
1178
|
+
#### Static Methods
|
|
1179
|
+
|
|
1180
|
+
##### `Locality.getDatabaseList(): Promise<IDBDatabaseInfo[]>`
|
|
1181
|
+
|
|
1182
|
+
Gets the list of all existing IndexedDB databases in the current origin (static method).
|
|
1183
|
+
|
|
1184
|
+
**Returns:** Array of database information objects containing name and version
|
|
1185
|
+
|
|
1186
|
+
**Example:**
|
|
1187
|
+
|
|
1188
|
+
```typescript
|
|
1189
|
+
import { Locality } from 'locality-idb';
|
|
1190
|
+
|
|
1191
|
+
const databases = await Locality.getDatabaseList();
|
|
1192
|
+
console.log(databases);
|
|
1193
|
+
// [{ name: 'app-db', version: 1 }, { name: 'cache-db', version: 2 }]
|
|
1194
|
+
```
|
|
1195
|
+
|
|
1196
|
+
> **Note:**
|
|
1197
|
+
>
|
|
1198
|
+
> - This method requires IndexedDB support in the browser.
|
|
1199
|
+
> - Returns an empty array if the browser doesn't support `indexedDB.databases()`.
|
|
1200
|
+
> - Can be called without instantiating the Locality class.
|
|
1201
|
+
|
|
1202
|
+
##### `Locality.deleteDatabase(name: string): Promise<void>`
|
|
1203
|
+
|
|
1204
|
+
Deletes an IndexedDB database by name (static method).
|
|
1205
|
+
|
|
1206
|
+
**Parameters:**
|
|
1207
|
+
|
|
1208
|
+
- `name`: The name of the database to delete
|
|
1209
|
+
|
|
1210
|
+
**Returns:** Promise that resolves when the database is deleted
|
|
1211
|
+
|
|
1212
|
+
**Example:**
|
|
1213
|
+
|
|
1214
|
+
```typescript
|
|
1215
|
+
import { Locality } from 'locality-idb';
|
|
1216
|
+
|
|
1217
|
+
// Delete a database without creating an instance
|
|
1218
|
+
await Locality.deleteDatabase('old-database');
|
|
1219
|
+
|
|
1220
|
+
// Alternative: Get list of databases first
|
|
1221
|
+
const databases = await Locality.getDatabaseList();
|
|
1222
|
+
for (const db of databases) {
|
|
1223
|
+
if (db.name.startsWith('temp-')) {
|
|
1224
|
+
await Locality.deleteDatabase(db.name);
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
```
|
|
1228
|
+
|
|
1229
|
+
> **Warning:** This will permanently remove all data from the specified database and cannot be undone.
|
|
1230
|
+
>
|
|
1231
|
+
> **Note:** This is a static method that can be called without creating a Locality instance. For deleting the current database instance, use the instance method `db.deleteDB()` instead.
|
|
1232
|
+
|
|
1233
|
+
---
|
|
1234
|
+
|
|
1126
1235
|
### Schema Functions
|
|
1127
1236
|
|
|
1128
1237
|
#### `defineSchema<Schema extends ColumnRecord, Keys extends keyof Schema>(schema: Schema): SchemaRecord<Schema, Keys>`
|
|
@@ -1747,7 +1856,7 @@ Opens an IndexedDB database with specified stores (low-level API).
|
|
|
1747
1856
|
|
|
1748
1857
|
- `name`: Database name
|
|
1749
1858
|
- `stores`: Array of store configurations
|
|
1750
|
-
- `version`: Database version (optional, default:
|
|
1859
|
+
- `version`: Database version (optional, default: `undefined`)
|
|
1751
1860
|
|
|
1752
1861
|
**Returns:** Promise resolving to `IDBDatabase` instance
|
|
1753
1862
|
|
package/dist/index.cjs
CHANGED
|
@@ -327,6 +327,15 @@ function _formatUUID(h, v, up) {
|
|
|
327
327
|
function _abortTransaction(error, reject) {
|
|
328
328
|
reject(error || /* @__PURE__ */ new Error("IndexedDB transaction was aborted!"));
|
|
329
329
|
}
|
|
330
|
+
/** Ensure `IndexedDB` is supported in the current environment */
|
|
331
|
+
function _ensureIndexedDB() {
|
|
332
|
+
if (!window.indexedDB) throw new Error("IndexedDB is not supported in this environment or browser!");
|
|
333
|
+
}
|
|
334
|
+
/** Get the list of existing `IndexedDB` databases */
|
|
335
|
+
async function _getDBList() {
|
|
336
|
+
if (!("databases" in window.indexedDB)) return [];
|
|
337
|
+
return await window.indexedDB.databases();
|
|
338
|
+
}
|
|
330
339
|
|
|
331
340
|
//#endregion
|
|
332
341
|
//#region src/factory.ts
|
|
@@ -334,13 +343,13 @@ function _abortTransaction(error, reject) {
|
|
|
334
343
|
* * Opens an `IndexedDB` database instance with the specified stores.
|
|
335
344
|
* @param name Database name
|
|
336
345
|
* @param stores Array of store configurations
|
|
337
|
-
* @param version Database version (default is `
|
|
346
|
+
* @param version Database version (default is `undefined`)
|
|
338
347
|
* @returns Promise that resolves to the opened {@link IDBDatabase} instance.
|
|
339
348
|
*/
|
|
340
349
|
function openDBWithStores(name, stores, version) {
|
|
341
350
|
return new Promise((resolve, reject) => {
|
|
342
|
-
|
|
343
|
-
const request =
|
|
351
|
+
_ensureIndexedDB();
|
|
352
|
+
const request = window.indexedDB.open(name, isNumber(version) ? version : void 0);
|
|
344
353
|
request.onupgradeneeded = (event) => {
|
|
345
354
|
const $request = event.target;
|
|
346
355
|
const db = $request.result;
|
|
@@ -409,15 +418,14 @@ function isTimestamp(value) {
|
|
|
409
418
|
* @returns A promise that resolves when the database is deleted
|
|
410
419
|
* @throws Error if `IndexedDB` is not supported or if the database does not exist
|
|
411
420
|
*/
|
|
412
|
-
function deleteDB(name) {
|
|
421
|
+
async function deleteDB(name) {
|
|
422
|
+
_ensureIndexedDB();
|
|
423
|
+
const dbList = await _getDBList();
|
|
413
424
|
return new Promise((resolve, reject) => {
|
|
414
|
-
if (!
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
420
|
-
}).catch(reject);
|
|
425
|
+
if (!dbList.some((db) => db.name === name)) {
|
|
426
|
+
reject(/* @__PURE__ */ new Error(`Database '${name}' does not exist in this system!`));
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
421
429
|
const request = window.indexedDB.deleteDatabase(name);
|
|
422
430
|
request.onsuccess = () => resolve();
|
|
423
431
|
request.onerror = () => reject(request.error);
|
|
@@ -1135,7 +1143,7 @@ var DeleteQuery = class {
|
|
|
1135
1143
|
* // Delete a user
|
|
1136
1144
|
* const deleted = await db.delete('users').where((user) => user.id === 1).run();
|
|
1137
1145
|
*/
|
|
1138
|
-
var Locality = class {
|
|
1146
|
+
var Locality = class Locality {
|
|
1139
1147
|
#name;
|
|
1140
1148
|
#schema;
|
|
1141
1149
|
#keyPaths;
|
|
@@ -1158,9 +1166,6 @@ var Locality = class {
|
|
|
1158
1166
|
this.#version = this.#db?.version;
|
|
1159
1167
|
});
|
|
1160
1168
|
}
|
|
1161
|
-
get version() {
|
|
1162
|
-
return this.#db?.version ?? this.#version ?? this.#configVersion;
|
|
1163
|
-
}
|
|
1164
1169
|
/** Build store configurations from schema. */
|
|
1165
1170
|
#buildStoresConfig() {
|
|
1166
1171
|
return Object.entries(this.#schema).map(([tableName, table]) => {
|
|
@@ -1194,6 +1199,17 @@ var Locality = class {
|
|
|
1194
1199
|
const columns = this.#schema[table].columns;
|
|
1195
1200
|
return Object.entries(columns).find(([_, col]) => col[IsPrimaryKey])?.[0];
|
|
1196
1201
|
}
|
|
1202
|
+
get version() {
|
|
1203
|
+
return this.#db?.version ?? this.#version ?? this.#configVersion;
|
|
1204
|
+
}
|
|
1205
|
+
/** @instance Get all table (store) names in the current database. */
|
|
1206
|
+
get tableList() {
|
|
1207
|
+
return Array.from(this.#db.objectStoreNames);
|
|
1208
|
+
}
|
|
1209
|
+
/** @instance Get the list of existing `IndexedDB` databases. */
|
|
1210
|
+
get dbList() {
|
|
1211
|
+
return Locality.getDatabaseList();
|
|
1212
|
+
}
|
|
1197
1213
|
/** @instance Waits for database initialization to complete. */
|
|
1198
1214
|
async ready() {
|
|
1199
1215
|
return this.#readyPromise;
|
|
@@ -1243,7 +1259,7 @@ var Locality = class {
|
|
|
1243
1259
|
/** @instance Closes and deletes the entire database. */
|
|
1244
1260
|
async deleteDB() {
|
|
1245
1261
|
this.#db.close();
|
|
1246
|
-
|
|
1262
|
+
await deleteDB(this.#name);
|
|
1247
1263
|
}
|
|
1248
1264
|
/** @instance Closes the current database connection. */
|
|
1249
1265
|
close() {
|
|
@@ -1416,6 +1432,15 @@ var Locality = class {
|
|
|
1416
1432
|
document.body.removeChild(link);
|
|
1417
1433
|
URL.revokeObjectURL(url);
|
|
1418
1434
|
}
|
|
1435
|
+
/** @static Get the list of existing `IndexedDB` databases. */
|
|
1436
|
+
static getDatabaseList() {
|
|
1437
|
+
_ensureIndexedDB();
|
|
1438
|
+
return _getDBList();
|
|
1439
|
+
}
|
|
1440
|
+
/** @static Delete an `IndexedDB` database by name. */
|
|
1441
|
+
static async deleteDatabase(name) {
|
|
1442
|
+
await deleteDB(name);
|
|
1443
|
+
}
|
|
1419
1444
|
};
|
|
1420
1445
|
|
|
1421
1446
|
//#endregion
|
package/dist/index.d.cts
CHANGED
|
@@ -644,6 +644,10 @@ declare class Locality<DBName extends string = string, Version extends number =
|
|
|
644
644
|
#private;
|
|
645
645
|
constructor(config: LocalityConfig<DBName, Version, Schema>);
|
|
646
646
|
get version(): Version;
|
|
647
|
+
/** @instance Get all table (store) names in the current database. */
|
|
648
|
+
get tableList(): LooseLiteral<TName>[];
|
|
649
|
+
/** @instance Get the list of existing `IndexedDB` databases. */
|
|
650
|
+
get dbList(): Promise<IDBDatabaseInfo[]>;
|
|
647
651
|
/** @instance Waits for database initialization to complete. */
|
|
648
652
|
ready(): Promise<void>;
|
|
649
653
|
/**
|
|
@@ -787,6 +791,10 @@ declare class Locality<DBName extends string = string, Version extends number =
|
|
|
787
791
|
* await db.export({ pretty: false });
|
|
788
792
|
*/
|
|
789
793
|
export(options?: ExportOptions<TName>): Promise<void>;
|
|
794
|
+
/** @static Get the list of existing `IndexedDB` databases. */
|
|
795
|
+
static getDatabaseList(): Promise<IDBDatabaseInfo[]>;
|
|
796
|
+
/** @static Delete an `IndexedDB` database by name. */
|
|
797
|
+
static deleteDatabase(name: string): Promise<void>;
|
|
790
798
|
}
|
|
791
799
|
//#endregion
|
|
792
800
|
//#region src/factory.d.ts
|
|
@@ -794,7 +802,7 @@ declare class Locality<DBName extends string = string, Version extends number =
|
|
|
794
802
|
* * Opens an `IndexedDB` database instance with the specified stores.
|
|
795
803
|
* @param name Database name
|
|
796
804
|
* @param stores Array of store configurations
|
|
797
|
-
* @param version Database version (default is `
|
|
805
|
+
* @param version Database version (default is `undefined`)
|
|
798
806
|
* @returns Promise that resolves to the opened {@link IDBDatabase} instance.
|
|
799
807
|
*/
|
|
800
808
|
declare function openDBWithStores(name: string, stores: StoreConfig[], version?: number): Promise<IDBDatabase>;
|
package/dist/index.d.mts
CHANGED
|
@@ -644,6 +644,10 @@ declare class Locality<DBName extends string = string, Version extends number =
|
|
|
644
644
|
#private;
|
|
645
645
|
constructor(config: LocalityConfig<DBName, Version, Schema>);
|
|
646
646
|
get version(): Version;
|
|
647
|
+
/** @instance Get all table (store) names in the current database. */
|
|
648
|
+
get tableList(): LooseLiteral<TName>[];
|
|
649
|
+
/** @instance Get the list of existing `IndexedDB` databases. */
|
|
650
|
+
get dbList(): Promise<IDBDatabaseInfo[]>;
|
|
647
651
|
/** @instance Waits for database initialization to complete. */
|
|
648
652
|
ready(): Promise<void>;
|
|
649
653
|
/**
|
|
@@ -787,6 +791,10 @@ declare class Locality<DBName extends string = string, Version extends number =
|
|
|
787
791
|
* await db.export({ pretty: false });
|
|
788
792
|
*/
|
|
789
793
|
export(options?: ExportOptions<TName>): Promise<void>;
|
|
794
|
+
/** @static Get the list of existing `IndexedDB` databases. */
|
|
795
|
+
static getDatabaseList(): Promise<IDBDatabaseInfo[]>;
|
|
796
|
+
/** @static Delete an `IndexedDB` database by name. */
|
|
797
|
+
static deleteDatabase(name: string): Promise<void>;
|
|
790
798
|
}
|
|
791
799
|
//#endregion
|
|
792
800
|
//#region src/factory.d.ts
|
|
@@ -794,7 +802,7 @@ declare class Locality<DBName extends string = string, Version extends number =
|
|
|
794
802
|
* * Opens an `IndexedDB` database instance with the specified stores.
|
|
795
803
|
* @param name Database name
|
|
796
804
|
* @param stores Array of store configurations
|
|
797
|
-
* @param version Database version (default is `
|
|
805
|
+
* @param version Database version (default is `undefined`)
|
|
798
806
|
* @returns Promise that resolves to the opened {@link IDBDatabase} instance.
|
|
799
807
|
*/
|
|
800
808
|
declare function openDBWithStores(name: string, stores: StoreConfig[], version?: number): Promise<IDBDatabase>;
|
package/dist/index.iife.js
CHANGED
|
@@ -329,6 +329,15 @@ var LocalityIDB = (function(exports) {
|
|
|
329
329
|
function _abortTransaction(error, reject) {
|
|
330
330
|
reject(error || /* @__PURE__ */ new Error("IndexedDB transaction was aborted!"));
|
|
331
331
|
}
|
|
332
|
+
/** Ensure `IndexedDB` is supported in the current environment */
|
|
333
|
+
function _ensureIndexedDB() {
|
|
334
|
+
if (!window.indexedDB) throw new Error("IndexedDB is not supported in this environment or browser!");
|
|
335
|
+
}
|
|
336
|
+
/** Get the list of existing `IndexedDB` databases */
|
|
337
|
+
async function _getDBList() {
|
|
338
|
+
if (!("databases" in window.indexedDB)) return [];
|
|
339
|
+
return await window.indexedDB.databases();
|
|
340
|
+
}
|
|
332
341
|
|
|
333
342
|
//#endregion
|
|
334
343
|
//#region src/factory.ts
|
|
@@ -336,13 +345,13 @@ var LocalityIDB = (function(exports) {
|
|
|
336
345
|
* * Opens an `IndexedDB` database instance with the specified stores.
|
|
337
346
|
* @param name Database name
|
|
338
347
|
* @param stores Array of store configurations
|
|
339
|
-
* @param version Database version (default is `
|
|
348
|
+
* @param version Database version (default is `undefined`)
|
|
340
349
|
* @returns Promise that resolves to the opened {@link IDBDatabase} instance.
|
|
341
350
|
*/
|
|
342
351
|
function openDBWithStores(name, stores, version) {
|
|
343
352
|
return new Promise((resolve, reject) => {
|
|
344
|
-
|
|
345
|
-
const request =
|
|
353
|
+
_ensureIndexedDB();
|
|
354
|
+
const request = window.indexedDB.open(name, isNumber(version) ? version : void 0);
|
|
346
355
|
request.onupgradeneeded = (event) => {
|
|
347
356
|
const $request = event.target;
|
|
348
357
|
const db = $request.result;
|
|
@@ -411,15 +420,14 @@ var LocalityIDB = (function(exports) {
|
|
|
411
420
|
* @returns A promise that resolves when the database is deleted
|
|
412
421
|
* @throws Error if `IndexedDB` is not supported or if the database does not exist
|
|
413
422
|
*/
|
|
414
|
-
function deleteDB(name) {
|
|
423
|
+
async function deleteDB(name) {
|
|
424
|
+
_ensureIndexedDB();
|
|
425
|
+
const dbList = await _getDBList();
|
|
415
426
|
return new Promise((resolve, reject) => {
|
|
416
|
-
if (!
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
return;
|
|
421
|
-
}
|
|
422
|
-
}).catch(reject);
|
|
427
|
+
if (!dbList.some((db) => db.name === name)) {
|
|
428
|
+
reject(/* @__PURE__ */ new Error(`Database '${name}' does not exist in this system!`));
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
423
431
|
const request = window.indexedDB.deleteDatabase(name);
|
|
424
432
|
request.onsuccess = () => resolve();
|
|
425
433
|
request.onerror = () => reject(request.error);
|
|
@@ -1137,7 +1145,7 @@ var LocalityIDB = (function(exports) {
|
|
|
1137
1145
|
* // Delete a user
|
|
1138
1146
|
* const deleted = await db.delete('users').where((user) => user.id === 1).run();
|
|
1139
1147
|
*/
|
|
1140
|
-
var Locality = class {
|
|
1148
|
+
var Locality = class Locality {
|
|
1141
1149
|
#name;
|
|
1142
1150
|
#schema;
|
|
1143
1151
|
#keyPaths;
|
|
@@ -1160,9 +1168,6 @@ var LocalityIDB = (function(exports) {
|
|
|
1160
1168
|
this.#version = this.#db?.version;
|
|
1161
1169
|
});
|
|
1162
1170
|
}
|
|
1163
|
-
get version() {
|
|
1164
|
-
return this.#db?.version ?? this.#version ?? this.#configVersion;
|
|
1165
|
-
}
|
|
1166
1171
|
/** Build store configurations from schema. */
|
|
1167
1172
|
#buildStoresConfig() {
|
|
1168
1173
|
return Object.entries(this.#schema).map(([tableName, table]) => {
|
|
@@ -1196,6 +1201,17 @@ var LocalityIDB = (function(exports) {
|
|
|
1196
1201
|
const columns = this.#schema[table].columns;
|
|
1197
1202
|
return Object.entries(columns).find(([_, col]) => col[IsPrimaryKey])?.[0];
|
|
1198
1203
|
}
|
|
1204
|
+
get version() {
|
|
1205
|
+
return this.#db?.version ?? this.#version ?? this.#configVersion;
|
|
1206
|
+
}
|
|
1207
|
+
/** @instance Get all table (store) names in the current database. */
|
|
1208
|
+
get tableList() {
|
|
1209
|
+
return Array.from(this.#db.objectStoreNames);
|
|
1210
|
+
}
|
|
1211
|
+
/** @instance Get the list of existing `IndexedDB` databases. */
|
|
1212
|
+
get dbList() {
|
|
1213
|
+
return Locality.getDatabaseList();
|
|
1214
|
+
}
|
|
1199
1215
|
/** @instance Waits for database initialization to complete. */
|
|
1200
1216
|
async ready() {
|
|
1201
1217
|
return this.#readyPromise;
|
|
@@ -1245,7 +1261,7 @@ var LocalityIDB = (function(exports) {
|
|
|
1245
1261
|
/** @instance Closes and deletes the entire database. */
|
|
1246
1262
|
async deleteDB() {
|
|
1247
1263
|
this.#db.close();
|
|
1248
|
-
|
|
1264
|
+
await deleteDB(this.#name);
|
|
1249
1265
|
}
|
|
1250
1266
|
/** @instance Closes the current database connection. */
|
|
1251
1267
|
close() {
|
|
@@ -1418,6 +1434,15 @@ var LocalityIDB = (function(exports) {
|
|
|
1418
1434
|
document.body.removeChild(link);
|
|
1419
1435
|
URL.revokeObjectURL(url);
|
|
1420
1436
|
}
|
|
1437
|
+
/** @static Get the list of existing `IndexedDB` databases. */
|
|
1438
|
+
static getDatabaseList() {
|
|
1439
|
+
_ensureIndexedDB();
|
|
1440
|
+
return _getDBList();
|
|
1441
|
+
}
|
|
1442
|
+
/** @static Delete an `IndexedDB` database by name. */
|
|
1443
|
+
static async deleteDatabase(name) {
|
|
1444
|
+
await deleteDB(name);
|
|
1445
|
+
}
|
|
1421
1446
|
};
|
|
1422
1447
|
|
|
1423
1448
|
//#endregion
|
package/dist/index.mjs
CHANGED
|
@@ -326,6 +326,15 @@ function _formatUUID(h, v, up) {
|
|
|
326
326
|
function _abortTransaction(error, reject) {
|
|
327
327
|
reject(error || /* @__PURE__ */ new Error("IndexedDB transaction was aborted!"));
|
|
328
328
|
}
|
|
329
|
+
/** Ensure `IndexedDB` is supported in the current environment */
|
|
330
|
+
function _ensureIndexedDB() {
|
|
331
|
+
if (!window.indexedDB) throw new Error("IndexedDB is not supported in this environment or browser!");
|
|
332
|
+
}
|
|
333
|
+
/** Get the list of existing `IndexedDB` databases */
|
|
334
|
+
async function _getDBList() {
|
|
335
|
+
if (!("databases" in window.indexedDB)) return [];
|
|
336
|
+
return await window.indexedDB.databases();
|
|
337
|
+
}
|
|
329
338
|
|
|
330
339
|
//#endregion
|
|
331
340
|
//#region src/factory.ts
|
|
@@ -333,13 +342,13 @@ function _abortTransaction(error, reject) {
|
|
|
333
342
|
* * Opens an `IndexedDB` database instance with the specified stores.
|
|
334
343
|
* @param name Database name
|
|
335
344
|
* @param stores Array of store configurations
|
|
336
|
-
* @param version Database version (default is `
|
|
345
|
+
* @param version Database version (default is `undefined`)
|
|
337
346
|
* @returns Promise that resolves to the opened {@link IDBDatabase} instance.
|
|
338
347
|
*/
|
|
339
348
|
function openDBWithStores(name, stores, version) {
|
|
340
349
|
return new Promise((resolve, reject) => {
|
|
341
|
-
|
|
342
|
-
const request =
|
|
350
|
+
_ensureIndexedDB();
|
|
351
|
+
const request = window.indexedDB.open(name, isNumber(version) ? version : void 0);
|
|
343
352
|
request.onupgradeneeded = (event) => {
|
|
344
353
|
const $request = event.target;
|
|
345
354
|
const db = $request.result;
|
|
@@ -408,15 +417,14 @@ function isTimestamp(value) {
|
|
|
408
417
|
* @returns A promise that resolves when the database is deleted
|
|
409
418
|
* @throws Error if `IndexedDB` is not supported or if the database does not exist
|
|
410
419
|
*/
|
|
411
|
-
function deleteDB(name) {
|
|
420
|
+
async function deleteDB(name) {
|
|
421
|
+
_ensureIndexedDB();
|
|
422
|
+
const dbList = await _getDBList();
|
|
412
423
|
return new Promise((resolve, reject) => {
|
|
413
|
-
if (!
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
return;
|
|
418
|
-
}
|
|
419
|
-
}).catch(reject);
|
|
424
|
+
if (!dbList.some((db) => db.name === name)) {
|
|
425
|
+
reject(/* @__PURE__ */ new Error(`Database '${name}' does not exist in this system!`));
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
420
428
|
const request = window.indexedDB.deleteDatabase(name);
|
|
421
429
|
request.onsuccess = () => resolve();
|
|
422
430
|
request.onerror = () => reject(request.error);
|
|
@@ -1134,7 +1142,7 @@ var DeleteQuery = class {
|
|
|
1134
1142
|
* // Delete a user
|
|
1135
1143
|
* const deleted = await db.delete('users').where((user) => user.id === 1).run();
|
|
1136
1144
|
*/
|
|
1137
|
-
var Locality = class {
|
|
1145
|
+
var Locality = class Locality {
|
|
1138
1146
|
#name;
|
|
1139
1147
|
#schema;
|
|
1140
1148
|
#keyPaths;
|
|
@@ -1157,9 +1165,6 @@ var Locality = class {
|
|
|
1157
1165
|
this.#version = this.#db?.version;
|
|
1158
1166
|
});
|
|
1159
1167
|
}
|
|
1160
|
-
get version() {
|
|
1161
|
-
return this.#db?.version ?? this.#version ?? this.#configVersion;
|
|
1162
|
-
}
|
|
1163
1168
|
/** Build store configurations from schema. */
|
|
1164
1169
|
#buildStoresConfig() {
|
|
1165
1170
|
return Object.entries(this.#schema).map(([tableName, table]) => {
|
|
@@ -1193,6 +1198,17 @@ var Locality = class {
|
|
|
1193
1198
|
const columns = this.#schema[table].columns;
|
|
1194
1199
|
return Object.entries(columns).find(([_, col]) => col[IsPrimaryKey])?.[0];
|
|
1195
1200
|
}
|
|
1201
|
+
get version() {
|
|
1202
|
+
return this.#db?.version ?? this.#version ?? this.#configVersion;
|
|
1203
|
+
}
|
|
1204
|
+
/** @instance Get all table (store) names in the current database. */
|
|
1205
|
+
get tableList() {
|
|
1206
|
+
return Array.from(this.#db.objectStoreNames);
|
|
1207
|
+
}
|
|
1208
|
+
/** @instance Get the list of existing `IndexedDB` databases. */
|
|
1209
|
+
get dbList() {
|
|
1210
|
+
return Locality.getDatabaseList();
|
|
1211
|
+
}
|
|
1196
1212
|
/** @instance Waits for database initialization to complete. */
|
|
1197
1213
|
async ready() {
|
|
1198
1214
|
return this.#readyPromise;
|
|
@@ -1242,7 +1258,7 @@ var Locality = class {
|
|
|
1242
1258
|
/** @instance Closes and deletes the entire database. */
|
|
1243
1259
|
async deleteDB() {
|
|
1244
1260
|
this.#db.close();
|
|
1245
|
-
|
|
1261
|
+
await deleteDB(this.#name);
|
|
1246
1262
|
}
|
|
1247
1263
|
/** @instance Closes the current database connection. */
|
|
1248
1264
|
close() {
|
|
@@ -1415,6 +1431,15 @@ var Locality = class {
|
|
|
1415
1431
|
document.body.removeChild(link);
|
|
1416
1432
|
URL.revokeObjectURL(url);
|
|
1417
1433
|
}
|
|
1434
|
+
/** @static Get the list of existing `IndexedDB` databases. */
|
|
1435
|
+
static getDatabaseList() {
|
|
1436
|
+
_ensureIndexedDB();
|
|
1437
|
+
return _getDBList();
|
|
1438
|
+
}
|
|
1439
|
+
/** @static Delete an `IndexedDB` database by name. */
|
|
1440
|
+
static async deleteDatabase(name) {
|
|
1441
|
+
await deleteDB(name);
|
|
1442
|
+
}
|
|
1418
1443
|
};
|
|
1419
1444
|
|
|
1420
1445
|
//#endregion
|