forked from Automattic/mongoose
/
browser.pug
82 lines (63 loc) · 2.86 KB
/
browser.pug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
extends layout
block append style
link(rel="stylesheet" href="/docs/css/inlinecpc.css")
script(type="text/javascript" src="/docs/js/native.js")
block content
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>
:markdown
<h1>Mongoose in the Browser</h1>
<script>
_native.init("CK7DT53U",{
targetClass: 'native-inline'
});
</script>
<div class="native-inline">
<a href="#native_link#"><span class="sponsor">Sponsor</span> #native_company# — #native_desc#</a>
</div>
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.
Use the below syntax to access the Mongoose browser library.
```javascript
// Using `require()`
const mongoose = require('mongoose/browser');
// Using ES6 imports
import mongoose from 'mongoose/browser';
```
<ul class="toc">
<li><a href="#usage">Using the Browser Library</a></li>
<li><a href="#webpack">Building With Webpack</a></li>
</ul>
<h2 id="usage">Using the Browser Library</h2>
Mongoose's browser library is very limited. The only use case it supports
is validating documents as shown below.
```javascript
import mongoose from 'mongoose/browser';
// 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());
```
<h2 id="webpack">Building With Webpack</h2>
Mongoose uses [ES2015 (also known as ES6)](https://www.ecma-international.org/ecma-262/6.0/)
syntax, so in order to use Mongoose with older browsers you'll need to use
a tool like [Babel](https://www.npmjs.com/package/babel-cli). As of version
5.x, Mongoose no longer has an officially supported pre-built browser bundle,
you need to compile the browser library yourself.
To build Mongoose's browser library using Webpack, you'll need to use
[`babel-loader`](https://www.npmjs.com/package/babel-loader). Because of
how Webpack loads `require()` statements, it pulls in a lot of built-in
Node.js modules. In order to avoid this, you need to use the
[`node` Webpack config option](https://webpack.js.org/configuration/node/)
as shown below.
```javascript
[require:webpack.*browser build$]
```