Class: Schema
Constructors
constructor
• new Schema(obj
, options?
)
Summary
Creates an instance of Schema.
Name
Schema
Example
const schema = new Schema({ name: String, age: { type: Number, intVal: true, min: 18 } });
Parameters
Name | Type | Description |
---|---|---|
obj | Schema | SchemaDef | Schema definition |
options | SchemaOptions | Settings to build schema |
Defined in
Properties
fields
• fields: FieldMap
Defined in
index
• index: SchemaIndex
= {}
Defined in
methods
• methods: any
= {}
Defined in
options
• options: SchemaOptions
Defined in
postHooks
• postHooks: any
= {}
Defined in
preHooks
• preHooks: any
= {}
Defined in
queries
• queries: SchemaQuery
= {}
Defined in
statics
• statics: any
= {}
Defined in
FactoryTypes
▪ Static
FactoryTypes: SupportFactoryTypes
Defined in
Types
▪ Static
Types: SupportTypes
Defined in
validators
▪ Static
validators: CustomValidations
= {}
Defined in
Methods
_addObject
▸ Private
_addObject(obj
): void
Parameters
Name | Type |
---|---|
obj | Record <string , unknown > |
Returns
void
Defined in
_addSchema
▸ Private
_addSchema(obj
): void
Parameters
Name | Type |
---|---|
obj | Schema |
Returns
void
Defined in
_initPostHooks
▸ Private
_initPostHooks(hook
, handlers
): void
Parameters
Name | Type |
---|---|
hook | HOOKS |
handlers | undefined | HookHandler | HookHandler [] |
Returns
void
Defined in
_initPreHooks
▸ Private
_initPreHooks(hook
, handlers
): void
Parameters
Name | Type |
---|---|
hook | HOOKS |
handlers | undefined | HookHandler | HookHandler [] |
Returns
void
Defined in
add
▸ add(obj
): Schema
Adds fields/schema type pairs to this schema.
Example
const plane = new Schema({ name: String });
const boeing = new Schema({ price: Number });
boeing.add(plane);
// You can add also add fields to this schema
boeing.add({ status: Boolean });
Parameters
Name | Type | Description |
---|---|---|
obj | Schema | Record <string , unknown > | Plain object to add, or another schema |
Returns
Schema
Defined in
applyDefaultsToObject
▸ applyDefaultsToObject(obj
): any
Applies default values defined on schema to an object instance.
Method
Example
const schema = new Schema({ amount: { type: Number, default: 5 } });
const result = schema.applyDefaultsToObject({});
console.log(result)
{ amount: 5 }
Parameters
Name | Type |
---|---|
obj | any |
Returns
any
Defined in
cast
▸ cast(data
, options?
): any
Cast a model instance using schema definition.
Method
Example
const schema = new Schema({ name: String, age: {type: Number }});
const result = schema.cast({ name: 'John Doe', age: '34' });
console.log(result)
{ name: 'John Doe', age: 34 }
Parameters
Name | Type |
---|---|
data | unknown |
options | CastOptions |
Returns
any
Defined in
path
▸ path(path
): undefined
| IOttomanType
Allows access to a specific field.
Example
const schema = new Schema({ amount: { type: Number, default: 5 } });
const field = schema.path('amount');
console.log(field.typeName);
Number
Parameters
Name | Type |
---|---|
path | string |
Returns
undefined
| IOttomanType
Defined in
plugin
▸ plugin(...fns
): Schema
Allows to apply plugins, to extend schema and model features.
Example
const schema = new Schema({ amount: { type: Number, default: 5 } });
schema.plugin((schema) => console.log(schema.path('amount').typeName));
Number
Parameters
Name | Type |
---|---|
...fns | PluginConstructor [] |
Returns
Defined in
post
▸ post(hook
, handler
): Schema
Register a hook function. Post hooks are executed after the hooked method.
Example
const schema = new Schema({ amount: { type: Number, default: 5} } );
schema.post(HOOKS.validate, (doc) => console.log(doc));
Parameters
Name | Type |
---|---|
hook | HookTypes |
handler | HookHandler |
Returns
Defined in
pre
▸ pre(hook
, handler
): Schema
Register a hook method. Pre hooks are executed before the hooked method.
Example
const schema = new Schema({ amount: { type: Number, default: 5} } );
schema.pre(HOOKS.validate, (doc) => console.log(doc));
Parameters
Name | Type |
---|---|
hook | HookTypes |
handler | HookHandler |
Returns
Defined in
validate
▸ validate(data
, options?
): any
Validate a model instance using the definition of the schema.
Method
Example
const schema = new Schema({ name: String, age: { type: Number, intVal: true, min: 18 } });
const result = schema.validate({name: 'John Doe', age: '34'});
console.log(result)
{name: 'John Doe', age: 34}
Parameters
Name | Type |
---|---|
data | unknown |
options | Object |
options.strict? | boolean |
Returns
any
Defined in
checkHook
▸ Static
Private
checkHook(hook
): void
Parameters
Name | Type |
---|---|
hook | HookTypes |
Returns
void