The MongoDB utility wraps the official mongodb driver in a small typed client. Each collection's shape is defined once with Zod in schema.ts, and a generator script writes typed methods for it, such as db.findUserById and db.updateSession, so queries and results are checked by TypeScript without an ORM. It lives in src/lib/server/mongodb, so SvelteKit keeps it out of the client bundle, and the code is copied into your project like every PlainSv item.
Install the item, then set MONGODB_URI in .env to your connection string, including the database name. The client connects when the module is first imported.
pnx remote0 add https://plainsv.com/r/mongodb.jsonMONGODB_URI="mongodb://localhost:27017/my-app"The utility has three files, and only schema.ts needs editing by hand.
src/lib/server/mongodb/schema.tsdb, client, database, and ObjectId from src/lib/server/mongodb/index.tssrc/lib/server/mongodb/generate.ts to rewrite the collection methods in index.tsAdd a key to dbCollectionsSchema with a Zod object. The key is the collection name, and every document needs a string _id. DbCollectionsSchema infers the TypeScript type of each collection from these schemas.
export const dbCollectionsSchema = {
user: z.object({
_id: z.string(),
email: z.string(),
image: z.string(),
createdAt: z.date(),
updatedAt: z.date(),
}),
};Run the generator with Bun after you add, rename, or remove a collection. It replaces everything between the //<COLLECTIONS> and //</COLLECTIONS> comments in index.ts, so keep those comments in place. Format the file with Prettier afterwards.
bun src/lib/server/mongodb/generate.tspnx prettier --write src/lib/server/mongodb/index.tsEach collection gets these methods, named after the collection, such as user:
createUser and createUsersfindUser, findUsers, and findUserByIdupdateUser, updateUsers, and updateUserByIddeleteUser, deleteUsers, and deleteUserByIduserAggregateCollections whose name starts with app, such as appData, only get createAppData, findAppData, and updateAppData, since they hold single config documents.
Call the generated methods from server code, such as a +page.server.ts load function, a +server.ts endpoint, or a function handler. Filters and updates are typed against the collection schema, and create methods fill in _id with a new ObjectId string when you leave it out.
import { db } from "#lib/server/mongodb/index.js";
export const load = async ({ locals }) => {
const user = await db.findUserById(locals.userData.id);
await db.updateUserById(locals.userData.id, { $set: { updatedAt: new Date() } });
return { email: user?.email };
};Pass return in the options to load only some fields. The result type narrows to the fields you pick.
const user = await db.findUserById(id, { return: { email: true } });Use database for anything the generated methods do not cover, such as indexes, transactions, or change streams. It is the Db instance from the official driver, and client is the connected MongoClient.
import { database } from "#lib/server/mongodb/index.js";
await database.collection("user").createIndex({ email: 1 }, { unique: true });Auth stores sessions with this utility in src/lib/server/auth/session.ts, and its callback routes create users. It uses the user and session collections, so keep both in schema.ts when you use auth.
/** Connected MongoDB client */
const client: MongoClient;
/** Default database from `MONGODB_URI` */
const database: Db;
/** Typed methods for every collection in `dbCollectionsSchema` */
const db: { findUserById(id: string, options?: QueryOptions<"user">): Promise<DbCollectionsSchema["user"] | null> /* ... */ };/** Zod schema for every collection */
const dbCollectionsSchema: Record<string, z.ZodType>;
/** TypeScript type of every collection, inferred from the schemas */
type DbCollectionsSchema = { [K in keyof typeof dbCollectionsSchema]: z.infer<(typeof dbCollectionsSchema)[K]> };