feat: add netlify-db driver#5420
Open
eduardoboucas wants to merge 15 commits intodrizzle-team:betafrom
Open
Conversation
…itch job and consolidating secret inheritance
fix: Fix condition for run-feature job
… in environments with forbidden `require()` (fixes drizzle-team#5107) (drizzle-team#5118)
* feat(drizzle-kit): support d1 via binding * chore: update version to 0.31.9 and add changelog for D1 API improvements * fix: Fix router workflow * chore: Clean up secrets in router workflow by commenting out unused variables --------- Co-authored-by: RomanNabukhotnyi <nabukhotnyiroman@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a first-party Netlify DB adapter (
drizzle-orm/netlify-db) that intelligently selects the optimal underlying Postgres driver based on the runtime environment.Netlify DB is a managed Postgres database. On Netlify's platform, the same application code runs in two very different contexts:
node-postgresThis adapter abstracts that decision away so consumers can write
drizzle()once and get the right driver automatically.How it works
Intelligent driver selection
The
drizzle()entry point accepts multiple configurations:When using zero-config or a connection string, the adapter creates a serverless session by default. If
NETLIFY_DB_DRIVER=serveris set, it delegates tonode-postgresinstead. When an explicitDrizzleClientis passed, the discriminated union (driver: 'serverless' | 'server') determines which adapter is used.Dual-transport serverless session
In serverless mode, the adapter uses two transports within a single session:
select,insert,update,delete)NeonHttpPreparedQuery)NeonPreparedQuery)This is handled by
NetlifyDbSession, which delegates toNeonHttpPreparedQueryfor regular queries and creates aNetlifyDbWsSession(backed by aPoolClientover WebSocket) whentransaction()is called.Nested transactions are supported via savepoints (
NetlifyDbTransaction).Server mode
When the driver is
'server',drizzle()delegates entirely to the existingnode-postgresadapter (drizzleNodePg), returning aNodePgDatabase. This path adds no new code — it reuses the battle-tested node-postgres adapter.Tests
Integration tests in
integration-tests/tests/pg/netlify-db.test.tsfollow the existing pattern used by other Postgres adapters (neon-http, neon-serverless, node-postgres):Shared test suite: Runs the full
tests()andcacheTests()suites frompg-common.ts(~205 tests), skipping only tests that don't apply to the HTTP transport (migration tests,db.executeformat differences, timestamp formatting). Custom versions of skipped tests are provided where applicable.Instantiation paths: Asserts every code path in
drizzle():{ connectionString })NETLIFY_DB_URLenv varNETLIFY_DB_DRIVER=server(verifies node-postgres path){ driver: 'serverless', httpClient, pool }){ driver: 'server', pool })Serverless transport selection: Asserts the dual-transport behavior:
Running the tests
I made the integrations test depend on a
NETLIFY_DB_URLenvironment variable containing the database connection string. I assume that would be something that we would need to supply to you and then you'd add it to the CI.In the meantime, you should be able to test it with any Postgres database, as I've been doing locally: