高级结构
¥Advanced Schemas
使用 loadClass()
从 ES6 类创建
¥Creating from ES6 Classes Using loadClass()
Mongoose 允许从 ES6 类 创建结构。loadClass()
函数允许你从 ES6 类中提取方法、静态和虚函数。类方法映射到结构方法,静态方法映射到结构静态,getter/setter 映射到虚拟方法。
¥Mongoose allows creating schemas from ES6 classes.
The loadClass()
function lets you pull in methods,
statics, and virtuals from an ES6 class. A class method maps to a schema
method, a static method maps to a schema static, and getters/setters map
to virtuals.
const schema = new Schema({ firstName: String, lastName: String });
class HumanClass {
get fullName() {
return 'My name';
}
}
class PersonClass extends HumanClass {
// `fullName` becomes a virtual
get fullName() {
return `${super.fullName} is ${this.firstName} ${this.lastName}`;
}
set fullName(v) {
const firstSpace = v.indexOf(' ');
this.firstName = v.split(' ')[0];
this.lastName = firstSpace === -1 ? '' : v.substring(firstSpace + 1);
}
// `getFullName()` becomes a document method
getFullName() {
return `${this.firstName} ${this.lastName}`;
}
// `findByFullName()` becomes a static
static findByFullName(name) {
const firstSpace = name.indexOf(' ');
const firstName = name.split(' ')[0];
const lastName = firstSpace === -1 ? '' : name.substring(firstSpace + 1);
return this.findOne({ firstName, lastName });
}
}
schema.loadClass(PersonClass);
const Person = db.model('Person', schema);
const doc = await Person.create({
firstName: 'Jon',
lastName: 'Snow'
});
assert.equal(doc.fullName, 'My name is Jon Snow');
doc.fullName = 'Jon Stark';
assert.equal(doc.firstName, 'Jon');
assert.equal(doc.lastName, 'Stark');
const foundPerson = await Person.findByFullName('Jon Snow');
assert.equal(foundPerson.fullName, 'My name is Jon Snow');