Skip to content

Commit 176e535

Browse files
committed
feat(idb): Update to newer API
IDB DomString constants have been removed from browser API and replaced by raw String values. Remove IDB DOMString constants that are no more supported and lead to an UndefinedBehaviorError. Add these constants in a typesafe way.
1 parent ef1f303 commit 176e535

File tree

2 files changed

+129
-44
lines changed

2 files changed

+129
-44
lines changed

src/main/scala/org/scalajs/dom/idb.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package org.scalajs.dom
55
*/
66
object idb {
77
type Cursor = raw.IDBCursor
8-
@inline def Cursor = raw.IDBCursor
8+
@inline def CursorDirection = raw.IDBCursorDirection
99
type CursorWithValue = raw.IDBCursorWithValue
1010
type Database = raw.IDBDatabase
1111
type Environment = raw.IDBEnvironment
@@ -17,6 +17,6 @@ object idb {
1717
type OpenDBRequest = raw.IDBOpenDBRequest
1818
type Request = raw.IDBRequest
1919
type Transaction = raw.IDBTransaction
20-
@inline def Transaction = raw.IDBTransaction
20+
@inline def TransactionMode = raw.IDBTransactionMode
2121
type VersionChangeEvent = raw.IDBVersionChangeEvent
2222
}

src/main/scala/org/scalajs/dom/raw/Idb.scala

+127-42
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,45 @@ package org.scalajs.dom.raw
1111

1212
import scala.scalajs.js
1313
import scala.scalajs.js.annotation._
14+
import scala.scalajs.js.|
15+
16+
/**
17+
* IndexedDB transaction mode
18+
* Provides constants for IDB Transaction modes
19+
* These constants have been removed from browser support
20+
* and replaced by String values
21+
*/
22+
@js.native
23+
sealed trait IDBTransactionMode extends js.Any
24+
25+
object IDBTransactionMode {
26+
27+
/**
28+
* Allows data to be read but not changed.
29+
* It is the default transaction mode.
30+
*
31+
* MDN
32+
*/
33+
val READ_ONLY = "readonly".asInstanceOf[IDBTransactionMode]
34+
35+
/**
36+
* Allows any operation to be performed, including ones that delete and create object
37+
* stores and indexes. This mode is for updating the version number of transactions
38+
* that were started using the setVersion() method of IDBDatabase objects.
39+
* Transactions of this mode cannot run concurrently with other transactions.
40+
*
41+
* MDN
42+
*/
43+
val VERSION_CHANGE = "versionchange".asInstanceOf[IDBTransactionMode]
44+
45+
/**
46+
* Allows reading and writing of data in existing data stores to be changed.
47+
*
48+
* MDN
49+
*/
50+
val READ_WRITE = "readwrite".asInstanceOf[IDBTransactionMode]
51+
52+
}
1453

1554
/**
1655
* The IDBObjectStore interface of the IndexedDB API represents an object store in
@@ -86,8 +125,25 @@ class IDBObjectStore extends js.Object {
86125
*/
87126
def put(value: js.Any, key: js.Any = js.native): IDBRequest = js.native
88127

89-
def openCursor(range: js.Any = js.native,
90-
direction: String = js.native): IDBRequest = js.native
128+
/**
129+
* The method sets the position of the cursor to the appropriate record,
130+
* based on the specified direction.
131+
*
132+
* MDN
133+
*/
134+
def openCursor(range: js.UndefOr[IDBKeyRange | js.Any] = js.undefined,
135+
direction: js.UndefOr[
136+
IDBCursorDirection] = js.undefined): IDBRequest = js.native
137+
138+
/**
139+
* The method sets the position of the cursor to the appropriate key,
140+
* based on the specified direction.
141+
*
142+
* MDN
143+
*/
144+
def openKeyCursor(range: js.UndefOr[IDBKeyRange | js.Any] = js.undefined,
145+
direction: js.UndefOr[
146+
IDBCursorDirection] = js.undefined): IDBRequest = js.native
91147

92148
/**
93149
* Note that this method must be called only from a VersionChange transaction mode
@@ -113,6 +169,32 @@ class IDBObjectStore extends js.Object {
113169
*/
114170
def get(key: js.Any): IDBRequest = js.native
115171

172+
/**
173+
* If a value is successfully found, then a structured clone of it is created and set as
174+
* the result of the request object.
175+
*
176+
* MDN
177+
*/
178+
def getAll(query: js.UndefOr[IDBKeyRange | js.Any] = js.undefined,
179+
count: js.UndefOr[Int] = js.undefined): IDBRequest = js.native
180+
181+
/**
182+
* If a value is successfully found, then a structured clone of it is created and set as
183+
* the result of the request object.
184+
*
185+
* MDN
186+
*/
187+
def getAllKeys(query: js.UndefOr[IDBKeyRange | js.Any] = js.undefined,
188+
count: js.UndefOr[Int] = js.undefined): IDBRequest = js.native
189+
190+
/**
191+
* If a value is successfully found, then a structured clone of it is created and set as
192+
* the result of the request object.
193+
*
194+
* MDN
195+
*/
196+
def getKey(key: js.Any): IDBRequest = js.native
197+
116198
/**
117199
* returns an IDBRequest object, and, in a separate thread, deletes the current
118200
* object store.
@@ -221,7 +303,7 @@ class IDBIndex extends js.Object {
221303
* MDN
222304
*/
223305
def openKeyCursor(range: IDBKeyRange = js.native,
224-
direction: String = js.native): IDBRequest = js.native
306+
direction: IDBCursorDirection = js.native): IDBRequest = js.native
225307

