Skip to content

ambiten


ambiten / core / Schema

Class: Schema<T>

Defined in: packages/core/src/lib-core/ambitenSchema.ts:301

The AmbitenSchema class allows you to define a schema for MongoDB documents. It supports:

  • schema definition
  • custom validation
  • indexes
  • relationships
  • virtual fields
  • context-aware middleware
  • garbage collection metadata

Extends

Type Parameters

T

T extends Document = Document

Constructors

Constructor

new Schema<T>(schemaDefinition): Schema<T>

Defined in: packages/core/src/lib-core/ambitenSchema.ts:302

Parameters

schemaDefinition

SchemaDefinition<T>

Returns

Schema<T>

Overrides

AmbitenSchema.constructor

Methods

addRelationship()

addRelationship(ref, localField): void

Defined in: packages/core/src/lib-core/ambitenSchema.ts:138

Adds a relationship to the schema.

Parameters

ref

string

localField

keyof T

Returns

void

Inherited from

AmbitenSchema.addRelationship


applyIndexes()

applyIndexes(collection): Promise<void>

Defined in: packages/core/src/lib-core/ambitenSchema.ts:129

Applies all defined indexes to a MongoDB collection.

Parameters

collection

Collection<any>

Returns

Promise<void>

Inherited from

AmbitenSchema.applyIndexes


applyVirtuals()

applyVirtuals(doc): void

Defined in: packages/core/src/lib-core/ambitenSchema.ts:159

Applies all virtual fields to a document.

Parameters

doc

T

Returns

void

Inherited from

AmbitenSchema.applyVirtuals


create()

create(data): Promise<T>

Defined in: packages/core/src/lib-core/ambitenSchema.ts:307

Parameters

data

OptionalUnlessRequiredId<T>

Returns

Promise<T>


executeMiddleware()

executeMiddleware(phase, operation, ctx): Promise<void>

Defined in: packages/core/src/lib-core/ambitenSchema.ts:256

Executes middleware for a given phase and operation.

Parameters

phase

"pre" | "post"

operation

AmbitenMiddlewareOperation

ctx

AmbitenMiddlewareContext<T>

Returns

Promise<void>

Inherited from

AmbitenSchema.executeMiddleware


executePost()

executePost(operation, ctx): Promise<void>

Defined in: packages/core/src/lib-core/ambitenSchema.ts:243

Executes post-middleware for an operation.

Parameters

operation

AmbitenMiddlewareOperation

ctx

AmbitenMiddlewareContext<T>

Returns

Promise<void>

Inherited from

AmbitenSchema.executePost


executePre()

executePre(operation, ctx): Promise<void>

Defined in: packages/core/src/lib-core/ambitenSchema.ts:230

Executes pre-middleware for an operation.

Parameters

operation

AmbitenMiddlewareOperation

ctx

AmbitenMiddlewareContext<T>

Returns

Promise<void>

Inherited from

AmbitenSchema.executePre


getGCConfig()

getGCConfig(): GCConfig | undefined

Defined in: packages/core/src/lib-core/ambitenSchema.ts:296

Gets garbage collection configuration.

Returns

GCConfig | undefined

Inherited from

AmbitenSchema.getGCConfig


getHooks()

getHooks(operation, phase?): AmbitenMiddlewareHandler<T>[]

Defined in: packages/core/src/lib-core/ambitenSchema.ts:218

Backward-compatible hook reader. Defaults to pre hooks to avoid breaking older callers that expect getHooks(action).

Parameters

operation

AmbitenMiddlewareOperation

phase?

"pre" | "post"

Returns

AmbitenMiddlewareHandler<T>[]

Inherited from

AmbitenSchema.getHooks


getPostHooks()

getPostHooks(operation): AmbitenMiddlewareHandler<T>[]

Defined in: packages/core/src/lib-core/ambitenSchema.ts:208

Returns all post-middleware for an operation.

Parameters

operation

AmbitenMiddlewareOperation

Returns

AmbitenMiddlewareHandler<T>[]

Inherited from

AmbitenSchema.getPostHooks


getPreHooks()

getPreHooks(operation): AmbitenMiddlewareHandler<T>[]

Defined in: packages/core/src/lib-core/ambitenSchema.ts:199

