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

Deno support #12397

Merged
merged 33 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
3f79b18
feat: run tests on Deno
vkarpov15 Sep 4, 2022
c1a4823
chore: correct action name
vkarpov15 Sep 4, 2022
52110b6
chore: setup MongoDB server on deno tests
vkarpov15 Sep 4, 2022
8697ce5
chore: correct download path re: #9056
vkarpov15 Sep 4, 2022
32e8055
test: convert #3973 test to async/await to try to work around deno te…
vkarpov15 Sep 5, 2022
f4d8f61
test: ignore deno.js for linter, convert a few more populate tests to…
vkarpov15 Sep 5, 2022
0698d9a
test: print error to debug why #3973 test only fails on Deno in GH wo…
vkarpov15 Sep 5, 2022
356a132
test: print some more errors re: #9056
vkarpov15 Sep 5, 2022
2bcebe0
avoid using elvis operator
vkarpov15 Sep 5, 2022
79fb5b2
test: try cleaning up castNonArrays option to fix Deno tests re: #9056
vkarpov15 Sep 5, 2022
f89f833
test: correctly reset castNonArrays
vkarpov15 Sep 5, 2022
5045b5b
test: skip child_process test in Deno, explicitly exit when tests are…
vkarpov15 Sep 5, 2022
c13f2a6
test: check if #8222 error is caught in Deno re: #9056
vkarpov15 Sep 5, 2022
d6f6ce8
test: temporarily skip strictQuery tests to see if they fix #9056 tes…
vkarpov15 Sep 5, 2022
72124b8
test: increase test timeout in deno and ensure strictQuery is set re:…
vkarpov15 Sep 5, 2022
b3c31f1
test: add back connection and index tests, skip because of dns issue …
vkarpov15 Sep 5, 2022
1fb268e
test: quick test fix re: #9046
vkarpov15 Sep 5, 2022
17e2831
test: correctly clean up model re: #9056
vkarpov15 Sep 5, 2022
80cb49f
Merge branch '6.7' into vkarpov15/deno
vkarpov15 Sep 30, 2022
53db34f
Merge branch '6.7' into vkarpov15/deno
vkarpov15 Oct 3, 2022
66d8cad
test: proof of concept for running Mongoose tests without --unstable
vkarpov15 Oct 3, 2022
cdc824d
chore: test with deno v1.26.x
vkarpov15 Oct 3, 2022
72bf47b
test: add missing test files to deno
vkarpov15 Oct 3, 2022
3fb3ccb
Merge branch '6.7' into vkarpov15/deno
vkarpov15 Oct 5, 2022
759e47d
docs: add deno instructions and badges to README
vkarpov15 Oct 5, 2022
872b9a3
docs: more info on required deno flags
vkarpov15 Oct 5, 2022
bd5e28a
Apply suggestions from code review
Uzlopak Oct 28, 2022
5517fbc
Merge branch '6.8' into vkarpov15/deno
vkarpov15 Nov 28, 2022
566c6e1
Merge branch 'vkarpov15/deno' of github.com:Automattic/mongoose into …
vkarpov15 Nov 28, 2022
e4e26f1
Update README.md
vkarpov15 Nov 28, 2022
0a37232
fix: use deno 1.28 in tests, use --allow-sys, no more --unstable
vkarpov15 Nov 28, 2022
b41f2de
test: clear data between query cursor tests
vkarpov15 Nov 28, 2022
68ec07d
quick note about alpha support for deno
vkarpov15 Nov 28, 2022
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
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,34 @@ jobs:
with:
name: coverage
path: coverage

test-deno:
runs-on: ubuntu-20.04
name: Deno tests
steps:
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2
- name: Setup
run: |
wget -q https://downloads.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-6.0.0.tgz
tar xf mongodb-linux-x86_64-ubuntu2004-6.0.0.tgz
mkdir -p ./data/db/27017 ./data/db/27000
printf "\ntimeout: 8000" >> ./.mocharc.yml
./mongodb-linux-x86_64-ubuntu2004-6.0.0/bin/mongod --setParameter ttlMonitorSleepSecs=1 --fork --dbpath ./data/db/27017 --syslog --port 27017
sleep 2
mongod --version
echo `pwd`/mongodb-linux-x86_64-ubuntu2004-6.0.0/bin >> $GITHUB_PATH
- name: Setup node
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # v3.4.1
with:
node-version: 16
- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.28.x
- run: deno --version
- run: npm install
- name: Run Deno tests
run: npm run test-deno

