jakearchibald / idb

IndexedDB, but with promises
https://www.npmjs.com/package/idb
ISC License
6.39k stars 361 forks source link

Data put not same as data retrieved #164

Closed han-so1omon closed 4 years ago

han-so1omon commented 4 years ago

Issue

I am inputting some data to an IDB table. However, upon getting it, the data is not the same. Specifically it does not contain the functions from a prototype object.

Code

healthService = NewHealthClient('http://localhost:12345')
await db.put('health', healthService, 1)
let hsCheck = await db.get('health', 1)
console.log(healthService, hsCheck)

Output


//-----------------------------------------------------------------
// Object 1 : Before putting in idb
client_: Object { a: "text", c: false, b: false }
​
hostname_: "http://localhost:50051"
​
<prototype>: {…}
​​
check: function check(request, metadata, callback)
​​
constructor: function HealthClient(hostname, credentials, options)
​​
watch: function watch(request, metadata)
​​
<prototype>: Object { … }

{…}

//-----------------------------------------------------------------
// Object 2 : After getting from idb
​
client_: Object { a: "text", c: false, b: false }
​
hostname_: "http://localhost:50051"
​
<prototype>: {…}
​​
__defineGetter__: function __defineGetter__()
​​
__defineSetter__: function __defineSetter__()
​​
__lookupGetter__: function __lookupGetter__()
​​
__lookupSetter__: function __lookupSetter__()
​​
__proto__: 
​​
constructor: function Object()
​​
hasOwnProperty: function hasOwnProperty()
​​
isPrototypeOf: function isPrototypeOf()
​​
propertyIsEnumerable: function propertyIsEnumerable()
​​
toLocaleString: function toLocaleString()
​​
toSource: function toSource()
​​
toString: function toString()
​​
valueOf: function valueOf()
​​
<get __proto__()>: function __proto__()
​​
<set __proto__()>: __proto__()
​```
jakearchibald commented 4 years ago

IndexedDB stores data via structured cloning, and doesn't walk to prototype chain and cannot store functions.

This isn't an issue with this library, it's how indexedDB works.

It'll never be able to store things like functions. Imagine if the function had a closure to a document, or a webgl context or a worker etc etc, how would that be stored?