Skip to content

Instantly share code, notes, and snippets.

@ansh

ansh/db.cr Secret

Created November 15, 2020 02:35
Show Gist options
  • Save ansh/e5d21a6de7e7139e1068cdde05e90147 to your computer and use it in GitHub Desktop.
Save ansh/e5d21a6de7e7139e1068cdde05e90147 to your computer and use it in GitHub Desktop.
require "granite/adapter/pg"
module CompressedString
extend self
def to_db(value : ::String?) : Granite::Columns::Type
bytea : Array(UInt8)
compressed = IO::Memory.new
Compress::Deflate::Writer.open(compressed) do |deflate|
deflate.print value
end
compressed.seek 0
bytea = compressed.each_byte.to_a
compressed.close
Slice.new(bytea.to_unsafe, bytea.size)
end
def from_rs(result : ::DB::ResultSet) : ::String?
value = result.read(Bytes?)
return nil if value.nil?
final = ""
string = IO::Memory.new value
Compress::Deflate::Reader.open(string) do |deflate|
final = deflate.gets_to_end
end
string.close
return final
end
end
# class Granite::AssociationCollection(Owner, Target)
# private def query
# if through.nil?
# "WHERE #{Target.table_name}.#{@foreign_key} = ?"
# else
# "JOIN #{through} ON #{through}.#{@foreign_key} = #{Target.table_name}.#{Target.primary_name} " \
# "WHERE #{through}.#{@foreign_key} = ?"
# end
# end
# end
Granite::Connections << Granite::Adapter::Pg.new(
name: "pg",
url: ENV["DATABASE_URL"]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment