Skip to content

@iamcalegari/mongoat / toObjectId

Function: toObjectId()

toObjectId(inputId?): ObjectId

Defined in: src/utils/database.ts:30

Converts a given input into an ObjectId.

Sem argumento (undefined) gera um ObjectId novo e aleatório (new ObjectId()) — uso legítimo para criar um _id novo antes de inserir um documento.

Quando um argumento É fornecido, valida com ObjectId.isValid ANTES de instanciar: bson@7 aceita apenas uma string de 24 hex chars, um ObjectIdLike ou um Uint8Array de 12 bytes — qualquer outra coisa (string malformada, número, array, etc.) lança MongoatValidationError (code: INVALID_OBJECT_ID) em vez de silenciosamente gerar um id aleatório que não bate com nada.

Parameters

inputId?

string | ObjectId | Uint8Array<ArrayBufferLike> | ObjectIdLike

The input value to be converted, which can be a string, ObjectId, ObjectIdLike, or Uint8Array. Omit to generate a new ObjectId.

Returns

ObjectId

A new ObjectId instance derived from the inputId, or a freshly generated one when omitted.