Re: [w3c/IndexedDB] Prebuilt IndexedDB format (#224)

Given that `putAll()` is no longer in development (https://github.com/w3c/IndexedDB/issues/69#issuecomment-898619783), I like the idea of directly streaming JSON into an object store. As mentioned before, this won't handle Dates, but in retrospect I think this may be an edge case. My hunch is that most folks are storing objects in IndexedDB that are fully serializable as JSON. (I have no data though. :grinning:)

Something like this?

```js
const reader = (await (await fetch('./array.json')).body).getReader()
const writer = db.getWriter('my_object_store') // WritableStream
await reader.pipeTo(writer)
```

`db.getWriter()` could assume a `'readwrite'` transaction, and it could take an options bag like `db.transaction()`:

```js
const writer = db.getWriter('my_object_store', { durability: 'relaxed' })
```

Working with the existing `db.transaction()` is also possible, but it may be weird to get the microtask timing right, given that we are reading data from the network. Also making it more high-level gives more flexibility to the engine to optimize stuff (maybe?).

If we wanted to support Dates in the future, we could also always have an optional transform or something:

```js
const writer = db.getWriter('my_object_store', { transform: item => {
  // transform a single item from the JSON array
  item.date = new Date(item.date) // parse string to Date
  return item
}})
```

(Maybe something like [module blocks](https://github.com/tc39/proposal-js-module-blocks) could even make it so that this `transform` function can run off-main-thread.)

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/IndexedDB/issues/224#issuecomment-903817561

Received on Monday, 23 August 2021 14:19:41 UTC