Skip to main content
Version: 5.9

EntityRepository <T>

Hierarchy

  • EntityRepository<T>
    • EntityRepository

Index

Constructors

constructor

  • new EntityRepository<T>(_em: MongoEntityManager<MongoDriver>, entityName: EntityName<T>): MongoEntityRepository<T>
  • Type parameters

    • T: object

    Parameters

    • _em: MongoEntityManager<MongoDriver>
    • entityName: EntityName<T>

    Returns MongoEntityRepository<T>

Methods

aggregate

  • aggregate(pipeline: any[]): Promise<any[]>
  • Shortcut to driver's aggregate method. Available in MongoDriver only.


    Parameters

    • pipeline: any[]

    Returns Promise<any[]>

assign

  • assign(entity: T, data: EntityData<T>, options?: AssignOptions): T
  • Shortcut for wrap(entity).assign(data, { em })


    Parameters

    • entity: T
    • data: EntityData<T>
    • optionaloptions: AssignOptions

    Returns T

canPopulate

  • canPopulate(property: string): boolean
  • Checks whether given property can be populated on the entity.


    Parameters

    • property: string

    Returns boolean

count

  • count<P>(where?: FilterQuery<T>, options?: CountOptions<T, P>): Promise<number>
  • Returns total number of entities matching your where query.


    Type parameters

    • P: string = never

    Parameters

    • where: FilterQuery<T> = ...
    • options: CountOptions<T, P> = {}

    Returns Promise<number>

create

  • create<P>(data: RequiredEntityData<T>, options?: CreateOptions): T
  • Creates new instance of given entity and populates it with given data. The entity constructor will be used unless you provide { managed: true } in the options parameter. The constructor will be given parameters based on the defined constructor of the entity. If the constructor parameter matches a property name, its value will be extracted from data. If no matching property exists, the whole data parameter will be passed. This means we can also define constructor(data: Partial<T>) and em.create() will pass the data into it (unless we have a property named data too).


    Type parameters

    • P = never

    Parameters

    • data: RequiredEntityData<T>
    • optionaloptions: CreateOptions

    Returns T

find

  • find<P>(where: FilterQuery<T>, options?: FindOptions<T, P>): Promise<Loaded<T, P>[]>
  • Finds all entities matching your where query. You can pass additional options via the options parameter.


    Type parameters

    • P: string = never

    Parameters

    • where: FilterQuery<T>
    • optionaloptions: FindOptions<T, P>

    Returns Promise<Loaded<T, P>[]>

findAll

  • findAll<P>(options?: FindOptions<T, P>): Promise<Loaded<T, P>[]>
  • Finds all entities of given type. You can pass additional options via the options parameter.


    Type parameters

    • P: string = never

    Parameters

    • optionaloptions: FindOptions<T, P>

    Returns Promise<Loaded<T, P>[]>

findAndCount

  • findAndCount<P>(where: FilterQuery<T>, options?: FindOptions<T, P>): Promise<[Loaded<T, P>[], number]>
  • Calls em.find() and em.count() with the same arguments (where applicable) and returns the results as tuple where first element is the array of entities and the second is the count.


    Type parameters

    • P: string = never

    Parameters

    • where: FilterQuery<T>
    • optionaloptions: FindOptions<T, P>

    Returns Promise<[Loaded<T, P>[], number]>

findOne

  • findOne<P>(where: FilterQuery<T>, options?: FindOneOptions<T, P>): Promise<null | Loaded<T, P>>
  • Finds first entity matching your where query.


    Type parameters

    • P: string = never

    Parameters

    • where: FilterQuery<T>
    • optionaloptions: FindOneOptions<T, P>

    Returns Promise<null | Loaded<T, P>>

findOneOrFail

  • findOneOrFail<P>(where: FilterQuery<T>, options?: FindOneOrFailOptions<T, P>): Promise<Loaded<T, P>>
  • Finds first entity matching your where query. If nothing found, it will throw an error. You can override the factory for creating this method via options.failHandler locally or via Configuration.findOneOrFailHandler globally.


    Type parameters

    • P: string = never

    Parameters

    • where: FilterQuery<T>
    • optionaloptions: FindOneOrFailOptions<T, P>

    Returns Promise<Loaded<T, P>>

flush

  • flush(): Promise<void>
  • Flushes all changes to objects that have been queued up to now to the database. This effectively synchronizes the in-memory state of managed objects with the database. This method is a shortcut for em.flush(), in other words, it will flush the whole UoW, not just entities registered via this particular repository.

    @deprecated

    this method will be removed in v6, you should work with the EntityManager instead


    Returns Promise<void>

