# moonbase
**Repository Path**: EchoDerk/moonbase
## Basic Information
- **Project Name**: moonbase
- **Description**: MoonBase - A lightweight embedded database kernel written in MoonBit
- **Primary Language**: Unknown
- **License**: Apache-2.0
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2026-10-13
- **Last Updated**: 2026-10-16
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# MoonBase
**A lightweight embedded database kernel written in MoonBit.**
MoonBase is a from-scratch embedded SQL database engine implementing core relational database concepts: recursive-descent SQL parser, B-tree storage engine with range scans, query executor with WHERE evaluation, ORDER BY / LIMIT, DELETE with index rebuild, and transaction support via write-ahead logging.
This project demonstrates that MoonBit is capable of systems programming traditionally done in C/Rust — memory-safe database kernels with predictable performance.
---
## Architecture
```
moonbase/
├── src/
│ ├── types/ Core type system (DataType, Value, Row, Column, Expr, Statement)
│ ├── sql/ Hand-written recursive-descent SQL parser (tokenizer + parser)
│ ├── btree/ B-tree index engine (insert, point lookup, range scan, node splitting)
│ ├── storage/ Table manager (schema + B-tree index + row store)
│ ├── executor/ Query executor (CREATE, INSERT, SELECT, DELETE, projection, sort)
│ └── tx/ Transaction manager (BEGIN, COMMIT, ROLLBACK with WAL)
└── cmd/moonbase-cli/ CLI demo
```
## Features
| Component | Feature | Status |
|---|---|---|
| **SQL Parser** | `CREATE TABLE`, `INSERT`, `SELECT`, `DELETE` | ✅ |
| **SQL Parser** | `WHERE` with `= <> < <= > >= AND OR` | ✅ |
| **SQL Parser** | `ORDER BY
[ASC\|DESC]`, `LIMIT n` | ✅ |
| **SQL Parser** | Multi-row `INSERT ... VALUES (...), (...)` | ✅ |
| **Storage** | B-tree index (order-4 to order-N configurable) | ✅ |
| **Storage** | Point lookup by primary key | ✅ |
| **Storage** | Range scan `[start, end]` | ✅ |
| **Storage** | Node splitting (leaf + internal) | ✅ |
| **Storage** | `replace_rows` — rebuilds the primary index after DELETE | ✅ |
| **Executor** | Full-table `SELECT *` | ✅ |
| **Executor** | Column projection `SELECT col1, col2` | ✅ |
| **Executor** | Column-name resolution against the schema | ✅ |
| **Executor** | `DELETE FROM t [WHERE ...]` (index rebuild) | ✅ |
| **Executor** | Mixed-type comparison (Int / Double / Text / Bool / Null) | ✅ |
| **Executor** | Code-point-wise text comparison (see note below) | ✅ |
| **Tx** | `BEGIN` / `COMMIT` / `ROLLBACK` | ✅ |
| **Tx** | Write-ahead log (in-memory) | ✅ |
| **Tests** | 35 unit tests across all packages | ✅ |
## Repository
- GitHub(主仓库):https://github.com/Derk2006/moonbase
- 码云 Gitee(镜像):https://gitee.com/EchoDerk/moonbase
## Quick Start
```bash
# Run the test suite
moon test
# Build the CLI demo (JavaScript backend, runs on Node.js)
moon build --target js cmd/moonbase-cli
node _build/js/debug/build/cmd/moonbase-cli/moonbase-cli.js
# WebAssembly backend is also supported
moon build --target wasm cmd/moonbase-cli
```
## Example
```sql
CREATE TABLE users (id INT PRIMARY KEY, name TEXT NOT NULL);
INSERT INTO users (id, name) VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie');
SELECT name FROM users WHERE id = 2;
-- name
-- Bob
-- (1 row)
SELECT * FROM users ORDER BY name DESC LIMIT 2;
-- id name
-- 3 Charlie
-- 1 Alice
-- (2 row(s))
DELETE FROM users WHERE id = 2;
-- Deleted 1 row(s) from 'users'.
```
## CLI demo output
```
> SELECT * FROM users ORDER BY name DESC
id name
3 Charlie
1 Alice
2 Bob
(3 row(s))
> DELETE FROM users WHERE id = 2
Deleted 1 row(s) from 'users'.
```
## Design Highlights
- **Zero external dependencies**: Pure MoonBit, no FFI to SQLite or other engines
- **B-tree from scratch**: Leaf/internal node splitting, leaf-level linked list for range scans
- **Type-safe SQL AST**: `Statement` enum with `Select`/`Insert`/`Create`/`Delete` variants
- **Schema-aware evaluation**: `src/executor/query.mbt` resolves column references by name, so
predicates and ORDER BY work on any column — not just the primary key
- **Reindex-on-write**: DELETE rebuilds the primary-key B-tree so point lookups stay consistent
## Note on text ordering
The MoonBit standard library orders strings by **length first**, then lexicographically
(`"Alice".compare("Bob") == 1`). That contradicts SQL semantics, where `'Alice' < 'Bob'`.
MoonBase therefore implements its own code-point-wise comparison
(`@executor.text_compare`) and uses it for text predicates and `ORDER BY`, with regression
tests covering the difference.
## License
Apache-2.0