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.

56 lines (55 loc) 1.7 kB
import { ObjectStore } from '../component/object-store.js'; import { Database } from '../component/database.js'; export class Transaction { ctx; constructor(ctx) { this.ctx = ctx; } abort() { return new Promise((resolve, reject) => { this.ctx.transaction.addEventListener('abort', () => { resolve(); }); /* istanbul ignore next */ this.ctx.transaction.addEventListener('error', (event) => { const target = event.target; reject(target.error); }); this.ctx.transaction.abort(); }); } commit() { return new Promise((resolve, reject) => { this.ctx.transaction.addEventListener('complete', () => { resolve(); }); /* istanbul ignore next */ this.ctx.transaction.addEventListener('error', (event) => { const target = event.target; reject(target.error); }); this.ctx.transaction.commit(); }); } objectStore(name) { const objectStore = this.ctx.transaction.objectStore(name); return new ObjectStore({ objectStore }); } get objectStoreNames() { return Array.from(this.ctx.transaction.objectStoreNames); } get db() { return new Database({ db: this.ctx.transaction.db }); } /* istanbul ignore next */ get durability() { return this.ctx.transaction.durability; } /* istanbul ignore next */ get error() { return this.ctx.transaction.error; } get mode() { return this.ctx.transaction.mode; } }