MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / insert

Method insert

packages/core/src/EntityManager.ts:1764–1806  ·  view source on GitHub ↗

* Fires native insert query. Calling this has no side effects on the context (identity map).

(
    entityNameOrEntity: EntityName<Entity> | Entity,
    data?: RequiredEntityData<Entity> | Entity,
    options: NativeInsertUpdateOptions<Entity> = {},
  )

Source from the content-addressed store, hash-verified

1762 * Fires native insert query. Calling this has no side effects on the context (identity map).
1763 */
1764 async insert<Entity extends object>(
1765 entityNameOrEntity: EntityName<Entity> | Entity,
1766 data?: RequiredEntityData<Entity> | Entity,
1767 options: NativeInsertUpdateOptions<Entity> = {},
1768 ): Promise<Primary<Entity>> {
1769 const em = this.getContext(false);
1770 em.prepareOptions(options);
1771
1772 let entityName: EntityName<Entity>;
1773
1774 if (data === undefined) {
1775 entityName = (entityNameOrEntity as Dictionary).constructor;
1776 data = entityNameOrEntity as Entity;
1777 } else {
1778 entityName = entityNameOrEntity as EntityName<Entity>;
1779 }
1780
1781 if (Utils.isEntity<Entity>(data)) {
1782 if (options.schema && helper(data).getSchema() == null) {
1783 helper(data).setSchema(options.schema);
1784 }
1785
1786 if (!helper(data).__managed) {
1787 em.#unitOfWork.unsetIdentity(data);
1788 }
1789
1790 const meta = helper(data).__meta;
1791 const payload = em.#comparator.prepareEntity(data);
1792 const cs = new ChangeSet(data, ChangeSetType.CREATE, payload, meta);
1793 await em.#unitOfWork.getChangeSetPersister().executeInserts([cs], { ctx: em.#transactionContext, ...options });
1794
1795 return cs.getPrimaryKey()!;
1796 }
1797
1798 data = QueryHelper.processObjectParams(data);
1799 validateParams(data, 'insert data');
1800 const res = await em.driver.nativeInsert<Entity>(entityName, data as EntityData<Entity>, {
1801 ctx: em.#transactionContext,
1802 ...options,
1803 });
1804
1805 return res.insertId!;
1806 }
1807
1808 /**
1809 * Clones rows matching the condition at the database level via INSERT...SELECT.

Callers

nothing calls this directly

Calls 13

getContextMethod · 0.95
getPrimaryKeyMethod · 0.95
helperFunction · 0.85
validateParamsFunction · 0.85
unsetIdentityMethod · 0.80
prepareEntityMethod · 0.80
executeInsertsMethod · 0.80
getChangeSetPersisterMethod · 0.80
processObjectParamsMethod · 0.80
getSchemaMethod · 0.65
setSchemaMethod · 0.65
prepareOptionsMethod · 0.45

Tested by

no test coverage detected