Back to Drizzle Orm

ConnectNile

src/mdx/get-started/postgresql/ConnectNile.mdx

latest1.5 KB
Original Source

import Callout from '@mdx/Callout.astro'; import CodeTabs from "@mdx/CodeTabs.astro";

Create a index.ts file in the src directory and initialize the connection:

<CodeTabs items={["node-postgres", "node-postgres with config", "your node-postgres driver"]}>

typescript
import 'dotenv/config';
import { drizzle } from 'drizzle-orm/node-postgres';

const db = drizzle(process.env.NILEDB_URL!);
typescript
import 'dotenv/config';
import { drizzle } from 'drizzle-orm/node-postgres';

// You can specify any property from the node-postgres connection options
const db = drizzle({ 
  connection: { 
    connectionString: process.env.NILEDB_URL!,
    ssl: true
  }
});
typescript
import 'dotenv/config';
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

const pool = new Pool({
  connectionString: process.env.NILEDB_URL!,
});
const db = drizzle({ client: pool });
</CodeTabs> <Callout title='multi-tenancy'> Nile provides **virtual tenant databases**. When you query Nile, you can set the tenant context and Nile will direct your queries to the virtual database for this particular tenant. All queries sent with tenant context will apply to that tenant alone (i.e. `select * from table` will result records only for this tenant). To learn more about how to set tenant context with Drizzle, check the **[official Nile-Drizzle example](https://www.thenile.dev/docs/getting-started/languages/drizzle#72-tenantdb)**. </Callout>