@creasource/reactive-idb
Version:
A reactive wrapper to indexedDb using Rxjs
163 lines • 10.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReactiveIDBObjectStore = void 0;
const operators_1 = require("rxjs/operators");
const reactive_idb_index_1 = require("./reactive-idb-index");
const wrap_request_util_1 = require("./utils/wrap-request.util");
class ReactiveIDBObjectStore {
/**
*
* @param store
* @param transaction
* @param transformer
*/
constructor(store, transaction, transformer = {
serialize: (o) => o,
deserialize: (v) => v,
}) {
this.store = store;
this.transaction = transaction;
this.transformer = transformer;
}
/**
* Returns true if the store has a key generator, and false otherwise.
*/
get autoIncrement() {
return this.store.autoIncrement;
}
/**
* Returns a list of the names of indexes in the store.
*/
get indexNames() {
return this.store.indexNames;
}
/**
* Returns the key path of the store, or null if none.
*/
get keyPath() {
return this.store.keyPath;
}
/**
* Returns the name of the store.
*/
get name() {
return this.store.name;
}
/**
* Adds or updates a record in store with the given value and key.
*
* If the store uses in-line keys and key is specified a "DataError" DOMException will be thrown.
*
* If put() is used, any existing record with the key will be replaced. If add() is used, and if a record with the key already exists the request will fail, with request's error set to a "ConstraintError" DOMException.
*
* If successful, request's result will be the record's key.
*/
add$(value, key) {
return (0, wrap_request_util_1.wrapRequest)(() => this.store.add(this.transformer.serialize(value), key));
}
/**
* Deletes all records in store.
*/
clear$() {
return (0, wrap_request_util_1.wrapRequest)(() => this.store.clear());
}
/**
* Retrieves the number of records matching the given key or key range in query.
*/
count$(key) {
return (0, wrap_request_util_1.wrapRequest)(() => this.store.count(key));
}
/**
* Creates a new index in store with the given name, keyPath and options and returns a new IDBIndex. If the keyPath and options define constraints that cannot be satisfied with the data already in store the upgrade transaction will abort with a "ConstraintError" DOMException.
*
* Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
*/
// createIndex(
// name: string,
// keyPath: string | string[],
// options?: IDBIndexParameters
// ): IDBIndex {
// return this.store.createIndex(name, keyPath, options);
// }
/**
* Deletes records in store with the given key or in the given key range in query.
*
* If successful, request's result will be undefined.
*/
delete$(key) {
return (0, wrap_request_util_1.wrapRequest)(() => this.store.delete(key));
}
/**
* Deletes the index in store with the given name.
*
* Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
*/
// deleteIndex(name: string): void {
// this.store.deleteIndex(name);
// }
/**
* Retrieves the value of the first record matching the given key or key range in query.
*
* If successful, request's result will be the value, or undefined if there was no matching record.
*/
get$(query) {
return (0, wrap_request_util_1.wrapRequest)(() => this.store.get(query)).pipe((0, operators_1.map)((value) => value !== undefined ? this.transformer.deserialize(value) : value));
}
/**
* Retrieves the values of the records matching the given key or key range in query (up to count if given).
*
* If successful, request's result will be an Array of the values.
*/
getAll$(query, count) {
return (0, wrap_request_util_1.wrapRequest)(() => this.store.getAll(query, count)).pipe((0, operators_1.map)((values) => values.map((v) => this.transformer.deserialize(v))));
}
/**
* Retrieves the keys of records matching the given key or key range in query (up to count if given).
*
* If successful, request's result will be an Array of the keys.
*/
getAllKeys$(query, count) {
return (0, wrap_request_util_1.wrapRequest)(() => this.store.getAllKeys(query, count));
}
/**
* Retrieves the key of the first record matching the given key or key range in query.
*
* If successful, request's result will be the key, or undefined if there was no matching record.
*/
getKey$(query) {
return (0, wrap_request_util_1.wrapRequest)(() => this.store.getKey(query));
}
index(name) {
return new reactive_idb_index_1.ReactiveIDBIndex(this.store.index(name), this);
}
/**
* Opens a cursor over the records matching query, ordered by direction. If query is null, all records in store are matched.
*
* If successful, request's result will be an IDBCursorWithValue pointing at the first matching record, or null if there were no matching records.
*/
openCursor$(query, direction) {
return (0, wrap_request_util_1.wrapRequest)(() => this.store.openCursor(query, direction), false);
}
/**
* Opens a cursor with key only flag set over the records matching query, ordered by direction. If query is null, all records in store are matched.
*
* If successful, request's result will be an IDBCursor pointing at the first matching record, or null if there were no matching records.
*/
openKeyCursor$(query, direction) {
return (0, wrap_request_util_1.wrapRequest)(() => this.store.openKeyCursor(query, direction), false);
}
/**
* Adds or updates a record in store with the given value and key.
*
* If the store uses in-line keys and key is specified a "DataError" DOMException will be thrown.
*
* If put() is used, any existing record with the key will be replaced. If add() is used, and if a record with the key already exists the request will fail, with request's error set to a "ConstraintError" DOMException.
*
* If successful, request's result will be the record's key.
*/
put$(value, key) {
return (0, wrap_request_util_1.wrapRequest)(() => this.store.put(this.transformer.serialize(value), key));
}
}
exports.ReactiveIDBObjectStore = ReactiveIDBObjectStore;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVhY3RpdmUtaWRiLW9iamVjdC1zdG9yZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9saWIvcmVhY3RpdmUtaWRiLW9iamVjdC1zdG9yZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFDQSw4Q0FBcUM7QUFHckMsNkRBQXdEO0FBRXhELGlFQUF3RDtBQUV4RCxNQUFhLHNCQUFzQjtJQTBCakM7Ozs7O09BS0c7SUFDSCxZQUNtQixLQUFxQixFQUM3QixXQUFtQyxFQUNuQyxjQUF5QztRQUNoRCxTQUFTLEVBQUUsQ0FBQyxDQUFDLEVBQUUsRUFBRSxDQUFDLENBQUM7UUFDbkIsV0FBVyxFQUFFLENBQUMsQ0FBQyxFQUFFLEVBQUUsQ0FBQyxDQUFNO0tBQzNCO1FBTGdCLFVBQUssR0FBTCxLQUFLLENBQWdCO1FBQzdCLGdCQUFXLEdBQVgsV0FBVyxDQUF3QjtRQUNuQyxnQkFBVyxHQUFYLFdBQVcsQ0FHbkI7SUFDQSxDQUFDO0lBdENKOztPQUVHO0lBQ0gsSUFBSSxhQUFhO1FBQ2YsT0FBTyxJQUFJLENBQUMsS0FBSyxDQUFDLGFBQWEsQ0FBQztJQUNsQyxDQUFDO0lBQ0Q7O09BRUc7SUFDSCxJQUFJLFVBQVU7UUFDWixPQUFPLElBQUksQ0FBQyxLQUFLLENBQUMsVUFBVSxDQUFDO0lBQy9CLENBQUM7SUFDRDs7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