@yydb/light-db
v0.0.0
Published
LightDB Storage Engine - A high-performance TypeScript database storage layer
Maintainers
Readme
LightDB Storage Engine 🚀
A high-performance TypeScript database storage layer with WAL (Write-Ahead Logging), indexes, and transaction support.
Features ✨
- High Performance: Optimized for speed with batch writing and columnar storage
- Transaction Support: ACID-compliant operations
- Indexing: B+ tree indexes for fast querying
- Write-Ahead Logging: Ensures data durability
- Recovery: Automatic recovery from crashes
- Memory-Mapped Files: Efficient I/O operations
- TypeScript Ready: Full TypeScript support with type definitions
Installation 📦
pnpm add @yydb/light-dbQuick Start 🚀
import { LightDB } from '@yydb/light-db';
// Initialize the database
const db = await LightDB.open('./mydb');
// Create a collection
const users = db.collection('users');
// Insert a document
await users.insert({ id: 1, name: 'John Doe', email: '[email protected]' });
// Find a document
const user = await users.findOne({ id: 1 });
console.log(user); // { id: 1, name: 'John Doe', email: '[email protected]' }
// Update a document
await users.update({ id: 1 }, { $set: { name: 'John Smith' } });
// Delete a document
await users.delete({ id: 1 });
// Close the database
await db.close();API Reference 📚
LightDB Class
open(path: string): Promise<LightDB>
Opens an existing database or creates a new one at the specified path.
collection(name: string): Collection
Returns a collection with the given name. Creates it if it doesn't exist.
transaction(): Promise<Transaction>
Starts a new transaction.
close(): Promise<void>
Closes the database connection.
Collection Class
insert(document: Document): Promise<void>
Inserts a new document into the collection.
find(query: Query): Promise<Document[]>
Finds all documents matching the query.
findOne(query: Query): Promise<Document | null>
Finds the first document matching the query.
update(query: Query, update: Update): Promise<number>
Updates documents matching the query and returns the number of updated documents.
delete(query: Query): Promise<number>
Deletes documents matching the query and returns the number of deleted documents.
createIndex(field: string): Promise<void>
Creates an index on the specified field for faster queries.
Transaction Class
commit(): Promise<void>
Commits the transaction.
rollback(): Promise<void>
Rolls back the transaction.
Performance Features ⚡
Batch Writing
LightDB uses batch writing to optimize disk I/O operations, significantly improving write performance for bulk operations.
Columnar Storage
For certain data types, LightDB uses columnar storage to reduce disk space and improve query performance.
Memory-Mapped Files
LightDB uses memory-mapped files for efficient I/O operations, reducing the overhead of traditional file I/O.
Architecture 🏗️
LightDB consists of several key components:
- Storage Engine: Manages the physical storage of data
- WAL (Write-Ahead Log): Ensures data durability and supports recovery
- Index Manager: Manages B+ tree indexes for fast querying
- Collection Layer: Provides a document-oriented API
- Transaction Manager: Handles ACID-compliant transactions
Recovery 🔄
LightDB automatically recovers from crashes using the WAL. When the database is opened after a crash, it replays the WAL to restore the database to a consistent state.
Contributing 🤝
Contributions are welcome! Please feel free to submit a Pull Request.
License 📄
LightDB is licensed under the MPL-2.0 License. See the LICENSE file for details.
