Skip to content

Commit

Permalink
Merge pull request #12397 from Automattic/vkarpov15/deno
Browse files Browse the repository at this point in the history
Deno support
  • Loading branch information
vkarpov15 committed Nov 28, 2022
2 parents 6f4fbee + 68ec07d commit 0c98cbb
Show file tree
Hide file tree
Showing 17 changed files with 222 additions and 116 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
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
@@ -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)

[![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/std@0.113.0/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
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
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
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
7 changes: 6 additions & 1 deletion test/connection.test.js
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();
}
const opts = {
serverSelectionTimeoutMS: 100
};
Expand Down
44 changes: 44 additions & 0 deletions test/deno.js
@@ -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`
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
@@ -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
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
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

0 comments on commit 0c98cbb

Please sign in to comment.