ottoman
Version:
Ottoman Couchbase ODM
142 lines (141 loc) • 4.65 kB
TypeScript
import { SchemaIndex, SchemaQuery } from '../model/index/types/index.types';
import { HookTypes } from '../utils/hooks';
import { CustomValidations, FieldMap, HookHandler, IOttomanType, PluginConstructor, SchemaDef, SchemaOptions, SupportFactoryTypes, SupportTypes } from './interfaces/schema.types';
import { CastOptions } from '../utils/cast-strategy';
export declare class Schema {
static FactoryTypes: SupportFactoryTypes;
static Types: SupportTypes;
static validators: CustomValidations;
statics: any;
methods: any;
preHooks: any;
postHooks: any;
index: SchemaIndex;
queries: SchemaQuery;
fields: FieldMap;
options: SchemaOptions;
/**
* @summary Creates an instance of Schema.
* @name Schema
* @class
* @public
*
* @param obj Schema definition
* @param options Settings to build schema
* @param options.strict removes fields if they aren't defined in the schema
* @param options.preHooks initialization of preHooks since Schema constructor
* @param options.postHooks initialization of postHooks since Schema constructor
* @returns Schema
*
* @example
* ```ts
* const schema = new Schema({ name: String, age: { type: Number, intVal: true, min: 18 } });
* ```
*/
constructor(obj: SchemaDef | Schema, options?: SchemaOptions);
/**
* Validate a model instance using the definition of the schema.
* @method
* @public
*
* @example
* ```ts
* 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}`
*/
validate(data: unknown, options?: {
strict?: boolean;
}): any;
/**
* Cast a model instance using schema definition.
* @method
* @public
*
* @example
* ```ts
* 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 }`
*/
cast(data: unknown, options?: CastOptions): any;
/**
* Applies default values defined on schema to an object instance.
* @method
* @public
* @param obj
* @example
* ```ts
* const schema = new Schema({ amount: { type: Number, default: 5 } });
* const result = schema.applyDefaultsToObject({});
* console.log(result)
* ```
* > `{ amount: 5 }`
*/
applyDefaultsToObject(obj: any): any;
/**
* Allows access to a specific field.
* @example
* ```ts
* const schema = new Schema({ amount: { type: Number, default: 5 } });
* const field = schema.path('amount');
* console.log(field.typeName);
* ```
* > Number
*/
path(path: string): IOttomanType | undefined;
/**
* Allows to apply plugins, to extend schema and model features.
* @example
* ```ts
* const schema = new Schema({ amount: { type: Number, default: 5 } });
* schema.plugin((schema) => console.log(schema.path('amount').typeName));
* ```
* > Number
*/
plugin(...fns: PluginConstructor[]): Schema;
/**
* Register a hook method.
* Pre hooks are executed before the hooked method.
* @example
* ```ts
* const schema = new Schema({ amount: { type: Number, default: 5} } );
* schema.pre(HOOKS.validate, (doc) => console.log(doc));
* ```
*/
pre(hook: HookTypes, handler: HookHandler): Schema;
/**
* Register a hook function.
* Post hooks are executed after the hooked method.
* @example
* ```ts
* const schema = new Schema({ amount: { type: Number, default: 5} } );
* schema.post(HOOKS.validate, (doc) => console.log(doc));
* ```
*/
post(hook: HookTypes, handler: HookHandler): Schema;
private static checkHook;
private _initPreHooks;
private _initPostHooks;
/**
* Adds fields/schema type pairs to this schema.
* @example
* ```ts
* 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 });
* ```
* @param obj Plain object to add, or another schema
* @return Schema
*/
add(obj: Record<string, unknown> | Schema): Schema;
private _addObject;
private _addSchema;
}