|
| 1 | +# @blite/client |
| 2 | + |
| 3 | +TypeScript/Node.js client for [BLite](https://github.com/EntglDb/BLite.Server) — the embedded document database server. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @blite/client |
| 9 | +``` |
| 10 | + |
| 11 | +## Quick start |
| 12 | + |
| 13 | +```ts |
| 14 | +import { BLiteClient, BsonId } from '@blite/client'; |
| 15 | + |
| 16 | +const client = new BLiteClient({ |
| 17 | + host: 'localhost', |
| 18 | + port: 2626, // default gRPC port |
| 19 | + apiKey: 'your-api-key', |
| 20 | + useTls: true, // set false only for local dev |
| 21 | +}); |
| 22 | + |
| 23 | +const users = client.getCollection('users'); |
| 24 | + |
| 25 | +// Insert |
| 26 | +const id = await users.insert({ name: 'Alice', age: 30, active: true }); |
| 27 | +console.log(id.toString()); // hex ObjectId or string, etc. |
| 28 | + |
| 29 | +// Find by ID |
| 30 | +const doc = await users.findById(id); |
| 31 | + |
| 32 | +// Query (fluent builder) |
| 33 | +const results = await users.query() |
| 34 | + .whereEq('active', true) |
| 35 | + .whereGte('age', 18) |
| 36 | + .orderBy('name') |
| 37 | + .skip(0).take(20) |
| 38 | + .toArray(); |
| 39 | + |
| 40 | +// Stream large result sets |
| 41 | +for await (const doc of users.query().whereEq('status', 'pending').execute()) { |
| 42 | + console.log(doc); |
| 43 | +} |
| 44 | + |
| 45 | +// Update |
| 46 | +await users.update(id, { ...doc, age: 31 }); |
| 47 | + |
| 48 | +// Delete |
| 49 | +await users.delete(id); |
| 50 | +``` |
| 51 | + |
| 52 | +## Typed collection |
| 53 | + |
| 54 | +Use a `CollectionMapper<T>` to map between a TypeScript type and `BsonDocument`. This gives you compile-time safety on read/write paths. |
| 55 | + |
| 56 | +```ts |
| 57 | +import { BLiteClient, CollectionMapper, BsonId, BsonDocument } from '@blite/client'; |
| 58 | + |
| 59 | +interface User { |
| 60 | + id?: BsonId; |
| 61 | + name: string; |
| 62 | + email: string; |
| 63 | + age: number; |
| 64 | +} |
| 65 | + |
| 66 | +const userMapper: CollectionMapper<User> = { |
| 67 | + collectionName: 'users', |
| 68 | + getId(u) { return u.id!; }, |
| 69 | + toDocument(u): BsonDocument { |
| 70 | + return { _id: u.id?.toString(), name: u.name, email: u.email, age: u.age }; |
| 71 | + }, |
| 72 | + fromDocument(doc): User { |
| 73 | + return { |
| 74 | + id: doc._id ? BsonId.fromString(String(doc._id)) : undefined, |
| 75 | + name: String(doc.name), |
| 76 | + email: String(doc.email), |
| 77 | + age: Number(doc.age), |
| 78 | + }; |
| 79 | + }, |
| 80 | +}; |
| 81 | + |
| 82 | +const client = new BLiteClient({ host: 'localhost', apiKey: 'key' }); |
| 83 | +const users = client.getTypedCollection(userMapper); |
| 84 | + |
| 85 | +const id = await users.insert({ name: 'Bob', email: 'bob@example.com', age: 25 }); |
| 86 | +const user = await users.findById(id); // → User | null |
| 87 | +const list = await users.query().whereGt('age', 20).toArray(); // → User[] |
| 88 | +``` |
| 89 | + |
| 90 | +## Transactions |
| 91 | + |
| 92 | +```ts |
| 93 | +const tx = await client.beginTransaction(); |
| 94 | +try { |
| 95 | + await col.insert({ key: 'a' }, tx); |
| 96 | + await col.insert({ key: 'b' }, tx); |
| 97 | + await tx.commit(); |
| 98 | +} catch { |
| 99 | + await tx.rollback(); |
| 100 | +} |
| 101 | + |
| 102 | +// Or with AsyncDisposable (Node.js 18.2+, TypeScript 5.2+): |
| 103 | +await using tx = await client.beginTransaction(); |
| 104 | +await col.insert({ key: 'a' }, tx); |
| 105 | +await col.insert({ key: 'b' }, tx); |
| 106 | +await tx.commit(); |
| 107 | +``` |
| 108 | + |
| 109 | +## Indexes |
| 110 | + |
| 111 | +```ts |
| 112 | +// BTree index (supports range queries) |
| 113 | +await users.createIndex('email', { unique: true }); |
| 114 | + |
| 115 | +// Vector index (HNSW) |
| 116 | +await products.createVectorIndex('embedding', 1536, { metric: 'Cosine' }); |
| 117 | + |
| 118 | +// Vector similarity search |
| 119 | +for await (const doc of products.vectorSearch(queryEmbedding, { k: 10 })) { |
| 120 | + console.log(doc); |
| 121 | +} |
| 122 | + |
| 123 | +// Spatial index |
| 124 | +await locations.createSpatialIndex('coordinates'); |
| 125 | +``` |
| 126 | + |
| 127 | +## Change Data Capture (CDC) |
| 128 | + |
| 129 | +```ts |
| 130 | +// Stream all write events for a collection |
| 131 | +for await (const event of users.watch(true /* capture payload */)) { |
| 132 | + console.log(event.operation, event.documentId.toString(), event.payload); |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +## Key-Value store |
| 137 | + |
| 138 | +```ts |
| 139 | +const kv = client.kv; |
| 140 | + |
| 141 | +await kv.set('session:abc', Buffer.from(JSON.stringify({ userId: 1 })), 3_600_000); // 1 h TTL |
| 142 | +const buf = await kv.get('session:abc'); |
| 143 | +const keys = await kv.scanKeys('session:'); |
| 144 | + |
| 145 | +// Batch |
| 146 | +await kv.batch((b) => { |
| 147 | + b.set('k1', Buffer.from('v1')); |
| 148 | + b.set('k2', Buffer.from('v2'), 60_000); |
| 149 | + b.delete('k3'); |
| 150 | +}); |
| 151 | +``` |
| 152 | + |
| 153 | +## Admin API |
| 154 | + |
| 155 | +```ts |
| 156 | +import { BLiteOperation } from '@blite/client'; |
| 157 | + |
| 158 | +const admin = client.admin; |
| 159 | + |
| 160 | +// Create a user with scoped permissions |
| 161 | +const apiKey = await admin.createUser('bob', { |
| 162 | + permissions: [ |
| 163 | + { collection: 'orders', ops: BLiteOperation.Query | BLiteOperation.Insert }, |
| 164 | + ], |
| 165 | +}); |
| 166 | + |
| 167 | +// Rotate key |
| 168 | +const newKey = await admin.rotateKey('bob'); |
| 169 | + |
| 170 | +// Tenant provisioning (multi-tenant mode) |
| 171 | +await admin.provisionTenant('tenant-acme'); |
| 172 | +await admin.deprovisionTenant('tenant-old', /* deleteFiles */ true); |
| 173 | +``` |
| 174 | + |
| 175 | +## Schema management |
| 176 | + |
| 177 | +```ts |
| 178 | +// Define a schema (enforced on write) |
| 179 | +await users.setSchema([ |
| 180 | + { name: 'name', typeCode: 0x02 /* String */, nullable: false }, |
| 181 | + { name: 'age', typeCode: 0x10 /* Int32 */, nullable: true }, |
| 182 | + { name: 'email', typeCode: 0x02 /* String */, nullable: false }, |
| 183 | +], 'UserSchema'); |
| 184 | + |
| 185 | +const info = await users.getSchema(); |
| 186 | +console.log(info.title, info.version, info.fields); |
| 187 | +``` |
| 188 | + |
| 189 | +## Time series |
| 190 | + |
| 191 | +```ts |
| 192 | +await metrics.configureTimeSeries('timestamp', 7 * 24 * 3_600_000); // 7-day retention |
| 193 | +const info = await metrics.getTimeSeriesInfo(); |
| 194 | +await metrics.forcePrune(); // prune expired documents on demand |
| 195 | +``` |
| 196 | + |
| 197 | +## Configuration reference |
| 198 | + |
| 199 | +| Option | Type | Default | Description | |
| 200 | +|--------|------|---------|-------------| |
| 201 | +| `host` | string | `'localhost'` | Server hostname | |
| 202 | +| `port` | number | `2626` | gRPC port | |
| 203 | +| `apiKey` | string | **required** | API key sent as `x-api-key` | |
| 204 | +| `useTls` | boolean | `true` | Enable TLS | |
| 205 | +| `address` | string | — | Full address override; skips host/port/useTls | |
| 206 | + |
| 207 | +## Key-map refresh |
| 208 | + |
| 209 | +BLite uses a compact binary format (C-BSON) where field names are replaced by 2-byte IDs. |
| 210 | +The client keeps a local cache of `fieldName ↔ ID` mappings and registers new fields automatically on every write. |
| 211 | + |
| 212 | +If you need to read documents written by other clients before making any writes yourself, pre-populate the cache: |
| 213 | + |
| 214 | +```ts |
| 215 | +await client.refreshKeyMap('users'); // any collection you have Query access to |
| 216 | +``` |
| 217 | + |
| 218 | +## Notes |
| 219 | + |
| 220 | +- **Node.js 18+** required (AsyncDisposable, `Buffer.writeBigInt64LE`, ES2020 target). |
| 221 | +- **int64** values in filter predicates: pass as `bigint` for values outside `[-2^53, 2^53]`. |
| 222 | +- **Decimal128** fields are decoded as a raw string representation for lossless round-trip; no arithmetic support. |
| 223 | +- The TypeScript client uses `DynamicService` only; `DocumentService` (typed BSON path with CLR type hints) is a C#-only concept. |
| 224 | + |
| 225 | +## License |
| 226 | + |
| 227 | +AGPL-3.0 © EntglDb |
0 commit comments