Skip to main content

Class: Schema

Defined in: schema/schema.ts:50

Constructors​

Constructor​

new Schema(obj, options): Schema

Defined in: schema/schema.ts:99

Parameters​

obj​

Schema definition

Schema | SchemaDef

options​

SchemaOptions = ...

Settings to build schema

Returns​

Schema

Schema

Name​

Schema

Example​

 const schema = new Schema({ name: String, age: { type: Number, intVal: true, min: 18 } });

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​

add()​

add(obj): Schema

Defined in: schema/schema.ts:334

Adds fields/schema type pairs to this schema.

Parameters​

obj​

Plain object to add, or another schema

Schema | Record<string, unknown>

Returns​

Schema

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

applyDefaultsToObject()​

applyDefaultsToObject(obj): any

Defined in: schema/schema.ts:199

Applies default values defined on schema to an object instance.

Parameters​

obj​

any

Returns​

any

Method​

Example​

  const schema = new Schema({ amount: { type: Number, default: 5 } });
const result = schema.applyDefaultsToObject({});
console.log(result)

{ amount: 5 }


cast()​

cast(data, options): any

Defined in: schema/schema.ts:181

Cast a model instance using schema definition.

Parameters​

data​

unknown

options​

CastOptions = {}

Returns​

any

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 }


path()​

path(path): undefined | IOttomanType

Defined in: schema/schema.ts:230

Allows access to a specific field.

Parameters​

path​

string

Returns​

undefined | IOttomanType

Example​

  const schema = new Schema({ amount: { type: Number, default: 5 } });
const field = schema.path('amount');
console.log(field.typeName);

Number


plugin()​

plugin(...fns): Schema

Defined in: schema/schema.ts:243

Allows to apply plugins, to extend schema and model features.

Parameters​

fns​

...PluginConstructor[]

Returns​

Schema

Example​

  const schema = new Schema({ amount: { type: Number, default: 5 } });
schema.plugin((schema) => console.log(schema.path('amount').typeName));

Number


post()​

post(hook, handler): Schema

Defined in: schema/schema.ts:279

Register a hook function. Post hooks are executed after the hooked method.

Parameters​

hook​

HookTypes

handler​

HookHandler

Returns​

Schema

Example​

  const schema = new Schema({ amount: { type: Number, default: 5} } );
schema.post(HOOKS.validate, (doc) => console.log(doc));

pre()​

pre(hook, handler): Schema

Defined in: schema/schema.ts:261

Register a hook method. Pre hooks are executed before the hooked method.

Parameters​

hook​

HookTypes

handler​

HookHandler

Returns​

Schema

Example​

  const schema = new Schema({ amount: { type: Number, default: 5} } );
schema.pre(HOOKS.validate, (doc) => console.log(doc));

validate()​

validate(data, options): any

Defined in: schema/schema.ts:160

Validate a model instance using the definition of the schema.

Parameters​

data​

unknown

options​
strict?​

boolean

Returns​

any

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}