Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoose browser implementation #9273

Closed
gilles-yvetot opened this issue Jul 22, 2020 · 5 comments
Closed

Mongoose browser implementation #9273

gilles-yvetot opened this issue Jul 22, 2020 · 5 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@gilles-yvetot
Copy link

gilles-yvetot commented Jul 22, 2020

Do you want to request a feature or report a bug?
Feature -> docs

Problem

@vkarpov15 I am not sure to understand the mongoose browser documentation. I have been a user of mongoose for many years now (❤️ ) but I am struggling to understand it.

  • You said there is no pre-build library of mongoose browser but I am still seeing one when installing mongoose on my project
  • I am using mongoose to define my schema and models in my library. This project is later consumed by all my backends projects. I'd love to be able to create a different build from the same schemas I have defined. But does it mean I need to re-write all of them and replacing const mongoose = require('mongoose') with const mongoose = require('mongoose/browser')? I am using ES6 everywhere

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node 12
Mongoose 5.9.25
Mongo 4.2

@vkarpov15 vkarpov15 added this to the 5.9.26 milestone Jul 24, 2020
vkarpov15 added a commit that referenced this issue Jul 27, 2020
@vkarpov15
Copy link
Collaborator

As long as you use Webpack and your webpack config has target: 'web', then you should be able to use const mongoose = require('mongoose') in your browser code.

Thanks for pointing out that we already have a pre-built bundle, this section of the docs is out of date after #8435

@vkarpov15
Copy link
Collaborator

I found a bit of a bug in the UMD build: the below script would fail because older versions of Babel didn't support extends Error very well. I upgraded Babel and can confirm the below script works with a basic webpack config.

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());

@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jul 27, 2020
@vkarpov15
Copy link
Collaborator

Just to clarify, you can still use import mongoose from 'mongoose/browser';, but then you're using your own Webpack config to compile Mongoose.

@gilles-yvetot
Copy link
Author

@vkarpov15 I don't know if I should open a new ticket or not.
I have tried the version 5.10.3 and everything is working fine with the recommended webpack config. I do have a warning for mongodb-client-encryption not being found

Module not found: Error: Can't resolve 'mongodb-client-encryption' in '/Users/gilles/Documents/omni/app/packages/extension/node_modules/mongodb/lib/operations'
 @ ./node_modules/mongodb/lib/operations/connect.js
 @ ./node_modules/mongodb/lib/mongo_client.js
 @ ./node_modules/mongodb/index.js
 @ ./node_modules/mongoose/lib/index.js
 @ ./node_modules/mongoose/index.js

@vkarpov15
Copy link
Collaborator

@gilles-yvetot are you using import mongoose from 'mongoose/browser' or import mongoose from 'mongoose'?

You should be safe to ignore that warning or stub out mongodb-client-encryption using webpack externals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants