All Projects → ProctorU → squint

ProctorU / squint

Licence: MIT license
Search PostgreSQL jsonb and hstore columns

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to squint

Filterrific
Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
Stars: ✭ 810 (+3015.38%)
Mutual labels:  activerecord, ruby-on-rails
activerecord-cockroachdb-adapter
CockroachDB adapter for ActiveRecord.
Stars: ✭ 90 (+246.15%)
Mutual labels:  activerecord, ruby-on-rails
Type scopes
Automatic scopes for ActiveRecord models.
Stars: ✭ 24 (-7.69%)
Mutual labels:  activerecord, ruby-on-rails
Database consistency
The tool to find inconsistency between models schema and database constraints.
Stars: ✭ 418 (+1507.69%)
Mutual labels:  activerecord, ruby-on-rails
active record-updated at
Touch `updated_at` by default with calls to `update_all` and `update_column(s)`
Stars: ✭ 27 (+3.85%)
Mutual labels:  activerecord, ruby-on-rails
Ransack
Object-based searching.
Stars: ✭ 5,020 (+19207.69%)
Mutual labels:  activerecord, ruby-on-rails
Model probe
Schema introspection for ActiveModel
Stars: ✭ 58 (+123.08%)
Mutual labels:  activerecord, ruby-on-rails
filtered
Filters ActiveRecord queries in a nice way
Stars: ✭ 28 (+7.69%)
Mutual labels:  activerecord, ruby-on-rails
activerecord-crate-adapter
Ruby on Rails ActiveRecord adapter for CrateDB
Stars: ✭ 27 (+3.85%)
Mutual labels:  activerecord, ruby-on-rails
Octopus
Database Sharding for ActiveRecord
Stars: ✭ 2,496 (+9500%)
Mutual labels:  activerecord, ruby-on-rails
Database validations
Database validations for ActiveRecord
Stars: ✭ 274 (+953.85%)
Mutual labels:  activerecord, ruby-on-rails
activerecord-setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Stars: ✭ 21 (-19.23%)
Mutual labels:  activerecord, ruby-on-rails
Elasticsearch Rails
Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
Stars: ✭ 2,896 (+11038.46%)
Mutual labels:  activerecord, ruby-on-rails
Active Record Query Trace
Rails plugin that logs/displays a backtrace of all SQL queries executed by Active Record
Stars: ✭ 785 (+2919.23%)
Mutual labels:  activerecord, ruby-on-rails
LocalSupport
A directory of local support services and volunteer opportunities
Stars: ✭ 60 (+130.77%)
Mutual labels:  activerecord, ruby-on-rails
Goldiloader
Just the right amount of Rails eager loading
Stars: ✭ 1,074 (+4030.77%)
Mutual labels:  activerecord, ruby-on-rails
Pg
Golang ORM with focus on PostgreSQL features and performance
Stars: ✭ 4,918 (+18815.38%)
Mutual labels:  hstore, jsonb
rails cursor pagination
Add cursor pagination to your ActiveRecord backed application
Stars: ✭ 21 (-19.23%)
Mutual labels:  activerecord, ruby-on-rails
Active record Events
Manage timestamps in ActiveRecord models
Stars: ✭ 109 (+319.23%)
Mutual labels:  activerecord, ruby-on-rails
prefixed ids
Friendly Prefixed IDs for your Ruby on Rails models
Stars: ✭ 159 (+511.54%)
Mutual labels:  activerecord, ruby-on-rails

Search PostgreSQL jsonb and hstore columns.


Full database searching inside columns containing semi-structured data like json, jsonb and hstore. Compatible with the awesome storext gem.

Table of contents

Status

All Contributors CircleCI

Quick Start

Add to your Gemfile:

gem 'squint'

Include it in your models:

class Post < ActiveRecord::Base
  include Squint
  # ...
end

Assuming a table with the following structure:

                                           Table "public.posts"
       Column              |            Type             |                     Modifiers
---------------------------+-----------------------------+----------------------------------------------------
 id                        | integer                     | not null default nextval('posts_id_seq'::regclass)
 title                     | character varying           |
 body                      | character varying           |
 request_info              | jsonb                       |
 properties                | hstore                      |
 storext_jsonb_attributes  | jsonb                       |
 storext_hstore_attributes | jsonb                       |
 created_at                | timestamp without time zone | not null
 updated_at                | timestamp without time zone | not null
Indexes:
    "posts_pkey" PRIMARY KEY, btree (id)

In your code use queries like:

Post.where(properties: { referer: 'http://example.com/one' } )
# SELECT "posts".* FROM "posts" WHERE "posts"."properties"->'referer' = 'http://example.com/one'

Post.where(properties: { referer: nil } )
# SELECT "posts".* FROM "posts" WHERE "posts"."properties"->'referer' IS NULL

Post.where(properties: { referer: ['http://example.com/one',nil] } )
# SELECT "posts".* FROM "posts" WHERE ("posts"."properties"->'referer' = 'http://example.com/one'
#                                   OR "posts"."properties"->'referer' IS NULL)

Post.where(request_info: { referer: ['http://example.com/one',nil] } )
# SELECT "posts".* FROM "posts" WHERE ("posts"."request_info"->>'referer' = 'http://example.com/one'
#                                   OR "posts"."request_info"->>'referer' IS NULL)

Squint only operates on json, jsonb and hstore columns. ActiveRecord will throw a StatementInvalid exception like always if the column type is unsupported by Squint.

Post.where(title: { not_there: "any value will do" } )
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "title"
LINE 1: SELECT COUNT(*) FROM "posts" WHERE "title"."not_there" = 'an...
                                           ^
: SELECT COUNT(*) FROM "posts" WHERE "title"."not_there" = 'any value will do'

Performance

To get the most performance out searching jsonb/hstore attributes, add a GIN (preferred) or GIST index to those columns. Find out more here

TL;DR:

SQL: 'CREATE INDEX name ON table USING GIN (column);'

Rails Migration: add_index(:table, :column_name, using: 'gin')

Storext attributes

Assuming the database schema above and a model like so:

class Post < ActiveRecord::Base
  include Storext.model
  include Squint

  store_attribute :storext_jsonb_attributes, :zip_code, String, default: '90210'
  store_attribute :storext_jsonb_attributes, :friend_count, Integer, default: 0
end

Example using StoreXT with a default value:

Post.where(storext_jsonb_attributes: { zip_code: '90210' } )
# -- jsonb
# SELECT "posts".* FROM "posts" WHERE ("posts"."storext_jsonb_attributes"->>'zip_code' = '90210' OR
#                                     (("posts"."storext_jsonb_attributes" ? 'zip_code') IS NULL OR
#                                      ("posts"."storext_jsonb_attributes" ? 'zip_code') = FALSE))
# -- hstore
# SELECT "posts".* FROM "posts" WHERE ("posts"."storext_hstore_attributes"->'zip_code' = '90210' OR
#                                     ((exist("posts"."storext_hstore_attributes", 'zip_code') = FALSE) OR
#                                       exist("posts"."storext_hstore_attributes", 'zip_code') IS NULL))
#
#

If (as in the example above) the default value for the StoreXT attribute is specified, then extra checks for missing column ( ("posts"."storext_jsonb_attributes" ? 'zip_code') IS NULL ) or missing key ( ("posts"."storext_jsonb_attributes" ? 'zip_code') = FALSE) ) are added

When non-default storext values are specified, these extra checks won't be added.

The Postgres SQL for jsonb and hstore is different. No support for checking for missing json columns exists, so don't use those with StoreXT + Squint

Developing

  1. Thank you!
  2. Clone the repository
  3. bundle
  4. bundle exec rake --rakefile test/dummy/Rakefile db:setup # create the db for tests
  5. bundle exec rake # run the tests
  6. make your changes in a thoughtfully named branch
  7. ensure good test coverage
  8. submit a Pull Request

Contributors

Thanks goes to these wonderful people (emoji key):


Kevin Brown

🎨 👀

Andrew Fomera

👀 💻

Ryan T. Hosford

💻

Matthew Jaeh

🎨 👀

Justin Licata

💻 🎨 📖 👀

Kyle Miracle

🐛 👀

David H. Wilkins

💬 🐛 💻 🎨 📖 💡 👀 ⚠️

Jay Wright

👀

James Cook

💻 ⚠️ 👀

This project follows the all-contributors specification. Contributions of any kind welcome!

Credits

Squint is maintained and funded by ProctorU.


ProctorU Engineering & Design

A simple online proctoring service that allows you to take exams or certification tests at home.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].