Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.
Closed
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
10 changes: 9 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"ecmaFeatures": {
"blockBindings": true,
},
"env": {
"mocha": true
"mocha": true,
"node": true,
"es6": true
},
"rules": {
"strict": 0
},
"extends": ["mongodb-js/node"]
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- 5
- 6
script: npm run-script ci
cache:
directories:
Expand Down
4 changes: 0 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
var Model = require('./lib/model');
var Collection = require('./lib/collection');
var fetch = require('./lib/fetch');

module.exports = Model;
module.exports.Collection = Collection;
module.exports.fetch = fetch;
148 changes: 0 additions & 148 deletions lib/fetch.js

This file was deleted.

84 changes: 51 additions & 33 deletions lib/index-field.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,56 @@
var Model = require('ampersand-model');
var Collection = require('ampersand-rest-collection');
var format = require('util').format;

var IndexField = Model.extend({
namespace: 'IndexType',
idAttribute: 'id',
props: {
field: 'string',
value: {
type: 'any',
values: [1, -1, '2dsphere', '2d', 'geoHaystack', 'text', 'hashed']
}
},
derived: {
id: {
deps: ['field', 'value'],
fn: function() {
return format('%s_%s', this.field, this.value);
}
},
geo: {
deps: ['value'],
fn: function() {
return this.value === '2dsphere' ||
this.value === '2d' ||
this.value === 'geoHaystack';
}
'use strict';

const _ = require('lodash');

/**
* 2dsphere constant.
*/
const TWOD_SPHERE = '2dsphere';

/**
* 2d constant.
*/
const TWOD = '2d';

/**
* geohaystack constant.
*/
const GEO_HAYSTACK = 'geoHaystack';

/**
* The valid value values.
*/
const VALID_VALUES = [ 1, -1, '2dsphere', '2d', 'geoHaystack', 'text', 'hashed' ];

/**
* Represents the indexed field.
*/
class IndexField {

/**
* Instantiate the index field.
*
* @param {String} field - The field name.
* @param {Object} value - The index value.
*/
constructor(field, value) {
this.field = field;
if (_.indexOf(VALID_VALUES, value) < 0) {
throw new Error(`Index types must be one of ${JSON.stringify(VALID_VALUES)} - got '${value}'`);
}
this.value = value;
}
});

var IndexFieldCollection = Collection.extend({
model: IndexField
});
/**
* Determine if the field is a geo index.
*
* @returns {Boolean} If the index field is a geo index.
*/
isGeo() {
return this.value === TWOD_SPHERE ||
this.value === TWOD ||
this.value === GEO_HAYSTACK;
}
}

module.exports = IndexField;
module.exports.Collection = IndexFieldCollection;
20 changes: 7 additions & 13 deletions lib/model.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
var Model = require('ampersand-model');
var _ = require('lodash');

var IndexFieldCollection = require('./index-field').Collection;
var WarningCollection = require('./warnings').WarningCollection;

// var debug = require('debug')('mongodb-index-model:index-model');
var IndexField = require('./index-field');

var indexModelProps = {
ns: 'string',
Expand All @@ -15,7 +12,9 @@ var indexModelProps = {
usageSince: 'date',
usageHost: 'string',
version: 'number',
extra: 'object'
extra: 'object',
fields: 'array',
warnings: 'array'
};

var IndexModel = Model.extend({
Expand Down Expand Up @@ -136,10 +135,6 @@ var IndexModel = Model.extend({
}
}
},
collections: {
fields: IndexFieldCollection,
warnings: WarningCollection
},
parse: function(attrs) {
// rename v to version
attrs.version = attrs.v;
Expand All @@ -153,12 +148,11 @@ var IndexModel = Model.extend({

// fields
attrs.fields = _.map(attrs.key, function(val, key) {
return {
field: key,
value: val
};
return new IndexField(key, val);
});

attrs.warnings = attrs.warnings || [];

return attrs;
},
serialize: function() {
Expand Down
41 changes: 41 additions & 0 deletions lib/warning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const _ = require('lodash');

/**
* Allowable warnings.
*/
const WARNINGS = {
'IXWARN_PREFIX': 1,
'IXWARN_UNUSED': 2
};

/**
* The warning values.
*/
const WARNING_VALUES = _.values(WARNINGS);

/**
* Wraps an index warning.
*/
class Warning {

/**
* Instantiate the warning.
*
* @param {Number} code - The error code.
* @param {String} message - The message.
* @param {String} details - The details.
*/
constructor(code, message, details) {
if (_.indexOf(WARNING_VALUES, code) < 0) {
throw new Error(`Index warning codes must be one of ${JSON.stringify(WARNING_VALUES)} - got '${code}'`);
}
this.code = code;
this.message = message;
this.details = details;
}
}

module.exports = Warning;
module.exports.WARNINGS = WARNINGS;
Loading