Class: Schema

Schema

Schema class represents the schema definition. It includes properties, methods, static methods, and any
middleware we want to define.

new Schema(descriptor, options)

Creates an object schema

Name Type Description
descriptor Object
options Object
Name Type Description
strict Boolean

By default (true), allow only values in the schema to be set.
When this is false, setting new fields will dynamically add the field
to the schema as type "any".

dotNotation Boolean

Allow fields to be set via dot notation. Default: true.
obj['user.name'] = 'Joe'; -> obj: { user: 'Joe' }

minimize Boolean

"minimize" schemas by removing empty objects. Default: true

toObject Object

toObject method options.

Name Type Description
minimize Boolean

"minimize" schemas by removing empty objects. Default: true

transform function

transform function

virtuals Boolean

whether to include virtual properties. Default: false

dateToISO Boolean

convert dates to string in ISO format using Date.toISOString(). Default: false

toJSON Object

options for toJSON method options, similar to above

strict Boolean

ensures that value passed in ot assigned that were not specified in our
schema do not get saved

onBeforeValueSet function

function called when write operations on an object takes place. Currently,
it will only notify of write operations on the object itself and will not notify you when child objects are written to.
If you return false or throw an error within the onBeforeValueSet handler, the write operation will be cancelled.
Throwing an error will add the error to the error stack.

onValueSet function

Similar to onBeforeValueSet, but called after we've set a value on the key,

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

add(key, descriptor)

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);

extend(other)

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.

get(key){*}

Gets a schema option.

Name Type Description
key String

option name

Returns:
Type Description
* the option value

method(name, func)

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.

queue(name, args)

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

set(key, value)

Sets/gets a schema option.

Name Type Description
key String

option name

value Object optional

if not passed, the current option value is returned

static(name, func)

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'

virtual(name, type, options)

Creates a virtual property for the created model with the given object
specifying the get and optionally set function

Name 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