Frameworks and runtimes
The SDK has no dependencies and uses the standard fetch, so it runs anywhere modern
JavaScript runs. Create the client once and reuse it.
Next.js (App Router)
Section titled “Next.js (App Router)”import { DriveDB } from '@faeizfurqan/drivedb';export const db = new DriveDB(process.env.DRIVEDB_URL!).db();
// app/api/leads/route.tsimport { db } from '@/lib/db';
export async function POST(request: Request) { const { name, email } = await request.json(); const { insertedId } = await db.collection('clients').insertOne({ name, email, status: 'Lead' }); return Response.json({ id: insertedId }, { status: 201 });}
export async function GET() { const leads = await db.collection('clients').find({ status: 'Lead' }).limit(50).toArray(); return Response.json(leads);}Keep DRIVEDB_URL a server-side environment variable (no NEXT_PUBLIC_ prefix).
Express
Section titled “Express”import express from 'express';import { DriveDB, DriveDBError } from '@faeizfurqan/drivedb';
const db = new DriveDB(process.env.DRIVEDB_URL!).db();const app = express();app.use(express.json());
app.get('/clients/:id', async (req, res, next) => { try { const client = await db.collection('clients').findOne({ _id: req.params.id }); client ? res.json(client) : res.sendStatus(404); } catch (error) { next(error); }});
app.use( (error: unknown, _req: express.Request, res: express.Response, _next: express.NextFunction) => { const status = error instanceof DriveDBError && error.status ? error.status : 500; res.status(status).json({ error: error instanceof Error ? error.message : 'Error' }); },);
app.listen(3000);Cloudflare Workers
Section titled “Cloudflare Workers”import { DriveDB } from '@faeizfurqan/drivedb';
export default { async fetch(request: Request, env: { DRIVEDB_URL: string }) { const db = new DriveDB(env.DRIVEDB_URL).db(); const count = await db.collection('clients').countDocuments({ status: 'Won' }); return Response.json({ won: count }); },};Store the connection string as a secret: wrangler secret put DRIVEDB_URL.
Deno and Bun
Section titled “Deno and Bun”import { DriveDB } from 'npm:@faeizfurqan/drivedb'; // Deno; in Bun: import { DriveDB } from '@faeizfurqan/drivedb'const db = new DriveDB(Deno.env.get('DRIVEDB_URL')!).db();Browsers
Section titled “Browsers”The API allows calls from any website (CORS), but anything in a web page is visible to its visitors. Only use a read-only connection string in browser code, and only for data you are happy to make public. Write through your own server instead.
<script type="module"> import { DriveDB } from 'https://esm.sh/@faeizfurqan/drivedb'; const db = new DriveDB('drivedb://kid_…:sk_…@your-app-domain/…').db(); // read-only! const items = await db.collection('menu').find({ available: true }).toArray();</script>Serverless tips
Section titled “Serverless tips”- Requests are plain HTTPS: there are no connection pools to warm up or close.
- Call
client.close()only if you opened change streams. - Each connection string allows 10 requests per second (bursts of 30). Batch writes with
insertManyorbulkWriterather than many single calls.
Made by Faeiz Furqan