// Copyright (c) 2017-2025 Cloudflare, Inc. // Licensed under the Apache 2.0 license found in the LICENSE file or at: // https://opensource.org/licenses/Apache-2.0 #pragma once #include "actor-state.h" #include #include #include namespace workerd::api { // Synchronous KV storage. Available as ctx.storage.kv on SQLite-backed DOs. class SyncKvStorage: public jsg::Object { public: SyncKvStorage(jsg::Ref storage): storage(kj::mv(storage)) {} struct ListOptions { jsg::Optional start; jsg::Optional startAfter; jsg::Optional end; jsg::Optional prefix; jsg::Optional reverse; jsg::Optional limit; JSG_STRUCT(start, startAfter, end, prefix, reverse, limit); JSG_STRUCT_TS_OVERRIDE(SyncKvListOptions); // Rename from SyncKvStorageListOptions }; jsg::JsValue get(jsg::Lock& js, kj::String key); JSG_ITERATOR_TYPE(ListIterator, jsg::JsArray, IoOwn, listNext); jsg::Ref list(jsg::Lock& js, jsg::Optional options); void put(jsg::Lock& js, kj::String key, jsg::JsValue value); kj::OneOf delete_(jsg::Lock& js, kj::String key); JSG_RESOURCE_TYPE(SyncKvStorage) { JSG_METHOD(get); JSG_METHOD(list); JSG_METHOD(put); JSG_METHOD_NAMED(delete, delete_); JSG_TS_OVERRIDE({ get(key: string): T | undefined; list(options?: SyncKvStorageListOptions): Iterable<[string, T]>; put(key: string, value: T): void; delete(key: string): boolean; }); } private: jsg::Ref storage; SqliteKv& getSqliteKv(jsg::Lock& js) { return storage->getSqliteKv(js); } static kj::Maybe listNext(jsg::Lock& js, IoOwn& state); }; #define EW_SYNC_KV_ISOLATE_TYPES api::SyncKvStorage, api::SyncKvStorage::ListOptions, \ api::SyncKvStorage::ListIterator, api::SyncKvStorage::ListIterator::Next } // namespace workerd::api