DirectConnectionExamples.tsx 3.5 KB

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