Skip to main content

Schema

@abimongo/core v1.1.4


@abimongo/core / Schema

Class: Schema

Defined in: core/src/lib-core/AbimongoSchema.ts:216

The AbimongoSchema class allows you to define a schema for your MongoDB documents, including custom validation, indexing, and relationships between collections. It also supports hooks for pre- and post-processing of documents, as well as virtual fields

Extends

Constructors

Constructor

new Schema(schemaDefinition): Schema

Defined in: core/src/lib-core/AbimongoSchema.ts:217

Parameters

schemaDefinition

SchemaDefinition<Document>

Returns

Schema

Overrides

AbimongoSchema.constructor

Methods

addHook()

addHook(event, fn): void

Defined in: core/src/lib-core/AbimongoSchema.ts:128

Adds a hook to be executed for a specific event.

Parameters

event

string

The event name (e.g., "save", "delete").

fn

HookFunction

The hook function to execute.

Returns

void

Inherited from

AbimongoSchema.addHook


addRelationship()

addRelationship(ref, localField): void

Defined in: core/src/lib-core/AbimongoSchema.ts:89

Adds a relationship to the schema.

Parameters

ref

string

The name of the referenced collection.

localField

The field in the current document that holds the reference.

string | number

Returns

void

Inherited from

AbimongoSchema.addRelationship


applyIndexes()

applyIndexes(collection): Promise<void>

Defined in: core/src/lib-core/AbimongoSchema.ts:78

Applies all defined indexes to a MongoDB collection.

Parameters

collection

Collection<any>

The MongoDB collection to apply indexes to.

Returns

Promise<void>

Resolves when all indexes are applied.

Inherited from

AbimongoSchema.applyIndexes


applyVirtuals()

applyVirtuals(doc): void

Defined in: core/src/lib-core/AbimongoSchema.ts:114

Applies all virtual fields to a document.

Parameters

doc

any

The document to apply virtual fields to.

Returns

void

Inherited from

AbimongoSchema.applyVirtuals


create()

create(data): Promise<Document>

Defined in: core/src/lib-core/AbimongoSchema.ts:223

Parameters

data

OptionalId<Document>

Returns

Promise<Document>


executeHooks()

executeHooks(event, data): Promise<void>

Defined in: core/src/lib-core/AbimongoSchema.ts:139

Executes all hooks for a specific event.

Parameters

event

string

The event name.

data

any

The data to pass to the hook functions.

Returns

Promise<void>

Resolves when all hooks are executed.

Inherited from

AbimongoSchema.executeHooks


getGCConfig()

getGCConfig(): GCConfig | undefined

Defined in: core/src/lib-core/AbimongoSchema.ts:208

Returns

GCConfig | undefined

Inherited from

AbimongoSchema.getGCConfig


getHooks()

getHooks(action): HookFunction[]

Defined in: core/src/lib-core/AbimongoSchema.ts:164

Retrieves all hooks for a specific action.

Parameters

action

string

The action name.

Returns

HookFunction[]

An array of hook functions.

Inherited from

AbimongoSchema.getHooks


getRelationships()

getRelationships(): Relationship<any>[]

Defined in: core/src/lib-core/AbimongoSchema.ts:97

Retrieves all relationships defined in the schema.

Returns

Relationship<any>[]

An array of relationships.

Inherited from

AbimongoSchema.getRelationships


getSchema()

getSchema(): SchemaDefinition<Document>

Defined in: core/src/lib-core/AbimongoSchema.ts:37

Retrieves the schema definition.

Returns

SchemaDefinition<Document>

The schema definition.

Inherited from

AbimongoSchema.getSchema


index()

index(fields, options?): void

Defined in: core/src/lib-core/AbimongoSchema.ts:69

Adds an index to the schema.

Parameters

fields

any

The fields to index.

options?

any

Optional index options.

Returns

void

Inherited from

AbimongoSchema.index


post()

post(action, fn): void

Defined in: core/src/lib-core/AbimongoSchema.ts:173

Adds a post-hook for a specific action.

Parameters

action

string

The action name (e.g., "save", "delete").

fn

HookFunction

The hook function to execute after the action.

Returns

void

Inherited from

AbimongoSchema.post


pre()

pre(action, fn): void

Defined in: core/src/lib-core/AbimongoSchema.ts:152

Adds a pre-hook for a specific action.

Parameters

action

string

The action name (e.g., "save", "delete").

fn

HookFunction

The hook function to execute before the action.

Returns

void

Inherited from

AbimongoSchema.pre


registerSchema()

registerSchema(schemaDefinition): void

Defined in: core/src/lib-core/AbimongoSchema.ts:198

Parameters

schemaDefinition

SchemaDefinition<Document>

Returns

void

Inherited from

AbimongoSchema.registerSchema


setGCConfig()

setGCConfig(config): this

Defined in: core/src/lib-core/AbimongoSchema.ts:203

Parameters

config

GCConfig

Returns

this

Inherited from

AbimongoSchema.setGCConfig


triggerMiddleware()

triggerMiddleware(action, data): Promise<void>

Defined in: core/src/lib-core/AbimongoSchema.ts:186

Triggers middleware for a specific action.

Parameters

action

string

The action name.

data

any

The data to pass to the middleware functions.

Returns

Promise<void>

Resolves when all middleware functions are executed.

Inherited from

AbimongoSchema.triggerMiddleware


validate()

validate(doc): void

Defined in: core/src/lib-core/AbimongoSchema.ts:55

Validates a document against the schema's custom validators.

Parameters

doc

OptionalId<Document>

The document to validate.

Returns

void

Throws

If validation fails for any field.

Inherited from

AbimongoSchema.validate


validator()

validator(field, fn): void

Defined in: core/src/lib-core/AbimongoSchema.ts:46

Adds a custom validator for a specific field.

Parameters

field

string

The field to validate.

fn

(value) => boolean

The validation function.

Returns

void

Inherited from

AbimongoSchema.validator


virtual()

virtual(name, getter): void

Defined in: core/src/lib-core/AbimongoSchema.ts:106

Adds a virtual field to the schema.

Parameters

name

string

The name of the virtual field.

getter

(doc) => any

The function to compute the virtual field's value.

Returns

void

Inherited from

AbimongoSchema.virtual


create()

static create<T>(schemaDefinition): Schema

Defined in: core/src/lib-core/AbimongoSchema.ts:238

Type Parameters

T

T extends Document

Parameters

schemaDefinition

SchemaDefinition<T>

Returns

Schema