npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

blackcat.js-database

v1.0.6

Published

json datatabase driver for blackcat.js

Readme

🐈‍⬛ blackcat.js-database

npm version npm downloads license typescript

Một database nhẹ, typed và hỗ trợ autocomplete path cho TypeScript. blackcat.js-database cho phép bạn lưu trữ và truy vấn dữ liệu bằng dot notation với type inference mạnh mẽ trong IDE.

Xem tài liệu ở đây.


Tính năng

  • Autocomplete path trong IDE (user.profile.name)
  • Type-safe với TypeScript
  • Hỗ trợ nested object
  • API tương tự Array
  • Driver system (memory, file, custom storage)
  • Query helpers (find, filter, map, ...)
  • Hỗ trợ thao tác với number và array

Cài đặt

npm install blackcat.js-database

Khởi tạo Database

import { Database, JSONDriver } from "blackcat.js-database";

const db = new Database({
  driver: db = new JSONDriver({
    filePath: "./database.json"
  })
});

Sử dụng với TypeScript Schema

type Schema = {
  users: {
    id: number;
    name: string;
    coins: number;
  }[];
  settings: {
    prefix: string;
  };
};

const db = new Database<Schema>({
  driver: db = new JSONDriver({
    filePath: "./database.json"
  })
});

Autocomplete trong IDE:

"users"
"users.0.id"
"settings.prefix"

Lấy dữ liệu

await db.get("users");
await db.get("settings.prefix");

Lấy toàn bộ database

await db.get();

Lấy bằng điều kiện nhất định

const data = await database.findOne("users", {
  name: "username"
});

Kiểm tra key tồn tại

await db.has("users");

Gán giá trị

await db.set("settings.prefix", "!");

Cập nhật giá trị

await db.update("settings.prefix", "?");

Xóa dữ liệu

await db.delete("settings.prefix");

Xóa toàn bộ database

await db.deleteAll();

xóa bằng điều kiện nhất đinh

const data = await database.deleteMany("member.user.database", {
  name: "username"
})

Number Operations

Cộng giá trị

await db.add("users.0.coins", 10);

Trừ giá trị

await db.subtract("users.0.coins", 5);

Array Operations

Thêm phần tử

await db.push("users", {
  id: 1,
  name: "Alice",
  coins: 0
});

Hoặc

await db.push("numbers", 1, 2, 3);

Thay thế phần tử trong mảng

await db.pull("users", 0, {
  id: 2,
  name: "Bob",
  coins: 10
});

Xóa phần tử

await db.pop("users", 0);

Hoặc

await db.pop("users", [0, 2]);

Lấy phần tử ngẫu nhiên

await db.random("users");

Query API

Database hỗ trợ các method tương tự Array.

find

const user = await db.find((u) => u.id === 1);

filter

const admins = await db.filter((u) => u.role === "admin");

map

const names = await db.map((u) => u.name);

findIndex

const index = await db.findIndex((u) => u.id === 1);

some

await db.some((u) => u.active === true);

every

await db.every((u) => u.active === true);

Utilities

keys

await db.keys();
await db.keys("users");

values

await db.values();
await db.values("users");

size

await db.size();

Kiểm tra kiểu dữ liệu

await db.isTargetArray("users");
await db.isTargetNumber("users.0.coins");

Driver System

Database sử dụng driver system để lưu trữ dữ liệu.

Driver chịu trách nhiệm:

  • đọc dữ liệu
  • ghi dữ liệu
  • xóa dữ liệu

Memory Driver (ví dụ)

Driver lưu dữ liệu trong RAM.

import { Database, MemoryDriver } from "blackcat.js-database";

const db = new Database({
  driver: new MemoryDriver()
});

JSON Driver (ví dụ)

Driver lưu dữ liệu trong file json.

import { Database, JSONDriver } from "blackcat.js-database";

const database = new Database({
    driver: db = new JSONDriver({
      filePath: "./database.json"
    })
});

Mongo Driver (ví dụ)

Driver lưu dữ liệu trong mongoose.

import { Database, MongoDriver } from "blackcat.js-database";

const database = new Database({ 
    driver: new MongoDriver({
        mongourl: "mongourl",
        databaseName: "BlackCat-Club",
        collectionName: "database-test",
        onLoad: ({ db, collection, databaseName }) => {
            console.log(`✅ Đã kết nối với cơ sở dữ liệu: ${databaseName}`);
            console.log(`📦 Bộ sưu tập: ${collection.collectionName}`);
            // Ví dụ advanced:
            // db.command({ ping: 1 })
        },
        onError: ({ error, databaseName }) => {
            console.error(`❌ Không thể kết nối với cơ sở dữ liệu: ${databaseName}`);
            console.error(error);
        }
    }),
});

SQLite Driver (ví dụ)

Driver lưu dữ liệu trong SQLite.

import { Database, SQLiteDriver } from "blackcat.js-database";

const database = new Database({ 
    driver: new SQLiteDriver({
        filePath: "database.sqlite",
        table: "json_store"
    })
});

Tạo Custom Driver

Bạn có thể tạo driver riêng bằng cách implement DatabaseDriver.

import { DatabaseDriver } from "blackcat.js-database";

class MyDriver implements DatabaseDriver {
  async all() {
    return {};
  }

  async set(data) {
  }

  async delete() {
  }

}

Sử dụng:

const db = new Database({
  driver: new MyDriver()
});

Ví dụ hoàn chỉnh

import { Database, JSONDriver } from "blackcat.js-database";

const db = new Database({
    driver: db = new JSONDriver({
      filePath: "./database.json"
    })
});
 
await db.set("users", []);

await db.push("users", {
  id: 1,
  name: "Alice",
  coins: 100
});

await db.add("users.0.coins", 50);

const user = await db.get("users.0");

console.log(user);

License

MIT