Permalink
Browse files

support disabling collection pluralization

closes #1350
  • Loading branch information...
1 parent cd1aa60 commit be5844aa790dbb35e965619c4a75caf9d26b1f21 @refack refack committed with aheckmann Sep 23, 2013
Showing with 79 additions and 8 deletions.
  1. +9 −2 lib/index.js
  2. +5 −3 lib/model.js
  3. +1 −0 lib/schema.js
  4. +3 −1 lib/utils.js
  5. +61 −2 test/utils.test.js
View
@@ -55,7 +55,10 @@ function Mongoose () {
this.plugins = [];
this.models = {};
this.modelSchemas = {};
- this.options = {};
+ // default global options
+ this.options = {
+ pluralization: true
+ };
this.createConnection(); // default connection
};
@@ -343,8 +346,12 @@ Mongoose.prototype.model = function (name, schema, collection, skipInit) {
}
}
+ // Apply relevant "global" options to the schema
+ if (!('pluralization' in schema.options)) schema.options.pluralization = this.options.pluralization;
+
+
if (!collection) {
- collection = schema.get('collection') || format(name);
+ collection = schema.get('collection') || format(name, schema.options);
}
var connection = options.connection || this.connection;
View
@@ -2554,14 +2554,16 @@ Model.__subclass = function subclass (conn, schema, collection) {
? schema
: model.prototype.schema;
+ var options = s.options || {};
+
if (!collection) {
collection = model.prototype.schema.get('collection')
- || utils.toCollectionName(model.modelName);
+ || utils.toCollectionName(model.modelName, options);
}
var collectionOptions = {
- bufferCommands: s ? s.options.bufferCommands : true
- , capped: s && s.options.capped
+ bufferCommands: s ? options.bufferCommands : true
+ , capped: s && options.capped
};
Model.prototype.collection = conn.collection(collection, collectionOptions);
View
@@ -170,6 +170,7 @@ Schema.prototype.defaultOptions = function (options) {
, _id: true
, noVirtualId: false // deprecated, use { id: false }
, id: true
+// , pluralization: true // only set this to override the global option
}, options);
if (options.read) {
View
@@ -20,9 +20,11 @@ var ReadPref = require('mongodb').ReadPreference
* @api private
*/
-exports.toCollectionName = function (name) {
+exports.toCollectionName = function (name, options) {
+ options = options || {};
if ('system.profile' === name) return name;
if ('system.indexes' === name) return name;
+ if (options.pluralization === false) return name;
return pluralize(name.toLowerCase());
};
View
@@ -341,7 +341,66 @@ describe('utils', function(){
var A = db.model(collectionName, ASchema);
assert.equal(A.collection.name, collectionName + 's');
done();
- })
+ });
+ it('should pluralize by default', function(done){
+ var db = start();
+
+ // don't set anything
+ //db.base.set('pluralization', false);
+
+ var ASchema = new Schema ({value: String});
+
+ var collectionName = 'singular';
+ var A = db.model(collectionName, ASchema);
+ assert.equal(A.collection.name, collectionName + 's');
+ done();
+ });
+ it('should not pluralize when global option set to true', function(done){
+ var db = start();
+
+ // set to false
+ db.base.set('pluralization', false);
+
+ var ASchema = new Schema ({value: String});
+
+ var collectionName = 'singular';
+ var A = db.model(collectionName, ASchema);
+ assert.equal(A.collection.name, collectionName);
+ done();
+ });
+ it('should not pluralize when global option set to false', function(done){
+ var db = start();
+ db.base.set('pluralization', false);
+
+ var ASchema = new Schema ({value: String});
+
+ var collectionName = 'singular';
+ var A = db.model(collectionName, ASchema);
+ assert.equal(A.collection.name, collectionName);
+ done();
+ });
+ it('should pluralize when local option set to true', function(done){
+ var db = start();
+ db.base.set('pluralization', false);
+
+ var ASchema = new Schema ({value: String}, {pluralization: true});
+
+ var collectionName = 'singular';
+ var A = db.model(collectionName, ASchema);
+ assert.equal(A.collection.name, collectionName + 's');
+ done();
+ });
+ it('should not pluralize when local option set to false', function(done){
+ var db = start();
+ db.base.set('pluralization', true);
+
+ var ASchema = new Schema ({value: String}, {pluralization: false});
+
+ var collectionName = 'singular';
+ var A = db.model(collectionName, ASchema);
+ assert.equal(A.collection.name, collectionName);
+ done();
+ });
});
-})
+});

0 comments on commit be5844a

Please sign in to comment.