Creates an object schema
Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
descriptor |
Object | |||||||||||||||||||||||||||||||||||||||||||
options |
Object |
|
Examples
var schema = new plaster.Schema({ name: String });
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 key
the property key
descriptor
the 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 other
Schema the schema to extend.
-
Gets a schema option.
Name Type Description key
String 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 name
String the name of the method
func
function 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 name
String name of the document method to call later
args
Array arguments to pass to the method
-
Sets/gets a schema option.
Name Type Description key
String option name
value
Object 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 name
String name of the statuc function
func
function 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 name
String name of the virtual property
type
String | function | Object optional type to be used for the virtual property. If not provided default is
'any' type.options
Object virtual options
Name Type Description get
function the virtual getter function
set
function 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