getCollection

  • getCollection(): Collection<T>
  • Returns Collection<T>

getEntityManager

  • getEntityManager(): MongoEntityManager<MongoDriver>
  • Returns the underlying EntityManager instance


    Returns MongoEntityManager<MongoDriver>

getEntityName

  • getEntityName(): string
  • Returns string

getReference

  • getReference<PK>(id: Primary<T>, options: Omit<GetReferenceOptions, wrapped> & { wrapped: true }): IdentifiedReference<T, PK>
  • getReference(id: Primary<T> | Primary<T>[]): T
  • getReference(id: Primary<T>, options: Omit<GetReferenceOptions, wrapped> & { wrapped: false }): T
  • Gets a reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded


    Type parameters

    • PK: string | number | symbol

    Parameters

    • id: Primary<T>
    • options: Omit<GetReferenceOptions, wrapped> & { wrapped: true }

    Returns IdentifiedReference<T, PK>

map

  • map(result: EntityDictionary<T>, options?: { schema?: string }): T
  • Maps raw database result to an entity and merges it to this EntityManager.


    Parameters

    • result: EntityDictionary<T>
    • optionaloptions: { schema?: string }
      • optionalschema: string

    Returns T

merge

  • merge(data: T | EntityData<T>, options?: MergeOptions): T
  • Merges given entity to this EntityManager so it becomes managed. You can force refreshing of existing entities via second parameter. By default it will return already loaded entities without modifying them.


    Parameters

    • data: T | EntityData<T>
    • optionaloptions: MergeOptions

    Returns T

nativeDelete

  • nativeDelete(where: FilterQuery<T>, options?: DeleteOptions<T>): Promise<number>
  • Fires native delete query. Calling this has no side effects on the context (identity map).


    Parameters

    • where: FilterQuery<T>
    • optionaloptions: DeleteOptions<T>

    Returns Promise<number>

nativeInsert

  • nativeInsert(data: T | EntityData<T>, options?: NativeInsertUpdateOptions<T>): Promise<Primary<T>>
  • Fires native insert query. Calling this has no side effects on the context (identity map).


    Parameters

    • data: T | EntityData<T>
    • optionaloptions: NativeInsertUpdateOptions<T>

    Returns Promise<Primary<T>>

nativeUpdate

  • nativeUpdate(where: FilterQuery<T>, data: EntityData<T>, options?: UpdateOptions<T>): Promise<number>
  • Fires native update query. Calling this has no side effects on the context (identity map).


    Parameters

    • where: FilterQuery<T>
    • data: EntityData<T>
    • optionaloptions: UpdateOptions<T>

    Returns Promise<number>

persist

  • persist(entity: Partial<any> | Partial<any>[]): EntityManager<IDatabaseDriver<Connection>>
  • Tells the EntityManager to make an instance managed and persistent. The entity will be entered into the database at or before transaction commit or as a result of the flush operation.

    @deprecated

    this method will be removed in v6, you should work with the EntityManager instead


    Parameters

    • entity: Partial<any> | Partial<any>[]

    Returns EntityManager<IDatabaseDriver<Connection>>

persistAndFlush

  • persistAndFlush(entity: Partial<any> | Partial<any>[]): Promise<void>
  • Persists your entity immediately, flushing all not yet persisted changes to the database too. Equivalent to em.persist(e).flush().

    @deprecated

    this method will be removed in v6, you should work with the EntityManager instead


    Parameters

    • entity: Partial<any> | Partial<any>[]

    Returns Promise<void>

persistLater

  • persistLater(entity: Partial<any> | Partial<any>[]): void
  • Tells the EntityManager to make an instance managed and persistent. The entity will be entered into the database at or before transaction commit or as a result of the flush operation.

    @deprecated

    use persist()


    Parameters

    • entity: Partial<any> | Partial<any>[]

    Returns void

