Merge branch 'snowpack-ts-storage-1' into snowpack-ts-storage-2
This commit is contained in:
commit
a95704528f
2 changed files with 23 additions and 5 deletions
|
@ -36,7 +36,7 @@ class QueryTargetWrapper<T> {
|
||||||
|
|
||||||
get keyPath(): string | string[] {
|
get keyPath(): string | string[] {
|
||||||
if ("objectStore" in this._qt) {
|
if ("objectStore" in this._qt) {
|
||||||
return (this._qt as IDBIndex).objectStore.keyPath;
|
return this._qt.objectStore.keyPath;
|
||||||
} else {
|
} else {
|
||||||
return this._qt.keyPath;
|
return this._qt.keyPath;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,13 +17,31 @@ limitations under the License.
|
||||||
|
|
||||||
import { StorageError } from "../common";
|
import { StorageError } from "../common";
|
||||||
|
|
||||||
|
function _sourceName(source: IDBIndex | IDBObjectStore): string {
|
||||||
|
return "objectStore" in source ?
|
||||||
|
`${source.objectStore.name}.${source.name}` :
|
||||||
|
source.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _sourceDatabase(source: IDBIndex | IDBObjectStore): string {
|
||||||
|
return "objectStore" in source ?
|
||||||
|
source.objectStore.transaction.db.name :
|
||||||
|
source.transaction.db.name;
|
||||||
|
}
|
||||||
|
|
||||||
export class IDBError extends StorageError {
|
export class IDBError extends StorageError {
|
||||||
storeName: string;
|
storeName: string;
|
||||||
databaseName: string;
|
databaseName: string;
|
||||||
|
|
||||||
constructor(message: string, source, cause: DOMException | null = null) {
|
constructor(message: string, source: IDBIndex | IDBCursor | IDBObjectStore, cause: DOMException | null = null) {
|
||||||
const storeName = source?.name || "<unknown store>";
|
let storeName: string, databaseName: string;
|
||||||
const databaseName = source?.transaction?.db?.name || "<unknown db>";
|
if (source instanceof IDBCursor) {
|
||||||
|
storeName = _sourceName(source.source);
|
||||||
|
databaseName = _sourceDatabase(source.source);
|
||||||
|
} else {
|
||||||
|
storeName = _sourceName(source);
|
||||||
|
databaseName = _sourceDatabase(source);
|
||||||
|
}
|
||||||
let fullMessage = `${message} on ${databaseName}.${storeName}`;
|
let fullMessage = `${message} on ${databaseName}.${storeName}`;
|
||||||
if (cause) {
|
if (cause) {
|
||||||
fullMessage += ": ";
|
fullMessage += ": ";
|
||||||
|
@ -52,7 +70,7 @@ export class IDBRequestError extends IDBError {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class IDBRequestAttemptError extends IDBError {
|
export class IDBRequestAttemptError extends IDBError {
|
||||||
constructor(method: string, source, cause: DOMException, params: any[]) {
|
constructor(method: string, source: IDBIndex | IDBObjectStore, cause: DOMException, params: any[]) {
|
||||||
super(`${method}(${params.map(p => JSON.stringify(p)).join(", ")}) failed`, source, cause);
|
super(`${method}(${params.map(p => JSON.stringify(p)).join(", ")}) failed`, source, cause);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue