Neon Postgres & Astro
यह कंटेंट अभी तक आपकी भाषा में उपलब्ध नहीं है।
Neon is a fully managed serverless Postgres database. It separates storage and compute to offer autoscaling, branching, and bottomless storage.
Adding Neon to your Astro project
Section titled Adding Neon to your Astro projectPrerequisites
Section titled Prerequisites- A Neon account with a created project
- Neon database connection string
- An Astro project with on-demand rendering (SSR) enabled
Environment configuration
Section titled Environment configurationTo use Neon with Astro, you will need to set a Neon environment variable. Create or edit the .env
file in your project root, and add the following code, replacing your own project details:
NEON_DATABASE_URL="postgresql://<user>:<password>@<endpoint_hostname>.neon.tech:<port>/<dbname>?sslmode=require"
For better TypeScript support, define environment variables in a src/env.d.ts
file:
interface ImportMetaEnv { readonly NEON_DATABASE_URL: string;}
interface ImportMeta { readonly env: ImportMetaEnv;}
.env
files in Astro.
Installing dependencies
Section titled Installing dependenciesInstall the @neondatabase/serverless
package to connect to Neon:
npm install @neondatabase/serverless
Creating a Neon client
Section titled Creating a Neon clientCreate a new file src/lib/neon.ts
with the following code to initialize your Neon client:
import { neon } from '@neondatabase/serverless';
export const sql = neon(import.meta.env.NEON_DATABASE_URL);
Querying your Neon database
Section titled Querying your Neon databaseYou can now use the Neon client to query your database from any .astro
component. The following example fetches the current time from the Postgres database:
---import { sql } from '../lib/neon';
const response = await sql`SELECT NOW() as current_time`;const currentTime = response[0].current_time;---
<h1>Current Time</h1><p>The time is: {currentTime}</p>
Database branching with Neon
Section titled Database branching with NeonNeon’s branching feature lets you create copies of your database for development or testing. Use this in your Astro project by creating different environment variables for each branch:
NEON_DATABASE_URL=your_development_branch_url
NEON_DATABASE_URL=your_production_branch_url
Resources
Section titled Resources- Neon documentation
- Neon serverless driver GitHub
- Connect an Astro site or application to Neon Postgres