LibWeb/IDB: Implement IDBObjectStore::createIndex

This commit is contained in:
stelar7 2025-04-01 18:37:23 +02:00 committed by Andrew Kaster
commit 3367352991
Notes: github-actions[bot] 2025-04-09 17:50:46 +00:00
7 changed files with 88 additions and 14 deletions

View file

@ -9,6 +9,7 @@
#include <LibWeb/Bindings/IDBObjectStorePrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/IndexedDB/IDBIndex.h>
#include <LibWeb/IndexedDB/IDBObjectStore.h>
namespace Web::IndexedDB {
@ -41,6 +42,7 @@ void IDBObjectStore::visit_edges(Visitor& visitor)
Base::visit_edges(visitor);
visitor.visit(m_store);
visitor.visit(m_transaction);
visitor.visit(m_indexes);
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-keypath
@ -101,4 +103,54 @@ WebIDL::ExceptionOr<void> IDBObjectStore::set_name(String const& value)
return {};
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-createindex
WebIDL::ExceptionOr<GC::Ref<IDBIndex>> IDBObjectStore::create_index(String const& name, KeyPath key_path, IDBIndexParameters options)
{
auto& realm = this->realm();
// 1. Let transaction be this's transaction.
auto transaction = this->transaction();
// 2. Let store be this's object store.
auto store = this->store();
// 3. If transaction is not an upgrade transaction, throw an "InvalidStateError" DOMException.
if (transaction->mode() != Bindings::IDBTransactionMode::Versionchange)
return WebIDL::InvalidStateError::create(realm, "Transaction is not an upgrade transaction"_string);
// FIXME: 4. If store has been deleted, throw an "InvalidStateError" DOMException.
// 5. If transactions state is not active, then throw a "TransactionInactiveError" DOMException.
if (transaction->state() != IDBTransaction::TransactionState::Active)
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while creating index"_string);
// 6. If an index named name already exists in store, throw a "ConstraintError" DOMException.
if (store->index_set().contains(name))
return WebIDL::ConstraintError::create(realm, "An index with the given name already exists"_string);
// 7. If keyPath is not a valid key path, throw a "SyntaxError" DOMException.
if (!is_valid_key_path(key_path))
return WebIDL::SyntaxError::create(realm, "Key path is not valid"_string);
// 8. Let unique be optionss unique member.
auto unique = options.unique;
// 9. Let multiEntry be optionss multiEntry member.
auto multi_entry = options.multi_entry;
// 10. If keyPath is a sequence and multiEntry is true, throw an "InvalidAccessError" DOMException.
if (key_path.has<Vector<String>>() && multi_entry)
return WebIDL::InvalidAccessError::create(realm, "Key path is a sequence and multiEntry is true"_string);
// 11. Let index be a new index in store.
// Set indexs name to name, key path to keyPath, unique flag to unique, and multiEntry flag to multiEntry.
auto index = Index::create(realm, store, name, key_path, unique, multi_entry);
// 12. Add index to this's index set.
this->index_set().set(name, index);
// 13. Return a new index handle associated with index and this.
return IDBIndex::create(realm, index, *this);
}
}