* 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> = {},
)
| 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). |
no test coverage detected