# Class: Schema
# Hierarchy
- Schema
# Constructors
# constructor
+ new Schema(obj
: SchemaDef | Schema, options
: SchemaOptions): Schema
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 | Default | Description |
---|---|---|---|
obj | SchemaDef | Schema | - | Schema definition |
options | SchemaOptions | { strict: true } | Settings to build schema |
Returns: Schema
Schema
# Properties
# fields
• fields: FieldMap
# index
• index: SchemaIndex
# methods
• methods: any
# options
• options: SchemaOptions
# postHooks
• postHooks: any
# preHooks
• preHooks: any
# queries
• queries: SchemaQuery
# statics
• statics: any
# Static
validators
▪ validators: CustomValidations
# Methods
# add
▸ add(obj
: Record‹string, unknown› | Schema): 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 | Record‹string, unknown› | Schema | Plain object to add, or another schema |
Returns: Schema
Schema
# applyDefaultsToObject
▸ applyDefaultsToObject(obj
: any): 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
# cast
▸ cast(data
: unknown, options
: CastOptions): 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 | Default |
---|---|---|
data | unknown | - |
options | CastOptions | {} |
Returns: any
# path
▸ path(path
: string): IOttomanType | undefined
Allows access to specify 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: IOttomanType | undefined
# plugin
▸ plugin(...fns
: PluginConstructor[]): 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: Schema
# post
▸ post(hook
: HookTypes, handler
: HookHandler): Schema
Allows to 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: Schema
# pre
▸ pre(hook
: HookTypes, handler
: HookHandler): Schema
Allows to 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: Schema
# validate
▸ validate(data
: unknown, options
: object): 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:
▪ data: unknown
▪Default value
options: object= {}
Name | Type |
---|---|
strict? | undefined | false | true |
Returns: any
# Object literals
# Static
FactoryTypes
# ▪ FactoryTypes: object
# Array
• Array: arrayTypeFactory = arrayTypeFactory
# Boolean
• Boolean: booleanTypeFactory = booleanTypeFactory
# Date
• Date: dateTypeFactory = dateTypeFactory
# Embed
• Embed: embedTypeFactory = embedTypeFactory
# Mixed
• Mixed: mixedTypeFactory = mixedTypeFactory
# Number
• Number: numberTypeFactory = numberTypeFactory
# Reference
• Reference: referenceTypeFactory = referenceTypeFactory
# String
• String: stringTypeFactory = stringTypeFactory
# Static
Types
# ▪ Types: object
# Array
• Array: ArrayType‹› = ArrayType.prototype
# Boolean
• Boolean: BooleanType‹› = BooleanType.prototype
# Date
• Date: DateType‹› = DateType.prototype
# Embed
• Embed: EmbedType‹› = EmbedType.prototype
# Mixed
• Mixed: MixedType‹› = MixedType.prototype
# Number
• Number: NumberType‹› = NumberType.prototype
# Reference
• Reference: ReferenceType‹› = ReferenceType.prototype
# String
• String: StringType‹› = StringType.prototype