226308
/**
227309
* Returns an IDBRequest object, and, in a separate thread, finds either the value in
@@ -239,7 +321,7 @@ class IDBIndex extends js.Object {
239321
* MDN
240322
*/
241323
def openCursor(range: IDBKeyRange = js.native,
242-
direction: String = js.native): IDBRequest = js.native
324+
direction: IDBCursorDirection = js.native): IDBRequest = js.native
243325
}
244326

245327
/**
@@ -273,7 +355,7 @@ class IDBCursor extends js.Object {
273355
*
274356
* MDN
275357
*/
276-
def direction: String = js.native
358+
def direction: IDBCursorDirection = js.native
277359

278360
/**
279361
* Returns the key for the record at the cursor's position. If the cursor is outside its
@@ -327,13 +409,45 @@ class IDBCursor extends js.Object {
327409
}
328410

329411
@js.native
330-
@JSGlobal
331-
object IDBCursor extends js.Object {
412+
sealed trait IDBCursorDirection extends js.Any
413+
414+
object IDBCursorDirection {
415+
416+
/**
417+
* The cursor shows all records, including duplicates.
418+
* It starts at the upper bound of the key range and moves downwards
419+
* (monotonically decreasing in the order of keys).
420+
*
421+
* MDN
422+
*/
423+
val PREV = "prev".asInstanceOf[IDBCursorDirection]
332424

333-
val PREV: String = js.native
334-
val PREV_NO_DUPLICATE: String = js.native
335-
val NEXT: String = js.native
336-
val NEXT_NO_DUPLICATE: String = js.native
425+
/**
426+
* The cursor shows all records, excluding duplicates.
427+
* If multiple records exist with the same key, only the first one iterated is retrieved.
428+
* It starts at the upper bound of the key range and moves downwards.
429+
*
430+
* MDN
431+
*/
432+
val PREV_UNIQUE = "prevunique".asInstanceOf[IDBCursorDirection]
433+
434+
/**
435+
* The cursor shows all records, including duplicates.
436+
* It starts at the lower bound of the key range and moves upwards
437+
* (monotonically increasing in the order of keys).
438+
*
439+
* MDN
440+
*/
441+
val NEXT = "next".asInstanceOf[IDBCursorDirection]
442+
443+
/**
444+
* The cursor shows all records, excluding duplicates.
445+
* If multiple records exist with the same key, only the first one iterated is retrieved.
446+
* It starts at the lower bound of the key range and moves upwards.
447+
*
448+
* MDN
449+
*/
450+
val NEXT_UNIQUE = "nextunique".asInstanceOf[IDBCursorDirection]
337451
}
338452

339453
/**
@@ -486,7 +600,7 @@ class IDBTransaction extends EventTarget {
486600
*
487601
* MDN
488602
*/
489-
def mode: String = js.native
603+
def mode: IDBTransactionMode = js.native
490604

491605
/**
492606
* The error returned in the event of an unsuccessful transaction. Null if the
@@ -533,35 +647,6 @@ class IDBTransaction extends EventTarget {
533647
def objectStore(name: String): IDBObjectStore = js.native
534648
}
535649

536-
@js.native
537-
@JSGlobal
538-
object IDBTransaction extends js.Object {
539-
540-
/**
541-
* Allows data to be read but not changed.
542-
*
543-
* MDN
544-
*/
545-
val READ_ONLY: String = js.native
546-
547-
/**
548-
* Allows any operation to be performed, including ones that delete and create object
549-
* stores and indexes. This mode is for updating the version number of transactions
550-
* that were started using the setVersion() method of IDBDatabase objects.
551-
* Transactions of this mode cannot run concurrently with other transactions.
552-
*
553-
* MDN
554-
*/
555-
val VERSION_CHANGE: String = js.native
556-
557-
/**
558-
* Allows reading and writing of data in existing data stores to be changed.
559-
*
560-
* MDN
561-
*/
562-
val READ_WRITE: String = js.native
563-
}
564-
565650
/**
566651
* The IDBDatabase interface of the IndexedDB API provides asynchronous access
567652
* to a connection to a database. Use it to create, manipulate, and delete
@@ -648,7 +733,7 @@ class IDBDatabase extends EventTarget {
648733
* MDN
649734
*/
650735
def transaction(storeNames: js.Any,
651-
mode: String = js.native): IDBTransaction = js.native
736+
mode: IDBTransactionMode = js.native): IDBTransaction = js.native
652737

653738
/**
654739
* As with createObjectStore, this method can be called only within a versionchange

0 commit comments

Comments
 (0)