Creates an object schema
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
descriptor |
Object | |||||||||||||||||||||||||||||||||||||||||||
options |
Object |
|
Examples
var schema = new plaster.Schema({ name: String });
with onBeforeValueSet
var User = plaster.schema({ name: String }, {
onBeforeValueSet: function(key, value) {
if(key === 'name' && value.indexOf('Joe') >= 0) {
return false;
});
}
});
var User = plaster.model('User', schema);
var user = new User();
user.name = 'Bill'; // name not set
user.name = 'Joe Smith'; // { name: 'Joe Smith' }
Methods
-
Adds the descriptor to the schema at the given key
Name Type Description keythe property key
descriptorthe property descriptor
Example
var userSchema = plaster.schema({firstName: String });
userSchema.add('lastName', String); -
Extends other schema. Copies descriptor properties, methods, statics, virtuals and middleware.
If this schema has a named property already, the property is not copied.Name Type Description otherSchema the schema to extend.
-
Gets a schema option.
Name Type Description keyString option name
Returns:
Type Description * the option value -
Creates a instance method for the created model.
An object of function names and functions can also be passed in.Name Type Description nameString the name of the method
funcfunction the actual function implementation
Example
var userSchema = plaster.schema({
firstName: String,
lastName: String
});userSchema.method('getFullName', function () {
return this.firstName + ' ' + this.lastName
});var User = plaster.model('User', userSchema);
var user = new User({
firstName: 'Joe',
lastName: 'Smith'
});console.log(user.getFullName()); // Joe Smith
-
Defines a post hook for the schema.
See hooks-fixed. -
Defines a pre hook for the schema.
See hooks-fixed. -
Adds a method call to the queue.
Name Type Description nameString name of the document method to call later
argsArray arguments to pass to the method
-
Sets/gets a schema option.
Name Type Description keyString option name
valueObject optional if not passed, the current option value is returned
-
Creates a static function for the created model.
An object of function names and functions can also be passed in.Name Type Description nameString name of the statuc function
funcfunction the actual function
- @example
var userSchema = plaster.schema({ name: String });
userSchema.static('foo', function () {
return 'bar';
});var User = plaster.model('User', userSchema);
console.log(User.foo()); // 'bar'
- @example
-
Creates a virtual property for the created model with the given object
specifying the get and optionally set functionName Type Description nameString name of the virtual property
typeString | function | Object optional type to be used for the virtual property. If not provided default is
'any' type.optionsObject virtual options
Name Type Description getfunction the virtual getter function
setfunction the virtual setter function. If not provided the virtual becomes read-only.
Example
var userSchema = plaster.schema({firstName: String, lastName: String});
userSchema.virtual('fullName', String, {
get: function () {
return this.firstName + ' ' + this.lastName;
},
set: function (v) {
if (v !== undefined) {
var parts = v.split(' ');
this.firstName = parts[0];
this.lastName = parts[1];
}
}
});var User = plaster.model('User', userSchema);
var user = new User({firstName: 'Joe', lastName: 'Smith'});
console.log(user.fullName); // Joe Smith
user.fullName = 'Bill Jones';
console.log(user.firstName); // Bill
console.log(user.lastName); // Jones
console.log(user.fullName); // Bill Jones