Repository is archived
No commit activity in last 3 years
No release in over 3 years
A shortcut to the COPY command from PostgreSQL. Give it SQL and get an Enumerator for the CSV that comes out of it.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.17
>= 0
>= 0
~> 4.3
~> 5.2.4
~> 13.0
~> 3.0
 Project Readme

SqlToCsvStream

DEPRECATION WARNING đź’€

⚠️ Warning This project is no longer maintained. ⚠️

Feel free to fork the gem for your own needs.

About

This is your favorite gem to produce CSV or JSON directly from SQL queries. It queries a PostgreSQL with a COPY statement and streams the result as CSV/JSON directly into a ruby enumerator.

This gem can be used in all ruby applications, but ships with a special renderer for Rails to easily render downloads from your rails controller.

Note: This is project is still in a proof-of-concept phase. We may rename some things, make the code more readable and very likely add some tests :)

Installation

Add this line to your application's Gemfile:

gem 'sql_to_csv_stream'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sql_to_csv_stream

If you use rails, you may register the new stream renderers in an initializer.

require 'sql_to_csv_stream'

SqlToCsvStream.register_rails_renderer

Usage

In Rails, you can use the stream renderer from a Controller:

class UsersController < ApplicationController
  def index
    @users = User.all.where(deleted_at: null)

    respond_to do |format|
      format.csv do
        render csv_stream: @users, filename: 'users.csv'
      end
      format.json do
        render json_stream: @users, filename: 'users.json'
      end
    end
  end
end

This, unlike many other CSV/JSON rendering techniques, instantly sends results to the user by streaming the content while it is being generated. This is light on memory. By streaming the data instantly, even large files (that need longer to generate than the HTTP server connection timeout value) can be produced without the connection being interrupted by a connection timeout.

The streaming renderer automatically responds with a gzipped encoding if the client accepts it. This drastically reduces file sizes we need to send over the wire.

Any SQL string the PostgreSQL COPY command accepts can be given to the renderer. Alternatively, any object may be given that produces such SQL on .to_sql or to_s. So you can use your favorite query-object pattern :)

If you are not in Rails or want to process CSV/JSON in any other way from within Rails, you can use the *Enumerator classes.

file = File.open('users.csv', 'w')
SqlToCsvStream::CsvEnumerator.new('SELECT * FROM users;').each do |csv_row|
  file.write
end
file.close

Or write the compressed file with:

csv_enum = SqlToCsvStream::CsvEnumerator.new('SELECT * FROM users;')
zip_enum = SqlToCsvStream::GzipWrapper.new(csv_enum)

file = File.open('users.csv.gz', 'w')
zip_enum.each do |csv_row|
  file.write
end
file.close

Writing a JSON file works similarly:

file = File.open('users.json', 'w')
SqlToCsvStream::JsonEnumerator.new('SELECT * FROM users;').each do |csv_row|
  file.write
end
file.close

For more options, see the class documentation of the CsvEnumerator or JsonEnumerator class.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/tessi/sql_to_csv_stream. New feature ideas are welcome too -- please present your ideas in an issue first so we can together discuss whether this idea fits into the scope of this project. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the SqlToCsvStream project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Previous work

We didn't invent streaming, nor did we were the first to have the idea to integrate this in ruby and/or rails. Some previous approaches are described here, here, here, or here. We are thankful for the previous work done which led us into the right direction and enabled us to (hopefully) improve upon it.