Coming from Mongoose
Mongoose is a data modelling library for Node: you define a schema and a model and work with the documents in your server code. MongoCamp starts from a declaration of the collection as well, but it gives you a typed, authenticated HTTP API and live data in the browser, without writing routes.
Side by side
| Mongoose | MongoCamp |
|---|---|
new Schema({ ... }) + model('Order', schema) | defineMongocampCollection({ name: 'orders', schema }) in server/mongocamp/orders.ts, with any Standard Schema library (Zod, Valibot, ArkType) |
InferSchemaType<typeof schema> | inferred automatically: useMongocampCollection('orders') returns typed documents in the browser and in server code |
| schema validators | the schema validates every write of the API (400 with issues), and becomes the $jsonSchema validator of MongoDB, which also checks other clients |
{ timestamps: true } | timestamps: true (also createdBy / updatedBy and nested paths) |
__v and optimisticConcurrency | versioned: true: an outdated write answers 409 with the stored document; the client sends the version by itself |
| soft-delete plugins | softDelete: true: hidden from every read, restore(), withDeleted for administrators, optional purge |
| history and audit plugins | audit: true: before and after of every write, history(id) |
pre('save'), pre('findOneAndUpdate') | before-hooks mongocamp:document:beforeInsert/Replace/Update/Delete, in a Nitro plugin |
| query middleware for tenancy | mongocamp:document:beforeRead scopes every read and every write target |
| per-document access in your own routes | owner: true: only the creator writes, reads 'own', 'all' or your function; foreign documents answer 404 |
index: true, unique: true, schema.index({ a: 1, b: -1 }) | indexes: ['a', ['a', '-b'], { fields: { number: 1 }, unique: true }], created at the start; unique indexes respect soft delete |
ref: 'Customer' + .populate('customer') | refs: { customerId: 'customers' } + find({ expand: ['customerId'] }): typed, one query per field and page, with the reader's rights on the customers |
Model in server code | useMongocampServerCollection('orders', { user: event }): the same rules as the API for the caller, or 'system' for jobs |
Model.watch() on the server | useMongocampLive('orders'): a live list in the browser, polling without a replica set |
Model.find() in a route you write | useMongocampCollection('orders').find({ filter: 'status:open' }) from the component, with users, roles and grants of the server |
One declaration
ts
// server/mongocamp/orders.ts
import { z } from 'zod'
export default defineMongocampCollection({
name: 'orders',
schema: z.object({
customer: z.string().min(1),
qty: z.int().min(1).default(1),
status: z.enum(['open', 'done']).default('open'),
}),
validation: 'error',
timestamps: { createdBy: true, updatedBy: true },
owner: { read: 'all' },
softDelete: true,
versioned: true,
audit: true,
})The type of an order now includes createdAt, updatedAt, createdBy, updatedBy, deletedAt? and _v; nobody added them to the schema, and no client can write them.
In a component
vue
<script setup lang="ts">
const orders = useMongocampCollection('orders')
const { documents, status } = useMongocampLive('orders', { filter: 'status:open' })
async function finish(order: (typeof documents.value)[number]) {
try {
// the version of the order goes with the change
await orders.update(order, { status: 'done' })
}
catch (error) {
if ((error as MongocampRequestError).isConflict) {
// someone else changed it: error.current is the stored order
}
}
}
</script>
<template>
<p>{{ status }}</p>
<ul>
<li v-for="order in documents" :key="documentId(order)">
{{ order.customer }} ({{ order.qty }})
<button v-if="orders.isMine(order)" @click="finish(order)">
done
</button>
</li>
</ul>
</template>No API route, no fetch wrapper, no type duplicated: the list updates when another user changes an order, and only the creator gets the button.
What stays different
- No model objects. Documents are plain objects; there is no
order.save(). The composable writes them. expandis one level. The customer of an order, not the customer's company too; no reverse lookups (the orders of a customer are a find) and no cascading deletes (a before-hook). For more, an aggregation with$lookup.- No transactions across requests. One request is one write; an import of many documents is all or nothing.
- Schemas describe stored documents. Values come as MongoDB stores them (
ObjectId,Date, whole numbers);z.objectdrops fields it does not know, usez.looseObjectto keep them. - Live updates need a replica set. Without one,
useMongocampLivepolls. A change of a referenced document (the customer) sends no event of the documents that refer to it (the orders).
See Declared Collections for every option.
