Skip to content

Add alias schema option #5184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 14, 2017
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
35 changes: 35 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,41 @@ function Schema(obj, options) {
if (this.options.timestamps) {
this.setupTimestamp(this.options.timestamps);
}

// Assign virtual properties based on alias option
aliasFields(this);
}

/*!
* Create virtual properties with alias field
*/
function aliasFields(schema) {
// console.log(schema.paths);
for (var path in schema.paths) {
if (!schema.paths[path].options) continue;

var prop = schema.paths[path].path;
var alias = schema.paths[path].options.alias;

if (alias) {
if ('string' === typeof alias && alias.length > 0) {
schema
.virtual(alias)
.get((function(p) {
return function() {
return this.get(p);
};
})(prop))
.set((function(p) {
return function(v) {
return this.set(p, v);
};
})(prop));
} else {
throw new Error('Invalid value for alias option on ' + prop + ', got ' + alias);
}
}
}
}

/*!
Expand Down
105 changes: 105 additions & 0 deletions test/schema.alias.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

/**
* Module dependencies.
*/

var start = require('./common'),
mongoose = start.mongoose,
assert = require('power-assert'),
Schema = mongoose.Schema;

describe('schema alias option', function() {
it('works with all basic schema types', function() {
var db = start();

var schema = new Schema({
string: { type: String, alias: 'StringAlias' },
number: { type: Number, alias: 'NumberAlias' },
date: { type: Date, alias: 'DateAlias' },
buffer: { type: Buffer, alias: 'BufferAlias' },
boolean: { type: Boolean, alias: 'BooleanAlias' },
mixed: { type: Schema.Types.Mixed, alias: 'MixedAlias' },
objectId: { type: Schema.Types.ObjectId, alias: 'ObjectIdAlias'},
array: { type: [], alias: 'ArrayAlias' }
});

var S = db.model('AliasSchemaType', schema);
S.create({
string: 'hello',
number: 1,
date: new Date(),
buffer: new Buffer('World'),
boolean: false,
mixed: [1, [], 'three', { four: 5 }],
objectId: new Schema.Types.ObjectId(),
array: ['a', 'b', 'c', 'd']
}, function(err, s) {
assert.ifError(err);

// Comparing with aliases
assert.equal(s.string, s.StringAlias);
assert.equal(s.number, s.NumberAlias);
assert.equal(s.date, s.DateAlias);
assert.equal(s.buffer, s.BufferAlias);
assert.equal(s.boolean, s.BooleanAlias);
assert.equal(s.mixed, s.MixedAlias);
assert.equal(s.objectId, s.ObjectIdAlias);
assert.equal(s.array, s.ArrayAlias);
});
});

it('works with nested schema types', function() {
var db = start();

var schema = new Schema({
nested: {
type: {
string: { type: String, alias: 'StringAlias' },
number: { type: Number, alias: 'NumberAlias' },
date: { type: Date, alias: 'DateAlias' },
buffer: { type: Buffer, alias: 'BufferAlias' },
boolean: { type: Boolean, alias: 'BooleanAlias' },
mixed: { type: Schema.Types.Mixed, alias: 'MixedAlias' },
objectId: { type: Schema.Types.ObjectId, alias: 'ObjectIdAlias'},
array: { type: [], alias: 'ArrayAlias' }
},
alias: 'NestedAlias'
}
});

var S = db.model('AliasNestedSchemaType', schema);
S.create({
nested: {
string: 'hello',
number: 1,
date: new Date(),
buffer: new Buffer('World'),
boolean: false,
mixed: [1, [], 'three', { four: 5 }],
objectId: new Schema.Types.ObjectId(),
array: ['a', 'b', 'c', 'd']
}
}, function(err, s) {
assert.ifError(err);

// Comparing with aliases
assert.equal(s.nested, s.NestedAlias);
assert.equal(s.nested.string, s.StringAlias);
assert.equal(s.nested.number, s.NumberAlias);
assert.equal(s.nested.date, s.DateAlias);
assert.equal(s.nested.buffer, s.BufferAlias);
assert.equal(s.nested.boolean, s.BooleanAlias);
assert.equal(s.nested.mixed, s.MixedAlias);
assert.equal(s.nested.objectId, s.ObjectIdAlias);
assert.equal(s.nested.array, s.ArrayAlias);
});
});

it('throws when alias option is invalid', function() {
assert.throws(function() {
new Schema({
foo: { type: String, alias: 456 }
});
});
});
});