Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions browser.js

This file was deleted.

37 changes: 2 additions & 35 deletions docs/browser.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,4 @@
# Mongoose in the Browser

Mongoose supports creating schemas and validating documents in the browser.
Mongoose's browser library does **not** support saving documents, [queries](http://mongoosejs.com/docs/queries.html), [populate](http://mongoosejs.com/docs/populate.html), [discriminators](http://mongoosejs.com/docs/discriminators.html), or any other Mongoose feature other than schemas and validating documents.

Mongoose has a pre-built bundle of the browser library. If you're bundling your code with [Webpack](https://webpack.js.org/), you should be able to import Mongoose's browser library as shown below if your Webpack `target` is `'web'`:

```javascript
import mongoose from 'mongoose';
```

You can use the below syntax to access the Mongoose browser library from Node.js:

```javascript
// Using `require()`
const mongoose = require('mongoose/browser');

// Using ES6 imports
import mongoose from 'mongoose/browser';
```

## Using the Browser Library {#usage}

Mongoose's browser library is very limited. The only use case it supports is validating documents as shown below.

```javascript
import mongoose from 'mongoose';

// Mongoose's browser library does **not** have models. It only supports
// schemas and documents. The primary use case is validating documents
// against Mongoose schemas.
const doc = new mongoose.Document({}, new mongoose.Schema({
name: { type: String, required: true }
}));
// Prints an error because `name` is required.
console.log(doc.validateSync());
```
As of Mongoose 9, [Mongoose's browser build is now in the `@mongoosejs/browser` npm package](https://github.com/mongoosejs/mongoose-browser).
The documentation has been moved to the [`@mongoosejs/browser` REAME](https://github.com/mongoosejs/mongoose-browser?tab=readme-ov-file#mongoosejsbrowser).
1 change: 0 additions & 1 deletion docs/nextjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ And Next.js forces ESM mode.
## Next.js Edge Runtime

Mongoose does **not** currently support [Next.js Edge Runtime](https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes#edge-runtime).
While you can import Mongoose in Edge Runtime, you'll get [Mongoose's browser library](browser.html).
There is no way for Mongoose to connect to MongoDB in Edge Runtime, because [Edge Runtime currently doesn't support Node.js `net` API](https://edge-runtime.vercel.app/features/available-apis#unsupported-apis), which is what the MongoDB Node Driver uses to connect to MongoDB.
143 changes: 0 additions & 143 deletions lib/browser.js

This file was deleted.

117 changes: 0 additions & 117 deletions lib/browserDocument.js

This file was deleted.

14 changes: 8 additions & 6 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,20 @@ function Document(obj, fields, skipId, options) {
}
options = Object.assign({}, options);

this.$__ = new InternalCache();

// Support `browserDocument.js` syntax
if (this.$__schema == null) {
const _schema = utils.isObject(fields) && !fields.instanceOfSchema ?
new Schema(fields) :
fields;

this.$__setSchema(_schema);
fields = skipId;
skipId = options;
options = arguments[4] || {};
}

this.$__ = new InternalCache();

// Avoid setting `isNew` to `true`, because it is `true` by default
if (options.isNew != null && options.isNew !== true) {
this.$isNew = options.isNew;
Expand Down Expand Up @@ -848,10 +849,10 @@ Document.prototype.updateOne = function updateOne(doc, options, callback) {
const query = this.constructor.updateOne({ _id: this._doc._id }, doc, options);
const self = this;
query.pre(function queryPreUpdateOne() {
return self.constructor._middleware.execPre('updateOne', self, [self]);
return self._execDocumentPreHooks('updateOne', [self]);
});
query.post(function queryPostUpdateOne() {
return self.constructor._middleware.execPost('updateOne', self, [self], {});
return self._execDocumentPostHooks('updateOne');
});

if (this.$session() != null) {
Expand Down Expand Up @@ -2903,15 +2904,15 @@ function _pushNestedArrayPaths(val, paths, path) {
*/

Document.prototype._execDocumentPreHooks = async function _execDocumentPreHooks(opName, ...args) {
return this.constructor._middleware.execPre(opName, this, [...args]);
return this.$__middleware.execPre(opName, this, [...args]);
};

/*!
* ignore
*/

Document.prototype._execDocumentPostHooks = async function _execDocumentPostHooks(opName, error) {
return this.constructor._middleware.execPost(opName, this, [this], { error });
return this.$__middleware.execPost(opName, this, [this], { error });
};

/*!
Expand Down Expand Up @@ -3653,6 +3654,7 @@ Document.prototype.$__setSchema = function(schema) {
this.schema = schema;
}
this.$__schema = schema;
this.$__middleware = schema._getDocumentMiddleware();
this[documentSchemaSymbol] = schema;
};

Expand Down
Loading