Skip to content

Commit

Permalink
don't allow overwriting default relation clause elements with where o…
Browse files Browse the repository at this point in the history
…ption in has_many relation
  • Loading branch information
leafo committed Jul 17, 2024
1 parent 5cde3b5 commit 951e3fe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
12 changes: 10 additions & 2 deletions lapis/db/model/relations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,11 @@ has_many = function(self, name, opts)
table.insert(additional_clause, where)
else
for k, v in pairs(where) do
additional_clause[k] = v
if type(k) == "number" then
table.insert(additional_clause, v)
else
additional_clause[k] = v
end
end
end
end
Expand All @@ -526,7 +530,11 @@ has_many = function(self, name, opts)
table.insert(additional_clause, more_where)
else
for k, v in pairs(more_where) do
additional_clause[k] = v
if type(k) == "number" then
table.insert(additional_clause, v)
else
additional_clause[k] = v
end
end
end
end
Expand Down
10 changes: 8 additions & 2 deletions lapis/db/model/relations.moon
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,10 @@ has_many = (name, opts) =>
table.insert additional_clause, where
else
for k,v in pairs where
additional_clause[k] = v
if type(k) == "number" -- append array clauses
table.insert additional_clause, v
else
additional_clause[k] = v

if more_where = calling_opts and calling_opts.where
additional_clause = { @@db.clause clause } unless additional_clause
Expand All @@ -409,7 +412,10 @@ has_many = (name, opts) =>
table.insert additional_clause, more_where
else
for k,v in pairs more_where
additional_clause[k] = v
if type(k) == "number" -- append array clauses
table.insert additional_clause, v
else
additional_clause[k] = v

if additional_clause and next additional_clause
clause = @@db.clause additional_clause
Expand Down
14 changes: 13 additions & 1 deletion spec/relations_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -536,18 +536,27 @@ describe "lapis.db.model.relations", ->

it "make has_many getter", ->
models.Posts = class extends Model
models.Things = class extends Model

models.Users = class extends Model
@relations: {
{"posts", has_many: "Posts"}
{"more_posts", has_many: "Posts", where: {color: "blue"}}
{"fresh_posts", has_many: "Posts", order: "id desc"}
}

{"with_key", has_many: "Things", key: "object_id", order: "id desc"}

{"with_key_where", has_many: "Things", key: "object_id", where: {
{"object_type = ?", 1}
}, order: "id desc"}
}

assert_queries {
[[SELECT * FROM "posts" WHERE "user_id" = 1234]]
[[SELECT * FROM "posts" WHERE "user_id" = 1234 AND "color" = 'blue']]
[[SELECT * FROM "posts" WHERE "user_id" = 1234 ORDER BY id desc]]
[[SELECT * FROM "things" WHERE "object_id" = 1234 ORDER BY id desc]]
[[SELECT * FROM "things" WHERE "object_id" = 1234 AND (object_type = 1) ORDER BY id desc]]
}, ->
user = models.Users\load id: 1234

Expand All @@ -557,6 +566,9 @@ describe "lapis.db.model.relations", ->
user\get_more_posts!
user\get_fresh_posts!

user\get_with_key!
user\get_with_key_where!

it "makes has many with db.clause", ->
models.Posts = class extends Model
models.Users = class extends Model
Expand Down

0 comments on commit 951e3fe

Please sign in to comment.