Options
All
  • Public
  • Public/Protected
  • All
Menu

Simple abstraction for MongoDB.

All filter arguments require the actual class instance value.

So, if you accept JSON, make sure to run const filter = partialPlainToClass(Model, {...}) first.

Hierarchy

  • Database

Index

Constructors

constructor

  • new Database(connection: Connection, defaultDatabaseName?: string): Database
  • Parameters

    • connection: Connection
    • Default value defaultDatabaseName: string = "app"

    Returns Database

Methods

add

  • add<T>(classType: ClassType<T>, item: T): Promise<boolean>
  • Adds a new item to the database. Sets _id if defined at your entity.

    Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • item: T

    Returns Promise<boolean>

close

  • close(): Promise<void>
  • Returns Promise<void>

count

  • count<T>(classType: ClassType<T>, filter?: object): Promise<number>
  • Returns the count of items in the database, that fit that given filter.

    Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • Default value filter: object = {}
      • [field: string]: any

    Returns Promise<number>

cursor

  • cursor<T>(classType: ClassType<T>, filter?: undefined | object, toClass?: boolean): Promise<Cursor<T>>
  • Returns a mongodb cursor, which you can further modify and then call toArray() to retrieve the documents.

    Depending on toClass to the cursor is a map() call applied, mongoToClass when toClass=true, partialMongoToPlain when toClass=false.

    Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • Optional filter: undefined | object
    • Default value toClass: boolean = true

    Returns Promise<Cursor<T>>

deleteMany

  • deleteMany<T>(classType: ClassType<T>, filter: object): Promise<void>
  • Removes ALL items from the database that matches given filter.

    Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • filter: object
      • [field: string]: any

    Returns Promise<void>

deleteOne

  • deleteOne<T>(classType: ClassType<T>, filter: object): Promise<void>
  • Removes ONE item from the database that matches given filter.

    Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • filter: object
      • [field: string]: any

    Returns Promise<void>

dropDatabase

  • dropDatabase(dbName: string): Promise<void>
  • Parameters

    • dbName: string

    Returns Promise<void>

find

  • find<T>(classType: ClassType<T>, filter?: undefined | object, toClass?: boolean): Promise<T[]>
  • Returns all available documents for given filter as instance classes.

    Use toClass=false to return the raw documents. Use find().map(v => mongoToPlain(classType, v)) so you can easily return that values back to the HTTP client very fast.

    Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • Optional filter: undefined | object
    • Default value toClass: boolean = true

    Returns Promise<T[]>

get

  • get<T>(classType: ClassType<T>, filter: object): Promise<T | undefined>
  • Returns one instance based on given filter, or null when not found.

    Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • filter: object
      • [field: string]: any

    Returns Promise<T | undefined>

getCollection

  • getCollection<T>(classType: ClassType<T>): Collection<T>
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>

    Returns Collection<T>

getCollectionName

  • getCollectionName<T>(classType: ClassType<T>): string
  • The naming policy defines the collection name, so we need typeorm.Connection for it.

    Type parameters

    • T

    Parameters

    • classType: ClassType<T>

    Returns string

has

  • has<T>(classType: ClassType<T>, filter?: object): Promise<boolean>
  • Returns true when at least one item in the database is found that fits given filter.

    Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • Default value filter: object = {}
      • [field: string]: any

    Returns Promise<boolean>

patch

  • patch<T>(classType: ClassType<T>, filter: object, patch: Partial<T>): Promise<number | undefined>
  • Patches an entity in the database and returns the new version number if successful, or null if not successful. It's possible to provide nested key-value pairs, where the path should be based on dot symbol separation.

    Example

    await patch(SimpleEntity, { ['children.0.label']: 'Changed label' });

    Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • filter: object
      • [field: string]: any
    • patch: Partial<T>

    Returns Promise<number | undefined>

remove

  • remove<T>(classType: ClassType<T>, id: string): Promise<boolean>
  • Removes ONE item from the database that has the given id. You need to use @ID() decorator for at least and max one property at your entity to use this method.

    Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • id: string

    Returns Promise<boolean>

update

  • update<T>(classType: ClassType<T>, update: T, filter?: undefined | object): Promise<number | undefined>
  • Updates an entity in the database and returns the new version number if successful, or null if not successful.

    If no filter is given, the ID of update is used.

    Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • update: T
    • Optional filter: undefined | object

    Returns Promise<number | undefined>

Generated using TypeDoc