* Clones rows matching the condition at the database level via INSERT...SELECT. * Automatically excludes auto-increment PKs, generated columns, and resets version properties. * Returns the cloned entity, registered in the identity map. * * @example * ```ts * // Clone by entity clas
(
entityNameOrEntity: EntityName<Entity> | Entity,
whereOrOverrides?: FilterQuery<NoInfer<Entity>> | EntityData<Entity>,
overridesOrOptions?: EntityData<Entity> | NativeInsertUpdateOptions<Entity>,
options?: NativeInsertUpdateOptions<Entity>,
)
| 1824 | * ``` |
| 1825 | */ |
| 1826 | async clone<Entity extends object>( |
| 1827 | entityNameOrEntity: EntityName<Entity> | Entity, |
| 1828 | whereOrOverrides?: FilterQuery<NoInfer<Entity>> | EntityData<Entity>, |
| 1829 | overridesOrOptions?: EntityData<Entity> | NativeInsertUpdateOptions<Entity>, |
| 1830 | options?: NativeInsertUpdateOptions<Entity>, |
| 1831 | ): Promise<Entity> { |
| 1832 | const em = this.getContext(false); |
| 1833 | let entityName: EntityName<Entity>; |
| 1834 | let where: FilterQuery<Entity>; |
| 1835 | let overrides: EntityData<Entity> | undefined; |
| 1836 | |
| 1837 | if (Utils.isEntity(entityNameOrEntity)) { |
| 1838 | // clone(entity, overrides?, options?) |
| 1839 | entityName = (entityNameOrEntity as Dictionary).constructor; |
| 1840 | where = helper(entityNameOrEntity).getPrimaryKey() as FilterQuery<Entity>; |
| 1841 | overrides = whereOrOverrides as EntityData<Entity>; |
| 1842 | options = overridesOrOptions as NativeInsertUpdateOptions<Entity>; |
| 1843 | } else { |
| 1844 | // clone(EntityClass, where, overrides?, options?) |
| 1845 | entityName = entityNameOrEntity; |
| 1846 | where = whereOrOverrides as FilterQuery<Entity>; |
| 1847 | overrides = overridesOrOptions as EntityData<Entity>; |
| 1848 | } |
| 1849 | |
| 1850 | options ??= {}; |
| 1851 | em.prepareOptions(options); |
| 1852 | |
| 1853 | const meta = em.metadata.get<Entity>(entityName); |
| 1854 | const res = await em.driver.nativeClone<Entity>(entityName, where, overrides, { |
| 1855 | ctx: em.#transactionContext, |
| 1856 | ...options, |
| 1857 | }); |
| 1858 | |
| 1859 | const pk = res.insertId ?? res.row?.[meta.primaryKeys[0]]; |
| 1860 | |
| 1861 | return em.findOneOrFail<Entity, any>(entityName, pk as any, { |
| 1862 | schema: options.schema, |
| 1863 | }); |
| 1864 | } |
| 1865 | |
| 1866 | /** |
| 1867 | * Fires native multi-insert query. Calling this has no side effects on the context (identity map). |
nothing calls this directly
no test coverage detected