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

jshint —> semistandard #71

Merged
merged 1 commit into from
Oct 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 0 additions & 32 deletions .jshintrc

This file was deleted.

1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.babelrc
.istanbul.yml
.travis.yml
.jshintrc
.editorconfig
.idea/
src/
Expand Down
11 changes: 5 additions & 6 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ const app = feathers()
// Turn on URL-encoded parser for REST services
.use(bodyParser.urlencoded({extended: true}));


const promise = new Promise(function(resolve) {
const promise = new Promise(function (resolve) {
// Connect to your MongoDB instance(s)
MongoClient.connect('mongodb://localhost:27017/feathers').then(function(db){
MongoClient.connect('mongodb://localhost:27017/feathers').then(function (db) {
// Connect to the db, create and register a Feathers service.
app.use('/messages', service({
Model: db.collection('messages'),
Expand All @@ -35,13 +34,13 @@ const promise = new Promise(function(resolve) {

// Start the server
var server = app.listen(3030);
server.on('listening', function() {
server.on('listening', function () {
console.log('Feathers Message MongoDB service running on 127.0.0.1:3030');
resolve(server);
});
}).catch(function(error){
}).catch(function (error) {
console.error(error);
});
});

module.exports = promise;
module.exports = promise;
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@
"release:major": "npm version major && npm publish",
"compile": "rm -rf lib/ && babel -d lib/ src/",
"watch": "babel --watch -d lib/ src/",
"jshint": "jshint src/. test/. --config",
"lint": "eslint-if-supported semistandard --fix",
"mocha": "mocha --opts mocha.opts",
"coverage": "istanbul cover _mocha -- --opts mocha.opts",
"test": "rm -rf .tmp && npm run compile && npm run jshint && npm run coverage",
"test": "rm -rf .tmp && npm run compile && npm run lint && npm run coverage",
"start": "node example/app"
},
"semistandard": {
"env": [
"mocha"
],
"ignore": [
"/lib"
]
},
"engines": {
"node": "~0.10.0",
"npm": "~1.4.0"
Expand All @@ -67,14 +75,15 @@
"babel-preset-es2015": "^6.3.13",
"body-parser": "^1.14.1",
"chai": "^3.4.1",
"eslint-if-supported": "^1.0.1",
"feathers": "^2.0.0-pre.4",
"feathers-hooks": "^1.1.0",
"feathers-rest": "^1.2.2",
"feathers-service-tests": "^0.8.0",
"feathers-socketio": "^1.3.3",
"istanbul": "^1.1.0-alpha.1",
"jshint": "^2.8.0",
"mocha": "^3.0.0",
"mongodb": "^2.1.3"
"mongodb": "^2.1.3",
"semistandard": "^9.1.0"
}
}
2 changes: 1 addition & 1 deletion src/error-handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import errors from 'feathers-errors';

export default function errorHandler(error) {
export default function errorHandler (error) {
let feathersError = error;

// NOTE (EK): The list of error code is way too massive to map
Expand Down
59 changes: 30 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import errorHandler from './error-handler';

// Create the service.
class Service {
constructor(options) {
constructor (options) {
if (!options) {
throw new Error('MongoDB options have to be provided');
}
Expand All @@ -22,19 +22,19 @@ class Service {
this.paginate = options.paginate || {};
}

extend(obj) {
extend (obj) {
return Proto.extend(obj, this);
}

_objectifyId(id) {
_objectifyId (id) {
if (this.id === '_id' && ObjectID.isValid(id)) {
id = new ObjectID(id.toString());
}

return id;
}

_multiOptions(id, params) {
_multiOptions (id, params) {
let query = Object.assign({}, params.query);
let options = Object.assign({ multi: true }, params.mongodb || params.options);

Expand All @@ -46,34 +46,36 @@ class Service {
return { query, options };
}

_getSelect(select) {
_getSelect (select) {
if (Array.isArray(select)) {
var result = {};
select.forEach(name => result[name] = 1);
select.forEach(name => {
result[name] = 1;
});
return result;
}

return select;
}

_find(params, count, getFilter = filter) {
_find (params, count, getFilter = filter) {
// Start with finding all, and limit when necessary.
let { filters, query } = getFilter(params.query|| {});
let { filters, query } = getFilter(params.query || {});
let q = this.Model.find(query);

if (filters.$select) {
q = this.Model.find(query, this._getSelect(filters.$select));
}

if (filters.$sort){
if (filters.$sort) {
q.sort(filters.$sort);
}

if (filters.$limit){
if (filters.$limit) {
q.limit(filters.$limit);
}

if (filters.$skip){
if (filters.$skip) {
q.skip(filters.$skip);
}

Expand All @@ -95,21 +97,21 @@ class Service {
return runQuery();
}

find(params) {
const paginate = (params && typeof params.paginate !== 'undefined') ?
params.paginate : this.paginate;
find (params) {
const paginate = (params && typeof params.paginate !== 'undefined')
? params.paginate : this.paginate;
const result = this._find(params, !!paginate.default,
query => filter(query, paginate)
);

if(!paginate.default) {
if (!paginate.default) {
return result.then(page => page.data);
}

return result;
}

_get(id) {
_get (id) {
id = this._objectifyId(id);

return this.Model.findOne({ [this.id]: id })
Expand All @@ -123,38 +125,37 @@ class Service {
.catch(errorHandler);
}

get(id, params) {
get (id, params) {
return this._get(id, params);
}

_findOrGet(id, params) {
if(id === null) {
_findOrGet (id, params) {
if (id === null) {
return this._find(params).then(page => page.data);
}

return this._get(id, params);
}

create(data) {
create (data) {
const setId = item => {
const entry = Object.assign({}, item);

// Generate a MongoId if we use a custom id
if(this.id !== '_id' && typeof entry[this.id] === 'undefined') {
if (this.id !== '_id' && typeof entry[this.id] === 'undefined') {
entry[this.id] = new ObjectID().toHexString();
}

return entry;
};


return this.Model
.insert(Array.isArray(data) ? data.map(setId) : setId(data))
.then(result => result.ops.length > 1 ? result.ops : result.ops[0])
.catch(errorHandler);
}

_normalizeId(id, data) {
_normalizeId (id, data) {
if (this.id === '_id') {
// Default Mongo IDs cannot be updated. The Mongo library handles
// this automatically.
Expand All @@ -166,15 +167,15 @@ class Service {
}
}

patch(id, data, params) {
patch (id, data, params) {
const { query, options } = this._multiOptions(id, params);
const patchParams = Object.assign({}, params, {
query: Object.assign({}, query)
});

// Account for potentially modified data
Object.keys(query).forEach(key => {
if(query[key] !== undefined && data[key] !== undefined &&
if (query[key] !== undefined && data[key] !== undefined &&
typeof data[key] !== 'object') {
patchParams.query[key] = data[key];
} else {
Expand All @@ -188,8 +189,8 @@ class Service {
.then(() => this._findOrGet(id, patchParams));
}

update(id, data, params) {
if(Array.isArray(data) || id === null) {
update (id, data, params) {
if (Array.isArray(data) || id === null) {
return Promise.reject('Not replacing multiple records. Did you mean `patch`?');
}

Expand All @@ -201,7 +202,7 @@ class Service {
.catch(errorHandler);
}

remove(id, params) {
remove (id, params) {
let { query, options } = this._multiOptions(id, params);

return this._findOrGet(id, params)
Expand All @@ -212,7 +213,7 @@ class Service {
}
}

export default function init(options) {
export default function init (options) {
return new Service(options);
}

Expand Down
16 changes: 8 additions & 8 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ describe('Feathers MongoDB Service', () => {

before(() =>
MongoClient.connect('mongodb://localhost:27017/feathers-test')
.then(function(database) {
.then(function (database) {
db = database;

app.use('/people', service({
Model: db.collection('people'),
events: [ 'testing' ]
})).use('/people-customid', service({
Model: db.collection('people-customid'),
id: 'customid',
events: [ 'testing' ]
}));
Model: db.collection('people'),
events: [ 'testing' ]
})).use('/people-customid', service({
Model: db.collection('people-customid'),
id: 'customid',
events: [ 'testing' ]
}));

db.collection('people-customid').removeMany();
db.collection('people').removeMany();
Expand Down
7 changes: 5 additions & 2 deletions test/test-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ const app = feathers()
// Turn on URL-encoded parser for REST services
.use(bodyParser.urlencoded({extended: true}));

export default new Promise(function(resolve) {
export default new Promise(function (resolve, reject) {
// Connect to your MongoDB instance(s)
MongoClient.connect('mongodb://localhost:27017/feathers-test', (error, db) => {
if (error) {
reject(error);
}
// Connect to the db, create and register a Feathers service.
app.use('/todos', service({
Model: db.collection('todos'),
Expand All @@ -34,7 +37,7 @@ export default new Promise(function(resolve) {

// Start the server
var server = app.listen(3030);
server.on('listening', function() {
server.on('listening', function () {
console.log('Feathers Message MongoDB service running on 127.0.0.1:3030');
resolve(server);
});
Expand Down