Level / awesome

An open list of awesome Level modules and resources.
MIT License
294 stars 22 forks source link

add level-ordered #52

Closed eranbnn closed 4 years ago

eranbnn commented 4 years ago

I've implemented a wrapper for level that simplifies my use case as it keeps inserted items ordered, and grants easy access to the last inserted item.

vweevers commented 4 years ago

FWIW, there's a simpler way that doesn't require maintaining state and uses lexicographic order (so that key 10 sorts after 2):

const mlts = require('monotonic-lexicographic-timestamp')()
const db = level('./db')

await db.put(mlts(), 'first')
await db.put(mlts(), 'second')

See also

const level = require('level')
const lexint = require('lexicographic-integer-encoding')('hex')

const db = level('./db', { keyEncoding: lexint })

await db.put(2, 'example')
await db.put(10, 'example')

// Without our encoding, the keys would sort as 10, 2.
db.createKeyStream().on('data', console.log) // 2, 10
eranbnn commented 4 years ago

Thank you, I'll probably use lexicographic-integer-encoding in my repo then 👍