@omegajs/sub-encoder
v1.0.0
Published
Facilitates the creation of subsidiary encodings for key/value data storage systems.
Downloads
3
Maintainers
Readme
Omega Sub-Encoder
@omegajs/sub-encoder
Facilitates the creation of subsidiary encodings for key/value data storage systems.
Install Via L1FE's NPM
npm config set registry https://npm.l1fe.tech
npm install @omegajs/sub-encoder
Install Via L1FE's Git Repository
git clone https://lab.l1fe.tech/omega/sub-encoder.git
cd sub-encoder
npm install
Usage
const enc = new SubEncoder()
const subA = enc.sub('sub-a')
const subB = enc.sub('sub-b', 'binary') // subs support custom key encodings
await odb.put('k1', 'b')
await odb.put('a1', 'a1', { keyEncoding: subA })
await odb.put('b1', 'b1', { keyEncoding: subB })
// k1 as normal
await odb.get('k1')
// a1 from the sub
await odb.get('a1', { keyEncoding: subA })
// also supports read streams
for await (const node of odb.createReadStream({ keyEncoding: subA })) {
// Iterates everything in the A sub
}
// The range options will be encoded properly too
for await (const node of odb.createReadStream({ lt: 'b2' }, { keyEncoding: subB })) {
}
API
const enc = new SubEncoder([prefix, encoding])
Create a new SubEncoder. Optionally set the initial prefix and encoding.
prefix
can be string or Buffer. If set, acts as an initial sub instead of starting at the ODatabase's base level.
subEnc = enc.sub(prefix, [encoding])
Make a new sub. Returns a SubEncoder, so subs can easily be nested.
prefix
can be string or Buffer.
buf = enc.encode(key)
Encode a key.
key = enc.decode(buf)
Decode a key.
encodedRange = enc.encodeRange(range)
Encode a range.
License
Apache-2.0