Closed eranbnn closed 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
Thank you, I'll probably use lexicographic-integer-encoding
in my repo then 👍
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.