Skip to content

@iamcalegari/mongoat / Model

Class: Model<ModelType> ​

Defined in: src/model/index.ts:559

Type Parameters ​

ModelType ​

ModelType extends Document = Document

Constructors ​

Constructor ​

new Model<ModelType>(props): Model<ModelType>

Defined in: src/model/index.ts:681

Parameters ​

props ​

CreateModelProps<ModelType>

Returns ​

Model<ModelType>

Properties ​

allowedMethods ​

allowedMethods: METHODS[]

Defined in: src/model/index.ts:572


collectionName ​

collectionName: string

Defined in: src/model/index.ts:560


documentDefaults ​

documentDefaults: DocumentDefaults<ModelType>

Defined in: src/model/index.ts:574


hooks ​

hooks: HookRegistry<ModelType>

Defined in: src/model/index.ts:590

Hook registry — one { pre: []; post: [] } array pair per METHODS value, populated by the declarative hooks constructor option and by the chainable .pre()/.post() (constructor hooks are applied first).


indexes ​

indexes: CreateIndexProps[]

Defined in: src/model/index.ts:562


methods ​

methods: string[]

Defined in: src/model/index.ts:570


validationAction ​

validationAction: string

Defined in: src/model/index.ts:566


validationLevel ​

validationLevel: string

Defined in: src/model/index.ts:568


validator ​

validator: object

Defined in: src/model/index.ts:564

$jsonSchema ​

$jsonSchema: ModelValidationSchema

Accessors ​

onHookError ​

Get Signature ​

get onHookError(): OnHookError<BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object>

Defined in: src/model/index.ts:611

Fallback destination for fireAndForget post-hook rejections — falls back to console.error (defaultOnHookError) so an error never disappears in total silence. Applied consistently across all CRUD methods, never per-method.

Declared as an accessor (not a plain field) over the live kConfigRefs.onHookError slot, instead of a separately-resolved value set once in the constructor: a plain field would have let a direct post-construction reassignment (model.onHookError = newHandler — nothing prevents it, this is a public writable member) desync from the kConfigRefs identity snapshot compared on re-registration, causing a later, genuinely-matching re-registration to falsely throw MODEL_CONFIG_CONFLICT. Reading and writing through the same slot diffConfig compares means there is nothing left to desync — a reassignment IS the value re-registration compares against.

Returns ​

OnHookError<BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object>

Set Signature ​

set onHookError(fn): void

Defined in: src/model/index.ts:622

Parameters ​
fn ​

OnHookError<BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object | BaseHookContext<ModelType> & object>

Returns ​

void

Methods ​

aggregate() ​

aggregate(pipeline, options?): Promise<Document[]>

Defined in: src/model/index.ts:1146

Parameters ​

pipeline ​

Document[]

options? ​

AggregateOptions = {}

Returns ​

Promise<Document[]>


bulkWrite() ​

bulkWrite(operations, options?): Promise<BulkWriteResult>

Defined in: src/model/index.ts:1531

Parameters ​

operations ​

AnyBulkWriteOperation<ModelType>[]

options? ​

BulkWriteOptions = {}

Returns ​

Promise<BulkWriteResult>


delete() ​

delete(filter, options?): Promise<WithId<ModelType> | null>

Defined in: src/model/index.ts:1479

Parameters ​

filter ​

Filter<ModelType>

options? ​

FindOneAndDeleteOptions = {}

Returns ​

Promise<WithId<ModelType> | null>


deleteMany() ​

deleteMany(filter, options?): Promise<DeleteResult>

Defined in: src/model/index.ts:1260

Parameters ​

filter ​

Filter<ModelType>

options? ​

DeleteOptions = {}

Returns ​

Promise<DeleteResult>


find() ​

find(filter?, options?): Promise<WithId<ModelType> | null>

Defined in: src/model/index.ts:1416

Parameters ​

filter? ​

Filter<ModelType> = {}

options? ​

FindOptions = {}

Returns ​

Promise<WithId<ModelType> | null>


findById() ​

findById(documentId, options?): Promise<WithId<ModelType> | null>

Defined in: src/model/index.ts:1439

Parameters ​

documentId ​

string | ObjectId

options? ​

FindOptions = {}

Returns ​

Promise<WithId<ModelType> | null>


findMany() ​

findMany(filter?, options?): Promise<WithId<ModelType>[]>

Defined in: src/model/index.ts:1234

Parameters ​

filter? ​

Filter<ModelType> = {}

options? ​

FindOptions = {}

