@lazycatcloud/minidb
TypeScript icon, indicating that this package has built-in type declarations

0.0.11 • Public • Published

minidb

minidb 目前只有一个远程db的实现,使用 lzcinit 中 minidb 作为后端服务。

import { MiniDB } from "@lazycatcloud/minidb";
const db = new MiniDB();
const collection = db.getCollection("awesome");

下面是一个 minidb 支持的查询语句例子

import { MiniDB } from "@lazycatcloud/minidb";
const db = new MiniDB();
const collection = db.getCollection("example");

async function mock() {
  const docs = [...Array(100)].map((_, index) => {
    const doc = {
      index,
      name: `name-${index}`,
    };
    if (index == 99) {
      doc.last = true;
    }
    return doc;
  });
  return collection.upsert(docs);
}

async function main() {
  await mock();

  // 根据 $in 查询, sort 默认升序
  await collection
    .find({ index: { $in: [0, 1, 2, 3, 4, 5] } }, { sort: ["index"] })
    .fetch()
    .then((res) => {
      res.forEach((e, index) => {
        console.assert(e.index === index, "output element index error");
      });
    });

  // 根据 $eq 查询, 查询对应的元素等于对应的值
  await collection.findOne({ index: 5 }).then((res) => {
    console.assert(res.index === 5);
  });
  await collection.findOne({ index: { $eq: 5 } }).then((res) => {
    console.assert(res.index === 5);
  });

  // 根据 $gt 查询大于指定条件的数据
  await collection
    .find({ index: { $gt: 90 } }, { sort: ["index"] })
    .fetch()
    .then((res) => {
      console.assert(res.length === 9);
      res.forEach((r) => {
        console.assert(r.index > 90);
      });
    });

  // 根据 $gte 查询大于等于指定条件的数据
  await collection
    .find({ index: { $gte: 90 } }, { sort: ["index"] })
    .fetch()
    .then((res) => {
      console.assert(res.length === 10);
      res.forEach((r) => {
        console.assert(r.index >= 90);
      });
    });

  // 根据 $lt 查询小于指定条件的数据
  await collection
    .find({ index: { $lt: 9 } }, { sort: ["index"] })
    .fetch()
    .then((res) => {
      console.assert(res.length === 9);
      res.forEach((r) => {
        console.assert(r.index < 9);
      });
    });

  // 根据 $lte 查询小于等于指定条件的数据
  await collection
    .find({ index: { $lte: 9 } }, { sort: ["index"] })
    .fetch()
    .then((res) => {
      console.assert(res.length === 10);
      res.forEach((r) => {
        console.assert(r.index <= 9);
      });
    });

  // 结合 $gte 和 $lte 一起使用,获取范围内的数据
  await collection
    .find({ index: { $lte: 10, $gte: 5 } }, { sort: ["index"] })
    .fetch()
    .then((res) => {
      console.assert(res.length === 6);
      res.forEach((r) => {
        console.assert(r.index <= 10 && r.index >= 5);
      });
    });

  // 根据 $ne 查询不等于某个值的数据, $ne 这个目前有 bug 先不要用
  await collection
    .find({ index: { $lte: 10, $gte: 5, $ne: 6 } }, { sort: ["index"] })
    .fetch()
    .then((res) => {
      console.assert(res.length === 5);
      res.forEach((r) => {
        console.assert(r.index <= 10 && r.index >= 5 && r.index !== 6);
      });
    });

  // 根据 $exists 查询存在某一个字段
  await collection
    .find({ $exists: "last" }, { sort: ["index"] })
    .fetch()
    .then((res) => {
      console.assert(res.length === 1);
      res.forEach((r) => {
        console.assert(r.index == 99);
      });
    });

  // 根据 $like 查询匹配的数据
  await collection
    .find({ name: { $like: `name-1` } }, { sort: ["index"] })
    .fetch()
    .then((res) => {
      console.assert(res.length === 11);
      res.forEach((r) => {
        console.assert(r.index == 1 || (r.index >= 10 && r.index <= 19));
      });
    });

  // $like 中还可以使用正则表达式来匹配
  await collection
    .find({ name: { $like: `name-1[5678]` } }, { sort: ["index"] })
    .fetch()
    .then((res) => {
      console.assert(res.length === 4);
      res.forEach((r) => {
        console.assert(r.index >= 15 && r.index <= 18);
      });
    });

  await db.removeCollection("example");
}

main();

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i @lazycatcloud/minidb

      Weekly Downloads

      19

      Version

      0.0.11

      License

      ISC

      Unpacked Size

      39.3 kB

      Total Files

      26

      Last publish

      Collaborators

      • dyh1226
      • sugartoys
      • hanzhenfang
      • tzmax
      • squarefong
      • 178me
      • evanmeek
      • jeesk
      • luhuaei
      • fkxxyz
      • cursorzz
      • snyh