Key encoding bad behaviour
See original GitHub issueThe main function of the library, which is exported when you use require('levelup')
have different behaviour if you use the option keyEncoding
instead of valueEncoding
, at least in the Json case.
I have forked the repository to help but I dont know if the problem it’s in the documentation or in the core code, so I just warn.
This is a really simple code to repeat the bug:
var levelup = require('levelup')
var db_a = levelup('./database_a',{ keyEncoding:'json' }),
db_b = levelup('./database_b',{ valueEncoding:'json' }),
t = {'a':1, 'b':{ 'a':'text', 'b':'more_text' }, 'c':3 };
db_a.put('name', t, function (err) {
if (err) return console.log('Ooops!', err);
db_a.get('name', function (err, value) {
if (err) return console.log('Ooops!', err);
console.log('db_a:');
console.log('typeof value = ',typeof value);
console.log(value);
})
});
db_b.put('name', t, function (err) {
if (err) return console.log('Ooops!', err);
db_b.get('name', function (err, value) {
if (err) return console.log('Ooops!', err);
console.log('db_b:');
console.log('typeof value = ',typeof value);
console.log(value);
})
});
And this is the output:
db_b:
typeof value = object
{ a: 1, b: { a: 'text', b: 'more_text' }, c: 3 }
db_a:
typeof value = string
[object Object]
Cheers.
Issue Analytics
- State:
- Created 10 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Memory (Encoding, Storage, Retrieval) - Noba Project
Good encoding techniques include relating new information to what one already knows, forming mental images, and creating associations among information that ...
Read more >How to change json encoding behaviour for serializable ...
Implementing that could be done by checking for any dict key that is called 'date' and contains a valid string representation of a...
Read more >Encoding/decoding model of communication - Wikipedia
The Encoding/Decoding model of communication was first developed by cultural studies scholar ... Decoding behavior without using words, displays non verbal communication.
Read more >Valence encoding in the amygdala influences motivated ...
Valence encoding in the amygdala influences motivated behavior ... between the positive and negative encoding neurons such that presentation ...
Read more >Controlling the cache key - Amazon CloudFront
Use cache policies to control the cache key for cache behaviors in your ... The cache key doesn't include other values that were...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The reason
db_a
returns[object Object]
instead of your object is that when you don’t specifyvalueEncoding: 'json'
levelUp will convert whatever value you pass todb#put
into a leveldb compatible format by callingval.toString()
instead ofJSON.stringify(val)
. If you want to use json values, always passvalueEncoding: 'json'
.I will try make some minimal changes to README.md and then make a pull requests. If you dont like it, just reject, no hassle. I know it can be a little fussy and the ‘key’, ‘value’ should be clear enought but I think it can help others.
Regards.