# 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 Description
obj SchemaDef | Schema Schema definition
options? SchemaOptions Settings to build schema

Returns: Schema

Schema

# Properties

# fields

fields: FieldMap


# index

index: SchemaIndex


# methods

methods: any


# postHooks

postHooks: any


# preHooks

preHooks: any


# queries

queries: SchemaQuery


# statics

statics: any


# validationStrategy

validationStrategy: VALIDATION_STRATEGY


# Static validators

validators: CustomValidations

# Methods

# 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(object: any): any

Casts 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.cast({name: 'John Doe', age: '34'});
  console.log(result)

{name: 'John Doe', age: 34}

Parameters:

Name Type
object any

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

# Object literals

# Static Types

#Types: object

# Array

Array: arrayTypeFactory = arrayTypeFactory

# Boolean

Boolean: booleanTypeFactory = booleanTypeFactory

# Date

Date: dateTypeFactory = dateTypeFactory

# Embed

Embed: embedTypeFactory = embedTypeFactory

# Number

Number: numberTypeFactory = numberTypeFactory

# Reference

Reference: referenceTypeFactory = referenceTypeFactory

# String

String: stringTypeFactory = stringTypeFactory