Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do nothing if em.insertMany() is passed an empty array #4640

Closed
squivix opened this issue Aug 19, 2023 · 1 comment
Closed

Do nothing if em.insertMany() is passed an empty array #4640

squivix opened this issue Aug 19, 2023 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@squivix
Copy link

squivix commented Aug 19, 2023

Is your feature request related to a problem? Please describe.
When calling em.insertMany() with an empty array instead of doing nothing it tries to insert anyway and generates an invalid query, resulting in:

insert into "entity" ("id") values  returning "id" - syntax error at or near "returning"

Describe the solution you'd like
I want it to ignore an empty array of entities. I know it's supposed to be a native insert query that provides direct access to the db, but I think this simple convenience like this is not too much to ask.

Describe alternatives you've considered
I can always wrap my call in a check. For example:

const entityAs = await this.em.find(EntityA, filters);
if (entityAs.length > 0)
    await this.em.insertMany(PivotEntityAEntityB, entityAs.map(entityA => ({entityA, entityB: entityB})));

but it's wordy

Additional context
The invalid SQL query mentioned earlier is encountered in miko-orm 5.6.7.

Meanwhile in 6.0.0-dev.87 I seem to get a TypeError instead:

TypeError: Cannot use 'in' operator to search for 'id' in undefined
     at /home/user/WebstormProjects/project/node_modules/@mikro-orm/knex/AbstractSqlDriver.js:318:45
     at Array.filter (<anonymous>)
     at PostgreSqlDriver.nativeInsertMany (/home/user/WebstormProjects/project/node_modules/@mikro-orm/knex/AbstractSqlDriver.js:318:18)
     at SqlEntityManager.insertMany (/home/user/WebstormProjects/project/node_modules/@mikro-orm/core/EntityManager.js:850:37)
@squivix squivix added the enhancement New feature or request label Aug 19, 2023
@B4nan B4nan added help wanted Extra attention is needed good first issue Good for newcomers labels Aug 21, 2023
@rubiin
Copy link
Contributor

rubiin commented Aug 22, 2023

@B4nan

if (data === undefined) {
I think we can also add a length check for data in here

@B4nan B4nan closed this as completed in 2f65bc8 Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants