NeDB Storage
NeDB is a key-value storage library that provides an ordered mapping from string keys to string values. It is an embedded persistent or in-memory database for Node.js, nw.js, Electron, and browsers. Written in 100% JavaScript with no binary dependencies.
Overview
The Namespace Wrapper provides a standardized interface to NeDB, ensuring consistent data handling.
Core Database Operations
Getting Database Instance
- TypeScript
- JavaScript
import { namespaceWrapper } from "@_koii/namespace-wrapper";
// Getting Database Instance
const db: any = await namespaceWrapper.getDB(); // Type it based on the actual database interface
const { namespaceWrapper } = require("@_koii/namespace-wrapper");
// Getting Database Instance
const db = await namespaceWrapper.getDB(); // Type it based on the actual database interface
Storing Data
TypeScript
await namespaceWrapper.storeSet("userCount", "42");
await namespaceWrapper.storeSet(
"config",
JSON.stringify({
taskName: "ImageProcessing",
version: "1.0.0",
settings: { maxRetries: 3, timeout: 5000 },
}),
);
Retrieving Data
- TypeScript
- JavaScript
const count: string | null = await namespaceWrapper.storeGet("userCount");
console.log("User count:", count);
const configStr: string | null = await namespaceWrapper.storeGet("config");
const config = JSON.parse(configStr!);
console.log("Task name:", config.taskName);
const count = await namespaceWrapper.storeGet("userCount");
console.log("User count:", count);
Advanced Database Operations
Querying Data
- TypeScript
- JavaScript
const db: any = await namespaceWrapper.getDB();
const allDocs = await db.find({});
const activeTasks = await db.find({ status: "active" });
const task = await db.findOne({ taskId: "123" });
const db = await namespaceWrapper.getDB();
const allDocs = await db.find({});
Inserting Data
await db.insert({ taskId: "123", status: "active", timestamp: Date.now() });
await db.insert([
{ taskId: "124", status: "pending" },
{ taskId: "125", status: "completed" },
]);
Updating Data
await db.update({ taskId: "123" }, { $set: { status: "completed" } });
await db.update(
{ status: "pending" },
{ $set: { status: "active" } },
{ multi: true },
);
Removing Data
await db.remove({ taskId: "123" });
await db.remove({ status: "completed" }, { multi: true });
Indexing
try {
const db = await namespaceWrapper.getDB();
// Ensure 'taskId' is indexed and unique
await db.ensureIndex({ fieldName: "taskId", unique: true });
// Ensure 'status' is indexed (not unique)
await db.ensureIndex({ fieldName: "status" });
console.log("Indexes successfully created!");
} catch (error) {
console.error("Error creating indexes:", error);
}
Best Practices
Error Handling
try {
await namespaceWrapper.storeSet("key", "value");
} catch (error) {
console.error("Database operation failed:", error);
}
Data Validation
- TypeScript
- JavaScript
function validateData(data: { taskId: string }) {
if (!data.taskId || typeof data.taskId !== "string") {
throw new Error("Invalid taskId");
}
}
try {
const data = { taskId: "123" };
validateData(data);
await db.insert(data);
} catch (error) {
console.error("Validation failed:", error);
}
function validateData(data) {
if (!data.taskId || typeof data.taskId !== "string") {
throw new Error("Invalid taskId");
}
}
try {
const data = { taskId: "123" };
validateData(data);
await db.insert(data); // Assuming `db.insert` supports async/await
} catch (error) {
console.error("Validation failed:", error);
}
Atomic Operations
await db.update({ counter: { $lt: 10 } }, { $inc: { counter: 1 } });
Indexing Strategy
await db.ensureIndex({ fieldName: "timestamp" });
await db.ensureIndex({ fieldName: "taskId", status: 1 });
Common Patterns
Caching Results
- TypeScript
- JavaScript
let cachedData: any[] | null = null;
let cacheTimestamp: number = 0;
async function getCachedData(): Promise<any[]> {
const now = Date.now();
if (!cachedData || now - cacheTimestamp > 60000) {
cachedData = await db.find({ status: "active" });
cacheTimestamp = now;
}
return cachedData;
}
let cachedData = null;
let cacheTimestamp = 0;
async function getCachedData() {
const now = Date.now();
if (!cachedData || now - cacheTimestamp > 60000) {
cachedData = await db.find({ status: "active" });
cacheTimestamp = now;
}
return cachedData;
}
Batch Operations
- TypeScript
- JavaScript
async function batchInsert(records: any[]): Promise<void> {
const batchSize = 100;
for (let i = 0; i < records.length; i += batchSize) {
const batch = records.slice(i, i + batchSize);
await db.insert(batch);
}
}
async function batchInsert(records) {
const batchSize = 100;
for (let i = 0; i < records.length; i += batchSize) {
const batch = records.slice(i, i + batchSize);
await db.insert(batch);
}
}
Data Migration
- TypeScript
- JavaScript
async function migrateData(): Promise<void> {
const oldRecords = await db.find({ version: "1.0" });
for (const record of oldRecords) {
const newRecord = transformRecord(record);
await db.update({ _id: record._id }, { $set: newRecord });
}
}
async function migrateData() {
const oldRecords = await db.find({ version: "1.0" });
for (const record of oldRecords) {
const newRecord = transformRecord(record);
await db.update({ _id: record._id }, { $set: newRecord });
}
}
Troubleshooting
Database Corruption
- TypeScript
- JavaScript
async function repairDatabase(): Promise<void> {
const db: any = await namespaceWrapper.getDB();
await db.loadDatabase();
console.log("Database reloaded");
}
async function repairDatabase() {
const db = await namespaceWrapper.getDB();
await db.loadDatabase();
console.log("Database reloaded");
}
Performance Issues
const startTime = Date.now();
const results = await db.find({});
const duration = Date.now() - startTime;
if (duration > 100) {
console.warn(`Slow query detected: ${duration}ms`);
}
Next Steps
To learn more about specific features, check out these guides:
- File System - Handle files and directories.
- Blockchain/Transaction Operations - Work with blockchain and transaction operations.
- Task Status - Get task state information with namespace methods.
- Network/Task Handling - Manage network data and tasks.
- Audit and Distribution - Manage network data and tasks.