Skip to content
  • NestJS
  • PostgreSQL
  • Security

One database, many companies: how StaffDock keeps each company’s data apart

StaffDock serves many UK companies from one shared database. Every query is scoped to one company inside the NestJS API, and tests prove one company can never read another’s data.

Toukir Ahmed Rony · · 4 min read

Diagram: one shared database in the middle, connected to Company A, Company B and Company C, with locks between the companies to show they cannot see each other’s data.

StaffDock is a right to work and staff document system for UK businesses. Companies upload their people's passports, share codes and visa documents, record their checks, and get warned before anything expires. It went live in October 2026, and 20 UK companies joined in the first month.

That means one system holds some of the most sensitive documents a business has, for many businesses at once. If one company could ever see another company's staff list, that would be the worst bug the product could have. So before I designed a single screen, I had to decide how companies are kept apart.

Three ways to separate companies

There are three common ways to build a system that many companies share:

  1. A separate database for each company. Strong separation, but every new company means a new database, and every change to the data structure has to run on all of them.
  2. A separate schema for each company inside one database. A bit lighter, but you still have many copies of every table to keep in step.
  3. One shared database, with every row tagged with its company. One structure and one set of changes for everyone, but the separation now depends on every query asking for the right company.

What I chose

StaffDock uses the third option: one shared PostgreSQL database, and every query scoped to one company inside the NestJS API.

The reason is simple. StaffDock runs on a shared server, and it changes often. With one schema there is one migration path, and every company gets every improvement at the same time.

The rule is enforced in the API, which is the only thing allowed to touch the database. The Next.js app only displays what the API sends back. Inside the API:

  • Every route is denied by default. A new endpoint is closed until it is given an explicit rule.
  • Roles are checked on every request, not only when someone signs in. The user is read again from the database each time.
  • Every query is scoped to one company. The company comes from the signed-in user, never from something the browser sends.

The last rule doesn't depend on developers remembering it. Every database call goes through one layer in the API, and that layer adds the company to the query itself. A table that hasn't been registered for company scoping is refused outright. This is the heart of it:

          if (!COMPANY_MODELS.has(model)) {
            throw new ForbiddenException(`${model} is not registered for company scoping`);
          }
          const companyId = scopedCompanyId(model);
          if (operation === "upsert") {
            a.where = { ...(a.where as Args | undefined), companyId };
            a.create = withData(a.create, companyId);
          } else if (CREATE_OPS.has(operation)) {
            a.data = withData(a.data, companyId);
          } else {
            a.where = { ...(a.where as Args | undefined), companyId };
          }

Writes are checked too: a write that carries a different company's id is refused.

Even the platform owner's admin panel follows this. The Super Admin can manage plans, payments and settings, but cannot see any company's people or documents.

The honest downside

This design has a weakness, and it is worth saying clearly. The database itself has no wall between companies. The protection lives in that one layer of the API.

There is also a second, separate connection that skips the company filter, because some work isn't done for one company: signing in, signing up, background jobs and the Super Admin panel. A mistake in code that uses that connection, or in hand-written SQL, would not be caught by PostgreSQL. It would happily return every company's rows.

A database per company would make that mistake impossible. PostgreSQL row-level security would add a second wall inside the database. I chose to keep the rule in one place, the API, and to make the tests the safety net.

Tests as the safety net

The automated tests (Vitest) cover the tenant rules directly. They check that one company can never read another company's people, sites, teams, files or activity, and that the Super Admin is refused any company's people, dashboard and activity. They run in CI on every push and pull request. If someone adds a query that leaks across companies, the build fails before it reaches a real customer.

At the time of writing, 81 automated tests pass in CI.

Files get a second layer

Keeping rows apart is only half of it. Staff documents are files, and files need protection too.

Every stored file gets a random name, sits in its own company's folder, can only be read by the server account, and is only served after a permission check. The most sensitive fields, such as share codes, dates of birth, National Insurance numbers and bank details, are encrypted with AES-256-GCM before they are saved. Companies can also choose to delete criminal record (DBS) files once they have been checked, and keep only the certificate number, so the system doesn't keep them longer than it needs to.

What I took from it

There is no free choice here. A database per company is safer by default but harder to run. A shared database is easier to run but only as safe as your discipline.

For StaffDock, the shared database was the right call, as long as the separation rule lives in one place and the tests check it on every change. If I were building for much larger companies, or under stricter rules, I would add row-level security as a second wall.