Options
All
  • Public
  • Public/Protected
  • All
Menu
label

COMMON

interface

Documentable

description

This concern requires the presence of fields that consist in delivering documentable information. This type of information is persisted in MongoDB collection documents.

e.g. alongside StateSchema, we define StateDocument which is a mixin that comprises of State and this Documentable class.

since

v0.2.0

Hierarchy

Index

Constructors

  • Parameters

    • Optional doc: any

    Returns Documentable

Properties

$locals: Record<string, unknown>

Empty object that you can use for storing properties on the document. This is handy for passing data to middleware without conflicting with Mongoose internals.

$op: "remove" | "validate" | "save"

A string containing the current operation that Mongoose is executing on this document. Can be null, 'save', 'validate', or 'remove'.

$where: Record<string, unknown>

Set this property to add additional query filters when Mongoose saves this document and isNew is false.

__v?: any

This documents __v.

_id?: any

This documents _id.

baseModelName?: string

If this is a discriminator model, baseModelName is the name of the base model.

collection: Collection<Document>

Collection the model uses.

db: Connection

Connection the model uses.

errors?: ValidationError

Returns the current validation errors.

id?: any

The string version of this documents _id.

isNew: boolean

Boolean flag specifying if the document is new.

schema: Schema<any, Model<any, any, any, any, any>, {}, {}, {}, {}, "type", {}>

The document's schema.

Accessors

  • get toQuery(): Record<string, unknown>
  • This method must return an object that can be used as the value for the mongodb find operations. Typically, this method returns an object where keys are primary key(s) of the entity and values are those of the document represented by the current instance.

    If not overloaded in child classes, we use the default auto-generated id column as it is always present with mongodb.

    Returns Record<string, unknown>

    The individual document data that is used in a query.

