Skip to content

Commit

Permalink
update relations spec to have more examples of preload options
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Nov 1, 2024
1 parent c1085f4 commit 128a245
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion spec/relations_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -1816,7 +1816,7 @@ describe "lapis.db.model.relations", ->
[[SELECT * FROM "user_data" WHERE "user_id" IN (11, 12)]]
}, sorted: true

it "passes preload opts", ->
it "passes preload opts to fetch relation", ->
local preload_objects, preload_opts

class Item extends Model
Expand All @@ -1834,6 +1834,7 @@ describe "lapis.db.model.relations", ->
preload items, things: {
[preload]: {
fields: "blue"
random: "option"
}
}

Expand All @@ -1842,8 +1843,28 @@ describe "lapis.db.model.relations", ->

assert.same {
fields: "blue"
random: "option"
}, preload_opts

it "with skip_included preload option", ->
models.Items = class Items extends Model
@relations: {
{"parents", has_many: "Items", key: "parent_id"}
}

items = {
Items\load { id: 123, parents: {} } -- this one already has it
Items\load { id: 234 }
}

preload items, parents: {
[preload]: { fields: "what", skip_included: true }
}

assert_queries {
[[SELECT what FROM "items" WHERE "parent_id" IN (234)]]
}

describe "optional relations", ->
it "single optional relation", ->
class OtherThing extends Model
Expand Down

0 comments on commit 128a245

Please sign in to comment.