2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-04-20 21:41:10 +02:00
|
|
|
import {QueryTarget} from "./QueryTarget.js";
|
2019-02-07 01:20:27 +01:00
|
|
|
import { reqAsPromise } from "./utils.js";
|
2019-06-26 22:00:50 +02:00
|
|
|
import { StorageError } from "../common.js";
|
|
|
|
|
|
|
|
class QueryTargetWrapper {
|
|
|
|
constructor(qt) {
|
|
|
|
this._qt = qt;
|
|
|
|
}
|
2019-09-15 12:24:27 +02:00
|
|
|
|
|
|
|
supports(methodName) {
|
|
|
|
return !!this._qt[methodName];
|
|
|
|
}
|
2019-06-26 22:00:50 +02:00
|
|
|
|
|
|
|
openKeyCursor(...params) {
|
2019-09-15 12:24:46 +02:00
|
|
|
// not supported on Edge 15
|
|
|
|
if (!this._qt.openKeyCursor) {
|
|
|
|
return this.openCursor(...params);
|
|
|
|
}
|
2019-06-26 22:00:50 +02:00
|
|
|
try {
|
|
|
|
return this._qt.openKeyCursor(...params);
|
|
|
|
} catch(err) {
|
|
|
|
throw new StorageError("openKeyCursor failed", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openCursor(...params) {
|
|
|
|
try {
|
|
|
|
return this._qt.openCursor(...params);
|
|
|
|
} catch(err) {
|
|
|
|
throw new StorageError("openCursor failed", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
put(...params) {
|
|
|
|
try {
|
|
|
|
return this._qt.put(...params);
|
|
|
|
} catch(err) {
|
|
|
|
throw new StorageError("put failed", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
add(...params) {
|
|
|
|
try {
|
|
|
|
return this._qt.add(...params);
|
|
|
|
} catch(err) {
|
|
|
|
throw new StorageError("add failed", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get(...params) {
|
|
|
|
try {
|
|
|
|
return this._qt.get(...params);
|
|
|
|
} catch(err) {
|
|
|
|
throw new StorageError("get failed", err);
|
|
|
|
}
|
|
|
|
}
|
2019-07-27 10:40:56 +02:00
|
|
|
|
|
|
|
getKey(...params) {
|
|
|
|
try {
|
|
|
|
return this._qt.getKey(...params);
|
|
|
|
} catch(err) {
|
|
|
|
throw new StorageError("getKey failed", err);
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 22:00:50 +02:00
|
|
|
|
2019-07-26 22:03:57 +02:00
|
|
|
delete(...params) {
|
|
|
|
try {
|
|
|
|
return this._qt.delete(...params);
|
|
|
|
} catch(err) {
|
|
|
|
throw new StorageError("delete failed", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 22:00:50 +02:00
|
|
|
index(...params) {
|
|
|
|
try {
|
|
|
|
return this._qt.index(...params);
|
|
|
|
} catch(err) {
|
|
|
|
throw new StorageError("index failed", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-05 00:21:50 +01:00
|
|
|
|
2020-04-20 21:26:39 +02:00
|
|
|
export class Store extends QueryTarget {
|
2019-06-26 22:31:36 +02:00
|
|
|
constructor(idbStore) {
|
|
|
|
super(new QueryTargetWrapper(idbStore));
|
|
|
|
}
|
2019-02-05 00:21:50 +01:00
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
get _idbStore() {
|
|
|
|
return this._target;
|
|
|
|
}
|
2019-02-05 00:21:50 +01:00
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
index(indexName) {
|
|
|
|
return new QueryTarget(new QueryTargetWrapper(this._idbStore.index(indexName)));
|
|
|
|
}
|
2019-02-07 01:20:27 +01:00
|
|
|
|
2019-10-12 22:19:16 +02:00
|
|
|
async put(value) {
|
|
|
|
try {
|
|
|
|
return await reqAsPromise(this._idbStore.put(value));
|
|
|
|
} catch(err) {
|
|
|
|
const originalErr = err.cause;
|
2020-08-18 17:27:40 +02:00
|
|
|
throw new StorageError(`put on ${err.databaseName}.${err.storeName} failed`, originalErr, value);
|
2019-10-12 22:19:16 +02:00
|
|
|
}
|
2019-06-26 22:31:36 +02:00
|
|
|
}
|
2019-02-07 01:20:27 +01:00
|
|
|
|
2019-10-12 22:19:16 +02:00
|
|
|
async add(value) {
|
|
|
|
try {
|
|
|
|
return await reqAsPromise(this._idbStore.add(value));
|
|
|
|
} catch(err) {
|
|
|
|
const originalErr = err.cause;
|
2020-08-18 17:27:40 +02:00
|
|
|
throw new StorageError(`add on ${err.databaseName}.${err.storeName} failed`, originalErr, value);
|
2019-10-12 22:19:16 +02:00
|
|
|
}
|
2019-06-26 22:31:36 +02:00
|
|
|
}
|
2019-07-26 22:03:57 +02:00
|
|
|
|
2020-08-18 17:27:40 +02:00
|
|
|
async delete(keyOrKeyRange) {
|
|
|
|
try {
|
|
|
|
return await reqAsPromise(this._idbStore.delete(keyOrKeyRange));
|
|
|
|
} catch(err) {
|
|
|
|
const originalErr = err.cause;
|
|
|
|
throw new StorageError(`delete on ${err.databaseName}.${err.storeName} failed`, originalErr, keyOrKeyRange);
|
|
|
|
}
|
|
|
|
|
2019-07-26 22:03:57 +02:00
|
|
|
}
|
2019-06-26 22:00:50 +02:00
|
|
|
}
|