Skip to content

Repository files navigation

@perryts/mongodb

Pure-TypeScript MongoDB wire-protocol driver. Runs on Node.js 22+ and Bun, and compiles to a native binary via Perry (LLVM AOT). Zero runtime dependencies — BSON, OP_MSG framing, SCRAM auth, TLS, SDAM, transactions, change streams, and GridFS are all implemented in hand-written TypeScript.

Install

npm install @perryts/mongodb
# or
bun add @perryts/mongodb

Usage

import{MongoClient,ObjectId}from'@perryts/mongodb';constclient=awaitMongoClient.connect('mongodb+srv://user:pw@cluster.example.net/app',);constusers=client.db('app').collection('users');// High-level CRUD helpersconstdoc=awaitusers.findOne({_id: newObjectId(id)});awaitusers.insertMany([{name: 'alice'},{name: 'bob'}]);awaitusers.updateOne({name: 'alice'},{$set: {active: true}});// Low-level: any MongoDB command, wire-format passthroughconstres=awaitclient.db('app').command({find: 'users',filter: {status: 'active'},limit: 10,});// Sessions + transactionsconstsession=client.startSession();awaitsession.withTransaction(async()=>{awaitusers.updateOne({_id: a},{$inc: {balance: -100}},{ session });awaitusers.updateOne({_id: b},{$inc: {balance: 100}},{ session });});// Change streamsforawait(constchangeofusers.watch()){console.log(change.operationType,change.fullDocument);}awaitclient.close();

Compared to the official mongodb driver

  • Pure TypeScript, zero runtime deps. No bson, no mongodb-connection-string-url, no native addons.
  • Perry AOT compilable. Same source compiles to a static native binary via the Perry toolchain.
  • Runs on Node 22+ and Bun. No fallback shims.
  • MongoDB 5.0+ only. OP_MSG wire protocol only; no OP_QUERY.
  • No CSFLE (client-side field level encryption). Deferred until a pure-TS mongocrypt replacement exists.
  • No zstd/snappy compression. zlib only (OP_COMPRESSED).

Benchmarks

10 operations, the same MongoDB 7.0 instance over loopback, mongo's official sample_mflix / sample_analytics / sample_training fixtures. Every cell verifies cross-driver equivalence by hashing the first iteration's result — all 7 cells produced byte-identical results across all 10 operations.

Latency, mean ms / op (lower is better):

opbun mongodbbun mongoosebun perrytsnode mongodbnode mongoosenode perrytsperry-aot perryts
findOne_by_id_movie0.700.460.350.350.520.260.24
find_movies sort+limit_5012.0510.359.1010.4610.498.638.49
count_grades_score_gte_8035.2535.0829.5534.6132.5928.8028.62
distinct_movies_genres_year_20008.147.856.787.2210.176.766.60
aggregate_lookup_customer0.851.310.810.941.330.650.63
aggregate_movies_group_by_year14.2413.3811.1913.8415.5010.9311.45
cursor_all_zips (~30k docs)83.5882.1380.7172.6097.6892.57n/a
insertMany_10007.079.698.697.758.697.2339.00
updateMany_inc_touches35.9338.3028.6540.2334.8929.3428.20
deleteMany_100013.1715.8710.2516.0811.1410.5566.90

@perryts/mongodb is the fastest (or tied-fastest) driver on 8 of 9 applicable ops under Bun and Node, and on 5 of 9 under Perry-AOT. The remaining gap is on bulk-write paths under Perry-AOT (insertMany_1000, deleteMany_1000) — these are slower than the official C-extension driver because Perry-AOT's per-method-call dispatch on BufferWriter primitives is currently ~10× heavier than V8's IC-cached path. That's a Perry codegen concern, not an algorithmic one; the same source under Bun and Node clears the official driver on the same workload.

cursor_all_zips is intentionally skipped on Perry-AOT — the ~30k-document sequential getMore chain currently runs on a 1ms-resolution clock there, which produces noisy quantized samples. It will be brought back when Perry exposes performance.now() or process.hrtime.

The full harness — three runtimes × three drivers × ten operations, with cross-cell result verification — lives in bench/. To reproduce locally:

mongosh # mongo://127.0.0.1:27017 with sample_mflix / sample_analytics / sample_training loadedcd bench && bash scripts/matrix.sh
bun run summarize.ts

License

MIT

About

Pure-TypeScript MongoDB wire-protocol driver. Runs on Node.js, Bun, and compiles to a single-file native binary via Perry. Zero native dependencies.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages