totaljs/framework4

INSERT an array of docs to the DB via one query

dispbd opened this issue · 1 comments

The database has an insert method and it is easy to add a new doc to the DB using it. But is there a way to add multiple docs (array) at once through one request?
I'll make a reservation right away - I know how this can be done sequentially or through cycles, etc. But these methods make many requests to the DB files, sequentially editing them - which is not very productive and can be time-consuming.

As an example, in my project, when creating a user, I can add dozens of accounts at once. It would be very cool if it was possible to add them through one function - the idea is that on the framework side there is only one request to a file, but several docs are added at once, which is very productive than dozens of requests. As an example:

  let m1 = {
      "type": "ps_com",
      "nickname": "BourgeoisKY",
      "id": "TEST_bfl1005rs41d",
      "userID": "TEST_400506001oy61b"
    },
    m2 = {
      "type": "ps_es",
      "nickname": "KillLioN",
      "id": "TEST_bfl1006rs40d",
      "userID": "TEST_400506001oy61b"
    };

  await NOSQL('accounts').insert(m1).insert(m2).promise();
  // OR
  await NOSQL('accounts').insert([m1, m2]).promise();

By the way, postgresql has similar functionality - https://i.vgy.me/aBWnym.png
Of course, difficulties can arise if the addition is done through the schemes, but the problem can also be solved - checking each element of the array through a loop.

Hi @dispbd,
Currently, no. But if you call .insert() multiple times, then DB will insert data together (but you must insert the data in a short time between itself). In the background is a queue that processes data, and it writes them on HDD after some time.

Thank you!