DirectConnectionExamples.tsx151 lines · main
1export type Example = {
2 installCommands?: string[]
3 postInstallCommands?: string[]
4 files?: {
5 name: string
6 content: string
7 }[]
8}
9
10export const examples = {
11 nodejs: {
12 installCommands: ['npm install postgres'],
13 files: [
14 {
15 name: 'db.js',
16 content: `import postgres from 'postgres'
17
18const connectionString = process.env.DATABASE_URL
19const sql = postgres(connectionString)
20
21export default sql`,
22 },
23 ],
24 },
25 golang: {
26 installCommands: ['go get github.com/jackc/pgx/v5'],
27 files: [
28 {
29 name: 'main.go',
30 content: `package main
31
32import (
33 "context"
34 "log"
35 "os"
36 "github.com/jackc/pgx/v5"
37)
38
39func main() {
40 conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
41 if err != nil {
42 log.Fatalf("Failed to connect to the database: %v", err)
43 }
44 defer conn.Close(context.Background())
45
46 // Example query to test connection
47 var version string
48 if err := conn.QueryRow(context.Background(), "SELECT version()").Scan(&version); err != nil {
49 log.Fatalf("Query failed: %v", err)
50 }
51
52 log.Println("Connected to:", version)
53}`,
54 },
55 ],
56 },
57 dotnet: {
58 installCommands: [
59 'dotnet add package Microsoft.Extensions.Configuration.Json --version YOUR_DOTNET_VERSION',
60 ],
61 postInstallCommands: [
62 'dotnet add package Microsoft.Extensions.Configuration.Json --version YOUR_DOTNET_VERSION',
63 ],
64 },
65 python: {
66 installCommands: ['pip install python-dotenv psycopg2'],
67 files: [
68 {
69 name: 'main.py',
70 content: `import psycopg2
71from dotenv import load_dotenv
72import os
73
74# Load environment variables from .env
75load_dotenv()
76
77# Fetch variables
78USER = os.getenv("user")
79PASSWORD = os.getenv("password")
80HOST = os.getenv("host")
81PORT = os.getenv("port")
82DBNAME = os.getenv("dbname")
83
84# Connect to the database
85try:
86 connection = psycopg2.connect(
87 user=USER,
88 password=PASSWORD,
89 host=HOST,
90 port=PORT,
91 dbname=DBNAME
92 )
93 print("Connection successful!")
94
95 # Create a cursor to execute SQL queries
96 cursor = connection.cursor()
97
98 # Example query
99 cursor.execute("SELECT NOW();")
100 result = cursor.fetchone()
101 print("Current Time:", result)
102
103 # Close the cursor and connection
104 cursor.close()
105 connection.close()
106 print("Connection closed.")
107
108except Exception as e:
109 print(f"Failed to connect: {e}")`,
110 },
111 ],
112 },
113 sqlalchemy: {
114 installCommands: ['pip install python-dotenv sqlalchemy psycopg2'],
115 files: [
116 {
117 name: 'main.py',
118 content: `from sqlalchemy import create_engine
119# from sqlalchemy.pool import NullPool
120from dotenv import load_dotenv
121import os
122
123# Load environment variables from .env
124load_dotenv()
125
126# Fetch variables
127USER = os.getenv("user")
128PASSWORD = os.getenv("password")
129HOST = os.getenv("host")
130PORT = os.getenv("port")
131DBNAME = os.getenv("dbname")
132
133# Construct the SQLAlchemy connection string
134DATABASE_URL = f"postgresql+psycopg2://{USER}:{PASSWORD}@{HOST}:{PORT}/{DBNAME}?sslmode=require"
135
136# Create the SQLAlchemy engine
137engine = create_engine(DATABASE_URL)
138# If using Transaction Pooler or Session Pooler, we want to ensure we disable SQLAlchemy client side pooling -
139# https://docs.sqlalchemy.org/en/20/core/pooling.html#switching-pool-implementations
140# engine = create_engine(DATABASE_URL, poolclass=NullPool)
141
142# Test the connection
143try:
144 with engine.connect() as connection:
145 print("Connection successful!")
146except Exception as e:
147 print(f"Failed to connect: {e}")`,
148 },
149 ],
150 },
151}