user2869736
user2869736

Reputation: 133

Why loopback 4 by default, doesnt support multiple insertion when I create Loopback 4 default REST CRUD controller?

I loopback 3, I was able to insert multiple records at once but in loopback 4 default REST Controller with CRUD functions don't support this. I can use createAll instead of create and do little changes to achieve this but I am curious to know is there any specific reason to prevent multiple records creation at one go?

Upvotes: 0

Views: 495

Answers (1)

Rifa Achrinza
Rifa Achrinza

Reputation: 1585

One main reason could be that createAll() isn't guaranteed atomic. As the default Juggler ORM is shared between both LoopBack 3 and 4, this issue applies to both versions of LoopBack.

The core explanation

At a minimum, Juggler connectors need to implement the basic CRUD functions such as create() and delete(). In addition, connectors may also implement "compound" functions such as createAll() and deleteAll().

However, not all connectors implement these "compound" functions. In these cases, Juggler will quietly create "stub" functions of the same name that wraps around the basic CRUD functions. For example, createAll() may just be a loop calling create() multiple times. This is useful as it means that developers can assume that those functions will exist. However, this breaks ACID compliance.

What about transactions?

Transactions are used to guarantee atomic operations based on a defined scope. However, not all connectors implement this - MongoDB being an example. Unlike "compound" functions, Juggler will not try to stub transactions. This means that the Transaction API may not exist for connectors that don't implement it.

Why not throw an error?

Throwing an error when createAll() isn't atomic or when the transactions API isn't implemented means that developers may need to write more integration tests to account for this possibility to provide database interoperability.

TLDR;

  1. createAll() isn't guaranteed atomic without transactions.
  2. Transactions aren't implemented in all connectors
  3. It's not exposed in the default CRUD controller to encourage developers to understand the quirks of createAll() instead of just using it out-of-the-box and potentially shooting themselves in the foot.

Upvotes: 1

Related Questions