News for hackers. Stuff that matters.
Re:Ask HN: How come there is no example code for B2B-SaaS apps? by thrownaway954 on Sunday May 03, 2020 @04:20AM (#23057679)
using an ORM it's very simple. once you setup the relationships between models, you always use the highest level model as the entry point of your query and the ORM will scope the query for you. ie in Rails, if your highest level model is Account you just always use @account.customer.find(18). the Rails ORM will automatically scope the query to the current account.

using multiple schemas becomes a nightmare when you want to perform a migration to alter the database schema. you have to somehow perform a migration across every single schema without breaking anything. god forbid you need to rollback. have everything in one database makes it really easy. as please, don't talk how using a single database restricts scaling later on. with how far databases have come and how cheap cloud computing is, scaling isn't an issue anymore.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by sleepyhead on Sunday May 03, 2020 @08:34AM (#23058613)
> the Rails ORM will automatically scope the query to the current account.

While that is true for simple queries it is not a valid statement for secure multi tenancy.

For example in this scenario: Account (Id). Order (Id, AccountId, CustomerId). Customer (Id, AccountId). You need to ensure Order can only use CustomerId that belongs to Account.

Rails does not include support for restricting deep relationships (grand children) to an account. Basically an attacker can misuse foreign keys from other accounts in new records (add a CustomerId from another Account on Order) or read data in relationship queries when initiating objects (show customer info from another account in an order).

There are ways of mitigating this but it requires doing more complex manual validation methods.

> using multiple schemas becomes a nightmare

There are gems that will solve some of this complexity but it is a risk. The most popular one is somewhat unmaintained.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by hackerm0nkey on Sunday May 03, 2020 @07:26AM (#23058360)
I come more from a JVM world. But aren't there any DB migration/refactoring tools in Rails to allow for changes to propagate across tenants ?

E.g for the JVM, there's Liquibase and Flyway, and I agree with you, setting that up took a good part of a couple of weeks of dedicated effort.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by HatchedLake721 on Sunday May 03, 2020 @04:51AM (#23057803)
The highest level entry point makes so much sense. I’ve been building multitenant saas for last 3 years enforcing “where tenant = x” and you’ve just blown my mind.
Re:Ask HN: How come there is no example code for B2B-SaaS apps? by hackerm0nkey on Sunday May 03, 2020 @07:22AM (#23058344)
On more than one occasion I've started with a simple table for all tenants (where tenant = x) because we didn't know at the time if we will have many.

Then progressively when we understood how the service will be used, refactored to keep separate tenant's data separate.

Ended up with each tenant in a given schema, we were using Java and relied on Hibernate (ORM) to help connecting to the correct DB (it does support multi-tenancy) as well as on a DB migration tool (Liquibase) so any changes we made to the schema get replicated over all tenants.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by thrownaway954 on Monday May 04, 2020 @12:07PM (#23069702)
that's why we're here... learning something new everyday is a great things. now imagine how cool it will be implementing this is your projects from here out :)
Re:Ask HN: How come there is no example code for B2B-SaaS apps? by mattmanser on Sunday May 03, 2020 @01:05PM (#23060283)
You can't really write reports or BI dashboards in ORMs, unless you want terrible performance.

So for simple queries, fine, but anything else this won't work.

Re:Ask HN: How come there is no example code for B2B-SaaS apps? by thrownaway954 on Monday May 04, 2020 @12:06PM (#23069678)
no kidding and you're being unreasonable... yes, what ii stated will get you 99% of the way there. what you're belly aching about is the 1% where you have to veer from this and do it yourself. big deal.

look... you wouldn't forego taking a plane across the country cause you have to take a cab the last 2 miles to the hotel. same thing here. just because something falls out at the last 1% doesn't mean it a bad way to do things.