Skip to content

mongoosejs/mongoose-browser

Repository files navigation

@mongoosejs/browser

Browser-based driver and pre-compiled bundle for running Mongoose in the browser, which supports creating schemas and validating documents in the browser. @mongoosejs/browser does not support saving documents, queries, populate, discriminators, or any other Mongoose feature other than creating schemas, defining models, and validating documents.

This package has a pre-built bundle of the browser library, including Mongoose. If you're bundling your code with Webpack, you should be able to import Mongoose's browser library as shown below if your Webpack target is 'web':

import mongoose from 'mongoose';

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

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

// Using ES6 imports
import mongoose from '@mongoosejs/browser';

Using the Browser Library

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

import mongoose from 'mongoose';

const TestModel = mongoose.model('Test', new mongoose.Schema({
  name: { type: String, required: true }
}));
const doc = new TestModel({});
// Prints an error because `name` is required.
console.log(doc.validateSync());

// Alternative syntax, bypassing creating a model.
const doc2 = new mongoose.Document({}, new mongoose.Schema({
  name: { type: String, required: true }
}));
// Prints an error because `name` is required.
console.log(doc2.validateSync());

You cannot use Mongoose's browser build to save() documents or execute queries.

About

Mongoose browser build

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published