Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 70 additions & 5 deletions docs/2.connectors/neon.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,76 @@ icon: cbi:neon

# NEON

> Connect DB0 to Neon Serverless Postgres.
> Very similar to [Postgres connector](/connectors/postgresql), but optimized for serverless environments.

:read-more{to="https://neon.tech/"}
:read-more{to="https://neon.com"}

::read-more{to="https://github.com/unjs/db0/issues/32"}
This connector is planned to be supported. Follow up via [unjs/db0#32](https://github.com/unjs/db0/issues/32).
::
## Why Neon Connector?

The fundamental difference is that Postgres Connector uses the [node-postgres](https://node-postgres.com/) driver, which uses a TCP connection, while Neon uses [neondatabase/serverless](https://neon.com/docs/serverless/serverless-driver) and uses a HTTP/Web-Sockets connector. While the drivers have feature parity, the connection type creates some runtime differences.

The HTTP/WS connection is usually preferred over TCP for serverless environments because:

- Historically, some runtimes did not work well with TCP connections.
- Reduced latency as a consequence of fewer required network trips per query.
- Reduce number of SCRAM authentication calls.

Additionally to the runtime differences, Neon connector also allows to automatically seed and instantiate a fresh Postgres instance if initialized without a connection string.

## Instant Postgres Provisioning

If the connector's client is instantiated without a connection string **in development**, the Neon connector will automatically generate a connection string. It will also seed schema and data if provided with a `.sql` file.

## Usage

Install Neon Servleress Driver for the postgres connection and Instagres' `get-db` package to auto-generate the connection string in development.

:pm-install{name="@neondatabse/serverless get-db"}

With those dependencies installed, you can immediately start building:

```ts
import { createDatabase } from "db0";
import neon from "db0/connectors/neon";

const db = createDatabase(
neon({
bindingName: "DB",
seed: "init.sql",
}),
);
```

```sql [init.sql]
CREATE TABLE IF NOT EXISTS xmen (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);

INSERT INTO xmen (name) VALUES
('Wolverine'),
('Cyclops'),
('Storm'),
('Jean Grey'),
('Beast'),
('Professor X'),
('Gambit'),
('Rogue'),
('Nightcrawler')
ON CONFLICT DO NOTHING;
```

## Options

### `connectionString` or `url`

- **Type:** `string` _(optional)_
- Manually provide a connection string to your Neon database.
- If not provided, it will use the value from the environment variable or automatically provision a database (in development).

### `seed`

- **Type:** `string` _(optional)_
- **Default:** `undefined`
- Path to a `.sql` file for seeding the database schema and initial data.
- If set to `false`, seeding will be disabled.
24 changes: 4 additions & 20 deletions docs/2.connectors/vercel.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,9 @@ icon: radix-icons:vercel-logo

# Vercel

> Connect DB0 to Vercel Postgres
> Vercel Postgres has migrated to Vercel Marketplace.

:read-more{to="https://vercel.com/docs/storage/vercel-postgres"}
Existing Vercel Postgres instances were migrated to [Neon](https://neon.com).
For best integration with db0, use the [Neon Connector](/connectors/neon).

::read-more{to="https://github.com/unjs/db0/issues/32"}
A dedicated `vercel` connector is planned to be supported. Follow up via [unjs/db0#32](https://github.com/unjs/db0/issues/32).
::

## Usage

Use [`postgres`](/connectors/postgresql) connector:

```js
import { createDatabase } from "db0";
import postgres from "db0/connectors/postgres";

const db = createDatabase(
postgres({
/* options */
}),
);
```
:read-more{to="https://neon.com/docs/guides/vercel-postgres-transition-guide"}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@cloudflare/workers-types": "^4.20251120.0",
"@electric-sql/pglite": "^0.3.14",
"@libsql/client": "^0.15.15",
"@neondatabase/serverless": "^1.0.2",
"@planetscale/database": "^1.19.0",
"@types/better-sqlite3": "^7.6.13",
"@types/bun": "^1.3.2",
Expand All @@ -58,6 +59,7 @@
"drizzle-orm": "^0.44.7",
"eslint": "^9.39.1",
"eslint-config-unjs": "^0.5.0",
"get-db": "^0.9.2",
"jiti": "^2.6.1",
"mlly": "^1.8.0",
"mysql2": "^3.15.3",
Expand Down
Loading