@iamcalegari/mongoat / Model
Class: Model<ModelType>
Defined in: src/model/index.ts:295
Type Parameters
ModelType
ModelType extends Document = Document
Constructors
Constructor
new Model<
ModelType>(props):Model<ModelType>
Defined in: src/model/index.ts:383
Parameters
props
CreateModelProps<ModelType>
Returns
Model<ModelType>
Properties
allowedMethods
allowedMethods:
METHODS[]
Defined in: src/model/index.ts:308
collectionName
collectionName:
string
Defined in: src/model/index.ts:296
documentDefaults
documentDefaults:
DocumentDefaults<ModelType>
Defined in: src/model/index.ts:310
hooks
hooks:
HookRegistry<ModelType>
Defined in: src/model/index.ts:326
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:298
methods
methods:
string[]
Defined in: src/model/index.ts:306
onHookError
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:336
Fallback destination for fireAndForget post-hook rejections — resolved once in the constructor from props.onHookError, falling back to console.error so an error never disappears in total silence. Applied consistently across all CRUD methods, never per-method.
validationAction
validationAction:
string
Defined in: src/model/index.ts:302
validationLevel
validationLevel:
string
Defined in: src/model/index.ts:304
validator
validator:
object
Defined in: src/model/index.ts:300
$jsonSchema
$jsonSchema:
ModelValidationSchema
Methods
aggregate()
aggregate(
pipeline,options?):Promise<Document[]>
Defined in: src/model/index.ts:771
Parameters
pipeline
Document[]
options?
AggregateOptions = {}
Returns
Promise<Document[]>
bulkWrite()
bulkWrite(
operations,options?):Promise<BulkWriteResult>
Defined in: src/model/index.ts:1156
Parameters
operations
AnyBulkWriteOperation<ModelType>[]
options?
BulkWriteOptions = {}
Returns
Promise<BulkWriteResult>
delete()
delete(
filter,options?):Promise<WithId<ModelType> |null>
Defined in: src/model/index.ts:1104
Parameters
filter
Filter<ModelType>
options?
FindOneAndDeleteOptions = {}
Returns
Promise<WithId<ModelType> | null>
deleteMany()
deleteMany(
filter,options?):Promise<DeleteResult>
Defined in: src/model/index.ts:885
Parameters
filter
Filter<ModelType>
options?
DeleteOptions = {}
Returns
Promise<DeleteResult>
find()
find(
filter?,options?):Promise<WithId<ModelType> |null>
Defined in: src/model/index.ts:1041
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:1064
Parameters
documentId
string | ObjectId
options?
FindOptions = {}
Returns
Promise<WithId<ModelType> | null>
findMany()
findMany(
filter?,options?):Promise<WithId<ModelType>[]>
Defined in: src/model/index.ts:859
Parameters
filter?
Filter<ModelType> = {}
options?
FindOptions = {}
Returns
Promise<WithId<ModelType>[]>
getCollection()
getCollection():
Collection<ModelType>
Defined in: src/model/index.ts:690
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:908
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:960
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:705
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:694
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:1133
Parameters
filter?
Filter<ModelType> = {}
options?
CountDocumentsOptions = {}
Returns
Promise<number>
update()
update(
filter,update,options?):Promise<WithId<ModelType> |null>
Defined in: src/model/index.ts:788
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:826
Parameters
filter
Filter<ModelType>
update
UpdateFilter<ModelType>
options?
UpdateOptions = {}
Returns
Promise<UpdateResult<Document>>
hasDatabase()
statichasDatabase():boolean
Defined in: src/model/index.ts:1214
Returns
boolean
plugin()
staticplugin(plugin):void
Defined in: src/model/index.ts:1234
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()
staticsetDatabase(database):void
Defined in: src/model/index.ts:1218
Parameters
database
Returns
void