UNPKG

@idxdb/promised

Version:

@idxdb/promised wraps the IndexedDB API. It allows you to easily store and retrieve data in an indexed db database using async/await syntax, making it easier to integrate with your existing codebase.

92 lines (91 loc) 3 kB
import { StoreIndex } from '../component/store-index.js'; import { ValueCursor } from '../component/value-cursor.js'; import { KeyCursor } from '../component/key-cursor.js'; import { requestResolver } from '../shared/request-resolver.js'; import { Transaction } from '../component/transaction.js'; export class ObjectStore { ctx; constructor(ctx) { this.ctx = ctx; } add(value, key) { const request = this.ctx.objectStore.add(value, key); return requestResolver(request); } clear() { const request = this.ctx.objectStore.clear(); return requestResolver(request); } count(query) { const request = this.ctx.objectStore.count(query); return requestResolver(request); } createIndex(indexName, keyPath, options) { const index = this.ctx.objectStore.createIndex(indexName, keyPath, options); return new StoreIndex({ index, objectStore: this }); } delete(query) { const request = this.ctx.objectStore.delete(query); return requestResolver(request); } deleteIndex(name) { return this.ctx.objectStore.deleteIndex(name); } get(key) { const request = this.ctx.objectStore.get(key); return requestResolver(request); } getAll(query, count) { const request = this.ctx.objectStore.getAll(query, count); return requestResolver(request); } getAllKeys(query, count) { const request = this.ctx.objectStore.getAllKeys(query, count); return requestResolver(request); } getKey(key) { const request = this.ctx.objectStore.getKey(key); return requestResolver(request); } index(name) { const index = this.ctx.objectStore.index(name); return new StoreIndex({ index, objectStore: this }); } openCursor(query, direction) { const request = this.ctx.objectStore.openCursor(query, direction); return new ValueCursor({ request, direction, source: new ObjectStore({ objectStore: this.ctx.objectStore }), }); } openKeyCursor(query, direction) { const request = this.ctx.objectStore.openKeyCursor(query, direction); return new KeyCursor({ request, direction, source: new ObjectStore({ objectStore: this.ctx.objectStore }), }); } put(value, key) { const request = this.ctx.objectStore.put(value, key); return requestResolver(request).then(() => void 0); } get indexNames() { return Array.from(this.ctx.objectStore.indexNames); } get autoIncrement() { return this.ctx.objectStore.autoIncrement; } get keyPath() { return this.ctx.objectStore.keyPath; } get name() { return this.ctx.objectStore.name; } get transaction() { return new Transaction({ transaction: this.ctx.objectStore.transaction, }); } }