0.0.21 • Published 4 years ago

@couchplus/collator v0.0.21

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Introduction

CouchPlus:Collator is a javascript library used to organize complex data on top of key value store. It works great as key encoder for FoundationDB and LevelUP. It can encode data such as null, boolean, number, string, array, object and undefined. It can also organize data in ascending, descending and mixed order.

By default, it order data as follows;

asc  - null
asc  - false
asc  - true
asc  - NaN
asc  - number
asc  - string
asc  - array
asc  - object
asc  - undefined
desc - undefined
desc - object
desc - array
desc - string
desc - number
desc - NaN
desc - true
desc - false
desc - null

Usage (FoundationDB)

In order to use this library with FoundationDB, you will first need to;

npm install @couchplus/collator foundationdb msgpack-lite

msgpack-lite is used for value encoding in this example, feel free to use any other encoder.

After that, you can start using it as follows;

const collator = require("@couchplus/collator").default;
const foundationdb = require("foundationdb");
const msgpack = require("msgpack-lite");
foundationdb.setAPIVersion(620);

(async function main() {
  const keyEncoding = collator;
  const valueEncoding = {
    pack: msgpack.encode.bind(msgpack),
    unpack: msgpack.decode.bind(msgpack),
  };

  const database = (await foundationdb.open())
    .withKeyEncoding(keyEncoding)
    .withValueEncoding(valueEncoding);

  await database.doTransaction(async (tn) => {
    // Use scalar data as key
    tn.set(false, { ok: true });
    tn.set(true, { ok: true });
    tn.set(NaN, { ok: true });
    tn.set(-Infinity, { ok: true });
    tn.set(-Number.MAX_SAFE_INTEGER, { ok: true });
    tn.set(-1234, { ok: true });
    tn.set(0, { ok: true });
    tn.set(1234, { ok: true });
    tn.set(Number.MAX_SAFE_INTEGER, { ok: true });
    tn.set(Infinity, { ok: true });
    tn.set("sample-key", { ok: true });

    // Use array as key (used in most cases)
    tn.set(["user", 1], { ok: true }); // namespace
    tn.set(["user", 1, "comments", collator.order.desc(Date.now())]); // mixed ordering
  });
})().catch(console.error);

Usage (LevelUP)

In order to use this library with LevelUP, you will first need to;

npm install @couchplus/collator levelup leveldown encoding-down msgpack-lite

msgpack-lite is used for value encoding at this example, feel free to use any other encoder.

After that, you can start using it as follows;

const collator = require("@couchplus/collator").default;
const encode = require("encoding-down");
const leveldown = require("leveldown");
const levelup = require("levelup");
const msgpack = require("msgpack-lite");

(async function main() {
  const keyEncoding = collator;
  const valueEncoding = {
    type: "msgpack",
    buffer: true,
    decode: msgpack.decode.bind(msgpack),
    encode: msgpack.encode.bind(msgpack),
  };
  const encoding = { keyEncoding, valueEncoding };
  const database = levelup(encode(leveldown("data"), encoding));

  // Use scalar data as key
  await database.put(false, { ok: true });
  await database.put(true, { ok: true });
  await database.put(NaN, { ok: true });
  await database.put(-Infinity, { ok: true });
  await database.put(-Number.MAX_SAFE_INTEGER, { ok: true });
  await database.put(-1234, { ok: true });
  await database.put(0, { ok: true });
  await database.put(1234, { ok: true });
  await database.put(Number.MAX_SAFE_INTEGER, { ok: true });
  await database.put(Infinity, { ok: true });
  await database.put("sample-key", { ok: true });

  // Use array as key (used in most cases)
  await database.put(["user", 1], { ok: true }); // namespace
  await database.put(["user", 1, "comments", collator.order.desc(Date.now())]); // mixed ordering
})().catch(console.error);

License

MIT License

Copyright (c) 2020 ENDY JASMI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
0.0.21

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.18

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.12

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.15

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago