pangolin/server/db
Parikshith 41139f2fd0
perf: add secondary FK indexes to core OSS schemas
Add 13 secondary indexes across 10 tables in the SQLite schema and
5 missing indexes to the PostgreSQL schema.

All list endpoints (listSites, listResources, listClients, listRoles,
listTargets, listUserOrgs) filter and join on these FK columns. In the
SQLite schema, no secondary indexes existed at all on these foreign keys,
forcing full sequential scans on every paginated request and session lookup.

SQLite changes (13 new indexes):
  - sites.orgId
  - resources.orgId
  - targets.resourceId, targets.siteId
  - newt.siteId
  - clients.orgId, clients.userId
  - labels.orgId
  - olms.userId
  - session.userId
  - userOrgs.userId, userOrgs.orgId
  - roles.orgId

PG changes (5 new indexes, rest already present):
  - labels.orgId
  - session.userId
  - userOrgs.userId, userOrgs.orgId
  - roles.orgId
  - olms.userId
2026-08-31 17:04:01 -04:00
..
pg perf: add secondary FK indexes to core OSS schemas 2026-08-31 17:04:01 -04:00
queries Send email whitelist user downstream headers 2026-08-14 10:51:59 -04:00
sqlite perf: add secondary FK indexes to core OSS schemas 2026-08-31 17:04:01 -04:00
asns.ts Format 2025-12-22 16:28:41 -05:00
countries.ts ♻️ remove deprecated ISO CS country code 2026-06-25 18:01:25 +02:00
ios_models.json add pretty apple device names 2026-01-15 17:59:45 -08:00
mac_models.json update mac models 2026-05-02 12:09:55 -07:00
maxmind.ts Chungus 2025-10-04 18:36:44 -07:00
maxmindAsn.ts Add ASN-based resource rule matching 2025-12-20 12:16:31 -05:00
migrate.ts Change back to lokowitz db method 2026-02-12 12:13:13 -08:00
names.json Format all files 2025-12-09 10:56:14 -05:00
names.ts add niceId to provider 2026-08-14 10:51:58 -04:00
README.md Change back to lokowitz db method 2026-02-12 12:13:13 -08:00
regions.ts Add region-based resource rule 2025-12-22 17:44:56 +01:00

Database

Pangolin can use a Postgres or SQLite database to store its data.

Development

Postgres

To use Postgres, edit server/db/index.ts to export all from server/db/pg/index.ts:

export * from "./pg";

Make sure you have a valid config file with a connection string:

postgres:
    connection_string: postgresql://postgres:postgres@localhost:5432

You can run an ephemeral Postgres database for local development using Docker:

docker run -d \
  --name postgres \
  --rm \
  -p 5432:5432 \
  -e POSTGRES_PASSWORD=postgres \
  -v $(mktemp -d):/var/lib/postgresql/data \
  postgres:17

Schema

server/db/pg/schema.ts and server/db/sqlite/schema.ts contain the database schema definitions. These need to be kept in sync with with each other.

Stick to common data types and avoid Postgres-specific features to ensure compatibility with SQLite.

SQLite

To use SQLite, edit server/db/index.ts to export all from server/db/sqlite/index.ts:

export * from "./sqlite";

No edits to the config are needed. If you keep the Postgres config, it will be ignored.

Generate and Push Migrations

Ensure drizzle-kit is installed.

Postgres

You must have a connection string in your config file, as shown above.

npm run db:generate
npm run db:push

SQLite

npm run db:generate
npm run db:push

Build Time

There is a dockerfile for each database type. The dockerfile swaps out the server/db/index.ts file to use the correct database type.