Returns all pre-middleware for an operation.

Parameters

operation

AmbitenMiddlewareOperation

Returns

AmbitenMiddlewareHandler<T>[]

Inherited from

AmbitenSchema.getPreHooks


getRelationships()

getRelationships(): Relationship<any>[]

Defined in: packages/core/src/lib-core/ambitenSchema.ts:145

Retrieves all relationships defined in the schema.

Returns

Relationship<any>[]

Inherited from

AmbitenSchema.getRelationships


getSchema()

getSchema(): SchemaDefinition<T>

Defined in: packages/core/src/lib-core/ambitenSchema.ts:62

Retrieves the schema definition.

Returns

SchemaDefinition<T>

Inherited from

AmbitenSchema.getSchema


index()

index(fields, options?): void

Defined in: packages/core/src/lib-core/ambitenSchema.ts:122

Adds an index to the schema.

Parameters

fields

any

options?

any

Returns

void

Inherited from

AmbitenSchema.index


post()

post(operation, fn): this

Defined in: packages/core/src/lib-core/ambitenSchema.ts:185

Adds a post-middleware handler for a specific operation.

Parameters

operation

AmbitenMiddlewareOperation

fn

AmbitenMiddlewareHandler<T>

Returns

this

Inherited from

AmbitenSchema.post


pre()

pre(operation, fn): this

Defined in: packages/core/src/lib-core/ambitenSchema.ts:171

Adds a pre-middleware handler for a specific operation.

Parameters

operation

AmbitenMiddlewareOperation

fn

AmbitenMiddlewareHandler<T>

Returns

this

Inherited from

AmbitenSchema.pre


registerSchema()

registerSchema(schemaDefinition): void

Defined in: packages/core/src/lib-core/ambitenSchema.ts:69

Re-registers the schema definition.

Parameters

schemaDefinition

SchemaDefinition<T>

Returns

void

Inherited from

AmbitenSchema.registerSchema


setGCConfig()

setGCConfig(config): this

Defined in: packages/core/src/lib-core/ambitenSchema.ts:288

Sets garbage collection configuration.

Parameters

config

GCConfig

Returns

this

Inherited from

AmbitenSchema.setGCConfig


triggerMiddleware()

triggerMiddleware(phase, operation, ctx): Promise<void>

Defined in: packages/core/src/lib-core/ambitenSchema.ts:277

Backward-compatible alias. If older callers use triggerMiddleware(action, data), they should be upgraded to pass explicit phase + operation + context.

Parameters

phase

"pre" | "post"

operation

AmbitenMiddlewareOperation

ctx

AmbitenMiddlewareContext<T>

Returns

Promise<void>

Inherited from

AmbitenSchema.triggerMiddleware


validate()

validate(doc): void

Defined in: packages/core/src/lib-core/ambitenSchema.ts:88

Validates a document synchronously. Throws if an async validator is encountered.

Parameters

doc

OptionalUnlessRequiredId<T>

Returns

void

Inherited from

AmbitenSchema.validate


validateAsync()

validateAsync(doc): Promise<void>

Defined in: packages/core/src/lib-core/ambitenSchema.ts:108

Validates a document asynchronously.

Parameters

doc

OptionalUnlessRequiredId<T>

Returns

Promise<void>

Inherited from

AmbitenSchema.validateAsync


validator()

validator(field, fn): void

Defined in: packages/core/src/lib-core/ambitenSchema.ts:77

Adds a custom validator for a specific field.

Parameters

field

string

fn

(value, doc?) => boolean | Promise<boolean>

Returns

void

Inherited from

AmbitenSchema.validator


virtual()

virtual(name, getter): void

Defined in: packages/core/src/lib-core/ambitenSchema.ts:152

Adds a virtual field to the schema.

Parameters

name

string

getter

(doc) => any

Returns

void

Inherited from

AmbitenSchema.virtual


create()

static create<T>(schemaDefinition): Schema<T>

Defined in: packages/core/src/lib-core/ambitenSchema.ts:333

Type Parameters

T

T extends Document = Document

Parameters

schemaDefinition

SchemaDefinition<T>

Returns

Schema<T>

Context-aware execution infrastructure for multi-tenant systems.