Skip to content

Commit 964432e

Browse files
committed
First commit
0 parents  commit 964432e

File tree

13 files changed

+4952
-0
lines changed

13 files changed

+4952
-0
lines changed

.babelrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"presets": [
3+
["env", {
4+
"targets": {
5+
"node": 4
6+
},
7+
}]
8+
],
9+
"plugins": [
10+
"transform-object-rest-spread",
11+
"transform-class-properties",
12+
"transform-flow-strip-types"
13+
]
14+
}

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flow-typed

.eslintrc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"extends": [
3+
"airbnb-base",
4+
"prettier"
5+
],
6+
"parser": "babel-eslint",
7+
"rules": {
8+
"no-underscore-dangle": 0,
9+
"arrow-body-style": 0,
10+
"no-unused-expressions": 0,
11+
"no-plusplus": 0,
12+
"no-console": 0,
13+
"func-names": 0,
14+
"comma-dangle": ["error", {
15+
"arrays": "always-multiline",
16+
"objects": "always-multiline",
17+
"imports": "always-multiline",
18+
"exports": "always-multiline",
19+
"functions": "ignore",
20+
}],
21+
"prettier/prettier": ["error", {
22+
"trailingComma": true,
23+
"singleQuote": true,
24+
"printWidth": 100
25+
}]
26+
},
27+
"env": {
28+
"jasmine": true,
29+
"jest": true
30+
},
31+
"plugins": [
32+
"flowtype",
33+
"prettier"
34+
]
35+
}

.flowconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[ignore]
2+
.*/node_modules/.*
3+
4+
[include]
5+
6+
[libs]
7+
8+
[options]

.gitignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
15+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
16+
.grunt
17+
18+
# node-waf configuration
19+
.lock-wscript
20+
21+
# Compiled binary addons (http://nodejs.org/api/addons.html)
22+
build/Release
23+
24+
# IntelliJ Files
25+
*.iml
26+
*.ipr
27+
*.iws
28+
/out/
29+
.idea/
30+
.idea_modules/
31+
32+
# Dependency directory
33+
node_modules
34+
35+
# Optional npm cache directory
36+
.npm
37+
38+
# Optional REPL history
39+
.node_repl_history
40+
41+
# Transpiled code
42+
/es
43+
/lib
44+
45+
coverage
46+
.nyc_output

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
*.log
3+
src
4+
flow-typed

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
sudo: false
2+
language: node_js
3+
cache:
4+
yarn: true
5+
directories:
6+
- node_modules
7+
notifications:
8+
email: true
9+
node_js:
10+
- "7"
11+
# - "6"
12+
# - "5"
13+
# - "4"
14+
script:
15+
# - yarn run coverage
16+
- yarn run build
17+
after_success:
18+
- 'curl -Lo travis_after_all.py https://git.io/travis_after_all'
19+
- python travis_after_all.py
20+
- export $(cat .to_export_back) &> /dev/null
21+
# - if [[ "$TRAVIS_JOB_NUMBER" == *.1 && "$TRAVIS_BRANCH" == "master" && "$TRAVIS_PULL_REQUEST" == "false" ]]; then bash <(curl -s https://codecov.io/bash); fi
22+
- if [[ "$TRAVIS_JOB_NUMBER" == *.1 && "$TRAVIS_BRANCH" == "master" && "$TRAVIS_PULL_REQUEST" == "false" ]]; then npm run semantic-release; fi
23+
branches:
24+
except:
25+
- /^v\d+\.\d+\.\d+$/

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
4+
## 0.0.1 (May 11, 2017)
5+
- First commit

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017-present Pavel Chertorogov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# mongodb-memory-server
2+
3+
[![NPM version](https://img.shields.io/npm/v/mongodb-memory-server.svg)](https://www.npmjs.com/package/mongodb-memory-server)
4+
[![Downloads stat](https://img.shields.io/npm/dt/mongodb-memory-server.svg)](http://www.npmtrends.com/mongodb-memory-server)
5+
[![Travis](https://img.shields.io/travis/nodkz/mongodb-memory-server.svg?maxAge=2592000)](https://travis-ci.org/nodkz/mongodb-memory-server)
6+
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
7+
8+
This package spins up a actual/real MongoDB Server programmatically from node for testing or mocking during development. By default it holds the data in memory. Fresh spinned up `mongod` process takes about 7Mb of memory. The server will allow you to connect your favorite ODM or client library to the MongoDB Server and run integration tests isolated from each other. You can start up several mongod simultaneously, it automatically discovers free ports.
9+
10+
This package use [mongodb-prebuilt](https://github.com/winfinit/mongodb-prebuilt) which on first start downloads the latest MongoDB binaries and save it to `~/.mongodb-prebuilt` folder. So first run may take a time. All further runs will use downloaded version.
11+
12+
## Installation
13+
```
14+
yarn add mongodb-memory-server --dev
15+
OR
16+
npm install mongodb-memory-server --save-dev
17+
```
18+
19+
## Usage
20+
21+
### Simple server start:
22+
```js
23+
import MongodbMemoryServer from 'mongodb-memory-server';
24+
25+
const mongod = new MongoMemoryServer();
26+
27+
const uri = await mongod.getConnectionString();
28+
const port = await mongod.getPort();
29+
const dbPath = await mongod.getDbPath();
30+
31+
mongod.stop();
32+
```
33+
34+
### Provide connection string to mongoose
35+
```js
36+
import mongoose from 'mongoose';
37+
import MongodbMemoryServer from 'mongodb-memory-server';
38+
39+
const mongoServer = new MongoMemoryServer();
40+
41+
mongoose.Promise = Promise;
42+
mongoServer.getConnectionString().then((mongoUri) => {
43+
const mongooseOpts = {
44+
server: {
45+
auto_reconnect: true,
46+
reconnectTries: Number.MAX_VALUE,
47+
reconnectInterval: 1000,
48+
},
49+
};
50+
51+
mongoose.connect(mongoUri, mongooseOpts);
52+
53+
mongoose.connection.on('error', (e) => {
54+
if (e.message.code === 'ETIMEDOUT') {
55+
console.log(e);
56+
mongoose.connect(mongoUri, mongooseOpts);
57+
}
58+
console.log(e);
59+
});
60+
61+
mongoose.connection.once('open', () => {
62+
console.log(`MongoDB successfully connected to ${mongoUri}`);
63+
});
64+
});
65+
```
66+
For additional information I recommend you to read this article [Testing a GraphQL Server using Jest with Mongoose](https://medium.com/entria/testing-a-graphql-server-using-jest-4e00d0e4980e)
67+
68+
69+
### Several mongoose connections simultaneously
70+
```js
71+
import mongoose from 'mongoose';
72+
import MongodbMemoryServer from 'mongodb-memory-server';
73+
74+
mongoose.Promise = Promise;
75+
76+
const mongoServer1 = new MongoMemoryServer();
77+
const mongoServer2 = new MongoMemoryServer();
78+
79+
const mongooseOpts = {
80+
server: {
81+
promiseLibrary = Promise;
82+
auto_reconnect: true,
83+
reconnectTries: Number.MAX_VALUE,
84+
reconnectInterval: 1000,
85+
},
86+
};
87+
88+
function addEvents(connection, mongoUri) {
89+
connection.on('error', (e) => {
90+
if (e.message.code === 'ETIMEDOUT') {
91+
console.log(e);
92+
connect(mongoUri, mongooseOpts);
93+
}
94+
console.log(e);
95+
});
96+
97+
connection.once('open', () => {
98+
console.log(`MongoDB successfully connected to ${mongoUri}`);
99+
});
100+
}
101+
102+
// Firstly create connection objects, which you may import in other files and create mongoose models.
103+
// Connection to databases will be estimated later (after model creation).
104+
const connections = {
105+
conn1: mongoose.createConnection(),
106+
conn2: mongoose.createConnection(),
107+
conn3: mongoose.createConnection(),
108+
};
109+
110+
mongoServer1.getConnectionString('server1_db1').then((mongoUri) => {
111+
connections.conn1.connect(mongoUri, mongooseOpts);
112+
addEvents(connections.conn1, mongoUri);
113+
});
114+
115+
mongoServer1.getConnectionString('server1_db2').then((mongoUri) => {
116+
connections.conn2.connect(mongoUri, mongooseOpts);
117+
addEvents(connections.conn2, mongoUri);
118+
});
119+
120+
mongoServer2.getConnectionString('server2_db').then((mongoUri) => {
121+
connections.conn3.connect(mongoUri, mongooseOpts);
122+
addEvents(connections.conn3, mongoUri);
123+
});
124+
125+
export default connections;
126+
127+
128+
// somewhere in other file
129+
import { Schema } from 'mongoose';
130+
import { conn1, conn2, conn3 } from './file_above';
131+
132+
const userSchema = new Schema({
133+
name: String,
134+
});
135+
136+
const taskSchema = new Schema({
137+
userId: String,
138+
task: String,
139+
});
140+
141+
export default {
142+
User: conn1.model('user', userSchema),
143+
Task: conn2.model('task', taskSchema),
144+
UserOnServer2: conn3.model('user', userSchema),
145+
}
146+
```
147+
148+
### Simple Mocha test example
149+
```js
150+
import mongoose from 'mongoose';
151+
import MongodbMemoryServer from 'mongodb-memory-server';
152+
153+
before(function(done) {
154+
const mongoServer = new MongodbMemoryServer();
155+
mongoServer.getConnectionString().then((mongoUri) => {
156+
mongoose.connect(mongoUri, function(err) {
157+
done(err);
158+
});
159+
});
160+
});
161+
162+
describe('...', function() {
163+
it("...", function() {
164+
// ...
165+
});
166+
});
167+
168+
```
169+
170+
171+
## Credits
172+
Inspired by alternative runners for [mongodb-prebuilt](https://github.com/winfinit/mongodb-prebuilt):
173+
- [mockgoose](https://github.com/mockgoose/Mockgoose)
174+
- [mongomem](https://github.com/CImrie/mongomem)
175+
176+
## License
177+
178+
MIT

0 commit comments

Comments
 (0)