title | description | author | authorEmail | date | tags | published | image | slug | ogImage | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Using Drizzle with Xata's Postgres service |
Get started with Drizzle ORM and Xata |
Alexis Rico |
03-20-2024 |
|
true |
|
drizzle-postgres-xata-integration |
Drizzle ORM is a very popular TypeScript ORM that provides type safe access to your database, automated migrations, and a custom data model definition.
With this week's beta release of Xata's Postgres service you are now able to connect to your Xata database with Drizzle over the Postgres wire protocol too with the drizzle-orm/node-postgres
or drizzle-orm/postgres-js
drivers.
Also, we are excited to announce that we have added support for the drizzle-orm/xata-http
driver. This driver allows you to connect to your Xata database using the Xata HTTP API. This is a great option if you are using Drizzle in a serverless environment or if you are using a language that does not have a native Postgres driver.
Before starting you will need an empty Xata database that has direct access to Postgres enabled.
You will first need to enable direct access to Postgres in your workspace settings.
You will also need to enable direct access to Postgres when you create your new database.
After this point you will want to update your Xata SDK to the next
release to ensure you have the latest beta features.
$ npm install @xata.io/client@next # yarn install or pnpm install
There are two ways to set up Drizzle: using the native Postgres adapters or the Xata HTTP adapter.
To use Drizzle with the node-postgres
driver, you will need to install the drizzle-orm
and pg
packages.
$ npm install drizzle-orm pg # yarn install or pnpm install
After that, you can use Drizzle as you normally would, but with the node-postgres
driver.
import { drizzle } from 'drizzle-orm/node-postgres';
import { getXataClient } from './xata'; // Generated client
import { Client } from 'pg';
const xata = getXataClient();
const client = new Client({ connectionString: xata.sql.connectionString });
await client.connect();
const db = drizzle(client);
To use Drizzle with the postgres-js
driver, you will need to install the drizzle-orm
and postgres-js
packages.
$ npm install drizzle-orm postgres # yarn install or pnpm install
After that, you can use Drizzle as you normally would, but with the postgres-js
driver.
import { drizzle } from 'drizzle-orm/postgres-js';
import { getXataClient } from './xata'; // Generated client
import postgres from 'postgres';
const xata = getXataClient();
const client = postgres(xata.sql.connectionString);
const db = drizzle(queryClient);
To use Drizzle with the xata-http
driver, you will need to install the drizzle-orm
package.
$ npm install drizzle-orm # yarn install or pnpm install
After that, you can use Drizzle as you normally would, but with the xata-http
driver.
import { drizzle } from 'drizzle-orm/xata-http';
import { getXataClient } from './xata'; // Generated client
const xata = getXataClient();
const db = drizzle(xata);
And that's it, it's that easy to connect Drizzle to your Xata database. This functionality is currently only available with the public beta of our Postgres service.
Sign up today to get started! We're always around if you have any questions. Pop into Discord and say hi or reach out on X | Twitter. Happy building 🦋