Một thư viện database JSON lightweight với API mô phỏng MongoDB/Mongoose để dễ dàng fallback hoặc migrate project mà không cần cài MongoDB.
- Không cần database server
- Lưu dữ liệu trực tiếp vào file JSON
- API gần giống MongoDB/Mongoose
- Hỗ trợ TypeScript
- Hỗ trợ update operators:
$set$inc$unset$push$pull
- Hỗ trợ:
findfindOnefindByIdcreatesaveinsertManyupdateOnefindOneAndUpdatefindOneAndDeletedeleteOnedeleteManyexistscountDocuments
- Hỗ trợ
upsert - Hỗ trợ
new: true
npm install @zibot/dbconst { Database, createModel } = require("@zibot/db");
const db = new Database("./database.json");
const users = createModel(db, "users");await users.create({
name: "Alice",
age: 20,
});
await users.save({
name: "Bob",
});const results = await users.find({
age: 20,
});
console.log(results);const user = await users.findOne({
name: "Alice",
});
console.log(user);const user = await users.findById("1746867000");
console.log(user);await users.updateOne(
{ name: "Alice" },
{
$set: {
age: 25,
},
},
);const updated = await users.findOneAndUpdate(
{ name: "Alice" },
{
$inc: {
coins: 100,
},
},
{
new: true,
},
);
console.log(updated);await users.updateOne(
{
name: "Unknown",
},
{
$set: {
age: 18,
},
},
{
upsert: true,
},
);await users.deleteOne({
name: "Alice",
});await users.deleteMany({
banned: true,
});const deleted = await users.findOneAndDelete({
name: "Bob",
});
console.log(deleted);await users.insertMany([
{
name: "A",
},
{
name: "B",
},
]);const exists = await users.exists({
name: "Alice",
});
console.log(exists);const total = await users.countDocuments({
verified: true,
});
console.log(total);await users.updateOne(
{ name: "Alice" },
{
$set: {
age: 30,
},
},
);await users.updateOne(
{ name: "Alice" },
{
$inc: {
coins: 50,
},
},
);await users.updateOne(
{ name: "Alice" },
{
$unset: {
tempField: true,
},
},
);await users.updateOne(
{ name: "Alice" },
{
$push: {
tags: "admin",
},
},
);await users.updateOne(
{ name: "Alice" },
{
$pull: {
tags: "admin",
},
},
);import { Database, createModel } from "@zibot/db";
interface User {
_id?: string;
name: string;
age: number;
coins: number;
tags: string[];
}
const db = new Database("./database.json");
const users = createModel<User>(db, "users");
await users.updateOne(
{ name: "Alice" },
{
$inc: {
coins: 100,
},
},
);new Database(path?: string)| Method | Description |
|---|---|
create() |
Tạo document |
save() |
Lưu document |
insertMany() |
Thêm nhiều document |
find() |
Tìm nhiều document |
findOne() |
Tìm một document |
findById() |
Tìm theo _id |
updateOne() |
Update document |
findOneAndUpdate() |
Update và trả document |
findOneAndDelete() |
Xóa và trả document |
deleteOne() |
Xóa một document |
deleteMany() |
Xóa nhiều document |
exists() |
Kiểm tra tồn tại |
countDocuments() |
Đếm document |
@zibot/db được thiết kế cho:
- Discord bots
- Local projects
- Lightweight apps
- Fallback database
- Development/testing
- Offline storage
- Small APIs
Không phù hợp cho:
- Dữ liệu cực lớn
- Concurrent write cực cao
- Multi-server production
MIT