Skip to content

Commit

Permalink
Support JSON::Any type in database operations
Browse files Browse the repository at this point in the history
- Updated `json` method in `src/table.cr` to use `JSON::Any` type for columns.
- Modified `src/insert.cr` and `src/update.cr` to handle `JSON::Any` type correctly during insert and update operations.
- Added `JSON::Any` to the list of supported types in `src/cql.cr`.
- Enhanced test in `spec/adapters/postgres_schema_spec.cr` to validate JSON::Any handling in insert and update operations.
  • Loading branch information
eliasjpr committed Sep 14, 2024
1 parent 4deba34 commit 4fbbb21
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
16 changes: 15 additions & 1 deletion spec/adapters/postgres_schema_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,27 @@ describe Cql::Schema do
# end

it "fetches user preferences" do
payload_json = %({"theme": "dark"})
json_any = JSON.parse(payload_json)

Example
.insert
.into(:user_pref)
.values(preferences: %({"theme": "dark"}))
.values(preferences: json_any)
.commit
result = Example.query.from(:user_pref).limit(1).first!(UserPref)
result.preferences.should be_a(JSON::Any)
result.preferences.should eq(json_any)

Example
.update
.table(:user_pref)
.set(preferences: json_any)
.where { user_pref.id.eq(1_i64) }
.commit

result.preferences.should be_a(JSON::Any)
result.preferences.should eq(json_any)
end
end

Expand Down
3 changes: 2 additions & 1 deletion src/cql.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ module Cql
String.class |
Time.class |
UUID.class |
Nil.class
Nil.class |
JSON::Any.class

# :nodoc:
DB_TYPE_MAPPING = {
Expand Down
8 changes: 7 additions & 1 deletion src/insert.cr
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,13 @@ module Cql

@columns = keys if @columns.empty?

vals = fields.values.map { |v| v.as(DB::Any) }
vals = fields.values.map do |v|
if v.is_a?(JSON::Any)
v.to_json.as(DB::Any)
else
v.as(DB::Any)
end
end
@values << vals.to_a
end

Expand Down
2 changes: 1 addition & 1 deletion src/table.cr
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ module Cql
end

def json(name : Symbol, as as_name : String? = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false)
col = Column(String).new(name, String, as_name, null, default, unique)
col = Column(JSON::Any).new(name, JSON::Any, as_name, null, default, unique)
col.table = self
@columns[name] = col
col.index = index ? add_index(columns: [name], unique: unique) : nil
Expand Down
7 changes: 6 additions & 1 deletion src/update.cr
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,12 @@ module Cql
setters.each do |k, v|
column = find_column(k)
column.validate!(v)
@setters << Expression::Setter.new(Expression::Column.new(column), v)
val = if v.is_a?(JSON::Any)
v.to_json.as(DB::Any)
else
v.as(DB::Any)
end
@setters << Expression::Setter.new(Expression::Column.new(column), val)
end
end

Expand Down

0 comments on commit 4fbbb21

Please sign in to comment.