All Projects → Dandush03 → pg_rls

Dandush03 / pg_rls

Licence: MIT license
PostgreSQL Row Level Security, The Rails right way to do multitenancy

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to pg rls

Multitenancy-Microservice-FederatedIdentity-Example
Multitenancy Federated Identity Example ASP.NET MVC C#
Stars: ✭ 33 (-68.27%)
Mutual labels:  multitenancy
Tenancy
Automatic multi-tenancy for Laravel. No code changes needed.
Stars: ✭ 2,133 (+1950.96%)
Mutual labels:  multitenancy
Bouncer
Eloquent roles and abilities.
Stars: ✭ 2,763 (+2556.73%)
Mutual labels:  multitenancy
multitenancy
A laravel package to provide multi-tenancy support
Stars: ✭ 20 (-80.77%)
Mutual labels:  multitenancy
multitenant-microservices-demo
Full Isolation in Multi-Tenant SaaS with Kubernetes + Istio
Stars: ✭ 57 (-45.19%)
Mutual labels:  multitenancy
sails-hook-multitenant
A multitenant Sails.js 1.X hook for Waterline ORM.
Stars: ✭ 15 (-85.58%)
Mutual labels:  multitenancy
detectify
Detectify is a gem for multi-tenant Rack apps, to help you retrieve domain and subdomain-related data from a database.
Stars: ✭ 79 (-24.04%)
Mutual labels:  multitenancy
dotnet-webapi-boilerplate
Clean Architecture Template for .NET 6.0 WebApi built with Multitenancy Support.
Stars: ✭ 2,200 (+2015.38%)
Mutual labels:  multitenancy
cloud-cap-multitenancy
SAP Cloud Application Programming Model (CAP) sample code project with multitenancy using service manager-created SAP HANA containers for tenant data isolation.
Stars: ✭ 33 (-68.27%)
Mutual labels:  multitenancy
Milvasoft
Helper structures for .Net 6 projects.
Stars: ✭ 24 (-76.92%)
Mutual labels:  multitenancy
office-building
🏢 Easy to use SaaS Multitenancy solution for Laravel
Stars: ✭ 21 (-79.81%)
Mutual labels:  multitenancy
grandnode2
Free, Open source, Fast, Headless, Multi-tenant eCommerce platform built with .NET Core, MongoDB, AWS DocumentDB, Azure CosmosDB, LiteDB, Vue.js.
Stars: ✭ 626 (+501.92%)
Mutual labels:  multitenancy
capsule-lens-extension
Lens Extension for Capsule Operator - Looking for a maintainer
Stars: ✭ 37 (-64.42%)
Mutual labels:  multitenancy
kcp
kcp is a Kubernetes-like control plane for workloads on many clusters
Stars: ✭ 1,881 (+1708.65%)
Mutual labels:  multitenancy
react-native-multitenant-boilerplate
No description or website provided.
Stars: ✭ 27 (-74.04%)
Mutual labels:  multitenancy

Contributors Forks Stargazers Issues LinkedIn Hireable Donate


PgRls

PostgreSQL Row Level Security
The Rails right way to do multitenancy


Explore the docs »

Report Bug · Request Feature · API Repo

Table of Contents

It's time we start doing multitenancy right! You can avoid creating a separate Postgres schema/databases for each customer or trying to ensure the WHERE clause of every single query includes the particular company. Just integrate PgRls seamlessly to your application.

This gem will integrate PostgreSQL RLS to help you develop a great multitenancy application.

Required Installation

Installing

Add this line to your application's Gemfile:

gem 'pg_rls'

And then execute:

$ bundle install

Or install it yourself with:

$ gem install pg_rls

Instructions

rails generate pg_rls:install company #=> where company eq tenant model name

You can change company to anything you'd like, for example, tenant This will generate the model and inject all the required code

For any new model that needs to be under rls, you can generate it by writing

rails generate pg_rls user #=> where user eq model name

and it will generate all the necesary information for you.

You can swtich to another tenant by using

PgRls::Tenant.switch :app #=> where app eq tenant name

Don't forget to update how you want PgRls to find your tenant, you can set multiple options by modifying api/config/initializers/pg_rls.rb search_methods

You can add the following configuration to your Database Config File to improve performance and remove PgRls::SecureConnection from aplication_record.rb

# app/config/database.yml
<% def db_username
    return PgRls.username unless ENV['AS_DB_ADMIN']

    Rails.application.credentials.dig(:database, :server_1, :username)
   end %>

...

development:
  <<: *default
  database: example_development
  username: <%= db_username %> # Apply this to production and all env including tests

...

Testing

If you are getting PG::InsufficientPrivilege: ERROR: permission denied you can override does permistion by running RAILS_ENV=test rake db:grant_usage

Many application uses some sort of database cleaner before running thair spec so on each test that we run we'll have an empty state. Usually, those gems clear our user configuration for the database. To solve this issue, we must implement the following:

# spec/rails_helper.rb

...
# some database cleaning strategy

config.before(:suite) do
  # Create the tenant which in this example is company and we are using FactoryBot
  FactoryBot.create(:company, subdomain: 'app')
  # In this default case our initializer is set to search by subdomain so will use it
  PgRls::Tenant.switch :app
end

...

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 the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/dandush03/pg_rls. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the 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 PgRls project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Note

Currently we only support subdomain as a searcher but will soon integrate slug/domain and cookies support we recommed the use of ``

Show your support

Give a ⭐️ if you like this project!

If this project help you reduce time to develop, you can give me a cup of coffee :)

paypal

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].