Skip to content

Commit

Permalink
only process sub-relations on preload() if value is string or table
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Dec 24, 2022
1 parent 7d1053d commit d86ae21
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
10 changes: 6 additions & 4 deletions lapis/db/model/relations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ local preload
local preload_relation
preload_relation = function(self, objects, name, ...)
local optional
if name:sub(1, 1) == "?" then
if type(name) == "string" and name:sub(1, 1) == "?" then
name = name:sub(2)
optional = true
end
Expand Down Expand Up @@ -79,10 +79,12 @@ preload_homogeneous = function(sub_relations, model, objects, front, ...)
for key, val in pairs(front) do
local _continue_0 = false
repeat
local relation = type(key) == "string" and key or val
local preload_opts = type(val) == "table" and val[preload] or nil
local val_type = type(val)
local key_type = type(key)
local relation = key_type == "string" and key or val
local preload_opts = val_type == "table" and val[preload] or nil
preload_relation(model, objects, relation, preload_opts)
if type(key) == "string" then
if key_type == "string" and (val_type == "string" or val_type == "table") then
local optional, relation_name
if key:sub(1, 1) == "?" then
optional, relation_name = true, key:sub(2)
Expand Down
14 changes: 10 additions & 4 deletions lapis/db/model/relations.moon
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ local preload
-- ...: options passed to preloader
preload_relation = (objects, name, ...) =>
-- relations prefixed with ? are optional loads, we can skip it if it doesn't exist
optional = if name\sub(1,1) == "?"
optional = if type(name) == "string" and name\sub(1,1) == "?"
name = name\sub 2
true

Expand Down Expand Up @@ -61,15 +61,21 @@ preload_homogeneous = (sub_relations, model, objects, front, ...) ->

if type(front) == "table"
for key, val in pairs front
relation = type(key) == "string" and key or val
val_type = type val
key_type = type key

relation = key_type == "string" and key or val

-- this lets you set pass preload opts by using the reference to the
-- preload function as a special key
preload_opts = type(val) == "table" and val[preload] or nil
preload_opts = val_type == "table" and val[preload] or nil

preload_relation model, objects, relation, preload_opts

if type(key) == "string"
-- are there sub-relations to preload?
-- { parent: "child" }
-- { parent: {"child1", "child2"} }
if key_type == "string" and (val_type == "string" or val_type == "table")
optional, relation_name = if key\sub(1,1) == "?"
true, key\sub 2
else
Expand Down

0 comments on commit d86ae21

Please sign in to comment.