Methods

  • $assertPopulated<Paths>(path: string | string[], values?: Partial<Paths>): Omit<Documentable, keyof Paths> & Paths
  • Assert that a given path or paths is populated. Throws an error if not populated.

    Type Parameters

    • Paths = {}

    Parameters

    • path: string | string[]
    • Optional values: Partial<Paths>

    Returns Omit<Documentable, keyof Paths> & Paths

  • $getAllSubdocs(): Document<any, any, any>[]
  • Returns Document<any, any, any>[]

  • $getPopulatedDocs(): Document<any, any, any>[]
  • Returns an array of all populated documents associated with the query

    Returns Document<any, any, any>[]

  • $ignore(path: string): void
  • Don't run validation on this path or persist changes to this path.

    Parameters

    • path: string

    Returns void

  • Increments the numeric value at path by the given val. When you call save() on this document, Mongoose will send a $inc as opposed to a $set.

    Parameters

    • path: string | string[]
    • Optional val: number

    Returns Documentable

  • $isDefault(path: string): boolean
  • Checks if a path is set to its default.

    Parameters

    • path: string

    Returns boolean

  • $isDeleted(val?: boolean): boolean
  • Getter/setter, determines whether the document was removed or not.

    Parameters

    • Optional val: boolean

    Returns boolean

  • $isEmpty(path: string): boolean
  • Returns true if the given path is nullish or only contains empty objects. Useful for determining whether this subdoc will get stripped out by the minimize option.

    Parameters

    • path: string

    Returns boolean

  • $isValid(path: string): boolean
  • Checks if a path is invalid

    Parameters

    • path: string

    Returns boolean

  • $markValid(path: string): void
  • Marks a path as valid, removing existing validation errors.

    Parameters

    • path: string

    Returns void

  • $model<ModelType>(name: string): ModelType
  • Returns the model with the given name on this document's associated connection.

    Type Parameters

    • ModelType = Model<unknown, {}, {}, {}, any>

    Parameters

    • name: string

    Returns ModelType

  • $parent(): Document<any, any, any>
  • If this document is a subdocument or populated document, returns the document's parent. Returns undefined otherwise.

    Returns Document<any, any, any>

  • $session(session?: ClientSession): ClientSession
  • Getter/setter around the session associated with this document. Used to automatically set session if you save() a doc that you got from a query with an associated session.

    Parameters

    • Optional session: ClientSession

    Returns ClientSession

  • $set(path: string, val: any, type: any, options?: DocumentSetOptions): Documentable
  • $set(path: string, val: any, options?: DocumentSetOptions): Documentable
  • $set(value: any): Documentable
  • Alias for set(), used internally to avoid conflicts

    Parameters

    • path: string
    • val: any
    • type: any
    • Optional options: DocumentSetOptions

    Returns Documentable

  • Parameters

    • path: string
    • val: any
    • Optional options: DocumentSetOptions

    Returns Documentable

  • Parameters

    • value: any

    Returns Documentable

  • delete(options: QueryOptions<unknown>, callback: Callback<any>): void
  • delete(callback: Callback<any>): void
  • delete(options?: QueryOptions<unknown>): any
  • Removes this document from the db.

    Parameters

    • options: QueryOptions<unknown>
    • callback: Callback<any>

    Returns void

  • Parameters

    • callback: Callback<any>

    Returns void

  • Parameters

    • Optional options: QueryOptions<unknown>

    Returns any

  • deleteOne(options: QueryOptions<unknown>, callback: Callback<any>): void
  • deleteOne(callback: Callback<any>): void
  • deleteOne(options?: QueryOptions<unknown>): any
  • Removes this document from the db.

    Parameters

    • options: QueryOptions<unknown>
    • callback: Callback<any>

    Returns void

  • Parameters

    • callback: Callback<any>

    Returns void

  • Parameters

    • Optional options: QueryOptions<unknown>

    Returns any

  • Takes a populated field and returns it to its unpopulated state. If called with no arguments, then all populated fields are returned to their unpopulated state.

    Parameters

    • Optional path: string | string[]

    Returns Documentable

  • directModifiedPaths(): string[]
  • Returns the list of paths that have been directly modified. A direct modified path is a path that you explicitly set, whether via doc.foo = 'bar', Object.assign(doc, { foo: 'bar' }), or doc.set('foo', 'bar').

    Returns string[]

  • equals(doc: Document<any, any, any>): boolean
  • Returns true if this document is equal to another document.

    Documents are considered equal when they have matching _ids, unless neither document has an _id, in which case this function falls back to using deepEqual().

    Parameters

    • doc: Document<any, any, any>

    Returns boolean

  • get(path: string, type?: any, options?: any): any
  • Returns the value of a path.

    Parameters

    • path: string
    • Optional type: any
    • Optional options: any

    Returns any

  • Returns the changes that happened to the document in the format that will be sent to MongoDB.

    Returns UpdateQuery<Documentable>

  • Signal that we desire an increment of this documents version.

    Returns Documentable

  • Initializes the document without setters or marking anything modified. Called internally after a document is returned from mongodb. Normally, you do not need to call this function on your own.

    Parameters

    • obj: AnyObject
    • Optional opts: AnyObject
    • Optional callback: Callback<Documentable>

    Returns Documentable

  • invalidate(path: string, errorMsg: string | NativeError, value?: any, kind?: string): NativeError
  • Marks a path as invalid, causing validation to fail.

    Parameters

    • path: string
    • errorMsg: string | NativeError
    • Optional value: any
    • Optional kind: string

    Returns NativeError

  • isDirectModified(path: string | string[]): boolean
  • Returns true if path was directly set and modified, else false.

    Parameters

    • path: string | string[]

    Returns boolean

  • isDirectSelected(path: string): boolean
  • Checks if path was explicitly selected. If no projection, always returns true.

    Parameters

    • path: string

    Returns boolean

  • isInit(path: string): boolean
  • Checks if path is in the init state, that is, it was set by Document#init() and not modified since.

    Parameters

    • path: string

    Returns boolean

  • isModified(path?: string | string[]): boolean
  • Returns true if any of the given paths are modified, else false. If no arguments, returns true if any path in this document is modified.

    Parameters

    • Optional path: string | string[]

    Returns boolean

  • isSelected(path: string): boolean
  • Checks if path was selected in the source query which initialized this document.

    Parameters

    • path: string

    Returns boolean

  • markModified(path: string, scope?: any): void
  • Marks the path as having pending changes to write to the db.

    Parameters

    • path: string
    • Optional scope: any

    Returns void

  • modifiedPaths(options?: { includeChildren?: boolean }): string[]
  • Returns the list of paths that have been modified.

    Parameters

    • Optional options: { includeChildren?: boolean }
      • Optional includeChildren?: boolean

    Returns string[]

  • Overwrite all values in this document with the values of obj, except for immutable properties. Behaves similarly to set(), except for it unsets all properties that aren't in obj.

    Parameters

    • obj: AnyObject

    Returns Documentable

  • populate<Paths>(path: string | PopulateOptions | (string | PopulateOptions)[]): Promise<MergeType<Documentable, Paths>>
  • populate<Paths>(path: string | PopulateOptions | (string | PopulateOptions)[], callback: Callback<MergeType<Documentable, Paths>>): void
  • populate<Paths>(path: string, select?: string | AnyObject, model?: Model<any, {}, {}, {}, any>, match?: AnyObject, options?: PopulateOptions): Promise<MergeType<Documentable, Paths>>
  • populate<Paths>(path: string, select?: string | AnyObject, model?: Model<any, {}, {}, {}, any>, match?: AnyObject, options?: PopulateOptions, callback?: Callback<MergeType<Documentable, Paths>>): void
  • Populates document references.

    Type Parameters

    • Paths = {}

    Parameters

    • path: string | PopulateOptions | (string | PopulateOptions)[]

    Returns Promise<MergeType<Documentable, Paths>>

  • Type Parameters

    • Paths = {}

    Parameters

    • path: string | PopulateOptions | (string | PopulateOptions)[]
    • callback: Callback<MergeType<Documentable, Paths>>

    Returns void

  • Type Parameters

    • Paths = {}

    Parameters

    • path: string
    • Optional select: string | AnyObject
    • Optional model: Model<any, {}, {}, {}, any>
    • Optional match: AnyObject
    • Optional options: PopulateOptions

    Returns Promise<MergeType<Documentable, Paths>>

  • Type Parameters

    • Paths = {}

    Parameters

    • path: string
    • Optional select: string | AnyObject
    • Optional model: Model<any, {}, {}, {}, any>
    • Optional match: AnyObject
    • Optional options: PopulateOptions
    • Optional callback: Callback<MergeType<Documentable, Paths>>

    Returns void

  • populated(path: string): any
  • Gets _id(s) used during population of the given path. If the path was not populated, returns undefined.

    Parameters

    • path: string

    Returns any

  • remove(options: QueryOptions<unknown>, callback: Callback<any>): void
  • remove(callback: Callback<any>): void
  • remove(options?: QueryOptions<unknown>): Promise<Documentable>
  • Removes this document from the db.

    Parameters

    • options: QueryOptions<unknown>
    • callback: Callback<any>

    Returns void

  • Parameters

    • callback: Callback<any>

    Returns void

  • Parameters

    • Optional options: QueryOptions<unknown>

    Returns Promise<Documentable>

  • replaceOne(replacement?: AnyObject, options?: QueryOptions<unknown>, callback?: Callback<any>): Query<any, Documentable, {}, Documentable>
  • Sends a replaceOne command with this document _id as the query selector.

    Parameters

    • Optional replacement: AnyObject
    • Optional options: QueryOptions<unknown>
    • Optional callback: Callback<any>

    Returns Query<any, Documentable, {}, Documentable>

  • Saves this document by inserting a new document into the database if document.isNew is true, or sends an updateOne operation with just the modified paths if isNew is false.

    Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    • Optional options: SaveOptions

    Returns Promise<Documentable>

  • Sets the value of a path, or many paths.

    Parameters

    • path: string
    • val: any
    • type: any
    • Optional options: any

    Returns Documentable

  • Parameters

    • path: string
    • val: any
    • Optional options: any

    Returns Documentable

  • Parameters

    • value: any

    Returns Documentable

  • toJSON<T>(options?: ToObjectOptions & { flattenMaps?: true }): FlattenMaps<T>
  • toJSON<T>(options: ToObjectOptions & { flattenMaps: false }): T
  • The return value of this method is used in calls to JSON.stringify(doc).

    Type Parameters

    • T = LeanDocument<any>

    Parameters

    • Optional options: ToObjectOptions & { flattenMaps?: true }

    Returns FlattenMaps<T>

  • Type Parameters

    • T = LeanDocument<any>

    Parameters

    • options: ToObjectOptions & { flattenMaps: false }

    Returns T

  • toObject<T>(options?: ToObjectOptions): Require_id<T>
  • Converts this document into a plain-old JavaScript object (POJO).

    Type Parameters

    • T = LeanDocument<any>

    Parameters

    • Optional options: ToObjectOptions

    Returns Require_id<T>

  • unmarkModified(path: string): void
  • Clears the modified state on the specified path.

    Parameters

    • path: string

    Returns void

  • Sends an update command with this document _id as the query selector.

    Parameters

    • Optional update: UpdateQuery<Documentable> | UpdateWithAggregationPipeline
    • Optional options: QueryOptions<unknown>
    • Optional callback: Callback<any>

    Returns Query<any, Documentable, {}, Documentable>

  • updateOne(update?: UpdateQuery<Documentable> | UpdateWithAggregationPipeline, options?: QueryOptions<unknown>, callback?: Callback<any>): Query<any, Documentable, {}, Documentable>
  • Sends an updateOne command with this document _id as the query selector.

    Parameters

    • Optional update: UpdateQuery<Documentable> | UpdateWithAggregationPipeline
    • Optional options: QueryOptions<unknown>
    • Optional callback: Callback<any>

    Returns Query<any, Documentable, {}, Documentable>

  • validate(pathsToValidate: PathsToValidate, options: AnyObject, callback: CallbackWithoutResult): void
  • validate(pathsToValidate: PathsToValidate, callback: CallbackWithoutResult): void
  • validate(callback: CallbackWithoutResult): void
  • validate(pathsToValidate?: PathsToValidate, options?: AnyObject): Promise<void>
  • validate(options: { pathsToSkip?: pathsToSkip }): Promise<void>
  • Executes registered validation rules for this document.

    Parameters

    • pathsToValidate: PathsToValidate
    • options: AnyObject
    • callback: CallbackWithoutResult

    Returns void

  • Parameters

    • pathsToValidate: PathsToValidate
    • callback: CallbackWithoutResult

    Returns void

  • Parameters

    • callback: CallbackWithoutResult

    Returns void

  • Parameters

    • Optional pathsToValidate: PathsToValidate
    • Optional options: AnyObject

    Returns Promise<void>

  • Parameters

    • options: { pathsToSkip?: pathsToSkip }
      • Optional pathsToSkip?: pathsToSkip

    Returns Promise<void>

  • validateSync(options: { pathsToSkip?: pathsToSkip }): ValidationError
  • validateSync(pathsToValidate?: PathsToValidate, options?: AnyObject): ValidationError
  • Executes registered validation rules (skipping asynchronous validators) for this document.

    Parameters

    • options: { pathsToSkip?: pathsToSkip }
      • [k: string]: any
      • Optional pathsToSkip?: pathsToSkip

    Returns ValidationError

  • Parameters

    • Optional pathsToValidate: PathsToValidate
    • Optional options: AnyObject

    Returns ValidationError

Generated using TypeDoc