Returns ​

Promise<WithId<ModelType>[]>


getCollection() ​

getCollection(): Collection<ModelType>

Defined in: src/model/index.ts:1065

Escape hatch honesto: devolve a Collection<ModelType> crua do driver oficial. Reaproveita o mesmo fail-loud pré-conexão de MongoatError ("Database not connected — call db.connect() first") usado internamente por todos os métodos CRUD.

ATENÇÃO — bypass DELIBERADO e TOTAL: a Collection retornada não passa pelo pipeline de hooks (pre/post nunca disparam para chamadas feitas diretamente nela) nem pelo gating de allowedMethods (esta função nunca é adicionada ao enum METHODS, então o Proxy de gating de métodos já a deixa passar sem checagem nenhuma). Ao chamar getCollection() você saiu da zona segura do ODM — agora é o driver puro, coerente com o core value do Mongoat de nunca bloquear o acesso ao driver nativo do MongoDB.

Returns ​

Collection<ModelType>

A Collection<ModelType> nativa do driver mongodb.


insert() ​

insert(document, options?): Promise<EnhancedOmit<ModelType, "_id"> & object & DefaultProperties>

Defined in: src/model/index.ts:1283

Parameters ​

document ​

OptionalUnlessRequiredId<ModelType>

options? ​

InsertOneOptions = {}

Returns ​

Promise<EnhancedOmit<ModelType, "_id"> & object & DefaultProperties>


insertMany() ​

insertMany(documents, options?): Promise<InsertManyResult<ModelType>>

Defined in: src/model/index.ts:1335

insertMany runs pre hooks PER DOCUMENT — parallelized ACROSS documents, while the pre hooks of the SAME document run sequentially. Post hooks run ONCE for the whole batch, against a single ctx.result (InsertManyResult) — there is no per-document result to hand each post hook.

Parameters ​

documents ​

OptionalUnlessRequiredId<ModelType>[]

options? ​

BulkWriteOptions = {}

Returns ​

Promise<InsertManyResult<ModelType>>


paginate() ​

paginate(page, pageSize): Promise<WithId<ModelType>[]>

Defined in: examples/plugins/augmentation.ts:65

Parameters ​

page ​

number

pageSize ​

number

Returns ​

Promise<WithId<ModelType>[]>


post() ​

post<M>(method, fn, options?): this

Defined in: src/model/index.ts:1080

Type Parameters ​

M ​

M extends METHODS

Parameters ​

method ​

M

fn ​

HookFn<HookContextMap<ModelType>[M]>

options? ​
fireAndForget? ​

boolean

Returns ​

this


pre() ​

pre<M>(method, fn): this

Defined in: src/model/index.ts:1069

Type Parameters ​

M ​

M extends METHODS

Parameters ​

method ​

M

fn ​

HookFn<HookContextMap<ModelType>[M]>

Returns ​

this


total() ​

total(filter?, options?): Promise<number>

Defined in: src/model/index.ts:1508

Parameters ​

filter? ​

Filter<ModelType> = {}

options? ​

CountDocumentsOptions = {}

Returns ​

Promise<number>


update() ​

update(filter, update, options?): Promise<WithId<ModelType> | null>

Defined in: src/model/index.ts:1163

Parameters ​

filter ​

Filter<ModelType>

update ​

UpdateFilter<ModelType>

options? ​

FindOneAndUpdateOptions = {}

Returns ​

Promise<WithId<ModelType> | null>


updateMany() ​

updateMany(filter, update, options?): Promise<UpdateResult<Document>>

Defined in: src/model/index.ts:1201

Parameters ​

filter ​

Filter<ModelType>

update ​

UpdateFilter<ModelType>

options? ​

UpdateOptions = {}

Returns ​

Promise<UpdateResult<Document>>


hasDatabase() ​

static hasDatabase(): boolean

Defined in: src/model/index.ts:1589

Returns ​

boolean


plugin() ​

static plugin(plugin): void

Defined in: src/model/index.ts:1609

Registers a plugin globally — applied to every model, before any per-model plugins array, in registration order.

Must be called before constructing the first model: once any model has been constructed (even a repeated construction that reuses an existing registration), the set of global plugins is considered final and a later call throws instead of silently producing a model set with inconsistent globals applied.

Parameters ​

plugin ​

Plugin<Document>

Returns ​

void


setDatabase() ​

static setDatabase(database): void

Defined in: src/model/index.ts:1593

Parameters ​

database ​

Database

Returns ​

void