Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronelliott committed Sep 29, 2015
0 parents commit af7be02
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage files/directories used by tools like istanbul
coverage
coverage.html
.coveralls.yml

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Debug log from npm
npm-debug.log

# intellij based editors
.idea
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- 0.10
- 0.12
- 4.0
script:
npm run test:coverage
after_success:
npm run coverage:upload
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# kj-resource-mongo
[![view on npm](http://img.shields.io/npm/v/kj-resource-mongo.svg)](https://www.npmjs.org/package/kj-resource-mongo)
[![view on npm](https://img.shields.io/npm/dm/kj-resource-mongo.svg)](https://www.npmjs.org/package/kj-resource-mongo)
[![Dependency Status](https://david-dm.org/ronelliott/kj-resource-mongo.svg)](https://david-dm.org/ronelliott/kj-resource-mongo)
[![Build Status](https://travis-ci.org/ronelliott/kj-resource-mongo.png)](https://travis-ci.org/ronelliott/kj-resource-mongo)
[![Coverage Status](https://coveralls.io/repos/ronelliott/kj-resource-mongo/badge.svg?branch=master)](https://coveralls.io/r/ronelliott/kj-resource-mongo?branch=master)
[![Code Climate](https://codeclimate.com/github/ronelliott/kj-resource-mongo/badges/gpa.svg)](https://codeclimate.com/github/ronelliott/kj-resource-mongo)


## Installation
Install using [NPM](https://github.com/isaacs/npm):

npm install kj-resource-mongo --save
33 changes: 33 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

var is = require('is'),
prequire = require('parent-require'),
mongodb = prequire('mongodb'),
MongoClient = mongodb.MongoClient;

module.exports = function($opts) {
var enabled = is.defined($opts.enabled) ? $opts.enabled : true,
inject = $opts.inject || '$mongo',
uri = $opts.uri;

if (enabled && (is.null(uri) || is.undefined(uri))) {
throw new Error('URI is not defined!');
}

return function($$resolver, callback) {
if (!enabled) {
callback();
return;
}

MongoClient.connect(uri, function(err, db) {
if (err) {
callback(err);
return;
}

$$resolver.add(inject, db);
callback();
});
};
};
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "kj-resource-mongo",
"version": "0.0.1",
"description": "Mongo resource creation for the kj webapp framework.",
"author": "Ron Elliott <ron@elliott.im>",
"license": "ISC",
"keywords": [
"webapp",
"framework",
"resource",
"crud",
"postgres"
],
"scripts": {
"coverage:upload": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"test": "mocha",
"test:coverage": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec"
},
"devDependencies": {
"coveralls": "^2.11.4",
"istanbul": "^0.3.17",
"mocha": "^2.2.5",
"proxyquire": "^1.7.2",
"reflekt": "^0.3.9",
"should": "^7.0.4",
"sinon": "^1.15.4"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ronelliott/kj-resource-mongo.git"
},
"bugs": {
"url": "https://github.com/ronelliott/kj-resource-mongo/issues"
},
"homepage": "https://github.com/ronelliott/kj-resource-mongo#readme",
"dependencies": {
"is": "^3.1.0",
"parent-require": "^1.0.0"
}
}
83 changes: 83 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict';

var should = require('should'),
sinon = require('sinon'),
reflekt = require('reflekt'),
proxyquire = require('proxyquire');

describe('mongo', function() {
beforeEach(function() {
var self = this;

this.mongodb = {
MongoClient: {
connect: sinon.spy(function(uri, callback) {
callback(null, 'db');
})
}
};

this.$$resolver = new reflekt.ObjectResolver();

this.initializer = proxyquire('./', {
'parent-require': function() {
return self.mongodb;
}
});
});

it('should throw an error if no uri is defined', function() {
var self = this;
(function() {
self.initializer({});
}).should.throw('URI is not defined!');
});

it('should create the resource if it is enabled', function(done) {
var self = this;
this.initializer({ enabled: true, uri: '' })(this.$$resolver, function() {
self.mongodb.MongoClient.connect.called.should.equal(true);
done();
});
});

it('should create the resource if enabled is undefined in the options', function(done) {
var self = this;
this.initializer({ uri: '' })(this.$$resolver, function() {
self.mongodb.MongoClient.connect.called.should.equal(true);
done();
});
});

it('should not create the resource if it is disabled', function(done) {
var self = this;
this.initializer({ enabled: false, uri: '' })(this.$$resolver, function() {
self.mongodb.MongoClient.connect.called.should.equal(false);
done();
});
});

it('should use the defined uri', function(done) {
var self = this;
this.initializer({ enabled: true, uri: 'foo' })(this.$$resolver, function() {
self.mongodb.MongoClient.connect.calledWith('foo').should.equal(true);
done();
});
});

it('should inject the resource using the defined name', function(done) {
var self = this;
this.initializer({ enabled: true, uri: 'foo', inject: 'foo' })(this.$$resolver, function() {
self.$$resolver('foo').should.equal('db');
done();
});
});

it('should inject the resource using `$mongo` if no name is defined', function(done) {
var self = this;
this.initializer({ enabled: true, uri: 'foo' })(this.$$resolver, function() {
self.$$resolver('$mongo').should.equal('db');
done();
});
});
});

0 comments on commit af7be02

Please sign in to comment.