test-replica-sets:
needs:
Expand Down
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Mongoose

Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed to work in an asynchronous environment. Mongoose supports both promises and callbacks.
Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed to work in an asynchronous environment. Mongoose supports [Node.js](https://nodejs.org/en/) and [Deno](https://deno.land/) (alpha).

[![Slack Status](https://img.shields.io/badge/slack-mongoosejsteam-34D058.svg?logo=slack )](https://mongoosejsteam.slack.com)
[![Build Status](https://github.com/Automattic/mongoose/workflows/Test/badge.svg)](https://github.com/Automattic/mongoose)
[![NPM version](https://badge.fury.io/js/mongoose.svg)](http://badge.fury.io/js/mongoose)
[![Deno version](https://deno.land/badge/mongoose/version)](https://deno.land/x/mongoose)
[![Deno popularity](https://deno.land/badge/mongoose/popularity)](https://deno.land/x/mongoose)
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved

[![npm](https://nodei.co/npm/mongoose.png)](https://www.npmjs.com/package/mongoose)

Expand Down Expand Up @@ -45,6 +46,8 @@ First install [Node.js](http://nodejs.org/) and [MongoDB](https://www.mongodb.or
$ npm install mongoose
```

Mongoose 6.8.0 also includes alpha support for [Deno](https://deno.land/).

## Importing

```javascript
Expand All @@ -55,6 +58,24 @@ const mongoose = require('mongoose');
import mongoose from 'mongoose';
```

Or, using [Deno's `createRequire()` for CommonJS support](https://deno.land/[email protected]/node/README.md?source=#commonjs-modules-loading) as follows.

```javascript
import { createRequire } from "https://deno.land/std/node/module.ts";
const require = createRequire(import.meta.url);

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/test')
.then(() => console.log('Connected!'));
```

You can then run the above script using the following.

```
deno run --allow-net --allow-read --allow-sys --allow-env mongoose-test.js
```

## Mongoose for Enterprise

Available as part of the Tidelift Subscription
Expand Down
2 changes: 1 addition & 1 deletion lib/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function SchemaType(path, options, instance) {
const defaultOptionsKeys = Object.keys(defaultOptions);

for (const option of defaultOptionsKeys) {
if (defaultOptions.hasOwnProperty(option) && !options.hasOwnProperty(option)) {
if (defaultOptions.hasOwnProperty(option) && !Object.prototype.hasOwnProperty.call(options, option)) {
options[option] = defaultOptions[option];
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"release-legacy": "git pull origin 5.x && git push origin 5.x --tags && npm publish --tag legacy",
"mongo": "node ./tools/repl.js",
"test": "mocha --exit ./test/*.test.js",
"test-deno": "deno run --allow-env --allow-read --allow-net --allow-run --allow-sys ./test/deno.js",
"test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
"test-tsd": "node ./test/types/check-types-filename && tsd",
"tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
Expand Down
2 changes: 2 additions & 0 deletions test/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ env:
rules:
# In `document.test.js` we sometimes use self assignment to test setters
no-self-assign: off
ignorePatterns:
- deno.js
hasezoey marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 6 additions & 1 deletion test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ describe('connections:', function() {
});

it('allows passing a schema', function() {
mongoose.deleteModel(/Test/);
const MyModel = mongoose.model('Test', new Schema({
name: String
}));
Expand Down Expand Up @@ -885,7 +886,11 @@ describe('connections:', function() {
return Model.create({ name: 'test' });
});

it('throws a MongooseServerSelectionError on server selection timeout (gh-8451)', () => {
it('throws a MongooseServerSelectionError on server selection timeout (gh-8451)', function() {
if (typeof Deno !== 'undefined') {
// In Deno dns throws an uncatchable error here.
return this.skip();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, are there any news regarding this "uncatchable dns error"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hasezoey nope, I don't think this issue is significant enough to block shipping deno support.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didnt mean to block with this question, just wanted to ask if there are any known updates regarding this problem to re-enable the test for deno

const opts = {
serverSelectionTimeoutMS: 100
};
Expand Down
44 changes: 44 additions & 0 deletions test/deno.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

import { createRequire } from "https://deno.land/std/node/module.ts";

// Workaround for Mocha getting terminal width, which currently requires `--unstable`
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
Object.defineProperty(process.stdout, 'getWindowSize', {
value: function() {
return [75, 40];
}
});

import { parse } from "https://deno.land/std/flags/mod.ts"
const args = parse(Deno.args);

const require = createRequire(import.meta.url);

const Mocha = require('mocha');
const fs = require('fs');
const path = require('path');

const mocha = new Mocha({
timeout: 8000,
...(args.g ? { fgrep: '' + args.g } : {})
});

const testDir = 'test';
const files = fs.readdirSync(testDir).
concat(fs.readdirSync(path.join(testDir, 'docs')).map(file => path.join('docs', file))).
concat(fs.readdirSync(path.join(testDir, 'helpers')).map(file => path.join('helpers', file)));

const ignoreFiles = new Set(['browser.test.js']);

for (const file of files) {
if (!file.endsWith('.test.js') || ignoreFiles.has(file)) {
continue;
}

mocha.addFile(path.join(testDir, file));
}

mocha.run(function(failures) {
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
process.exit(process.exitCode);
});
7 changes: 6 additions & 1 deletion test/docs/lean.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const assert = require('assert');
const v8Serialize = require('v8').serialize;
const start = require('../common');

// This file is in `es-next` because it uses async/await for convenience
Expand All @@ -23,6 +22,12 @@ describe('Lean Tutorial', function() {
});

it('compare sizes lean vs not lean', async function() {
// acquit:ignore:start
if (typeof Deno !== 'undefined') {
return this.skip(); // Deno does not support v8.serialize()
}
const v8Serialize = require('v8').serialize;
// acquit:ignore:end
const schema = new mongoose.Schema({ name: String });
const MyModel = mongoose.model('Test', schema);

Expand Down
5 changes: 5 additions & 0 deletions test/errors.validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ describe('ValidationError', function() {

describe('when user code defines a r/o Error#toJSON', function() {
it('should not fail', function(done) {
if (typeof Deno !== 'undefined') {
// Deno doesn't support child_process.fork
return this.skip();
}

const err = [];
const child = require('child_process')
.fork('./test/isolated/project-has-error.toJSON.js', ['--no-warnings'], { silent: true });
Expand Down
5 changes: 5 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,11 @@ describe('mongoose module:', function() {
});

it('connect with url doesnt cause unhandled rejection (gh-6997)', async function() {
if (typeof Deno !== 'undefined') {
// In Deno dns throws an uncatchable error here.
return this.skip();
}

const m = new mongoose.Mongoose();
const _options = Object.assign({}, options, { serverSelectionTimeoutMS: 100 });
const error = await m.connect('mongodb://doesnotexist:27009/test', _options).then(() => null, err => err);
Expand Down
Loading