diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp index 255026e319b..4a5b4999236 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp @@ -165,4 +165,28 @@ GC::Ref IDBObjectStore::index_names() return create_a_sorted_name_list(realm(), names); } +// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-index +WebIDL::ExceptionOr> IDBObjectStore::index(String const& name) +{ + // 1. Let transaction be this’s transaction. + auto transaction = this->transaction(); + + // 2. Let store be this’s object store. + [[maybe_unused]] auto store = this->store(); + + // FIXME: 3. If store has been deleted, throw an "InvalidStateError" DOMException. + + // 4. If transaction’s state is finished, then throw an "InvalidStateError" DOMException. + if (transaction->state() == IDBTransaction::TransactionState::Finished) + return WebIDL::InvalidStateError::create(realm(), "Transaction is finished"_string); + + // 5. Let index be the index named name in this’s index set if one exists, or throw a "NotFoundError" DOMException otherwise. + auto index = m_indexes.get(name); + if (!index.has_value()) + return WebIDL::NotFoundError::create(realm(), "Index not found"_string); + + // 6. Return an index handle associated with index and this. + return IDBIndex::create(realm(), *index, *this); +} + } diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h index 8baf4a1869c..0d3aac2a589 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h @@ -41,6 +41,7 @@ public: WebIDL::ExceptionOr> create_index(String const&, KeyPath, IDBIndexParameters options); [[nodiscard]] GC::Ref index_names(); + WebIDL::ExceptionOr> index(String const&); protected: explicit IDBObjectStore(JS::Realm&, GC::Ref, GC::Ref); diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl b/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl index 550f7d3c60f..947ecd6bcf5 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl @@ -21,7 +21,7 @@ interface IDBObjectStore { [FIXME, NewObject] IDBRequest count(optional any query); [FIXME, NewObject] IDBRequest openCursor(optional any query, optional IDBCursorDirection direction = "next"); [FIXME, NewObject] IDBRequest openKeyCursor(optional any query, optional IDBCursorDirection direction = "next"); - [FIXME] IDBIndex index(DOMString name); + IDBIndex index(DOMString name); [NewObject] IDBIndex createIndex(DOMString name, (DOMString or sequence) keyPath, optional IDBIndexParameters options = {}); [FIXME] undefined deleteIndex(DOMString name); };