Skip to main content

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

NameTypeDescription
objSchema | SchemaDefSchema definition
optionsSchemaOptionsSettings to build schema

Defined in

schema/schema.ts:99

Properties

fields

fields: FieldMap

Defined in

schema/schema.ts:78


index

index: SchemaIndex = {}

Defined in

schema/schema.ts:76


methods

methods: any = {}

Defined in

schema/schema.ts:73


options

options: SchemaOptions

Defined in

schema/schema.ts:79


postHooks

postHooks: any = {}

Defined in

schema/schema.ts:75


preHooks

preHooks: any = {}

Defined in

schema/schema.ts:74


queries

queries: SchemaQuery = {}

Defined in

schema/schema.ts:77


statics

statics: any = {}

Defined in

schema/schema.ts:72


FactoryTypes

Static FactoryTypes: SupportFactoryTypes

Defined in

schema/schema.ts:51


Types

Static Types: SupportTypes

Defined in

schema/schema.ts:61


validators

Static validators: CustomValidations = {}

Defined in

schema/schema.ts:71

Methods

_addObject

Private _addObject(obj): void

Parameters

NameType
objRecord<string, unknown>

Returns

void

Defined in

schema/schema.ts:345


_addSchema

Private _addSchema(obj): void

Parameters

NameType
objSchema

Returns

void

Defined in

schema/schema.ts:351


_initPostHooks

Private _initPostHooks(hook, handlers): void

Parameters

NameType
hookHOOKS
handlersundefined | HookHandler | HookHandler[]

Returns

void

Defined in

schema/schema.ts:307


_initPreHooks

Private _initPreHooks(hook, handlers): void

Parameters

NameType
hookHOOKS
handlersundefined | HookHandler | HookHandler[]

Returns

void

Defined in

schema/schema.ts:294


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

NameTypeDescription
objSchema | Record<string, unknown>Plain object to add, or another schema

Returns

Schema

Schema

Defined in

schema/schema.ts:334


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

NameType
objany

Returns

any

Defined in

schema/schema.ts:199


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

NameType
dataunknown
optionsCastOptions

Returns

any

Defined in

schema/schema.ts:181


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

NameType
pathstring

Returns

undefined | IOttomanType

Defined in

schema/schema.ts:230


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

NameType
...fnsPluginConstructor[]

Returns

Schema

Defined in

schema/schema.ts:243


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

NameType
hookHookTypes
handlerHookHandler

Returns

Schema

Defined in

schema/schema.ts:279


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

NameType
hookHookTypes
handlerHookHandler

Returns

Schema

Defined in

schema/schema.ts:261


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

NameType
dataunknown
optionsObject
options.strict?boolean

Returns

any

Defined in

schema/schema.ts:160


checkHook

Static Private checkHook(hook): void

Parameters

NameType
hookHookTypes

Returns

void

Defined in

schema/schema.ts:288