Skip to content

Commit bafa678

Browse files
authored
Merge pull request #5184 from rocketspacer/master
Add alias schema option
2 parents e047cf1 + 7976f4e commit bafa678

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

lib/schema.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,41 @@ function Schema(obj, options) {
128128
if (this.options.timestamps) {
129129
this.setupTimestamp(this.options.timestamps);
130130
}
131+
132+
// Assign virtual properties based on alias option
133+
aliasFields(this);
134+
}
135+
136+
/*!
137+
* Create virtual properties with alias field
138+
*/
139+
function aliasFields(schema) {
140+
// console.log(schema.paths);
141+
for (var path in schema.paths) {
142+
if (!schema.paths[path].options) continue;
143+
144+
var prop = schema.paths[path].path;
145+
var alias = schema.paths[path].options.alias;
146+
147+
if (alias) {
148+
if ('string' === typeof alias && alias.length > 0) {
149+
schema
150+
.virtual(alias)
151+
.get((function(p) {
152+
return function() {
153+
return this.get(p);
154+
};
155+
})(prop))
156+
.set((function(p) {
157+
return function(v) {
158+
return this.set(p, v);
159+
};
160+
})(prop));
161+
} else {
162+
throw new Error('Invalid value for alias option on ' + prop + ', got ' + alias);
163+
}
164+
}
165+
}
131166
}
132167

133168
/*!

test/schema.alias.test.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var start = require('./common'),
7+
mongoose = start.mongoose,
8+
assert = require('power-assert'),
9+
Schema = mongoose.Schema;
10+
11+
describe('schema alias option', function() {
12+
it('works with all basic schema types', function() {
13+
var db = start();
14+
15+
var schema = new Schema({
16+
string: { type: String, alias: 'StringAlias' },
17+
number: { type: Number, alias: 'NumberAlias' },
18+
date: { type: Date, alias: 'DateAlias' },
19+
buffer: { type: Buffer, alias: 'BufferAlias' },
20+
boolean: { type: Boolean, alias: 'BooleanAlias' },
21+
mixed: { type: Schema.Types.Mixed, alias: 'MixedAlias' },
22+
objectId: { type: Schema.Types.ObjectId, alias: 'ObjectIdAlias'},
23+
array: { type: [], alias: 'ArrayAlias' }
24+
});
25+
26+
var S = db.model('AliasSchemaType', schema);
27+
S.create({
28+
string: 'hello',
29+
number: 1,
30+
date: new Date(),
31+
buffer: new Buffer('World'),
32+
boolean: false,
33+
mixed: [1, [], 'three', { four: 5 }],
34+
objectId: new Schema.Types.ObjectId(),
35+
array: ['a', 'b', 'c', 'd']
36+
}, function(err, s) {
37+
assert.ifError(err);
38+
39+
// Comparing with aliases
40+
assert.equal(s.string, s.StringAlias);
41+
assert.equal(s.number, s.NumberAlias);
42+
assert.equal(s.date, s.DateAlias);
43+
assert.equal(s.buffer, s.BufferAlias);
44+
assert.equal(s.boolean, s.BooleanAlias);
45+
assert.equal(s.mixed, s.MixedAlias);
46+
assert.equal(s.objectId, s.ObjectIdAlias);
47+
assert.equal(s.array, s.ArrayAlias);
48+
});
49+
});
50+
51+
it('works with nested schema types', function() {
52+
var db = start();
53+
54+
var schema = new Schema({
55+
nested: {
56+
type: {
57+
string: { type: String, alias: 'StringAlias' },
58+
number: { type: Number, alias: 'NumberAlias' },
59+
date: { type: Date, alias: 'DateAlias' },
60+
buffer: { type: Buffer, alias: 'BufferAlias' },
61+
boolean: { type: Boolean, alias: 'BooleanAlias' },
62+
mixed: { type: Schema.Types.Mixed, alias: 'MixedAlias' },
63+
objectId: { type: Schema.Types.ObjectId, alias: 'ObjectIdAlias'},
64+
array: { type: [], alias: 'ArrayAlias' }
65+
},
66+
alias: 'NestedAlias'
67+
}
68+
});
69+
70+
var S = db.model('AliasNestedSchemaType', schema);
71+
S.create({
72+
nested: {
73+
string: 'hello',
74+
number: 1,
75+
date: new Date(),
76+
buffer: new Buffer('World'),
77+
boolean: false,
78+
mixed: [1, [], 'three', { four: 5 }],
79+
objectId: new Schema.Types.ObjectId(),
80+
array: ['a', 'b', 'c', 'd']
81+
}
82+
}, function(err, s) {
83+
assert.ifError(err);
84+
85+
// Comparing with aliases
86+
assert.equal(s.nested, s.NestedAlias);
87+
assert.equal(s.nested.string, s.StringAlias);
88+
assert.equal(s.nested.number, s.NumberAlias);
89+
assert.equal(s.nested.date, s.DateAlias);
90+
assert.equal(s.nested.buffer, s.BufferAlias);
91+
assert.equal(s.nested.boolean, s.BooleanAlias);
92+
assert.equal(s.nested.mixed, s.MixedAlias);
93+
assert.equal(s.nested.objectId, s.ObjectIdAlias);
94+
assert.equal(s.nested.array, s.ArrayAlias);
95+
});
96+
});
97+
98+
it('throws when alias option is invalid', function() {
99+
assert.throws(function() {
100+
new Schema({
101+
foo: { type: String, alias: 456 }
102+
});
103+
});
104+
});
105+
});

0 commit comments

Comments
 (0)