populate

  • populate<P>(entities: T | T[], populate: boolean | (P extends any ? ((P & `${string}.`) extends never ? P : P & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<T, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<T, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${string}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<T, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<T, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<T, never> ? (Exclude<GetStringKey<T, Q, never>, undefined | null> extends unknown ? Exclude<P, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<T, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<T, never> : never : never)[], options?: EntityLoaderOptions<T, P>): Promise<Loaded<T, P>[]>
  • Loads specified relations in batch. This will execute one query for each relation, that will populate it on all of the specified entities.


    Type parameters

    • P: string = never

    Parameters

    • entities: T | T[]
    • populate: boolean | (P extends any ? ((P & `${string}.`) extends never ? P : P & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<T, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<T, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${string}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<T, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<T, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<T, never> ? (Exclude<GetStringKey<T, Q, never>, undefined | null> extends unknown ? Exclude<P, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<T, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<T, never> : never : never)[]
    • optionaloptions: EntityLoaderOptions<T, P>

    Returns Promise<Loaded<T, P>[]>

remove

  • remove(entity: Partial<any>): EntityManager<IDatabaseDriver<Connection>>
  • Marks entity for removal. A removed entity will be removed from the database at or before transaction commit or as a result of the flush operation.

    To remove entities by condition, use em.nativeDelete().

    @deprecated

    this method will be removed in v6, you should work with the EntityManager instead


    Parameters

    • entity: Partial<any>

    Returns EntityManager<IDatabaseDriver<Connection>>

removeAndFlush

  • removeAndFlush(entity: Partial<any>): Promise<void>
  • Removes an entity instance immediately, flushing all not yet persisted changes to the database too. Equivalent to em.remove(e).flush()

    @deprecated

    this method will be removed in v6, you should work with the EntityManager instead


    Parameters

    • entity: Partial<any>

    Returns Promise<void>

removeLater

  • removeLater(entity: Partial<any>): void
  • Marks entity for removal. A removed entity will be removed from the database at or before transaction commit or as a result of the flush operation.

    @deprecated

    use remove()


    Parameters

    • entity: Partial<any>

    Returns void

upsert

  • upsert(entityOrData?: T | EntityData<T>, options?: NativeInsertUpdateOptions<T>): Promise<T>
  • Creates or updates the entity, based on whether it is already present in the database. This method performs an insert on conflict merge query ensuring the database is in sync, returning a managed entity instance. The method accepts either entityName together with the entity data, or just entity instance.

    // insert into "author" ("age", "email") values (33, 'foo@bar.com') on conflict ("email") do update set "age" = 41
    const author = await em.getRepository(Author).upsert({ email: 'foo@bar.com', age: 33 });

    The entity data needs to contain either the primary key, or any other unique property. Let's consider the following example, where Author.email is a unique property:

    // insert into "author" ("age", "email") values (33, 'foo@bar.com') on conflict ("email") do update set "age" = 41
    // select "id" from "author" where "email" = 'foo@bar.com'
    const author = await em.getRepository(Author).upsert({ email: 'foo@bar.com', age: 33 });

    Depending on the driver support, this will either use a returning query, or a separate select query, to fetch the primary key if it's missing from the data.

    If the entity is already present in current context, there won't be any queries - instead, the entity data will be assigned and an explicit flush will be required for those changes to be persisted.


    Parameters

    • optionalentityOrData: T | EntityData<T>
    • optionaloptions: NativeInsertUpdateOptions<T>

    Returns Promise<T>

upsertMany

  • upsertMany(entitiesOrData?: EntityData<T>[] | T[], options?: NativeInsertUpdateOptions<T>): Promise<T[]>
  • Creates or updates the entity, based on whether it is already present in the database. This method performs an insert on conflict merge query ensuring the database is in sync, returning a managed entity instance.

    // insert into "author" ("age", "email") values (33, 'foo@bar.com') on conflict ("email") do update set "age" = 41
    const authors = await em.getRepository(Author).upsertMany([{ email: 'foo@bar.com', age: 33 }, ...]);

    The entity data needs to contain either the primary key, or any other unique property. Let's consider the following example, where Author.email is a unique property:

    // insert into "author" ("age", "email") values (33, 'foo@bar.com'), (666, 'lol@lol.lol') on conflict ("email") do update set "age" = excluded."age"
    // select "id" from "author" where "email" = 'foo@bar.com'
    const author = await em.getRepository(Author).upsertMany([
    { email: 'foo@bar.com', age: 33 },
    { email: 'lol@lol.lol', age: 666 },
    ]);

    Depending on the driver support, this will either use a returning query, or a separate select query, to fetch the primary key if it's missing from the data.

    If the entity is already present in current context, there won't be any queries - instead, the entity data will be assigned and an explicit flush will be required for those changes to be persisted.


    Parameters

    • optionalentitiesOrData: EntityData<T>[] | T[]
    • optionaloptions: NativeInsertUpdateOptions<T>

    Returns Promise<T[]>