@remix-run/data-table-postgres
v0.5.0
Published
PostgreSQL database implementation for remix/data-table
Maintainers
Readme
data-table-postgres
PostgreSQL database driver for remix/data-table, backed by pg.
Features
- Native
pgIntegration: Creates a pool frompgconfiguration or uses an existing pool or client - Full
data-tableAPI Support: Queries, relations, writes, and transactions - PostgreSQL Compiler: SQL compilation is handled automatically for PostgreSQL
- Multi-Statement Migrations:
executeScript()runsup.sql/down.sqlfiles natively viapg - Postgres Capabilities Enabled By Default:
returning: truesavepoints: trueupsert: truetransactionalDdl: truemigrationLock: true
Installation
npm i remix pgUsage
import { createPostgresDatabase } from 'remix/data-table/postgres'
let db = createPostgresDatabase({
connectionString: process.env.DATABASE_URL,
})Use db.query(...), relation loading, and transactions from remix/data-table. Import any driver-specific types you need directly from pg.
Config-backed databases support db.wipe() and db.reset(). Call await db.close() during application shutdown to close the internally created pool. You may pass an existing pg pool or client when your application owns the driver lifecycle; db.close() leaves supplied clients alone, and destructive lifecycle methods are unavailable in that mode. db.wipe() requires a database name resolvable from the connection config (database, the path of connectionString, or the PGDATABASE environment variable) and throws when none is present.
Migration runs reserve one connection for the PostgreSQL advisory lock, migration SQL, and journal updates. Lock acquisition waits up to 60 seconds (via lock_timeout) and fails with an error instead of blocking forever. After a successful run the connection is unlocked and returned to the pool; if the migration or unlock fails, the reserved connection is destroyed instead of being reused, so a dirty session can never leak back into the pool. Nested migration lock acquisition throws instead of deadlocking.
Database Capabilities
data-table-postgres reports this capability set by default:
returning: truesavepoints: trueupsert: truetransactionalDdl: truemigrationLock: true
Advanced Usage
Transaction Options
Transaction options are passed through to PostgreSQL as hints.
await db.transaction(async (txDb) => txDb.exec('select 1'), {
isolationLevel: 'serializable',
readOnly: false,
})Running integration tests locally
To start a local Postgres container matching CI:
podman run --name postgres \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=remix \
-p 5432:5432 \
-d postgres:16Then run:
REMIX_DATA_TABLE_POSTGRES_TEST_URL=postgres://postgres:[email protected]:5432/remix \
pnpm test src/lib/driver.integration.test.tsRemove the container when you are done:
podman rm -f postgresRelated Packages
data-table- Core query/relations APIdata-schema- Schema parsing and validationdata-table-mysql- MySQL database driverdata-table-sqlite- SQLite database driver
License
See LICENSE
