[Vuejs]-Should i fetch single items in vuex with getters or with actions and SQL statements

0👍

First of all read up on sql injection. Your second endpoint is currently unsafe and can be used to compromise any and all data in your database.

Besides that, the following line does not do what you think it does:

onecustomer: (state) => (id) => this.state.customers.filter(customer => customer.id = id)

In this line you assign id to customer.id for each customer, and return an array. You probably want something like this

onecustomer: (state) => (id) => this.state.customers.find(customer => customer.id === id)

In general, doing network requests is an expensive operation. You need to send a request, wait for the response, then parse the response. Your second option is better in the sense that you only send the data once. In most cases however you paginate such queries and you might not have the data available when you load a single customer.

If you preload all the data, then having one request to load all customers is all you need. If you do not preload all data, just load a single customer when you do not have the data already. In most cases, you need the two endpoints you have in your first option.

Leave a comment