lapis icon indicating copy to clipboard operation
lapis copied to clipboard

Model find should allow for a list of columns to retrieve

Open karai17 opened this issue 5 years ago • 2 comments

local get_columns = { fields = "id, email, name" }
local id = 5

-- works
local user = self:find_all({id}, get_columns)

-- wish it worked :(
local user = self:find(id, get_columns)

karai17 avatar Feb 07 '19 15:02 karai17

so I'm considering deprecating find_all, find and select would be the remaining functions. I think it would be okay to add options to find, but it handles two different argument forms, so think I didn't originally add it because it would be a little bizarre

(the select for many items by id would use db.list: Model:select("where id in ?", db.list {1,2,3,4}))

leafo avatar Feb 07 '19 18:02 leafo

The way I view find and select is that find returns a single row and select returns a list of rows (which could have 0, 1, or many rows). So if I want to pull a single entry out of the db, I want to use find and not need to futz with looking through the select list. Otherwise they should probably be functionally similar.

Using find with its first arg format, listing values in the defined order of primary keys, seems a bit clunky to me. I typically find it easier to pass tables into a function if I am just sending a list, which is coincidentally already possible with find.

Perhaps a better implementation woudl be that the first arg is either the id column, or a list of columns and their desired values. The second arg would be the same options arg that can be passed into find_all and select.

Consolidating the argument formats into one format across the whole frame would probably be a good idea.

local user = self:find(1)
-- select * from users where id=1 limit 1

local user = self: find(1, { fields = "id, name, email" }
-- select id, name, email from users where id=1 limit 1

local player = self:find({ user_id=1, name="karai" }, { fields="id, name, birthday" })
-- select id, name, birthday from players where user_id=1 and name='karai' limit 1

karai17 avatar Feb 07 '19 19:02 karai17