Create, read, update, delete
import { DriveDB } from '@faeizfurqan/drivedb';
// The connection string from "Connect an app", kept out of your code.const client = new DriveDB(process.env.DRIVEDB_URL!);const db = client.db();const clients = db.collection('clients');
// Createconst { insertedId } = await clients.insertOne({ name: 'Ada Lovelace', company: 'Analytical Engines', status: 'Lead', dealValue: 4800,});
// Readconst leads = await clients.find({ status: 'Lead' }).sort({ dealValue: -1 }).toArray();console.log(`${leads.length} leads; the biggest is ${leads[0]?.name}`);
// Updateawait clients.updateOne({ _id: insertedId }, { $set: { status: 'Won' }, $inc: { dealValue: 200 } });
// Deleteawait clients.deleteOne({ _id: insertedId });| Task | Method |
|---|---|
| Add documents | insertOne(doc), insertMany(docs) (up to 1,000, all or nothing) |
| Read | find(filter), findOne(filter), countDocuments(filter), distinct(field) |
| Change fields | updateOne(filter, update), updateMany(filter, update) |
| Replace a whole document | replaceOne(filter, doc) |
| Remove | deleteOne(filter), deleteMany(filter) |
| Change and return | findOneAndUpdate, findOneAndDelete |
Deleted records go to Recently deleted in the app, where people can restore them.
Typed collections
Section titled “Typed collections”Pass an interface to collection() to type filters, updates and results:
import { DriveDB } from '@faeizfurqan/drivedb';
// Describe your documents once; filters, updates and results are type-checked.interface Client { name: string; company?: string; status?: 'Lead' | 'Contacted' | 'Proposal' | 'Won' | 'Lost'; dealValue?: number; nextFollowUp?: string; // dates are ISO strings: "2026-03-01" email?: string;}
const db = new DriveDB(process.env.DRIVEDB_URL!).db();const clients = db.collection<Client>('clients');
const won = await clients.findOne({ status: 'Won' });if (won) { // won._id, won._createdAt and won._updatedAt are always there. console.log(won.name, won._id, won._createdAt);}Choosing ids
Section titled “Choosing ids”Leave _id out and one is generated. To use your own ids (an order number, a user id from your
auth system), set _id to a string of up to 64 letters, digits, - and _:
await orders.insertOne({ _id: 'order-10042', total: 59.9 });await orders.findOne({ _id: 'order-10042' });Inserting an _id that exists fails with DuplicateKey.
Made by Faeiz Furqan