c95bc4cd44
- Add mcp.md to: bg, fa, ru, tr, zh, ar - Add index.md to Bulgarian (bg) - Add 24 missing German (de) documentation files Translations for all supported languages now complete.
6.1 KiB
6.1 KiB
Client SDKs
BaraDB bietet offizielle Client-Bibliotheken für JavaScript/TypeScript, Python, Nim und Rust.
JavaScript / TypeScript
Installation
npm install baradb
# oder
yarn add baradb
Grundlegende Verwendung
import { Client } from 'baradb';
const client = new Client('localhost', 9472);
await client.connect();
// Einfache Abfrage
const result = await client.query('SELECT name, age FROM users WHERE age > 18');
console.log(result.rows);
// Parametrisierte Abfrage
const result2 = await client.query(
'SELECT * FROM users WHERE name = ?',
['Alice']
);
// Batch-Einfügung
await client.batch([
"INSERT users { name := 'Alice', age := 30 }",
"INSERT users { name := 'Bob', age := 25 }",
]);
// Transaktionen
await client.begin();
await client.query("INSERT orders { total := 100 }");
await client.query("UPDATE users SET balance = balance - 100 WHERE name = 'Alice'");
await client.commit();
await client.close();
Konkurrierende Abfragen
Der JavaScript-Client serialisiert konkurrierende Anfragen über eine einzelne TCP-Verbindung automatisch via interner Request-Queue. Sie können sicher mehrere parallele Operationen starten — ihre Binärframes werden sich nicht auf dem Wire überlappen:
const [users, orders, stats] = await Promise.all([
client.query('SELECT * FROM users'),
client.query('SELECT * FROM orders'),
client.query('SELECT count(*) FROM visits')
]);
WebSocket-Streaming
import { WebSocketClient } from 'baradb/ws';
const ws = new WebSocketClient('ws://localhost:9471');
ws.onMessage = (data) => console.log(data);
await ws.connect();
await ws.send('SUBSCRIBE updates');
Python
Installation
pip install baradb
Grundlegende Verwendung
from baradb import Client
client = Client("localhost", 9472)
client.connect()
# Einfache Abfrage
result = client.query("SELECT name, age FROM users WHERE age > 18")
for row in result:
print(row["name"], row["age"])
# Parametrisierte Abfrage
result = client.query(
"SELECT * FROM users WHERE name = ?",
["Alice"]
)
# Batch-Operationen
client.batch([
"INSERT users { name := 'Alice', age := 30 }",
"INSERT users { name := 'Bob', age := 25 }",
])
# Context Manager (auto-close)
with Client("localhost", 9472) as c:
result = c.query("SELECT count(*) FROM users")
print(result[0]["count"])
Async Client
import asyncio
from baradb import AsyncClient
async def main():
client = AsyncClient("localhost", 9472)
await client.connect()
result = await client.query("SELECT * FROM users")
print(result.rows)
await client.close()
asyncio.run(main())
Nim (Embedded Mode)
Abhängigkeit hinzufügen
# In Ihrer .nimble Datei
requires "barabadb >= 0.1.0"
Embedded-Verwendung
import barabadb/storage/lsm
import barabadb/storage/btree
import barabadb/vector/engine
import barabadb/graph/engine
# Key-Value Store
var db = newLSMTree("./data")
db.put("user:1", cast[seq[byte]]("Alice"))
let (found, value) = db.get("user:1")
db.close()
# B-Tree Index
var btree = newBTreeIndex[string, int]()
btree.insert("Alice", 30)
let ages = btree.get("Alice")
# Vector Search
var idx = newHNSWIndex(dimensions = 128)
idx.insert(1, @[0.1'f32, 0.2, 0.3], {"category": "A"}.toTable)
let results = idx.search(@[0.1'f32, 0.2, 0.3], k = 10)
# Graph
var g = newGraph()
let alice = g.addNode("Person", {"name": "Alice"}.toTable)
let bob = g.addNode("Person", {"name": "Bob"}.toTable)
discard g.addEdge(alice, bob, "knows")
let path = g.shortestPath(alice, bob)
Client-Bibliothek
import barabadb/client/client
var c = newBaraClient("localhost", 9472)
c.connect()
let result = c.query("SELECT name FROM users")
for row in result.rows:
echo row["name"]
c.close()
Rust
Abhängigkeit hinzufügen
[dependencies]
baradb = "0.1"
tokio = { version = "1", features = ["full"] }
Grundlegende Verwendung
use baradb::Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = Client::connect("localhost:9472").await?;
let result = client
.query("SELECT name, age FROM users WHERE age > 18")
.await?;
for row in result.rows {
println!("{} is {} years old", row["name"], row["age"]);
}
client.close().await?;
Ok(())
}
HTTP/REST (Sprachunabhängig)
Alle Sprachen können die HTTP/REST API direkt verwenden:
# Abfrage
curl -X POST http://localhost:9470/api/query \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"query": "SELECT * FROM users WHERE age > 18"}'
# Einfügen
curl -X POST http://localhost:9470/api/query \
-H "Content-Type: application/json" \
-d '{"query": "INSERT users { name := \"Alice\", age := 30 }"}'
# Schema
curl http://localhost:9470/api/schema
# Health
curl http://localhost:9470/health
# Metrics
curl http://localhost:9470/metrics
Connection Pooling
Alle offiziellen Clients unterstützen Connection Pooling:
JavaScript
import { Pool } from 'baradb';
const pool = new Pool({
host: 'localhost',
port: 9472,
min: 5,
max: 50,
idleTimeout: 30000,
});
const client = await pool.acquire();
try {
const result = await client.query('SELECT 1');
} finally {
pool.release(client);
}
Python
from baradb import Pool
pool = Pool("localhost", 9472, min_size=5, max_size=50)
with pool.connection() as conn:
result = conn.query("SELECT 1")
Datentyp-Mapping
| BaraDB Type | JavaScript | Python | Nim | Rust |
|---|---|---|---|---|
null |
null |
None |
nil |
Option::None |
bool |
boolean |
bool |
bool |
bool |
int8/16/32/64 |
number |
int |
int |
i8/i16/i32/i64 |
float32/64 |
number |
float |
float32/float64 |
f32/f64 |
str |
string |
str |
string |
String |
bytes |
Uint8Array |
bytes |
seq[byte] |
Vec<u8> |
array |
Array |
list |
seq |
Vec |
object |
Object |
dict |
Table |
HashMap |
vector |
Float32Array |
list[float] |
seq[float32] |
Vec<f32> |