@@ -126,21 +126,16 @@ import sqlalchemy
126
126
# initialize Connector object
127
127
connector = Connector()
128
128
129
- # function to return the database connection
130
- def getconn () -> pymysql.connections.Connection:
131
- conn: pymysql.connections.Connection = connector.connect(
129
+ # initialize SQLAlchemy connection pool with Connector
130
+ pool = sqlalchemy.create_engine(
131
+ " mysql+pymysql://" ,
132
+ creator = lambda : connector.connect(
132
133
" project:region:instance" ,
133
134
" pymysql" ,
134
135
user = " my-user" ,
135
136
password = " my-password" ,
136
137
db = " my-db-name"
137
- )
138
- return conn
139
-
140
- # create connection pool
141
- pool = sqlalchemy.create_engine(
142
- " mysql+pymysql://" ,
143
- creator = getconn,
138
+ ),
144
139
)
145
140
```
146
141
@@ -207,33 +202,21 @@ Connector as a context manager:
207
202
208
203
``` python
209
204
from google.cloud.sql.connector import Connector
210
- import pymysql
211
205
import sqlalchemy
212
206
213
- # helper function to return SQLAlchemy connection pool
214
- def init_connection_pool (connector : Connector) -> sqlalchemy.engine.Engine:
215
- # function used to generate database connection
216
- def getconn () -> pymysql.connections.Connection:
217
- conn = connector.connect(
207
+ # initialize Cloud SQL Python Connector as context manager
208
+ with Connector() as connector:
209
+ # initialize SQLAlchemy connection pool with Connector
210
+ pool = sqlalchemy.create_engine(
211
+ " mysql+pymysql://" ,
212
+ creator = lambda : connector.connect(
218
213
" project:region:instance" ,
219
214
" pymysql" ,
220
215
user = " my-user" ,
221
216
password = " my-password" ,
222
217
db = " my-db-name"
223
- )
224
- return conn
225
-
226
- # create connection pool
227
- pool = sqlalchemy.create_engine(
228
- " mysql+pymysql://" ,
229
- creator = getconn,
218
+ ),
230
219
)
231
- return pool
232
-
233
- # initialize Cloud SQL Python Connector as context manager
234
- with Connector() as connector:
235
- # initialize connection pool
236
- pool = init_connection_pool(connector)
237
220
# insert statement
238
221
insert_stmt = sqlalchemy.text(
239
222
" INSERT INTO my_table (id, title) VALUES (:id, :title)" ,
@@ -401,30 +384,19 @@ from google.cloud.sql.connector import Connector, DnsResolver
401
384
import pymysql
402
385
import sqlalchemy
403
386
404
- # helper function to return SQLAlchemy connection pool
405
- def init_connection_pool (connector : Connector) -> sqlalchemy.engine.Engine:
406
- # function used to generate database connection
407
- def getconn () -> pymysql.connections.Connection:
408
- conn = connector.connect(
387
+ # initialize Cloud SQL Python Connector with `resolver=DnsResolver`
388
+ with Connector(resolver = DnsResolver) as connector:
389
+ # initialize SQLAlchemy connection pool with Connector
390
+ pool = sqlalchemy.create_engine(
391
+ " mysql+pymysql://" ,
392
+ creator = lambda : connector.connect(
409
393
" prod-db.mycompany.example.com" , # using DNS name
410
394
" pymysql" ,
411
395
user = " my-user" ,
412
396
password = " my-password" ,
413
397
db = " my-db-name"
414
- )
415
- return conn
416
-
417
- # create connection pool
418
- pool = sqlalchemy.create_engine(
419
- " mysql+pymysql://" ,
420
- creator = getconn,
398
+ ),
421
399
)
422
- return pool
423
-
424
- # initialize Cloud SQL Python Connector with `resolver=DnsResolver`
425
- with Connector(resolver = DnsResolver) as connector:
426
- # initialize connection pool
427
- pool = init_connection_pool(connector)
428
400
# ... use SQLAlchemy engine normally
429
401
```
430
402
@@ -501,25 +473,19 @@ from google.cloud.sql.connector import Connector
501
473
# initialize Python Connector object
502
474
connector = Connector()
503
475
504
- # Python Connector database connection function
505
- def getconn ():
506
- conn = connector.connect(
476
+ app = Flask(__name__ )
477
+
478
+ # configure Flask-SQLAlchemy to use Python Connector
479
+ app.config[' SQLALCHEMY_DATABASE_URI' ] = " postgresql+pg8000://"
480
+ app.config[' SQLALCHEMY_ENGINE_OPTIONS' ] = {
481
+ " creator" : lambda : conn = connector.connect(
507
482
" project:region:instance-name" , # Cloud SQL Instance Connection Name
508
483
" pg8000" ,
509
484
user = " my-user" ,
510
485
password = " my-password" ,
511
486
db = " my-database" ,
512
487
ip_type = " public" # "private" for private IP
513
488
)
514
- return conn
515
-
516
-
517
- app = Flask(__name__ )
518
-
519
- # configure Flask-SQLAlchemy to use Python Connector
520
- app.config[' SQLALCHEMY_DATABASE_URI' ] = " postgresql+pg8000://"
521
- app.config[' SQLALCHEMY_ENGINE_OPTIONS' ] = {
522
- " creator" : getconn
523
489
}
524
490
525
491
# initialize the app with the extension
@@ -540,38 +506,27 @@ your web application using [SQLAlchemy ORM](https://docs.sqlalchemy.org/en/14/or
540
506
through the following:
541
507
542
508
``` python
543
- from sqlalchemy import create_engine
544
- from sqlalchemy.engine import Engine
509
+ import sqlalchemy
545
510
from sqlalchemy.ext.declarative import declarative_base
546
511
from sqlalchemy.orm import sessionmaker
547
512
from google.cloud.sql.connector import Connector
548
513
549
- # helper function to return SQLAlchemy connection pool
550
- def init_connection_pool (connector : Connector) -> Engine:
551
- # Python Connector database connection function
552
- def getconn ():
553
- conn = connector.connect(
554
- " project:region:instance-name" , # Cloud SQL Instance Connection Name
555
- " pg8000" ,
556
- user = " my-user" ,
557
- password = " my-password" ,
558
- db = " my-database" ,
559
- ip_type = " public" # "private" for private IP
560
- )
561
- return conn
562
-
563
- SQLALCHEMY_DATABASE_URL = " postgresql+pg8000://"
564
-
565
- engine = create_engine(
566
- SQLALCHEMY_DATABASE_URL , creator = getconn
567
- )
568
- return engine
569
514
570
515
# initialize Cloud SQL Python Connector
571
516
connector = Connector()
572
517
573
518
# create connection pool engine
574
- engine = init_connection_pool(connector)
519
+ engine = sqlalchemy.create_engine(
520
+ " postgresql+pg8000://" ,
521
+ creator = lambda : connector.connect(
522
+ " project:region:instance-name" , # Cloud SQL Instance Connection Name
523
+ " pg8000" ,
524
+ user = " my-user" ,
525
+ password = " my-password" ,
526
+ db = " my-database" ,
527
+ ip_type = " public" # "private" for private IP
528
+ ),
529
+ )
575
530
576
531
# create SQLAlchemy ORM session
577
532
SessionLocal = sessionmaker(autocommit = False , autoflush = False , bind = engine)
@@ -640,40 +595,29 @@ async def main():
640
595
#### SQLAlchemy Async Engine
641
596
642
597
``` python
643
- import asyncpg
644
-
645
598
import sqlalchemy
646
599
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
647
600
648
601
from google.cloud.sql.connector import Connector, create_async_connector
649
602
650
- async def init_connection_pool (connector : Connector) -> AsyncEngine:
651
- # creation function to generate asyncpg connections as 'async_creator' arg
652
- async def getconn () -> asyncpg.Connection:
653
- conn: asyncpg.Connection = await connector.connect_async(
603
+
604
+ async def main ():
605
+ # initialize Connector object for connections to Cloud SQL
606
+ connector = await create_async_connector()
607
+
608
+ # The Cloud SQL Python Connector can be used along with SQLAlchemy using the
609
+ # 'async_creator' argument to 'create_async_engine'
610
+ pool = create_async_engine(
611
+ " postgresql+asyncpg://" ,
612
+ async_creator = lambda : connector.connect_async(
654
613
" project:region:instance" , # Cloud SQL instance connection name
655
614
" asyncpg" ,
656
615
user = " my-user" ,
657
616
password = " my-password" ,
658
617
db = " my-db-name"
659
618
# ... additional database driver args
660
- )
661
- return conn
662
-
663
- # The Cloud SQL Python Connector can be used along with SQLAlchemy using the
664
- # 'async_creator' argument to 'create_async_engine'
665
- pool = create_async_engine(
666
- " postgresql+asyncpg://" ,
667
- async_creator = getconn,
619
+ ),
668
620
)
669
- return pool
670
-
671
- async def main ():
672
- # initialize Connector object for connections to Cloud SQL
673
- connector = await create_async_connector()
674
-
675
- # initialize connection pool
676
- pool = await init_connection_pool(connector)
677
621
678
622
# example query
679
623
async with pool.connect() as conn:
@@ -744,33 +688,24 @@ from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
744
688
745
689
from google.cloud.sql.connector import Connector
746
690
747
- async def init_connection_pool (connector : Connector) -> AsyncEngine:
748
- # creation function to generate asyncpg connections as 'async_creator' arg
749
- async def getconn () -> asyncpg.Connection:
750
- conn: asyncpg.Connection = await connector.connect_async(
751
- " project:region:instance" , # Cloud SQL instance connection name
752
- " asyncpg" ,
753
- user = " my-user" ,
754
- password = " my-password" ,
755
- db = " my-db-name"
756
- # ... additional database driver args
757
- )
758
- return conn
759
-
760
- # The Cloud SQL Python Connector can be used along with SQLAlchemy using the
761
- # 'async_creator' argument to 'create_async_engine'
762
- pool = create_async_engine(
763
- " postgresql+asyncpg://" ,
764
- async_creator = getconn,
765
- )
766
- return pool
767
691
768
692
async def main ():
769
693
# initialize Connector object for connections to Cloud SQL
770
694
loop = asyncio.get_running_loop()
771
695
async with Connector(loop = loop) as connector:
772
- # initialize connection pool
773
- pool = await init_connection_pool(connector)
696
+ # The Cloud SQL Python Connector can be used along with SQLAlchemy using the
697
+ # 'async_creator' argument to 'create_async_engine'
698
+ pool = create_async_engine(
699
+ " postgresql+asyncpg://" ,
700
+ async_creator = lambda : connector.connect_async(
701
+ " project:region:instance" , # Cloud SQL instance connection name
702
+ " asyncpg" ,
703
+ user = " my-user" ,
704
+ password = " my-password" ,
705
+ db = " my-db-name"
706
+ # ... additional database driver args
707
+ ),
708
+ )
774
709
775
710
# example query
776
711
async with pool.connect() as conn:
0 commit comments