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

Method insertMany

packages/core/src/EntityManager.ts:1869–1921  ·  view source on GitHub ↗

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

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

Source from the content-addressed store, hash-verified

1867 * Fires native multi-insert query. Calling this has no side effects on the context (identity map).
1868 */
1869 async insertMany<Entity extends object>(
1870 entityNameOrEntities: EntityName<Entity> | Entity[],
1871 data?: RequiredEntityData<Entity>[] | Entity[],
1872 options: NativeInsertUpdateOptions<Entity> = {},
1873 ): Promise<Primary<Entity>[]> {
1874 const em = this.getContext(false);
1875 em.prepareOptions(options);
1876
1877 let entityName: EntityName<Entity>;
1878
1879 if (data === undefined) {
1880 entityName = ((entityNameOrEntities as Entity[])[0] as Dictionary).constructor;
1881 data = entityNameOrEntities as Entity[];
1882 } else {
1883 entityName = entityNameOrEntities as EntityName<Entity>;
1884 }
1885
1886 if (data.length === 0) {
1887 return [];
1888 }
1889
1890 if (Utils.isEntity<Entity>(data[0])) {
1891 const meta = helper<Entity>(data[0]).__meta;
1892 const css = data.map(row => {
1893 if (options.schema && helper(row).getSchema() == null) {
1894 helper(row).setSchema(options.schema);
1895 }
1896
1897 if (!helper(row).__managed) {
1898 em.#unitOfWork.unsetIdentity(row);
1899 }
1900
1901 const payload = em.#comparator.prepareEntity(row) as EntityData<Entity>;
1902 return new ChangeSet<Entity>(row as Entity, ChangeSetType.CREATE, payload, meta);
1903 });
1904 await em.#unitOfWork.getChangeSetPersister().executeInserts(css, { ctx: em.#transactionContext, ...options });
1905
1906 return css.map(cs => cs.getPrimaryKey()!);
1907 }
1908
1909 data = data.map(row => QueryHelper.processObjectParams(row));
1910 data.forEach(row => validateParams(row, 'insert data'));
1911 const res = await em.driver.nativeInsertMany<Entity>(entityName, data as EntityData<Entity>[], {
1912 ctx: em.#transactionContext,
1913 ...options,
1914 });
1915
1916 if (res.insertedIds) {
1917 return res.insertedIds;
1918 }
1919
1920 return [res.insertId];
1921 }
1922
1923 /**
1924 * Fires native update query. Calling this has no side effects on the context (identity map).

Calls 14

getContextMethod · 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
getPrimaryKeyMethod · 0.65
prepareOptionsMethod · 0.45

Tested by

no test coverage detected