|
1 |
| -# mongoose-browser |
2 |
| -Mongoose browser build |
| 1 | +# @mongoosejs/browser |
| 2 | + |
| 3 | +Browser-based driver and pre-compiled bundle for running Mongoose in the browser, which supports creating schemas and validating documents in the browser. |
| 4 | +`@mongoosejs/browser` 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 creating schemas, defining models, and validating documents. |
| 5 | + |
| 6 | +This package has a pre-built bundle of the browser library, including Mongoose. |
| 7 | +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'`: |
| 8 | + |
| 9 | +```javascript |
| 10 | +import mongoose from 'mongoose'; |
| 11 | +``` |
| 12 | + |
| 13 | +You can use the below syntax to access the Mongoose browser library from Node.js: |
| 14 | + |
| 15 | +```javascript |
| 16 | +// Using `require()` |
| 17 | +const mongoose = require('@mongoosejs/browser'); |
| 18 | + |
| 19 | +// Using ES6 imports |
| 20 | +import mongoose from '@mongoosejs/browser'; |
| 21 | +``` |
| 22 | + |
| 23 | +## Using the Browser Library {#usage} |
| 24 | + |
| 25 | +Mongoose's browser library is very limited. The only use case it supports is validating documents as shown below. |
| 26 | + |
| 27 | +```javascript |
| 28 | +import mongoose from 'mongoose'; |
| 29 | + |
| 30 | +const TestModel = mongoose.model('Test', new mongoose.Schema({ |
| 31 | + name: { type: String, required: true } |
| 32 | +})); |
| 33 | +const doc = new TestModel({}); |
| 34 | +// Prints an error because `name` is required. |
| 35 | +console.log(doc.validateSync()); |
| 36 | + |
| 37 | +// Alternative syntax, bypassing creating a model. |
| 38 | +const doc2 = new mongoose.Document({}, new mongoose.Schema({ |
| 39 | + name: { type: String, required: true } |
| 40 | +})); |
| 41 | +// Prints an error because `name` is required. |
| 42 | +console.log(doc2.validateSync()); |
| 43 | +``` |
| 44 | + |
| 45 | +You cannot use Mongoose's browser build to `save()` documents or execute queries. |
0 commit comments