• Stars
    star
    324
  • Rank 129,708 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 11 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PostgresExt-Serializers

Build Status Code Climate Gem Version

This Gem patches the ArraySerializer in Active Model Serializers to offload JSON serialization to PostgreSQL, which is much faster and more memory efficient than serialization in Ruby.

Instead of instantiating lots of ActiveRecord models, which are then serialized to a hash using ActiveModel Serializers and finally converted to JSON, this Gem builds the entire JSON in the database and returns it as a single string that can be passed through to the client unmodified.

How does it work?

You can read Avoid Rails When Generating JSON responses with PostgreSQL by Dan McClain on the DockYard Blog or watch a YouTube video of his Postgres Open 2014 Talk Using PostgreSQL, not Rails, to make Rails faster (Slides) to understand how this Gem works under the hood.

Supported Versions

This gem currently only works with the older active_model_serializers 0.8.x and activerecord 4.0/4.1/4.2. For activerecord 4.2 support you need at least postgres_ext version 2.4.0.

You must be using at least Postgres 9.2, but 9.4 or later is recommended.

Support for newer AMS versions might be added in the future.

Looking for help?

If it is a bug please open an issue on Github. If you need help using the gem please ask the question on Stack Overflow. Be sure to tag the question with DockYard so we can find it.

Installation

Add this line to your application's Gemfile:

gem 'postgres_ext-serializers'

And then execute:

$ bundle

Or install it yourself as:

$ gem install postgres_ext-serializers

Usage

Just require 'postgres_ext/serializers' and use ActiveModel::Serializers as you normally would! Postgres_ext-serializers will take over anytime you try to serialize an ActiveRecord::Relation.

Methods in Serializers and Models

If you are using methods to compute properties for your JSON responses in your models or serializers, postgres_ext-serializers will try to discover a SQL version of this call by looking for a class method with the same name and the suffix __sql. Here's an example:

class MyModel < ActiveRecord::Base
  def full_name
    "#{object.first_name} #{object.last_name}"
  end

  def self.full_name__sql
    "first_name || ' ' || last_name"
  end
end

class MySerializer < ActiveModel::Serializer
  def full_name
    "#{object.first_name} #{object.last_name}"
  end

  def full_name__sql
    "first_name || ' ' || last_name"
  end
end

There is no instance of MyModel created so sql computed properties needs to be a class method. Right now, this string is used as a SQL literal, so be sure to not use untrusted values in the return value.

Note: Postgres date, timestamp or timestamptz format

Postgres 9.2 and 9.3 by default renders dates according to the current DateStyle Postgres setting, but many JSON processors require dates to be in ISO 8601 format e.g. Firefox or Internet Explorer will parse string as invalid date with default DateStyle setting. Postgres 9.4 onwards now uses ISO 8601 for JSON serialization instead.

Developing

To work on postgres_ext-serializers locally, follow these steps:

  1. Run bundle install, this will install (almost) all the development dependencies
  2. Run gem install byebug (not a declared dependency to not break CI)
  3. Run bundle exec rake setup, this will set up the .env file necessary to run the tests and set up the database
  4. Run bundle exec rake db:create, this will create the test database
  5. Run bundle exec rake db:migrate, this will set up the database tables required by the test
  6. Run BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-4.2.x' bundle install --quiet to create the Gemfile.lock.
  7. Run bundle exec rake test:all to run tests against all supported versions of Active Record (currently 4.0.x, 4.1.x, 4.2.x)

Authors

Dan McClain twitter github

We are very thankful for the many contributors

Versioning

This gem follows Semantic Versioning

Want to help?

Please do! We are always looking to improve this gem.

Legal

DockYard, Inc © 2014

@dockyard

Licensed under the MIT license

More Repositories

1

client_side_validations

Client Side Validations made easy for Ruby on Rails
Ruby
2,686
star
2

ember-validations

Validations for Ember Objects
JavaScript
832
star
3

postgres_ext

Adds support for missing PostgreSQL data types to ActiveRecord
Ruby
643
star
4

ember-easy-form

Easily build semantic forms in Ember
JavaScript
565
star
5

party_foul

Use GitHub to track your application errors!
Ruby
517
star
6

ruby-destroyed_at

ActiveRecord Mixin for Safe Destroys
Ruby
349
star
7

capybara-email

Test your ActionMailer and Mailer messages with Capybara
Ruby
339
star
8

client_side_validations-simple_form

Simple Form plugin for ClientSideValidations
JavaScript
254
star
9

ember-appkit-rails

Ember Appkit for Rails
Ruby
238
star
10

ember-suave

Make your Ember App Stylish
JavaScript
179
star
11

ember-one-way-controls

Native one way input
JavaScript
176
star
12

ember-data-route

Common teardown scenario for ember routes backed by a data model
JavaScript
120
star
13

ember-cli-i18n

Simple Internationalization support for ember-cli apps
JavaScript
112
star
14

dismissible_helpers

Ruby
96
star
15

es6_module_transpiler-rails

Transpile ES6 Modules in the Rails Asset Pipeline
JavaScript
87
star
16

capybara-extensions

Complements Capybara with additional finders and matchers.
Ruby
66
star
17

ruby-context_validations

Context Aware Validations for Rails
Ruby
65
star
18

pg_array_parser

Ruby
61
star
19

pages

Easy pages in Rails
Ruby
49
star
20

ember-skeleton

Show fast-loading temporary images in place of an eventual slow-loading image
JavaScript
41
star
21

ember-cli-proxy-fixtures

Ember CLI Proxy Fixtures
JavaScript
38
star
22

client_side_validations-mongoid

Mongoid plugin for ClientSideValidations
Ruby
28
star
23

ruby-easy_auth

Dead simple drop-in identity based Rails authentication
Ruby
28
star
24

ember-admin-bootstrap

Ember Admin with a Twitter Bootstrap Theme
CSS
25
star
25

client_side_validations-formtastic

Formtastic plugin for ClientSideValidations
Ruby
18
star
26

ember-wuphf

JavaScript
17
star
27

postgres_ext-postgis

Ruby
14
star
28

ember-cli-one-script

This addon combines your `vendor.js` and `<your-app-name>.js` into a single file called `app.js`
JavaScript
14
star
29

client_side_validations-turbolinks

Turbolinks Plugin for ClientSideValidations
Ruby
9
star
30

fixtory

Not quite fixtures, not quite factories
Ruby
9
star
31

ember-new-modules-shim

JavaScript
6
star
32

client_side_validations-backbone

Backbone plugin for ClientSideValidations
JavaScript
6
star
33

ember-cli-test-interactions

Ember acceptance test helpers.
JavaScript
3
star
34

minitest-moar

Moar Minitest Pluzsh!
Ruby
3
star
35

ruby-easy_auth-password

Password plugin for EasyAuth
Ruby
3
star
36

mail_congress

Ruby
2
star
37

ruby-easy_auth-oauth2

Ruby
1
star
38

ruby-easy_auth-twitter

Ruby
1
star
39

ruby-easy_auth-linked_in

Ruby
1
star
40

comet

Elixir
1
star