Skip to content

Commit

Permalink
memoize computed source key function for composite keys
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Oct 3, 2023
1 parent 6975663 commit 9e18df5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
14 changes: 14 additions & 0 deletions lapis/db/base_model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,20 @@ do
dest_key = dest_key[1]
composite_foreign_key = false
else
do
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #source_key do
local k = source_key[_index_0]
if type(k) == "function" then
_accum_0[_len_0] = _memoize1(k)
else
_accum_0[_len_0] = k
end
_len_0 = _len_0 + 1
end
source_key = _accum_0
end
composite_foreign_key = true
end
else
Expand Down
6 changes: 6 additions & 0 deletions lapis/db/base_model.moon
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ class BaseModel
dest_key = dest_key[1]
false
else
-- memoize all computed source keys
source_key = for k in *source_key
if type(k) == "function"
_memoize1 k
else
k
true
else
false
Expand Down
15 changes: 13 additions & 2 deletions spec/model_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -1117,11 +1117,22 @@ describe "lapis.db.model", ->

mock_query "SELECT", thing_items

called_count_a = 0
called_count_b = 0

ThingItems\include_in things, {
aid: (t) -> t.alpha_id
bid: (t) -> t.beta_id
aid: (t) ->
called_count_a += 1
t.alpha_id

bid: (t) ->
called_count_b += 1
t.beta_id
}

assert.same 5, called_count_a
assert.same 5, called_count_b

assert_queries {
{
[[SELECT * FROM "thing_items" WHERE ("aid", "bid") IN ((100, 201), (101, 202), (101, 203), (102, 204), (102, 205))]]
Expand Down

0 comments on commit 9e18df5

Please sign in to comment.