Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental/json serializable columns #695

Merged
merged 8 commits into from
Jul 18, 2021
11 changes: 10 additions & 1 deletion spec/json_column_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe "JSON Columns" do
end

describe "serialized" do
it "saves the raw value" do
it "saves the serialized value" do
SaveBlob.create(metadata: BlobMetadata.from_json("{}")) do |operation, blob|
operation.saved?.should be_true
blob.should_not be_nil
Expand All @@ -80,5 +80,14 @@ describe "JSON Columns" do
blob.not_nil!.media.should be_nil
end
end

it "queries serialized columns" do
one = BlobMetadata.from_json({name: "One", code: 4}.to_json)
two = BlobMetadata.from_json({name: "Two", code: 9}.to_json)
BlobFactory.create &.metadata(one)
BlobFactory.create &.metadata(two)

BlobQuery.new.metadata(two).select_count.should eq(1)
end
end
end
8 changes: 8 additions & 0 deletions src/avram/charms/json_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ module JSON::Serializable
end

module Lucky(T)
alias ColumnType = JSON::Any
jwoertink marked this conversation as resolved.
Show resolved Hide resolved
include Avram::Type

def self.criteria(query : R, column) forall R
Criteria(R, T).new(query, column)
end

def from_db!(value)
value
end
Expand All @@ -23,6 +28,9 @@ module JSON::Serializable
def to_db(value)
value.to_json
end

class Criteria(T, V) < Avram::Criteria(T, V)
end
end
end

Expand Down