locality-idb 1.5.0 → 1.5.1
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 +43 -14
- package/dist/index.cjs +29 -29
- package/dist/index.d.cts +22 -9
- package/dist/index.d.mts +22 -9
- package/dist/index.iife.js +29 -29
- package/dist/index.mjs +29 -29
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -990,6 +990,25 @@ const db = new Locality({
|
|
|
990
990
|
|
|
991
991
|
#### Properties
|
|
992
992
|
|
|
993
|
+
##### `dbName: string` (getter)
|
|
994
|
+
|
|
995
|
+
Gets the current database name.
|
|
996
|
+
|
|
997
|
+
**Returns:** The database name
|
|
998
|
+
|
|
999
|
+
**Example:**
|
|
1000
|
+
|
|
1001
|
+
```typescript
|
|
1002
|
+
const db = new Locality({
|
|
1003
|
+
dbName: 'my-database',
|
|
1004
|
+
version: 1,
|
|
1005
|
+
schema: mySchema,
|
|
1006
|
+
});
|
|
1007
|
+
|
|
1008
|
+
await db.ready(); // (optional) for extra safety
|
|
1009
|
+
console.log(db.dbName); // 'my-database'
|
|
1010
|
+
```
|
|
1011
|
+
|
|
993
1012
|
##### `version: number` (getter)
|
|
994
1013
|
|
|
995
1014
|
Gets the current database version.
|
|
@@ -1230,24 +1249,34 @@ await db.export({
|
|
|
1230
1249
|
**Exported JSON Structure:**
|
|
1231
1250
|
|
|
1232
1251
|
```typescript
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1252
|
+
/** Exported table data structure */
|
|
1253
|
+
type ExportedTableData<T extends string, S extends SchemaDefinition> = {
|
|
1254
|
+
[K in T]: InferSelectType<S[K]>[];
|
|
1255
|
+
};
|
|
1256
|
+
|
|
1257
|
+
/** Exported database data structure */
|
|
1258
|
+
type ExportData<T extends string, S extends SchemaDefinition> = {
|
|
1259
|
+
/** Optional metadata about the export */
|
|
1260
|
+
metadata?: {
|
|
1261
|
+
/** Database name */
|
|
1262
|
+
dbName: string;
|
|
1263
|
+
/** Database version */
|
|
1264
|
+
version: number;
|
|
1265
|
+
/** Export creation time */
|
|
1266
|
+
exportedAt: Timestamp;
|
|
1267
|
+
/** List of exported table names */
|
|
1268
|
+
tables: T[];
|
|
1269
|
+
};
|
|
1270
|
+
/** Actual exported data, mapping table names to arrays of records */
|
|
1271
|
+
data: ExportedTableData<T, S>;
|
|
1272
|
+
};
|
|
1244
1273
|
```
|
|
1245
1274
|
|
|
1246
|
-
#### `exportToObject(options?:
|
|
1275
|
+
#### `exportToObject(options?: ExportObjectOptions): Promise<ExportData>`
|
|
1247
1276
|
|
|
1248
1277
|
Exports database data as an object without triggering a download.
|
|
1249
1278
|
|
|
1250
|
-
**Parameters:** Same as `export()
|
|
1279
|
+
**Parameters:** Same as [`export()`](#exportoptions-exportoptions-promisevoid), except `filename` and `pretty` are omitted.
|
|
1251
1280
|
|
|
1252
1281
|
**Returns:** Promise that resolves to an `ExportData` object.
|
|
1253
1282
|
|
|
@@ -1257,7 +1286,7 @@ Exports database data as an object without triggering a download.
|
|
|
1257
1286
|
const exported = await db.exportToObject();
|
|
1258
1287
|
```
|
|
1259
1288
|
|
|
1260
|
-
#### `import(data: ExportData |
|
|
1289
|
+
#### `import(data: ExportData | ExportedTableData, options?: ImportOptions): Promise<void>`
|
|
1261
1290
|
|
|
1262
1291
|
Imports database data using merge, replace, or upsert modes.
|
|
1263
1292
|
|
package/dist/index.cjs
CHANGED
|
@@ -1372,6 +1372,10 @@ var Locality = class Locality {
|
|
|
1372
1372
|
const columns = this.#schema[table].columns;
|
|
1373
1373
|
return Object.entries(columns).find(([_, col]) => col[IsPrimaryKey])?.[0];
|
|
1374
1374
|
}
|
|
1375
|
+
/** @instance Get the current database name. */
|
|
1376
|
+
get dbName() {
|
|
1377
|
+
return this.#name;
|
|
1378
|
+
}
|
|
1375
1379
|
get version() {
|
|
1376
1380
|
return this.#db?.version ?? this.#version ?? this.#configVersion;
|
|
1377
1381
|
}
|
|
@@ -1605,7 +1609,7 @@ var Locality = class Locality {
|
|
|
1605
1609
|
await this.#readyPromise;
|
|
1606
1610
|
const { tables, includeMetadata = true } = options || {};
|
|
1607
1611
|
const tablesToExport = tables ?? Object.keys(this.#schema);
|
|
1608
|
-
for (const table of tablesToExport) if (!(table in this.#schema)) throw new RangeError(`Table '${
|
|
1612
|
+
for (const table of tablesToExport) if (!(table in this.#schema)) throw new RangeError(`Table '${table}' does not exist in schema.`);
|
|
1609
1613
|
if (tablesToExport.length === 0) return {
|
|
1610
1614
|
metadata: includeMetadata ? {
|
|
1611
1615
|
dbName: this.#name,
|
|
@@ -1620,21 +1624,22 @@ var Locality = class Locality {
|
|
|
1620
1624
|
return new Promise((resolve, reject) => {
|
|
1621
1625
|
transaction.onabort = () => _abortTransaction(transaction.error, reject);
|
|
1622
1626
|
transaction.onerror = () => reject(transaction.error);
|
|
1623
|
-
const readPromises = tablesToExport.map((table) =>
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1627
|
+
const readPromises = tablesToExport.map((table) => {
|
|
1628
|
+
return new Promise((res, rej) => {
|
|
1629
|
+
const request = transaction.objectStore(table).getAll();
|
|
1630
|
+
request.onsuccess = () => {
|
|
1631
|
+
exportData[table] = request.result;
|
|
1632
|
+
res();
|
|
1633
|
+
};
|
|
1634
|
+
request.onerror = () => rej(request.error);
|
|
1635
|
+
});
|
|
1636
|
+
});
|
|
1631
1637
|
Promise.all(readPromises).then(() => {
|
|
1632
1638
|
const exportObj = {};
|
|
1633
|
-
const ts = getTimestamp();
|
|
1634
1639
|
if (includeMetadata) exportObj.metadata = {
|
|
1635
1640
|
dbName: this.#name,
|
|
1636
1641
|
version: this.version,
|
|
1637
|
-
exportedAt:
|
|
1642
|
+
exportedAt: getTimestamp(),
|
|
1638
1643
|
tables: tablesToExport
|
|
1639
1644
|
};
|
|
1640
1645
|
exportObj.data = exportData;
|
|
@@ -1642,19 +1647,12 @@ var Locality = class Locality {
|
|
|
1642
1647
|
}).catch(reject);
|
|
1643
1648
|
});
|
|
1644
1649
|
}
|
|
1645
|
-
/**
|
|
1646
|
-
* @instance Import data into the database.
|
|
1647
|
-
*
|
|
1648
|
-
* @remarks
|
|
1649
|
-
* - Accepts either raw table data or an {@link ExportData} object.
|
|
1650
|
-
* - Supports merge, replace, and upsert modes.
|
|
1651
|
-
*/
|
|
1652
1650
|
async import(data, options) {
|
|
1653
1651
|
await this.#readyPromise;
|
|
1654
1652
|
const { mode = "merge", tables } = options || {};
|
|
1655
1653
|
const dataMap = "data" in data ? data.data : data;
|
|
1656
1654
|
const tablesToImport = tables ?? Object.keys(dataMap);
|
|
1657
|
-
for (const table of tablesToImport) if (!(table in this.#schema)) throw new RangeError(`Table '${
|
|
1655
|
+
for (const table of tablesToImport) if (!(table in this.#schema)) throw new RangeError(`Table '${table}' does not exist in schema.`);
|
|
1658
1656
|
if (tablesToImport.length === 0) return;
|
|
1659
1657
|
const transaction = this.#db.transaction(tablesToImport, "readwrite");
|
|
1660
1658
|
return new Promise((resolve, reject) => {
|
|
@@ -1671,7 +1669,7 @@ var Locality = class Locality {
|
|
|
1671
1669
|
if (rows.length === 0) return;
|
|
1672
1670
|
const writePromises = rows.map((row) => {
|
|
1673
1671
|
return new Promise((res, rej) => {
|
|
1674
|
-
const prepared = validateAndPrepareData(row, this.#schema[table].columns, this.#keyPaths[table],
|
|
1672
|
+
const prepared = validateAndPrepareData(row, this.#schema[table].columns, this.#keyPaths[table], table);
|
|
1675
1673
|
const request = mode === "upsert" ? store.put(prepared) : store.add(prepared);
|
|
1676
1674
|
request.onsuccess = () => res();
|
|
1677
1675
|
request.onerror = () => rej(request.error);
|
|
@@ -1694,11 +1692,13 @@ var Locality = class Locality {
|
|
|
1694
1692
|
return new Promise((resolve, reject) => {
|
|
1695
1693
|
transaction.onabort = () => _abortTransaction(transaction.error, reject);
|
|
1696
1694
|
transaction.onerror = () => reject(transaction.error);
|
|
1697
|
-
const clearPromises = tables.map((table) =>
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1695
|
+
const clearPromises = tables.map((table) => {
|
|
1696
|
+
return new Promise((res, rej) => {
|
|
1697
|
+
const request = transaction.objectStore(table).clear();
|
|
1698
|
+
request.onsuccess = () => res();
|
|
1699
|
+
request.onerror = () => rej(request.error);
|
|
1700
|
+
});
|
|
1701
|
+
});
|
|
1702
1702
|
Promise.all(clearPromises).then(() => resolve()).catch((err) => {
|
|
1703
1703
|
transaction.abort();
|
|
1704
1704
|
reject(err);
|
|
@@ -1714,23 +1714,23 @@ var Locality = class Locality {
|
|
|
1714
1714
|
*/
|
|
1715
1715
|
async dropTable(table) {
|
|
1716
1716
|
await this.#readyPromise;
|
|
1717
|
-
if (!(table in this.#schema)) throw new RangeError(`Table '${
|
|
1717
|
+
if (!(table in this.#schema)) throw new RangeError(`Table '${table}' does not exist in schema.`);
|
|
1718
1718
|
const nextStores = this.#buildStoresConfig().filter((store) => store.name !== table);
|
|
1719
1719
|
const nextVersion = this.version + 1;
|
|
1720
1720
|
this.#db.close();
|
|
1721
1721
|
this.#readyPromise = openDBWithStores(this.#name, nextStores, nextVersion).then((db) => {
|
|
1722
1722
|
this.#db = db;
|
|
1723
1723
|
const keyPaths = this.#keyPaths;
|
|
1724
|
-
delete keyPaths[
|
|
1724
|
+
delete keyPaths[table];
|
|
1725
1725
|
}).finally(() => {
|
|
1726
1726
|
this.#version = this.#db?.version;
|
|
1727
1727
|
});
|
|
1728
1728
|
await this.#readyPromise;
|
|
1729
1729
|
}
|
|
1730
1730
|
/** @static Get the list of existing `IndexedDB` databases. */
|
|
1731
|
-
static getDatabaseList() {
|
|
1731
|
+
static async getDatabaseList() {
|
|
1732
1732
|
_ensureIndexedDB();
|
|
1733
|
-
return _getDBList();
|
|
1733
|
+
return await _getDBList();
|
|
1734
1734
|
}
|
|
1735
1735
|
/** @static Delete an `IndexedDB` database by name. */
|
|
1736
1736
|
static async deleteDatabase(name) {
|
package/dist/index.d.cts
CHANGED
|
@@ -614,21 +614,24 @@ type ExportOptions<T extends string> = {
|
|
|
614
614
|
pretty?: boolean; /** Optional flag to include export metadata (default: `true`) */
|
|
615
615
|
includeMetadata?: boolean;
|
|
616
616
|
};
|
|
617
|
+
type ExportObjectOptions<T extends string> = Omit<ExportOptions<T>, 'filename' | 'pretty'>;
|
|
617
618
|
/** Import options for database `import` method */
|
|
618
619
|
type ImportOptions<T extends string> = {
|
|
619
620
|
/** Optional array of table names to import (imports all tables (store) if not specified) */tables?: T[]; /** Import mode: `'replace'`, `'merge'`, or `'upsert'` (default: `'merge'`) */
|
|
620
621
|
mode?: 'replace' | 'merge' | 'upsert';
|
|
621
622
|
};
|
|
623
|
+
/** Exported table data structure */
|
|
624
|
+
type ExportedTableData<T extends string, S extends SchemaDefinition> = Prettify<{ [K in T]: InferSelectType<S[K]>[] }>;
|
|
622
625
|
/** Exported database data structure */
|
|
623
|
-
type ExportData = {
|
|
626
|
+
type ExportData<T extends string, S extends SchemaDefinition> = Prettify<{
|
|
624
627
|
/** Optional metadata about the export */metadata?: {
|
|
625
628
|
/** Database name */dbName: string; /** Database version */
|
|
626
629
|
version: number; /** Export creation time */
|
|
627
630
|
exportedAt: Timestamp; /** List of exported table names */
|
|
628
|
-
tables:
|
|
631
|
+
tables: T[];
|
|
629
632
|
}; /** Actual exported data, mapping table names to arrays of records */
|
|
630
|
-
data:
|
|
631
|
-
}
|
|
633
|
+
data: ExportedTableData<T, S>;
|
|
634
|
+
}>;
|
|
632
635
|
/** Transaction context type providing methods for database operations within a transaction */
|
|
633
636
|
type TransactionContext<Schema extends SchemaDefinition, TName extends keyof Schema, Tables extends TName[]> = {
|
|
634
637
|
/** Inserts a new record into the specified table */insert: <T extends Tables[number], Raw extends InferInsertType<Schema[T]>, Inserted extends Raw | Raw[], Data extends InferSelectType<Schema[T]>, Return extends (Inserted extends Array<infer _> ? Data[] : Data)>(table: T) => InsertQuery<Raw, Inserted, Data, Return>; /** Updates an existing record in the specified table */
|
|
@@ -681,7 +684,9 @@ type TransactionCallback<Schema extends SchemaDefinition, TName extends keyof Sc
|
|
|
681
684
|
declare class Locality<DBName extends string = string, Version extends number = 1, Schema extends SchemaDefinition = SchemaDefinition, TName extends keyof Schema & string = keyof Schema & string> {
|
|
682
685
|
#private;
|
|
683
686
|
constructor(config: LocalityConfig<DBName, Version, Schema>);
|
|
684
|
-
|
|
687
|
+
/** @instance Get the current database name. */
|
|
688
|
+
get dbName(): DBName;
|
|
689
|
+
get version(): LooseLiteral<Version>;
|
|
685
690
|
/** @instance Get all table (store) names in the current database. */
|
|
686
691
|
get tableList(): LooseLiteral<TName>[];
|
|
687
692
|
/** @instance Get the list of existing `IndexedDB` databases. */
|
|
@@ -836,15 +841,23 @@ declare class Locality<DBName extends string = string, Version extends number =
|
|
|
836
841
|
* - Exports all tables by default, or only specified tables if provided.
|
|
837
842
|
* - Returns a JSON-serializable object with schema metadata and table data.
|
|
838
843
|
*/
|
|
839
|
-
exportToObject(options?:
|
|
844
|
+
exportToObject(options?: ExportObjectOptions<TName>): Promise<ExportData<TName, Schema>>;
|
|
845
|
+
/**
|
|
846
|
+
* @instance Import data into the database.
|
|
847
|
+
*
|
|
848
|
+
* @remarks
|
|
849
|
+
* - Accepts either an {@link ExportData} object or raw table data {@link ExportedTableData}.
|
|
850
|
+
* - Supports merge, replace, and upsert modes.
|
|
851
|
+
*/
|
|
852
|
+
import(data: ExportData<TName, Schema>, options?: ImportOptions<TName>): Promise<void>;
|
|
840
853
|
/**
|
|
841
854
|
* @instance Import data into the database.
|
|
842
855
|
*
|
|
843
856
|
* @remarks
|
|
844
|
-
* - Accepts either raw table data or an {@link ExportData} object.
|
|
857
|
+
* - Accepts either raw table data {@link ExportedTableData} or an {@link ExportData} object.
|
|
845
858
|
* - Supports merge, replace, and upsert modes.
|
|
846
859
|
*/
|
|
847
|
-
import(data:
|
|
860
|
+
import(data: ExportedTableData<TName, Schema>, options?: ImportOptions<TName>): Promise<void>;
|
|
848
861
|
/** @instance Clear all records from all tables. */
|
|
849
862
|
clearAll(): Promise<void>;
|
|
850
863
|
/**
|
|
@@ -1403,4 +1416,4 @@ declare function isUUID(value: unknown): value is UUID<UUIDVersion>;
|
|
|
1403
1416
|
*/
|
|
1404
1417
|
declare function validateColumnType<T extends TypeName>(type: T, value: unknown): string | null;
|
|
1405
1418
|
//#endregion
|
|
1406
|
-
export { $InferAutoInc, $InferDefault, $InferIndex, $InferNullable, $InferOptional, $InferPrimaryKey, $InferRow, $InferTimestamp, $InferUUID, $InferUnique, $UUID, $UUIDVersion, $UnionToIntersection, $ValidateSinglePK, AdvancedTypes, ArrayToTuple, AsyncFunction, BasicPrimitive, Branded, type Column, ColumnDefinition, ColumnRecord, ColumnType, Constructor, CursorCallback, DateLike, DefaultValue, Email, ExportData, ExportOptions, FirstOverloadParams, ForcedAny, GenericFn, GenericObject, ImportOptions, IndexConfig, IndexKeyType, InferInsertType, InferSelectType, InferUpdateType, IsAutoInc, IsIndexed, IsNullable, IsOptional, IsPrimaryKey, IsUnique, List, Locality, LocalityConfig, LooseLiteral, MapObjectValues, Maybe, NestedPrimitiveKey, NormalPrimitive, Numeric, OnUpdate, type PKColumn, PageOptions, PageResult, Prettify, PrimaryKeyType, Primitive, RejectFn, SchemaDefinition, SchemaRecord, SelectFields, SortDirection, StoreConfig, type Table, Timestamp, TransactionCallback, TransactionContext, Tuple, TypeName, URLString, UUID, UUIDVersion, UniqueKeyType, UpdaterFn, ValidateFn, ValidatedColumnDefinition, ValidatorFn, VoidFn, WherePredicate, column, defineSchema, deleteDB, getTimestamp, isEmail, isTimestamp, isURL, isUUID, openDBWithStores, table, uuidV4, validateColumnType };
|
|
1419
|
+
export { $InferAutoInc, $InferDefault, $InferIndex, $InferNullable, $InferOptional, $InferPrimaryKey, $InferRow, $InferTimestamp, $InferUUID, $InferUnique, $UUID, $UUIDVersion, $UnionToIntersection, $ValidateSinglePK, AdvancedTypes, ArrayToTuple, AsyncFunction, BasicPrimitive, Branded, type Column, ColumnDefinition, ColumnRecord, ColumnType, Constructor, CursorCallback, DateLike, DefaultValue, Email, ExportData, ExportObjectOptions, ExportOptions, ExportedTableData, FirstOverloadParams, ForcedAny, GenericFn, GenericObject, ImportOptions, IndexConfig, IndexKeyType, InferInsertType, InferSelectType, InferUpdateType, IsAutoInc, IsIndexed, IsNullable, IsOptional, IsPrimaryKey, IsUnique, List, Locality, LocalityConfig, LooseLiteral, MapObjectValues, Maybe, NestedPrimitiveKey, NormalPrimitive, Numeric, OnUpdate, type PKColumn, PageOptions, PageResult, Prettify, PrimaryKeyType, Primitive, RejectFn, SchemaDefinition, SchemaRecord, SelectFields, SortDirection, StoreConfig, type Table, Timestamp, TransactionCallback, TransactionContext, Tuple, TypeName, URLString, UUID, UUIDVersion, UniqueKeyType, UpdaterFn, ValidateFn, ValidatedColumnDefinition, ValidatorFn, VoidFn, WherePredicate, column, defineSchema, deleteDB, getTimestamp, isEmail, isTimestamp, isURL, isUUID, openDBWithStores, table, uuidV4, validateColumnType };
|
package/dist/index.d.mts
CHANGED
|
@@ -614,21 +614,24 @@ type ExportOptions<T extends string> = {
|
|
|
614
614
|
pretty?: boolean; /** Optional flag to include export metadata (default: `true`) */
|
|
615
615
|
includeMetadata?: boolean;
|
|
616
616
|
};
|
|
617
|
+
type ExportObjectOptions<T extends string> = Omit<ExportOptions<T>, 'filename' | 'pretty'>;
|
|
617
618
|
/** Import options for database `import` method */
|
|
618
619
|
type ImportOptions<T extends string> = {
|
|
619
620
|
/** Optional array of table names to import (imports all tables (store) if not specified) */tables?: T[]; /** Import mode: `'replace'`, `'merge'`, or `'upsert'` (default: `'merge'`) */
|
|
620
621
|
mode?: 'replace' | 'merge' | 'upsert';
|
|
621
622
|
};
|
|
623
|
+
/** Exported table data structure */
|
|
624
|
+
type ExportedTableData<T extends string, S extends SchemaDefinition> = Prettify<{ [K in T]: InferSelectType<S[K]>[] }>;
|
|
622
625
|
/** Exported database data structure */
|
|
623
|
-
type ExportData = {
|
|
626
|
+
type ExportData<T extends string, S extends SchemaDefinition> = Prettify<{
|
|
624
627
|
/** Optional metadata about the export */metadata?: {
|
|
625
628
|
/** Database name */dbName: string; /** Database version */
|
|
626
629
|
version: number; /** Export creation time */
|
|
627
630
|
exportedAt: Timestamp; /** List of exported table names */
|
|
628
|
-
tables:
|
|
631
|
+
tables: T[];
|
|
629
632
|
}; /** Actual exported data, mapping table names to arrays of records */
|
|
630
|
-
data:
|
|
631
|
-
}
|
|
633
|
+
data: ExportedTableData<T, S>;
|
|
634
|
+
}>;
|
|
632
635
|
/** Transaction context type providing methods for database operations within a transaction */
|
|
633
636
|
type TransactionContext<Schema extends SchemaDefinition, TName extends keyof Schema, Tables extends TName[]> = {
|
|
634
637
|
/** Inserts a new record into the specified table */insert: <T extends Tables[number], Raw extends InferInsertType<Schema[T]>, Inserted extends Raw | Raw[], Data extends InferSelectType<Schema[T]>, Return extends (Inserted extends Array<infer _> ? Data[] : Data)>(table: T) => InsertQuery<Raw, Inserted, Data, Return>; /** Updates an existing record in the specified table */
|
|
@@ -681,7 +684,9 @@ type TransactionCallback<Schema extends SchemaDefinition, TName extends keyof Sc
|
|
|
681
684
|
declare class Locality<DBName extends string = string, Version extends number = 1, Schema extends SchemaDefinition = SchemaDefinition, TName extends keyof Schema & string = keyof Schema & string> {
|
|
682
685
|
#private;
|
|
683
686
|
constructor(config: LocalityConfig<DBName, Version, Schema>);
|
|
684
|
-
|
|
687
|
+
/** @instance Get the current database name. */
|
|
688
|
+
get dbName(): DBName;
|
|
689
|
+
get version(): LooseLiteral<Version>;
|
|
685
690
|
/** @instance Get all table (store) names in the current database. */
|
|
686
691
|
get tableList(): LooseLiteral<TName>[];
|
|
687
692
|
/** @instance Get the list of existing `IndexedDB` databases. */
|
|
@@ -836,15 +841,23 @@ declare class Locality<DBName extends string = string, Version extends number =
|
|
|
836
841
|
* - Exports all tables by default, or only specified tables if provided.
|
|
837
842
|
* - Returns a JSON-serializable object with schema metadata and table data.
|
|
838
843
|
*/
|
|
839
|
-
exportToObject(options?:
|
|
844
|
+
exportToObject(options?: ExportObjectOptions<TName>): Promise<ExportData<TName, Schema>>;
|
|
845
|
+
/**
|
|
846
|
+
* @instance Import data into the database.
|
|
847
|
+
*
|
|
848
|
+
* @remarks
|
|
849
|
+
* - Accepts either an {@link ExportData} object or raw table data {@link ExportedTableData}.
|
|
850
|
+
* - Supports merge, replace, and upsert modes.
|
|
851
|
+
*/
|
|
852
|
+
import(data: ExportData<TName, Schema>, options?: ImportOptions<TName>): Promise<void>;
|
|
840
853
|
/**
|
|
841
854
|
* @instance Import data into the database.
|
|
842
855
|
*
|
|
843
856
|
* @remarks
|
|
844
|
-
* - Accepts either raw table data or an {@link ExportData} object.
|
|
857
|
+
* - Accepts either raw table data {@link ExportedTableData} or an {@link ExportData} object.
|
|
845
858
|
* - Supports merge, replace, and upsert modes.
|
|
846
859
|
*/
|
|
847
|
-
import(data:
|
|
860
|
+
import(data: ExportedTableData<TName, Schema>, options?: ImportOptions<TName>): Promise<void>;
|
|
848
861
|
/** @instance Clear all records from all tables. */
|
|
849
862
|
clearAll(): Promise<void>;
|
|
850
863
|
/**
|
|
@@ -1403,4 +1416,4 @@ declare function isUUID(value: unknown): value is UUID<UUIDVersion>;
|
|
|
1403
1416
|
*/
|
|
1404
1417
|
declare function validateColumnType<T extends TypeName>(type: T, value: unknown): string | null;
|
|
1405
1418
|
//#endregion
|
|
1406
|
-
export { $InferAutoInc, $InferDefault, $InferIndex, $InferNullable, $InferOptional, $InferPrimaryKey, $InferRow, $InferTimestamp, $InferUUID, $InferUnique, $UUID, $UUIDVersion, $UnionToIntersection, $ValidateSinglePK, AdvancedTypes, ArrayToTuple, AsyncFunction, BasicPrimitive, Branded, type Column, ColumnDefinition, ColumnRecord, ColumnType, Constructor, CursorCallback, DateLike, DefaultValue, Email, ExportData, ExportOptions, FirstOverloadParams, ForcedAny, GenericFn, GenericObject, ImportOptions, IndexConfig, IndexKeyType, InferInsertType, InferSelectType, InferUpdateType, IsAutoInc, IsIndexed, IsNullable, IsOptional, IsPrimaryKey, IsUnique, List, Locality, LocalityConfig, LooseLiteral, MapObjectValues, Maybe, NestedPrimitiveKey, NormalPrimitive, Numeric, OnUpdate, type PKColumn, PageOptions, PageResult, Prettify, PrimaryKeyType, Primitive, RejectFn, SchemaDefinition, SchemaRecord, SelectFields, SortDirection, StoreConfig, type Table, Timestamp, TransactionCallback, TransactionContext, Tuple, TypeName, URLString, UUID, UUIDVersion, UniqueKeyType, UpdaterFn, ValidateFn, ValidatedColumnDefinition, ValidatorFn, VoidFn, WherePredicate, column, defineSchema, deleteDB, getTimestamp, isEmail, isTimestamp, isURL, isUUID, openDBWithStores, table, uuidV4, validateColumnType };
|
|
1419
|
+
export { $InferAutoInc, $InferDefault, $InferIndex, $InferNullable, $InferOptional, $InferPrimaryKey, $InferRow, $InferTimestamp, $InferUUID, $InferUnique, $UUID, $UUIDVersion, $UnionToIntersection, $ValidateSinglePK, AdvancedTypes, ArrayToTuple, AsyncFunction, BasicPrimitive, Branded, type Column, ColumnDefinition, ColumnRecord, ColumnType, Constructor, CursorCallback, DateLike, DefaultValue, Email, ExportData, ExportObjectOptions, ExportOptions, ExportedTableData, FirstOverloadParams, ForcedAny, GenericFn, GenericObject, ImportOptions, IndexConfig, IndexKeyType, InferInsertType, InferSelectType, InferUpdateType, IsAutoInc, IsIndexed, IsNullable, IsOptional, IsPrimaryKey, IsUnique, List, Locality, LocalityConfig, LooseLiteral, MapObjectValues, Maybe, NestedPrimitiveKey, NormalPrimitive, Numeric, OnUpdate, type PKColumn, PageOptions, PageResult, Prettify, PrimaryKeyType, Primitive, RejectFn, SchemaDefinition, SchemaRecord, SelectFields, SortDirection, StoreConfig, type Table, Timestamp, TransactionCallback, TransactionContext, Tuple, TypeName, URLString, UUID, UUIDVersion, UniqueKeyType, UpdaterFn, ValidateFn, ValidatedColumnDefinition, ValidatorFn, VoidFn, WherePredicate, column, defineSchema, deleteDB, getTimestamp, isEmail, isTimestamp, isURL, isUUID, openDBWithStores, table, uuidV4, validateColumnType };
|
package/dist/index.iife.js
CHANGED
|
@@ -1374,6 +1374,10 @@ var LocalityIDB = (function(exports) {
|
|
|
1374
1374
|
const columns = this.#schema[table].columns;
|
|
1375
1375
|
return Object.entries(columns).find(([_, col]) => col[IsPrimaryKey])?.[0];
|
|
1376
1376
|
}
|
|
1377
|
+
/** @instance Get the current database name. */
|
|
1378
|
+
get dbName() {
|
|
1379
|
+
return this.#name;
|
|
1380
|
+
}
|
|
1377
1381
|
get version() {
|
|
1378
1382
|
return this.#db?.version ?? this.#version ?? this.#configVersion;
|
|
1379
1383
|
}
|
|
@@ -1607,7 +1611,7 @@ var LocalityIDB = (function(exports) {
|
|
|
1607
1611
|
await this.#readyPromise;
|
|
1608
1612
|
const { tables, includeMetadata = true } = options || {};
|
|
1609
1613
|
const tablesToExport = tables ?? Object.keys(this.#schema);
|
|
1610
|
-
for (const table of tablesToExport) if (!(table in this.#schema)) throw new RangeError(`Table '${
|
|
1614
|
+
for (const table of tablesToExport) if (!(table in this.#schema)) throw new RangeError(`Table '${table}' does not exist in schema.`);
|
|
1611
1615
|
if (tablesToExport.length === 0) return {
|
|
1612
1616
|
metadata: includeMetadata ? {
|
|
1613
1617
|
dbName: this.#name,
|
|
@@ -1622,21 +1626,22 @@ var LocalityIDB = (function(exports) {
|
|
|
1622
1626
|
return new Promise((resolve, reject) => {
|
|
1623
1627
|
transaction.onabort = () => _abortTransaction(transaction.error, reject);
|
|
1624
1628
|
transaction.onerror = () => reject(transaction.error);
|
|
1625
|
-
const readPromises = tablesToExport.map((table) =>
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1629
|
+
const readPromises = tablesToExport.map((table) => {
|
|
1630
|
+
return new Promise((res, rej) => {
|
|
1631
|
+
const request = transaction.objectStore(table).getAll();
|
|
1632
|
+
request.onsuccess = () => {
|
|
1633
|
+
exportData[table] = request.result;
|
|
1634
|
+
res();
|
|
1635
|
+
};
|
|
1636
|
+
request.onerror = () => rej(request.error);
|
|
1637
|
+
});
|
|
1638
|
+
});
|
|
1633
1639
|
Promise.all(readPromises).then(() => {
|
|
1634
1640
|
const exportObj = {};
|
|
1635
|
-
const ts = getTimestamp();
|
|
1636
1641
|
if (includeMetadata) exportObj.metadata = {
|
|
1637
1642
|
dbName: this.#name,
|
|
1638
1643
|
version: this.version,
|
|
1639
|
-
exportedAt:
|
|
1644
|
+
exportedAt: getTimestamp(),
|
|
1640
1645
|
tables: tablesToExport
|
|
1641
1646
|
};
|
|
1642
1647
|
exportObj.data = exportData;
|
|
@@ -1644,19 +1649,12 @@ var LocalityIDB = (function(exports) {
|
|
|
1644
1649
|
}).catch(reject);
|
|
1645
1650
|
});
|
|
1646
1651
|
}
|
|
1647
|
-
/**
|
|
1648
|
-
* @instance Import data into the database.
|
|
1649
|
-
*
|
|
1650
|
-
* @remarks
|
|
1651
|
-
* - Accepts either raw table data or an {@link ExportData} object.
|
|
1652
|
-
* - Supports merge, replace, and upsert modes.
|
|
1653
|
-
*/
|
|
1654
1652
|
async import(data, options) {
|
|
1655
1653
|
await this.#readyPromise;
|
|
1656
1654
|
const { mode = "merge", tables } = options || {};
|
|
1657
1655
|
const dataMap = "data" in data ? data.data : data;
|
|
1658
1656
|
const tablesToImport = tables ?? Object.keys(dataMap);
|
|
1659
|
-
for (const table of tablesToImport) if (!(table in this.#schema)) throw new RangeError(`Table '${
|
|
1657
|
+
for (const table of tablesToImport) if (!(table in this.#schema)) throw new RangeError(`Table '${table}' does not exist in schema.`);
|
|
1660
1658
|
if (tablesToImport.length === 0) return;
|
|
1661
1659
|
const transaction = this.#db.transaction(tablesToImport, "readwrite");
|
|
1662
1660
|
return new Promise((resolve, reject) => {
|
|
@@ -1673,7 +1671,7 @@ var LocalityIDB = (function(exports) {
|
|
|
1673
1671
|
if (rows.length === 0) return;
|
|
1674
1672
|
const writePromises = rows.map((row) => {
|
|
1675
1673
|
return new Promise((res, rej) => {
|
|
1676
|
-
const prepared = validateAndPrepareData(row, this.#schema[table].columns, this.#keyPaths[table],
|
|
1674
|
+
const prepared = validateAndPrepareData(row, this.#schema[table].columns, this.#keyPaths[table], table);
|
|
1677
1675
|
const request = mode === "upsert" ? store.put(prepared) : store.add(prepared);
|
|
1678
1676
|
request.onsuccess = () => res();
|
|
1679
1677
|
request.onerror = () => rej(request.error);
|
|
@@ -1696,11 +1694,13 @@ var LocalityIDB = (function(exports) {
|
|
|
1696
1694
|
return new Promise((resolve, reject) => {
|
|
1697
1695
|
transaction.onabort = () => _abortTransaction(transaction.error, reject);
|
|
1698
1696
|
transaction.onerror = () => reject(transaction.error);
|
|
1699
|
-
const clearPromises = tables.map((table) =>
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1697
|
+
const clearPromises = tables.map((table) => {
|
|
1698
|
+
return new Promise((res, rej) => {
|
|
1699
|
+
const request = transaction.objectStore(table).clear();
|
|
1700
|
+
request.onsuccess = () => res();
|
|
1701
|
+
request.onerror = () => rej(request.error);
|
|
1702
|
+
});
|
|
1703
|
+
});
|
|
1704
1704
|
Promise.all(clearPromises).then(() => resolve()).catch((err) => {
|
|
1705
1705
|
transaction.abort();
|
|
1706
1706
|
reject(err);
|
|
@@ -1716,23 +1716,23 @@ var LocalityIDB = (function(exports) {
|
|
|
1716
1716
|
*/
|
|
1717
1717
|
async dropTable(table) {
|
|
1718
1718
|
await this.#readyPromise;
|
|
1719
|
-
if (!(table in this.#schema)) throw new RangeError(`Table '${
|
|
1719
|
+
if (!(table in this.#schema)) throw new RangeError(`Table '${table}' does not exist in schema.`);
|
|
1720
1720
|
const nextStores = this.#buildStoresConfig().filter((store) => store.name !== table);
|
|
1721
1721
|
const nextVersion = this.version + 1;
|
|
1722
1722
|
this.#db.close();
|
|
1723
1723
|
this.#readyPromise = openDBWithStores(this.#name, nextStores, nextVersion).then((db) => {
|
|
1724
1724
|
this.#db = db;
|
|
1725
1725
|
const keyPaths = this.#keyPaths;
|
|
1726
|
-
delete keyPaths[
|
|
1726
|
+
delete keyPaths[table];
|
|
1727
1727
|
}).finally(() => {
|
|
1728
1728
|
this.#version = this.#db?.version;
|
|
1729
1729
|
});
|
|
1730
1730
|
await this.#readyPromise;
|
|
1731
1731
|
}
|
|
1732
1732
|
/** @static Get the list of existing `IndexedDB` databases. */
|
|
1733
|
-
static getDatabaseList() {
|
|
1733
|
+
static async getDatabaseList() {
|
|
1734
1734
|
_ensureIndexedDB();
|
|
1735
|
-
return _getDBList();
|
|
1735
|
+
return await _getDBList();
|
|
1736
1736
|
}
|
|
1737
1737
|
/** @static Delete an `IndexedDB` database by name. */
|
|
1738
1738
|
static async deleteDatabase(name) {
|
package/dist/index.mjs
CHANGED
|
@@ -1371,6 +1371,10 @@ var Locality = class Locality {
|
|
|
1371
1371
|
const columns = this.#schema[table].columns;
|
|
1372
1372
|
return Object.entries(columns).find(([_, col]) => col[IsPrimaryKey])?.[0];
|
|
1373
1373
|
}
|
|
1374
|
+
/** @instance Get the current database name. */
|
|
1375
|
+
get dbName() {
|
|
1376
|
+
return this.#name;
|
|
1377
|
+
}
|
|
1374
1378
|
get version() {
|
|
1375
1379
|
return this.#db?.version ?? this.#version ?? this.#configVersion;
|
|
1376
1380
|
}
|
|
@@ -1604,7 +1608,7 @@ var Locality = class Locality {
|
|
|
1604
1608
|
await this.#readyPromise;
|
|
1605
1609
|
const { tables, includeMetadata = true } = options || {};
|
|
1606
1610
|
const tablesToExport = tables ?? Object.keys(this.#schema);
|
|
1607
|
-
for (const table of tablesToExport) if (!(table in this.#schema)) throw new RangeError(`Table '${
|
|
1611
|
+
for (const table of tablesToExport) if (!(table in this.#schema)) throw new RangeError(`Table '${table}' does not exist in schema.`);
|
|
1608
1612
|
if (tablesToExport.length === 0) return {
|
|
1609
1613
|
metadata: includeMetadata ? {
|
|
1610
1614
|
dbName: this.#name,
|
|
@@ -1619,21 +1623,22 @@ var Locality = class Locality {
|
|
|
1619
1623
|
return new Promise((resolve, reject) => {
|
|
1620
1624
|
transaction.onabort = () => _abortTransaction(transaction.error, reject);
|
|
1621
1625
|
transaction.onerror = () => reject(transaction.error);
|
|
1622
|
-
const readPromises = tablesToExport.map((table) =>
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1626
|
+
const readPromises = tablesToExport.map((table) => {
|
|
1627
|
+
return new Promise((res, rej) => {
|
|
1628
|
+
const request = transaction.objectStore(table).getAll();
|
|
1629
|
+
request.onsuccess = () => {
|
|
1630
|
+
exportData[table] = request.result;
|
|
1631
|
+
res();
|
|
1632
|
+
};
|
|
1633
|
+
request.onerror = () => rej(request.error);
|
|
1634
|
+
});
|
|
1635
|
+
});
|
|
1630
1636
|
Promise.all(readPromises).then(() => {
|
|
1631
1637
|
const exportObj = {};
|
|
1632
|
-
const ts = getTimestamp();
|
|
1633
1638
|
if (includeMetadata) exportObj.metadata = {
|
|
1634
1639
|
dbName: this.#name,
|
|
1635
1640
|
version: this.version,
|
|
1636
|
-
exportedAt:
|
|
1641
|
+
exportedAt: getTimestamp(),
|
|
1637
1642
|
tables: tablesToExport
|
|
1638
1643
|
};
|
|
1639
1644
|
exportObj.data = exportData;
|
|
@@ -1641,19 +1646,12 @@ var Locality = class Locality {
|
|
|
1641
1646
|
}).catch(reject);
|
|
1642
1647
|
});
|
|
1643
1648
|
}
|
|
1644
|
-
/**
|
|
1645
|
-
* @instance Import data into the database.
|
|
1646
|
-
*
|
|
1647
|
-
* @remarks
|
|
1648
|
-
* - Accepts either raw table data or an {@link ExportData} object.
|
|
1649
|
-
* - Supports merge, replace, and upsert modes.
|
|
1650
|
-
*/
|
|
1651
1649
|
async import(data, options) {
|
|
1652
1650
|
await this.#readyPromise;
|
|
1653
1651
|
const { mode = "merge", tables } = options || {};
|
|
1654
1652
|
const dataMap = "data" in data ? data.data : data;
|
|
1655
1653
|
const tablesToImport = tables ?? Object.keys(dataMap);
|
|
1656
|
-
for (const table of tablesToImport) if (!(table in this.#schema)) throw new RangeError(`Table '${
|
|
1654
|
+
for (const table of tablesToImport) if (!(table in this.#schema)) throw new RangeError(`Table '${table}' does not exist in schema.`);
|
|
1657
1655
|
if (tablesToImport.length === 0) return;
|
|
1658
1656
|
const transaction = this.#db.transaction(tablesToImport, "readwrite");
|
|
1659
1657
|
return new Promise((resolve, reject) => {
|
|
@@ -1670,7 +1668,7 @@ var Locality = class Locality {
|
|
|
1670
1668
|
if (rows.length === 0) return;
|
|
1671
1669
|
const writePromises = rows.map((row) => {
|
|
1672
1670
|
return new Promise((res, rej) => {
|
|
1673
|
-
const prepared = validateAndPrepareData(row, this.#schema[table].columns, this.#keyPaths[table],
|
|
1671
|
+
const prepared = validateAndPrepareData(row, this.#schema[table].columns, this.#keyPaths[table], table);
|
|
1674
1672
|
const request = mode === "upsert" ? store.put(prepared) : store.add(prepared);
|
|
1675
1673
|
request.onsuccess = () => res();
|
|
1676
1674
|
request.onerror = () => rej(request.error);
|
|
@@ -1693,11 +1691,13 @@ var Locality = class Locality {
|
|
|
1693
1691
|
return new Promise((resolve, reject) => {
|
|
1694
1692
|
transaction.onabort = () => _abortTransaction(transaction.error, reject);
|
|
1695
1693
|
transaction.onerror = () => reject(transaction.error);
|
|
1696
|
-
const clearPromises = tables.map((table) =>
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1694
|
+
const clearPromises = tables.map((table) => {
|
|
1695
|
+
return new Promise((res, rej) => {
|
|
1696
|
+
const request = transaction.objectStore(table).clear();
|
|
1697
|
+
request.onsuccess = () => res();
|
|
1698
|
+
request.onerror = () => rej(request.error);
|
|
1699
|
+
});
|
|
1700
|
+
});
|
|
1701
1701
|
Promise.all(clearPromises).then(() => resolve()).catch((err) => {
|
|
1702
1702
|
transaction.abort();
|
|
1703
1703
|
reject(err);
|
|
@@ -1713,23 +1713,23 @@ var Locality = class Locality {
|
|
|
1713
1713
|
*/
|
|
1714
1714
|
async dropTable(table) {
|
|
1715
1715
|
await this.#readyPromise;
|
|
1716
|
-
if (!(table in this.#schema)) throw new RangeError(`Table '${
|
|
1716
|
+
if (!(table in this.#schema)) throw new RangeError(`Table '${table}' does not exist in schema.`);
|
|
1717
1717
|
const nextStores = this.#buildStoresConfig().filter((store) => store.name !== table);
|
|
1718
1718
|
const nextVersion = this.version + 1;
|
|
1719
1719
|
this.#db.close();
|
|
1720
1720
|
this.#readyPromise = openDBWithStores(this.#name, nextStores, nextVersion).then((db) => {
|
|
1721
1721
|
this.#db = db;
|
|
1722
1722
|
const keyPaths = this.#keyPaths;
|
|
1723
|
-
delete keyPaths[
|
|
1723
|
+
delete keyPaths[table];
|
|
1724
1724
|
}).finally(() => {
|
|
1725
1725
|
this.#version = this.#db?.version;
|
|
1726
1726
|
});
|
|
1727
1727
|
await this.#readyPromise;
|
|
1728
1728
|
}
|
|
1729
1729
|
/** @static Get the list of existing `IndexedDB` databases. */
|
|
1730
|
-
static getDatabaseList() {
|
|
1730
|
+
static async getDatabaseList() {
|
|
1731
1731
|
_ensureIndexedDB();
|
|
1732
|
-
return _getDBList();
|
|
1732
|
+
return await _getDBList();
|
|
1733
1733
|
}
|
|
1734
1734
|
/** @static Delete an `IndexedDB` database by name. */
|
|
1735
1735
|
static async deleteDatabase(name) {
|