Skip to content

Reference

Configuration Matrix

Create Engine Support Functions by Database Type Confirmed by testing [SQLITE, PostgreSQL] To Be Tested [MySQL, Oracle, MSSQL] and should be considered experimental.

Option SQLite PostgreSQL MySQL Oracle MSSQL
echo Yes Yes Yes Yes Yes
future Yes Yes Yes Yes Yes
pool_pre_ping Yes Yes Yes Yes Yes
pool_size No Yes Yes Yes Yes
max_overflow No Yes Yes Yes Yes
pool_recycle Yes Yes Yes Yes Yes
pool_timeout No Yes Yes Yes Yes

dsg_lib.async_database_functions.database_operations

This module contains tests for the DatabaseOperations class in the dsg_lib module.

The DatabaseOperations class provides methods for performing CRUD operations on a database using SQLAlchemy's asynchronous session.

The methods include:

  • create_one: Creates a single record in the database.
  • create_many: Creates multiple records in the database.
  • read_one: Reads a single record from the database.
  • read_many: Reads multiple records from the database.
  • update_one: Updates a single record in the database.
  • update_many: Updates multiple records in the database.
  • delete_one: Deletes a single record from the database.
  • delete_many: Deletes multiple records from the database.
  • count_query: Counts the number of records that match a given query.

Each method is tested to ensure it performs the expected operation and handles errors correctly. The tests use the pytest-asyncio plugin to run the asynchronous methods in an event loop, and the unittest.mock library to mock the database session and simulate errors.

The tests are organized into a single class, TestDatabaseOperations, which contains one test method for each method in the DatabaseOperations class. Each test method follows the Arrange-Act-Assert pattern: it sets up the necessary objects and state (Arrange), calls the method being tested (Act), and checks that the results are as expected (Assert).

DatabaseOperations

This class provides methods for performing CRUD operations on a database using SQLAlchemy's asynchronous session.

The methods include:

  • create_one: Creates a single record in the database.
  • create_many: Creates multiple records in the database.
  • read_one: Reads a single record from the database.
  • read_many: Reads multiple records from the database.
  • update_one: Updates a single record in the database.
  • update_many: Updates multiple records in the database.
  • delete_one: Deletes a single record from the database.
  • delete_many: Deletes multiple records from the database.
  • count_query: Counts the number of records that match a given query.
  • get_column_details: Gets the details of the columns in a table.
  • get_primary_keys: Gets the primary keys of a table.
  • get_table_names: Gets the names of all tables in the database.

Examples:

data = await db_ops.create_one(User(name='John Doe'))
data = await db_ops.create_many([User(name='John Doe'), User(name='Jane Doe')])
data = await db_ops.read_one(User, 1)
data = await db_ops.read_many(User, [1, 2, 3])
data = await db_ops.update_one(User, 1, {'name': 'John Smith'})
data = await db_ops.update_many(User, [1, 2], [{'name': 'John Smith'}, {'name': 'Jane Smith'}])
data = await db_ops.delete_one(User, 1)
data = await db_ops.delete_many(User, [1, 2, 3])
data = await db_ops.count_query(select(User))
data = await db_ops.get_column_details(User)
data = await db_ops.get_primary_keys(User)
data = await db_ops.get_table_names()

Source code in dsg_lib/async_database_functions/database_operations.py
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
class DatabaseOperations:
    """
    This class provides methods for performing CRUD operations on a database using SQLAlchemy's asynchronous session.

    The methods include:

    - `create_one`: Creates a single record in the database.
    - `create_many`: Creates multiple records in the database.
    - `read_one`: Reads a single record from the database.
    - `read_many`: Reads multiple records from the database.
    - `update_one`: Updates a single record in the database.
    - `update_many`: Updates multiple records in the database.
    - `delete_one`: Deletes a single record from the database.
    - `delete_many`: Deletes multiple records from the database.
    - `count_query`: Counts the number of records that match a given query.
    - `get_column_details`: Gets the details of the columns in a table.
    - `get_primary_keys`: Gets the primary keys of a table.
    - `get_table_names`: Gets the names of all tables in the database.

    Examples:
    ```python

    data = await db_ops.create_one(User(name='John Doe'))
    data = await db_ops.create_many([User(name='John Doe'), User(name='Jane Doe')])
    data = await db_ops.read_one(User, 1)
    data = await db_ops.read_many(User, [1, 2, 3])
    data = await db_ops.update_one(User, 1, {'name': 'John Smith'})
    data = await db_ops.update_many(User, [1, 2], [{'name': 'John Smith'}, {'name': 'Jane Smith'}])
    data = await db_ops.delete_one(User, 1)
    data = await db_ops.delete_many(User, [1, 2, 3])
    data = await db_ops.count_query(select(User))
    data = await db_ops.get_column_details(User)
    data = await db_ops.get_primary_keys(User)
    data = await db_ops.get_table_names()
    ```
    """

    def __init__(self, async_db: AsyncDatabase):
        """
        Initializes a new instance of the DatabaseOperations class.

        Args:
            async_db (module_name.AsyncDatabase): An instance of the
            AsyncDatabase class for performing asynchronous database operations.

        Example:
        ```python
        from dsg_lib.async_database_functions import (
        async_database,
        base_schema,
        database_config,
        database_operations,
        )

        config = {
            # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            # "pool_pre_ping": True,
            # "pool_size": 10,
            # "max_overflow": 10,
            "pool_recycle": 3600,
            # "pool_timeout": 30,
        }

        db_config = database_config.DBConfig(config)

        async_db = async_database.AsyncDatabase(db_config)

        db_ops = database_operations.DatabaseOperations(async_db)

        ```
        """
        # Log the start of the initialization
        logger.debug('Initializing DatabaseOperations instance')

        # Store the AsyncDatabase instance in the async_db attribute This
        # instance will be used for performing asynchronous database operations
        self.async_db = async_db

        # Log the successful initialization
        logger.info('DatabaseOperations instance initialized successfully')

    async def get_columns_details(self, table):
        """
        Retrieves the details of the columns of a given table.

        This asynchronous method accepts a table object and returns a
        dictionary. Each key in the dictionary is a column name from the table,
        and the corresponding value is another dictionary containing details
        about that column, such as type, if it's nullable, if it's a primary
        key, if it's unique, its autoincrement status, and its default value.

        Args:
            table (Table): An instance of the SQLAlchemy Table class
            representing the database table for which column details are
            required.

        Returns:
            dict: A dictionary where each key is a column name, and each value
            is a dictionary with the column's details.

        Raises:
            Exception: If any error occurs during the database operation.

        Example:
        ```python
        from sqlalchemy import Table, MetaData, Column,
        Integer, String from dsg_lib.async_database_functions import module_name metadata = MetaData()
        my_table = Table('my_table', metadata,
                        Column('id', Integer, primary_key=True), Column('name',
                        String))

        from dsg_lib.async_database_functions import (
            async_database,
            base_schema,
            database_config,
            database_operations,
        )
        # Create a DBConfig instance
        config = {
            # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            # "pool_pre_ping": True,
            # "pool_size": 10,
            # "max_overflow": 10,
            "pool_recycle": 3600,
            # "pool_timeout": 30,
        }
        # create database configuration
        db_config = database_config.DBConfig(config)
        # Create an AsyncDatabase instance
        async_db = async_database.AsyncDatabase(db_config)
        # Create a DatabaseOperations instance
        db_ops = database_operations.DatabaseOperations(async_db)
        # get columns details
        columns = await db_ops.get_columns_details(my_table)
        ```
        """
        # Log the start of the operation
        logger.debug(f'Starting get_columns_details operation for table: {table.__name__}')

        try:
            # Log the start of the column retrieval
            logger.debug(f'Getting columns for table: {table.__name__}')

            # Retrieve the details of the columns and store them in a dictionary
            # The keys are the column names and the values are dictionaries
            # containing the column details
            columns = {
                c.name: {
                    'type': str(c.type),
                    'nullable': c.nullable,
                    'primary_key': c.primary_key,
                    'unique': c.unique,
                    'autoincrement': c.autoincrement,
                    'default': (
                        str(c.default.arg)
                        if c.default is not None and not callable(c.default.arg)
                        else None
                    ),
                }
                for c in table.__table__.columns
            }

            # Log the successful column retrieval
            logger.info(f'Successfully retrieved columns for table: {table.__name__}')

            return columns
        except Exception as ex:  # pragma: no cover
            # Handle any exceptions that occur during the column retrieval
            logger.error(
                f'An error occurred while getting columns for table: {table.__name__}'
            )  # pragma: no cover
            return handle_exceptions(ex)  # pragma: no cover

    async def get_primary_keys(self, table):
        """
        Retrieves the primary keys of a given table.

        This asynchronous method accepts a table object and returns a list
        containing the names of its primary keys. It is useful for understanding
        the structure of the table and for operations that require knowledge of
        the primary keys.

        Args:
            table (Table): An instance of the SQLAlchemy Table class
            representing the database table for which primary keys are required.

        Returns:
            list: A list containing the names of the primary keys of the table.

        Raises:
            Exception: If any error occurs during the database operation.

        Example:
            ```python
            from sqlalchemy import Table, MetaData, Column, Integer,
                String from dsg_lib.async_database_functions import module_name metadata = MetaData()
                my_table = Table('my_table', metadata,
                                Column('id', Integer, primary_key=True),
                                Column('name', String, primary_key=True))
            from dsg_lib.async_database_functions import (
                async_database,
                base_schema,
                database_config,
                database_operations,
            )
            # Create a DBConfig instance
            config = {
                # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
                "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
                "echo": False,
                "future": True,
                # "pool_pre_ping": True,
                # "pool_size": 10,
                # "max_overflow": 10,
                "pool_recycle": 3600,
                # "pool_timeout": 30,
            }
            # create database configuration
            db_config = database_config.DBConfig(config)
            # Create an AsyncDatabase instance
            async_db = async_database.AsyncDatabase(db_config)
            # Create a DatabaseOperations instance
            db_ops = database_operations.DatabaseOperations(async_db)

            # get primary keys
            primary_keys = await db_ops.get_primary_keys(my_table)
            ```
        """
        # Log the start of the operation
        logger.debug(f'Starting get_primary_keys operation for table: {table.__name__}')

        try:
            # Log the start of the primary key retrieval
            logger.debug(f'Getting primary keys for table: {table.__name__}')

            # Retrieve the primary keys and store them in a list
            primary_keys = table.__table__.primary_key.columns.keys()

            # Log the successful primary key retrieval
            logger.info(f'Primary keys retrieved successfully: {primary_keys}')

            return primary_keys

        except Exception as ex:  # pragma: no cover
            # Handle any exceptions that occur during the primary key retrieval
            logger.error(f'Exception occurred: {ex}')  # pragma: no cover
            return handle_exceptions(ex)  # pragma: no cover

    async def get_table_names(self):
        """
        Retrieves the names of all tables in the database.

        This asynchronous method returns a list containing the names of all
        tables in the database. It is useful for database introspection,
        allowing the user to know which tables are available in the current
        database context.

        Returns:
            list: A list containing the names of all tables in the database.

        Raises:
            Exception: If any error occurs during the database operation.

        Example:
            ```python
            from dsg_lib.async_database_functions import (
            async_database,
            base_schema,
            database_config,
            database_operations,
            )
            # Create a DBConfig instance
            config = {
                # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
                "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
                "echo": False,
                "future": True,
                # "pool_pre_ping": True,
                # "pool_size": 10,
                # "max_overflow": 10,
                "pool_recycle": 3600,
                # "pool_timeout": 30,
            }
            # create database configuration
            db_config = database_config.DBConfig(config)
            # Create an AsyncDatabase instance
            async_db = async_database.AsyncDatabase(db_config)
            # Create a DatabaseOperations instance
            db_ops = database_operations.DatabaseOperations(async_db)
            # get table names
            table_names = await db_ops.get_table_names()
            ```
        """
        # Log the start of the operation
        logger.debug('Starting get_table_names operation')

        try:
            # Log the start of the table name retrieval
            logger.debug('Retrieving table names')

            # Retrieve the table names and store them in a list The keys of the
            # metadata.tables dictionary are the table names
            table_names = list(self.async_db.Base.metadata.tables.keys())

            # Log the successful table name retrieval
            logger.info(f'Table names retrieved successfully: {table_names}')

            return table_names

        except Exception as ex:  # pragma: no cover
            # Handle any exceptions that occur during the table name retrieval
            logger.error(f'Exception occurred: {ex}')  # pragma: no cover
            return handle_exceptions(ex)  # pragma: no cover

    async def read_one_record(self, query):
        """
        Retrieves a single record from the database based on the provided query.

        This asynchronous method accepts a SQL query object and returns the
        first record that matches the query. If no record matches the query, it
        returns None. This method is useful for fetching specific data
        when the expected result is a single record.

        Parameters:
            query (Select): An instance of the SQLAlchemy Select class,
            representing the query to be executed.

        Returns:
            Result: The first record that matches the query or None if no record matches.

        Raises:
            Exception: If any error occurs during the database operation.

        Example:
            ```python
            from dsg_lib.async_database_functions import (
            async_database,
            base_schema,
            database_config,
            database_operations,
            )
            # Create a DBConfig instance
            config = {
                # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
                "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
                "echo": False,
                "future": True,
                # "pool_pre_ping": True,
                # "pool_size": 10,
                # "max_overflow": 10,
                "pool_recycle": 3600,
                # "pool_timeout": 30,
            }
            # create database configuration
            db_config = database_config.DBConfig(config)
            # Create an AsyncDatabase instance
            async_db = async_database.AsyncDatabase(db_config)
            # Create a DatabaseOperations instance
            db_ops = database_operations.DatabaseOperations(async_db)
            # read one record
            record = await db_ops.read_one_record(select(User).where(User.name == 'John Doe'))
            ```
        """
        # Log the start of the operation
        logger.debug(f'Starting read_one_record operation for {query}')

        try:
            # Start a new database session
            async with self.async_db.get_db_session() as session:
                # Log the start of the record retrieval
                logger.debug(f'Getting record with query: {query}')

                # Execute the query and retrieve the first record
                result = await session.execute(query)
                record = result.scalar_one()

                # Log the successful record retrieval
                logger.info(f'Record retrieved successfully: {record}')

                return record

        except NoResultFound:
            # No record was found
            logger.info('No record found')
            return None

        except Exception as ex:  # pragma: no cover
            # Handle any exceptions that occur during the record retrieval
            logger.error(f'Exception occurred: {ex}')  # pragma: no cover
            return handle_exceptions(ex)  # pragma: no cover

    async def create_one(self, record):
        """
        Adds a single record to the database.

        This asynchronous method accepts a record object and adds it to the
        database. If the operation is successful, it returns the added record.
        The method is useful for inserting a new row into a database table.

        Parameters:
            record (Base): An instance of the SQLAlchemy declarative base class
            representing the record to be added to the database.

        Returns:
            Base: The instance of the record that was added to the database.

        Raises:
            Exception: If any error occurs during the database operation.

        Example:
            ```python
            from dsg_lib.async_database_functions import (
            async_database,
            base_schema,
            database_config,
            database_operations,
            )
            # Create a DBConfig instance
            config = {
                # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
                "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
                "echo": False,
                "future": True,
                # "pool_pre_ping": True,
                # "pool_size": 10,
                # "max_overflow": 10,
                "pool_recycle": 3600,
                # "pool_timeout": 30,
            }
            # create database configuration
            db_config = database_config.DBConfig(config)
            # Create an AsyncDatabase instance
            async_db = async_database.AsyncDatabase(db_config)
            # Create a DatabaseOperations instance
            db_ops = database_operations.DatabaseOperations(async_db)
            # create one record
            record = await db_ops.create_one(User(name='John Doe'))
            ```
        """
        # Log the start of the operation
        logger.debug('Starting create_one operation')

        try:
            # Start a new database session
            async with self.async_db.get_db_session() as session:
                # Log the record being added
                logger.debug(f'Adding record to session: {record.__dict__}')

                # Add the record to the session and commit the changes
                session.add(record)
                await session.commit()

                # Log the successful record addition
                logger.info(f'Record added successfully: {record}')

                return record

        except Exception as ex:
            # Handle any exceptions that occur during the record addition
            logger.error(f'Exception occurred: {ex}')
            return handle_exceptions(ex)

    async def create_many(self, records):
        """
        Adds multiple records to the database.

        This asynchronous method accepts a list of record objects and adds them
        to the database. If the operation is successful, it returns the added
        records. This method is useful for bulk inserting multiple rows into a
        database table efficiently.

        Parameters:
            records (list[Base]): A list of instances of the SQLAlchemy
            declarative base class, each representing a record to be added to
            the database.

        Returns:
            list[Base]: A list of instances of the records that were added to
            the database.

        Raises:
            Exception: If any error occurs during the database operation.

        Example:
            ```python
            from dsg_lib.async_database_functions import (
            async_database,
            base_schema,
            database_config,
            database_operations,
            )
            # Create a DBConfig instance
            config = {
                # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
                "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
                "echo": False,
                "future": True,
                # "pool_pre_ping": True,
                # "pool_size": 10,
                # "max_overflow": 10,
                "pool_recycle": 3600,
                # "pool_timeout": 30,
            }
            # create database configuration
            db_config = database_config.DBConfig(config)
            # Create an AsyncDatabase instance
            async_db = async_database.AsyncDatabase(db_config)
            # Create a DatabaseOperations instance
            db_ops = database_operations.DatabaseOperations(async_db)
            # create many records
            records = await db_ops.create_many([User(name='John Doe'), User(name='Jane Doe')])
            ```
        """
        # Log the start of the operation
        logger.debug('Starting create_many operation')

        try:
            # Start a timer to measure the operation time
            t0 = time.time()

            # Start a new database session
            async with self.async_db.get_db_session() as session:
                # Log the number of records being added
                logger.debug(f'Adding {len(records)} records to session')

                # Add the records to the session and commit the changes
                session.add_all(records)
                await session.commit()

                # Log the added records
                records_data = [record.__dict__ for record in records]
                logger.debug(f'Records added to session: {records_data}')

                # Calculate the operation time and log the successful record
                # addition
                num_records = len(records)
                t1 = time.time() - t0
                logger.info(
                    f'Record operations were successful. {num_records} records were created in {t1:.4f} seconds.'
                )

                return records

        except Exception as ex:
            # Handle any exceptions that occur during the record addition
            logger.error(f'Exception occurred: {ex}')
            return handle_exceptions(ex)

    async def count_query(self, query):
        """
        Executes a count query on the database and returns the number of records
        that match the query.

        This asynchronous method accepts a SQLAlchemy `Select` query object and
        returns the count of records that match the query. This is particularly
        useful for getting the total number of records that satisfy certain
        conditions without actually fetching the records themselves.

        Parameters:
            query (Select): A SQLAlchemy `Select` query object specifying the
            conditions to count records for.

        Returns:
            int: The number of records that match the query.

        Raises:
            Exception: If any error occurs during the execution of the query.

        Example:
            ```python
            from dsg_lib.async_database_functions import (
            async_database,
            base_schema,
            database_config,
            database_operations,
            )
            # Create a DBConfig instance
            config = {
                # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
                "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
                "echo": False,
                "future": True,
                # "pool_pre_ping": True,
                # "pool_size": 10,
                # "max_overflow": 10,
                "pool_recycle": 3600,
                # "pool_timeout": 30,
            }
            # create database configuration
            db_config = database_config.DBConfig(config)
            # Create an AsyncDatabase instance
            async_db = async_database.AsyncDatabase(db_config)
            # Create a DatabaseOperations instance
            db_ops = database_operations.DatabaseOperations(async_db)
            # count query
            count = await db_ops.count_query(select(User).where(User.age > 30))
            ```
        """
        # Log the start of the operation
        logger.debug('Starting count_query operation')

        try:
            # Start a new database session
            async with self.async_db.get_db_session() as session:
                # Log the query being executed
                logger.debug(f'Executing count query: {query}')

                # Execute the count query and retrieve the count
                result = await session.execute(select(func.count()).select_from(query.subquery()))
                count = result.scalar()

                # Log the successful query execution
                logger.info(f'Count query executed successfully. Result: {count}')

                return count

        except Exception as ex:
            # Handle any exceptions that occur during the query execution
            logger.error(f'Exception occurred: {ex}')
            return handle_exceptions(ex)

    async def read_query(self, query, limit=500, offset=0):
        """
        Executes a fetch query on the database and returns a list of records
        that match the query.

        This asynchronous method accepts a SQLAlchemy `Select` query object
        along with optional limit and offset parameters. It returns a list of
        records that match the query, with the number of records controlled by
        the limit, and the starting point of the records determined by the
        offset.

        Parameters:
            query (Select): A SQLAlchemy `Select` query object specifying the
            conditions to fetch records for. limit (int, optional): The maximum
            number of records to return. Defaults to 500. offset (int,
            optional): The number of records to skip before starting to return
            records. Defaults to 0.

        Returns:
            list: A list of records that match the query.

        Raises:
            Exception: If any error occurs during the execution of the query.

        Example:
            ```python
            from dsg_lib.async_database_functions import (
            async_database,
            base_schema,
            database_config,
            database_operations,
            )
            # Create a DBConfig instance
            config = {
                # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
                "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
                "echo": False,
                "future": True,
                # "pool_pre_ping": True,
                # "pool_size": 10,
                # "max_overflow": 10,
                "pool_recycle": 3600,
                # "pool_timeout": 30,
            }
            # create database configuration
            db_config = database_config.DBConfig(config)
            # Create an AsyncDatabase instance
            async_db = async_database.AsyncDatabase(db_config)
            # Create a DatabaseOperations instance
            db_ops = database_operations.DatabaseOperations(async_db)
            # read query
            records = await db_ops.read_query(select(User).where(User.age > 30), limit=10)
            ```
        """
        # Log the start of the operation
        logger.debug('Starting read_query operation')

        try:
            # Start a new database session
            async with self.async_db.get_db_session() as session:
                # Log the query being executed
                logger.debug(
                    f'Executing fetch query: {query} with limit: {limit} and offset: {offset}'
                )

                # Execute the fetch query and retrieve the records
                result = await session.execute(query.limit(limit).offset(offset))
                records = result.scalars().all()
                logger.debug(f'read_query result: {records}')
                # Log the successful query execution
                if all(isinstance(record, tuple) for record in records):
                    logger.debug(f'read_query result is a tuple {type(records)}')
                    # If all records are tuples, convert them to dictionaries
                    records_data = [
                        dict(zip(('request_group_id', 'count'), record, strict=False))
                        for record in records
                    ]
                else:
                    logger.debug(f'read_query result is a dictionary {type(records)}')
                    # Otherwise, try to convert the records to dictionaries using the __dict__ attribute
                    records_data = [record.__dict__ for record in records]

                logger.info(f'Fetch query executed successfully. Records: {records_data}')

                return records

        except Exception as ex:
            # Handle any exceptions that occur during the query execution
            logger.error(f'Exception occurred: {ex}')
            return handle_exceptions(ex)

    async def read_multi_query(self, queries: Dict[str, str], limit=500, offset=0):
        """
        Executes multiple fetch queries on the database and returns a dictionary
        of results for each query.

        This asynchronous method takes a dictionary where each key is a query
        name and each value is a SQLAlchemy `Select` query object. It also
        accepts optional limit and offset parameters. The method executes each
        query and returns a dictionary where each key is the query name, and the
        corresponding value is a list of records that match that query.

        Parameters:
            queries (Dict[str, Select]): A dictionary of SQLAlchemy `Select`
            query objects. limit (int, optional): The maximum number of records
            to return for each query. Defaults to 500. offset (int, optional):
            The number of records to skip before returning records for each
            query. Defaults to 0.

        Returns:
            dict: A dictionary where each key is a query name and each value is
            a list of records that match the query.

        Raises:
            Exception: If any error occurs during the execution of the queries.

        Example:
            ```python
            from dsg_lib.async_database_functions import (
            async_database,
            base_schema,
            database_config,
            database_operations,
            )
            # Create a DBConfig instance
            config = {
                # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
                "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
                "echo": False,
                "future": True,
                # "pool_pre_ping": True,
                # "pool_size": 10,
                # "max_overflow": 10,
                "pool_recycle": 3600,
                # "pool_timeout": 30,
            }
            # create database configuration
            db_config = database_config.DBConfig(config)
            # Create an AsyncDatabase instance
            async_db = async_database.AsyncDatabase(db_config)
            # Create a DatabaseOperations instance
            db_ops = database_operations.DatabaseOperations(async_db)
            # read multi query
            queries = {
                "query1": select(User).where(User.age > 30),
                "query2": select(User).where(User.age < 20),
            }
            results = await db_ops.read_multi_query(queries, limit=10)
            ```
        """
        # Log the start of the operation
        logger.debug('Starting read_multi_query operation')

        try:
            results = {}
            # Start a new database session
            async with self.async_db.get_db_session() as session:
                for query_name, query in queries.items():
                    # Log the query being executed
                    logger.debug(f'Executing fetch query: {query}')

                    # Execute the fetch query and retrieve the records
                    result = await session.execute(query.limit(limit).offset(offset))
                    data = result.scalars().all()

                    # Convert the records to dictionaries for logging
                    data_dicts = [record.__dict__ for record in data]
                    logger.debug(f"Fetch result for query '{query_name}': {data_dicts}")

                    # Log the successful query execution
                    logger.info(
                        f'Fetch query executed successfully: {query_name} with {len(data)} records'
                    )

                    # Store the records in the results dictionary
                    results[query_name] = data
            return results

        except Exception as ex:
            # Handle any exceptions that occur during the query execution
            logger.error(f'Exception occurred: {ex}')
            return handle_exceptions(ex)

    async def update_one(self, table, record_id: str, new_values: dict):
        """
        Updates a single record in the database identified by its ID.

        This asynchronous method takes a SQLAlchemy `Table` object, a record ID,
        and a dictionary of new values to update the record. It updates the
        specified record in the given table with the new values. The method does
        not allow updating certain fields, such as 'id' or 'date_created'.

        Parameters:
            table (Table): The SQLAlchemy `Table` object representing the table
            in the database. record_id (str): The ID of the record to be
            updated. new_values (dict): A dictionary containing the fields to
            update and their new values.

        Returns:
            Base: The updated record if successful; otherwise, an error
            dictionary.

        Raises:
            Exception: If any error occurs during the update operation.

        Example:
            ```python
            from dsg_lib.async_database_functions import (
            async_database,
            base_schema,
            database_config,
            database_operations,
            )
            # Create a DBConfig instance
            config = {
                # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
                "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
                "echo": False,
                "future": True,
                # "pool_pre_ping": True,
                # "pool_size": 10,
                # "max_overflow": 10,
                "pool_recycle": 3600,
                # "pool_timeout": 30,
            }
            # create database configuration
            db_config = database_config.DBConfig(config)
            # Create an AsyncDatabase instance
            async_db = async_database.AsyncDatabase(db_config)
            # Create a DatabaseOperations instance
            db_ops = database_operations.DatabaseOperations(async_db)
            # update one record
            record = await db_ops.update_one(User, 1, {'name': 'John Smith'})
            ```
        """
        non_updatable_fields = ['id', 'date_created']

        # Log the start of the operation
        logger.debug(
            f'Starting update_one operation for record_id: {record_id} in table: {table.__name__}'
        )

        try:
            # Start a new database session
            async with self.async_db.get_db_session() as session:
                # Log the record being fetched
                logger.debug(f'Fetching record with id: {record_id}')

                # Fetch the record
                record = await session.get(table, record_id)
                if not record:
                    # Log the error if no record is found
                    logger.error(f'No record found with pkid: {record_id}')
                    return {
                        'error': 'Record not found',
                        'details': f'No record found with pkid {record_id}',
                    }

                # Log the record being updated
                logger.debug(f'Updating record with new values: {new_values}')

                # Update the record with the new values
                for key, value in new_values.items():
                    if key not in non_updatable_fields:
                        setattr(record, key, value)
                await session.commit()

                # Log the successful record update
                logger.info(f'Record updated successfully: {record.pkid}')
                return record

        except Exception as ex:
            # Handle any exceptions that occur during the record update
            logger.error(f'Exception occurred: {ex}')
            return handle_exceptions(ex)

    async def delete_one(self, table, record_id: str):
        """
        Deletes a single record from the database based on the provided table
        and record ID.

        This asynchronous method accepts a SQLAlchemy `Table` object and a
        record ID. It attempts to delete the record with the given ID from the
        specified table. If the record is successfully deleted, it returns a
        success message. If no record with the given ID is found, it returns an
        error message.

        Args:
            table (Table): An instance of the SQLAlchemy `Table` class
            representing the database table from which the record will be
            deleted. record_id (str): The ID of the record to be deleted.

        Returns:
            dict: A dictionary containing a success message if the record was
            deleted successfully, or an error message if the record was not
            found or an exception occurred.

        Raises:
            Exception: If any error occurs during the delete operation.

        Example:
            ```python
            from dsg_lib.async_database_functions import (
            async_database,
            base_schema,
            database_config,
            database_operations,
            )
            # Create a DBConfig instance
            config = {
                # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
                "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
                "echo": False,
                "future": True,
                # "pool_pre_ping": True,
                # "pool_size": 10,
                # "max_overflow": 10,
                "pool_recycle": 3600,
                # "pool_timeout": 30,
            }
            # create database configuration
            db_config = database_config.DBConfig(config)
            # Create an AsyncDatabase instance
            async_db = async_database.AsyncDatabase(db_config)
            # Create a DatabaseOperations instance
            db_ops = database_operations.DatabaseOperations(async_db)
            # delete one record
            result = await db_ops.delete_one(User, 1)
            ```
        """
        # Log the start of the operation
        logger.debug(
            f'Starting delete_one operation for record_id: {record_id} in table: {table.__name__}'
        )

        try:
            # Start a new database session
            async with self.async_db.get_db_session() as session:
                # Log the record being fetched
                logger.debug(f'Fetching record with id: {record_id}')

                # Fetch the record
                record = await session.get(table, record_id)

                # If the record doesn't exist, return an error
                if not record:
                    logger.error(f'No record found with pkid: {record_id}')
                    return {
                        'error': 'Record not found',
                        'details': f'No record found with pkid {record_id}',
                    }

                # Log the record being deleted
                logger.debug(f'Deleting record with id: {record_id}')

                # Delete the record
                await session.delete(record)

                # Log the successful record deletion from the session
                logger.debug(f'Record deleted from session: {record}')

                # Log the start of the commit
                logger.debug(f'Committing changes to delete record with id: {record_id}')

                # Commit the changes
                await session.commit()

                # Log the successful record deletion
                logger.info(f'Record deleted successfully: {record_id}')

                return {'success': 'Record deleted successfully'}

        except Exception as ex:
            # Handle any exceptions that occur during the record deletion
            logger.error(f'Exception occurred: {ex}')
            return handle_exceptions(ex)

    async def delete_many(
        self,
        table: Type[DeclarativeMeta],
        id_column_name: str = 'pkid',
        id_values: List[int] = None,
    ) -> int:
        """
        Deletes multiple records from the specified table in the database.

        This method takes a table, an optional id column name, and a list of id values. It deletes the records in the table where the id column matches any of the id values in the list.

        Args:
            table (Type[DeclarativeMeta]): The table from which to delete records.
            id_column_name (str, optional): The name of the id column in the table. Defaults to "pkid".
            id_values (List[int], optional): A list of id values for the records to delete. Defaults to [].

        Returns:
            int: The number of records deleted from the table.

        Example:
        ```python
        from dsg_lib.async_database_functions import (
            async_database,
            base_schema,
            database_config,
            database_operations,
        )
        # Create a DBConfig instance
        config = {
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            "pool_recycle": 3600,
        }
        # create database configuration
        db_config = database_config.DBConfig(config)
        # Create an AsyncDatabase instance
        async_db = async_database.AsyncDatabase(db_config)
        # Create a DatabaseOperations instance
        db_ops = database_operations.DatabaseOperations(async_db)
        # Delete multiple records
        deleted_count = await db_ops.delete_many(User, 'id', [1, 2, 3])
        print(f"Deleted {deleted_count} records.")
        ```
        """
        if id_values is None:
            id_values = []
        try:
            # Start a timer to measure the operation time
            t0 = time.time()

            # Start a new database session
            async with self.async_db.get_db_session() as session:
                # Log the number of records being deleted
                logger.debug(f'Deleting {len(id_values)} records from session')

                # Create delete statement
                stmt = delete(table).where(getattr(table, id_column_name).in_(id_values))

                # Execute the delete statement and fetch result
                result = await session.execute(stmt)

                # Commit the changes
                await session.commit()

                # Get the count of deleted records
                deleted_count = result.rowcount

                # Log the deleted records
                logger.debug(f'Records deleted from session: {deleted_count}')

                # Calculate the operation time and log the successful record deletion
                t1 = time.time() - t0
                logger.info(
                    f'Record operations were successful. {deleted_count} records were deleted in {t1:.4f} seconds.'
                )

                return deleted_count

        except Exception as ex:
            # Handle any exceptions that occur during the record deletion
            logger.error(f'Exception occurred: {ex}')
            return handle_exceptions(ex)

__init__(async_db)

Initializes a new instance of the DatabaseOperations class.

Parameters:

Name Type Description Default
async_db AsyncDatabase

An instance of the

required

Example:

from dsg_lib.async_database_functions import (
async_database,
base_schema,
database_config,
database_operations,
)

config = {
    # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    # "pool_pre_ping": True,
    # "pool_size": 10,
    # "max_overflow": 10,
    "pool_recycle": 3600,
    # "pool_timeout": 30,
}

db_config = database_config.DBConfig(config)

async_db = async_database.AsyncDatabase(db_config)

db_ops = database_operations.DatabaseOperations(async_db)

Source code in dsg_lib/async_database_functions/database_operations.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def __init__(self, async_db: AsyncDatabase):
    """
    Initializes a new instance of the DatabaseOperations class.

    Args:
        async_db (module_name.AsyncDatabase): An instance of the
        AsyncDatabase class for performing asynchronous database operations.

    Example:
    ```python
    from dsg_lib.async_database_functions import (
    async_database,
    base_schema,
    database_config,
    database_operations,
    )

    config = {
        # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
        "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
        "echo": False,
        "future": True,
        # "pool_pre_ping": True,
        # "pool_size": 10,
        # "max_overflow": 10,
        "pool_recycle": 3600,
        # "pool_timeout": 30,
    }

    db_config = database_config.DBConfig(config)

    async_db = async_database.AsyncDatabase(db_config)

    db_ops = database_operations.DatabaseOperations(async_db)

    ```
    """
    # Log the start of the initialization
    logger.debug('Initializing DatabaseOperations instance')

    # Store the AsyncDatabase instance in the async_db attribute This
    # instance will be used for performing asynchronous database operations
    self.async_db = async_db

    # Log the successful initialization
    logger.info('DatabaseOperations instance initialized successfully')

count_query(query) async

Executes a count query on the database and returns the number of records that match the query.

This asynchronous method accepts a SQLAlchemy Select query object and returns the count of records that match the query. This is particularly useful for getting the total number of records that satisfy certain conditions without actually fetching the records themselves.

Parameters:

Name Type Description Default
query Select

A SQLAlchemy Select query object specifying the

required

Returns:

Name Type Description
int

The number of records that match the query.

Raises:

Type Description
Exception

If any error occurs during the execution of the query.

Example
from dsg_lib.async_database_functions import (
async_database,
base_schema,
database_config,
database_operations,
)
# Create a DBConfig instance
config = {
    # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    # "pool_pre_ping": True,
    # "pool_size": 10,
    # "max_overflow": 10,
    "pool_recycle": 3600,
    # "pool_timeout": 30,
}
# create database configuration
db_config = database_config.DBConfig(config)
# Create an AsyncDatabase instance
async_db = async_database.AsyncDatabase(db_config)
# Create a DatabaseOperations instance
db_ops = database_operations.DatabaseOperations(async_db)
# count query
count = await db_ops.count_query(select(User).where(User.age > 30))
Source code in dsg_lib/async_database_functions/database_operations.py
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
async def count_query(self, query):
    """
    Executes a count query on the database and returns the number of records
    that match the query.

    This asynchronous method accepts a SQLAlchemy `Select` query object and
    returns the count of records that match the query. This is particularly
    useful for getting the total number of records that satisfy certain
    conditions without actually fetching the records themselves.

    Parameters:
        query (Select): A SQLAlchemy `Select` query object specifying the
        conditions to count records for.

    Returns:
        int: The number of records that match the query.

    Raises:
        Exception: If any error occurs during the execution of the query.

    Example:
        ```python
        from dsg_lib.async_database_functions import (
        async_database,
        base_schema,
        database_config,
        database_operations,
        )
        # Create a DBConfig instance
        config = {
            # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            # "pool_pre_ping": True,
            # "pool_size": 10,
            # "max_overflow": 10,
            "pool_recycle": 3600,
            # "pool_timeout": 30,
        }
        # create database configuration
        db_config = database_config.DBConfig(config)
        # Create an AsyncDatabase instance
        async_db = async_database.AsyncDatabase(db_config)
        # Create a DatabaseOperations instance
        db_ops = database_operations.DatabaseOperations(async_db)
        # count query
        count = await db_ops.count_query(select(User).where(User.age > 30))
        ```
    """
    # Log the start of the operation
    logger.debug('Starting count_query operation')

    try:
        # Start a new database session
        async with self.async_db.get_db_session() as session:
            # Log the query being executed
            logger.debug(f'Executing count query: {query}')

            # Execute the count query and retrieve the count
            result = await session.execute(select(func.count()).select_from(query.subquery()))
            count = result.scalar()

            # Log the successful query execution
            logger.info(f'Count query executed successfully. Result: {count}')

            return count

    except Exception as ex:
        # Handle any exceptions that occur during the query execution
        logger.error(f'Exception occurred: {ex}')
        return handle_exceptions(ex)

create_many(records) async

Adds multiple records to the database.

This asynchronous method accepts a list of record objects and adds them to the database. If the operation is successful, it returns the added records. This method is useful for bulk inserting multiple rows into a database table efficiently.

Parameters:

Name Type Description Default
records list[Base]

A list of instances of the SQLAlchemy

required

Returns:

Type Description

list[Base]: A list of instances of the records that were added to

the database.

Raises:

Type Description
Exception

If any error occurs during the database operation.

Example
from dsg_lib.async_database_functions import (
async_database,
base_schema,
database_config,
database_operations,
)
# Create a DBConfig instance
config = {
    # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    # "pool_pre_ping": True,
    # "pool_size": 10,
    # "max_overflow": 10,
    "pool_recycle": 3600,
    # "pool_timeout": 30,
}
# create database configuration
db_config = database_config.DBConfig(config)
# Create an AsyncDatabase instance
async_db = async_database.AsyncDatabase(db_config)
# Create a DatabaseOperations instance
db_ops = database_operations.DatabaseOperations(async_db)
# create many records
records = await db_ops.create_many([User(name='John Doe'), User(name='Jane Doe')])
Source code in dsg_lib/async_database_functions/database_operations.py
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
async def create_many(self, records):
    """
    Adds multiple records to the database.

    This asynchronous method accepts a list of record objects and adds them
    to the database. If the operation is successful, it returns the added
    records. This method is useful for bulk inserting multiple rows into a
    database table efficiently.

    Parameters:
        records (list[Base]): A list of instances of the SQLAlchemy
        declarative base class, each representing a record to be added to
        the database.

    Returns:
        list[Base]: A list of instances of the records that were added to
        the database.

    Raises:
        Exception: If any error occurs during the database operation.

    Example:
        ```python
        from dsg_lib.async_database_functions import (
        async_database,
        base_schema,
        database_config,
        database_operations,
        )
        # Create a DBConfig instance
        config = {
            # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            # "pool_pre_ping": True,
            # "pool_size": 10,
            # "max_overflow": 10,
            "pool_recycle": 3600,
            # "pool_timeout": 30,
        }
        # create database configuration
        db_config = database_config.DBConfig(config)
        # Create an AsyncDatabase instance
        async_db = async_database.AsyncDatabase(db_config)
        # Create a DatabaseOperations instance
        db_ops = database_operations.DatabaseOperations(async_db)
        # create many records
        records = await db_ops.create_many([User(name='John Doe'), User(name='Jane Doe')])
        ```
    """
    # Log the start of the operation
    logger.debug('Starting create_many operation')

    try:
        # Start a timer to measure the operation time
        t0 = time.time()

        # Start a new database session
        async with self.async_db.get_db_session() as session:
            # Log the number of records being added
            logger.debug(f'Adding {len(records)} records to session')

            # Add the records to the session and commit the changes
            session.add_all(records)
            await session.commit()

            # Log the added records
            records_data = [record.__dict__ for record in records]
            logger.debug(f'Records added to session: {records_data}')

            # Calculate the operation time and log the successful record
            # addition
            num_records = len(records)
            t1 = time.time() - t0
            logger.info(
                f'Record operations were successful. {num_records} records were created in {t1:.4f} seconds.'
            )

            return records

    except Exception as ex:
        # Handle any exceptions that occur during the record addition
        logger.error(f'Exception occurred: {ex}')
        return handle_exceptions(ex)

create_one(record) async

Adds a single record to the database.

This asynchronous method accepts a record object and adds it to the database. If the operation is successful, it returns the added record. The method is useful for inserting a new row into a database table.

Parameters:

Name Type Description Default
record Base

An instance of the SQLAlchemy declarative base class

required

Returns:

Name Type Description
Base

The instance of the record that was added to the database.

Raises:

Type Description
Exception

If any error occurs during the database operation.

Example
from dsg_lib.async_database_functions import (
async_database,
base_schema,
database_config,
database_operations,
)
# Create a DBConfig instance
config = {
    # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    # "pool_pre_ping": True,
    # "pool_size": 10,
    # "max_overflow": 10,
    "pool_recycle": 3600,
    # "pool_timeout": 30,
}
# create database configuration
db_config = database_config.DBConfig(config)
# Create an AsyncDatabase instance
async_db = async_database.AsyncDatabase(db_config)
# Create a DatabaseOperations instance
db_ops = database_operations.DatabaseOperations(async_db)
# create one record
record = await db_ops.create_one(User(name='John Doe'))
Source code in dsg_lib/async_database_functions/database_operations.py
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
async def create_one(self, record):
    """
    Adds a single record to the database.

    This asynchronous method accepts a record object and adds it to the
    database. If the operation is successful, it returns the added record.
    The method is useful for inserting a new row into a database table.

    Parameters:
        record (Base): An instance of the SQLAlchemy declarative base class
        representing the record to be added to the database.

    Returns:
        Base: The instance of the record that was added to the database.

    Raises:
        Exception: If any error occurs during the database operation.

    Example:
        ```python
        from dsg_lib.async_database_functions import (
        async_database,
        base_schema,
        database_config,
        database_operations,
        )
        # Create a DBConfig instance
        config = {
            # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            # "pool_pre_ping": True,
            # "pool_size": 10,
            # "max_overflow": 10,
            "pool_recycle": 3600,
            # "pool_timeout": 30,
        }
        # create database configuration
        db_config = database_config.DBConfig(config)
        # Create an AsyncDatabase instance
        async_db = async_database.AsyncDatabase(db_config)
        # Create a DatabaseOperations instance
        db_ops = database_operations.DatabaseOperations(async_db)
        # create one record
        record = await db_ops.create_one(User(name='John Doe'))
        ```
    """
    # Log the start of the operation
    logger.debug('Starting create_one operation')

    try:
        # Start a new database session
        async with self.async_db.get_db_session() as session:
            # Log the record being added
            logger.debug(f'Adding record to session: {record.__dict__}')

            # Add the record to the session and commit the changes
            session.add(record)
            await session.commit()

            # Log the successful record addition
            logger.info(f'Record added successfully: {record}')

            return record

    except Exception as ex:
        # Handle any exceptions that occur during the record addition
        logger.error(f'Exception occurred: {ex}')
        return handle_exceptions(ex)

delete_many(table, id_column_name='pkid', id_values=None) async

Deletes multiple records from the specified table in the database.

This method takes a table, an optional id column name, and a list of id values. It deletes the records in the table where the id column matches any of the id values in the list.

Parameters:

Name Type Description Default
table Type[DeclarativeMeta]

The table from which to delete records.

required
id_column_name str

The name of the id column in the table. Defaults to "pkid".

'pkid'
id_values List[int]

A list of id values for the records to delete. Defaults to [].

None

Returns:

Name Type Description
int int

The number of records deleted from the table.

Example:

from dsg_lib.async_database_functions import (
    async_database,
    base_schema,
    database_config,
    database_operations,
)
# Create a DBConfig instance
config = {
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    "pool_recycle": 3600,
}
# create database configuration
db_config = database_config.DBConfig(config)
# Create an AsyncDatabase instance
async_db = async_database.AsyncDatabase(db_config)
# Create a DatabaseOperations instance
db_ops = database_operations.DatabaseOperations(async_db)
# Delete multiple records
deleted_count = await db_ops.delete_many(User, 'id', [1, 2, 3])
print(f"Deleted {deleted_count} records.")

Source code in dsg_lib/async_database_functions/database_operations.py
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
async def delete_many(
    self,
    table: Type[DeclarativeMeta],
    id_column_name: str = 'pkid',
    id_values: List[int] = None,
) -> int:
    """
    Deletes multiple records from the specified table in the database.

    This method takes a table, an optional id column name, and a list of id values. It deletes the records in the table where the id column matches any of the id values in the list.

    Args:
        table (Type[DeclarativeMeta]): The table from which to delete records.
        id_column_name (str, optional): The name of the id column in the table. Defaults to "pkid".
        id_values (List[int], optional): A list of id values for the records to delete. Defaults to [].

    Returns:
        int: The number of records deleted from the table.

    Example:
    ```python
    from dsg_lib.async_database_functions import (
        async_database,
        base_schema,
        database_config,
        database_operations,
    )
    # Create a DBConfig instance
    config = {
        "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
        "echo": False,
        "future": True,
        "pool_recycle": 3600,
    }
    # create database configuration
    db_config = database_config.DBConfig(config)
    # Create an AsyncDatabase instance
    async_db = async_database.AsyncDatabase(db_config)
    # Create a DatabaseOperations instance
    db_ops = database_operations.DatabaseOperations(async_db)
    # Delete multiple records
    deleted_count = await db_ops.delete_many(User, 'id', [1, 2, 3])
    print(f"Deleted {deleted_count} records.")
    ```
    """
    if id_values is None:
        id_values = []
    try:
        # Start a timer to measure the operation time
        t0 = time.time()

        # Start a new database session
        async with self.async_db.get_db_session() as session:
            # Log the number of records being deleted
            logger.debug(f'Deleting {len(id_values)} records from session')

            # Create delete statement
            stmt = delete(table).where(getattr(table, id_column_name).in_(id_values))

            # Execute the delete statement and fetch result
            result = await session.execute(stmt)

            # Commit the changes
            await session.commit()

            # Get the count of deleted records
            deleted_count = result.rowcount

            # Log the deleted records
            logger.debug(f'Records deleted from session: {deleted_count}')

            # Calculate the operation time and log the successful record deletion
            t1 = time.time() - t0
            logger.info(
                f'Record operations were successful. {deleted_count} records were deleted in {t1:.4f} seconds.'
            )

            return deleted_count

    except Exception as ex:
        # Handle any exceptions that occur during the record deletion
        logger.error(f'Exception occurred: {ex}')
        return handle_exceptions(ex)

delete_one(table, record_id) async

Deletes a single record from the database based on the provided table and record ID.

This asynchronous method accepts a SQLAlchemy Table object and a record ID. It attempts to delete the record with the given ID from the specified table. If the record is successfully deleted, it returns a success message. If no record with the given ID is found, it returns an error message.

Parameters:

Name Type Description Default
table Table

An instance of the SQLAlchemy Table class

required
deleted. record_id (str

The ID of the record to be deleted.

required

Returns:

Name Type Description
dict

A dictionary containing a success message if the record was

deleted successfully, or an error message if the record was not

found or an exception occurred.

Raises:

Type Description
Exception

If any error occurs during the delete operation.

Example
from dsg_lib.async_database_functions import (
async_database,
base_schema,
database_config,
database_operations,
)
# Create a DBConfig instance
config = {
    # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    # "pool_pre_ping": True,
    # "pool_size": 10,
    # "max_overflow": 10,
    "pool_recycle": 3600,
    # "pool_timeout": 30,
}
# create database configuration
db_config = database_config.DBConfig(config)
# Create an AsyncDatabase instance
async_db = async_database.AsyncDatabase(db_config)
# Create a DatabaseOperations instance
db_ops = database_operations.DatabaseOperations(async_db)
# delete one record
result = await db_ops.delete_one(User, 1)
Source code in dsg_lib/async_database_functions/database_operations.py
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
async def delete_one(self, table, record_id: str):
    """
    Deletes a single record from the database based on the provided table
    and record ID.

    This asynchronous method accepts a SQLAlchemy `Table` object and a
    record ID. It attempts to delete the record with the given ID from the
    specified table. If the record is successfully deleted, it returns a
    success message. If no record with the given ID is found, it returns an
    error message.

    Args:
        table (Table): An instance of the SQLAlchemy `Table` class
        representing the database table from which the record will be
        deleted. record_id (str): The ID of the record to be deleted.

    Returns:
        dict: A dictionary containing a success message if the record was
        deleted successfully, or an error message if the record was not
        found or an exception occurred.

    Raises:
        Exception: If any error occurs during the delete operation.

    Example:
        ```python
        from dsg_lib.async_database_functions import (
        async_database,
        base_schema,
        database_config,
        database_operations,
        )
        # Create a DBConfig instance
        config = {
            # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            # "pool_pre_ping": True,
            # "pool_size": 10,
            # "max_overflow": 10,
            "pool_recycle": 3600,
            # "pool_timeout": 30,
        }
        # create database configuration
        db_config = database_config.DBConfig(config)
        # Create an AsyncDatabase instance
        async_db = async_database.AsyncDatabase(db_config)
        # Create a DatabaseOperations instance
        db_ops = database_operations.DatabaseOperations(async_db)
        # delete one record
        result = await db_ops.delete_one(User, 1)
        ```
    """
    # Log the start of the operation
    logger.debug(
        f'Starting delete_one operation for record_id: {record_id} in table: {table.__name__}'
    )

    try:
        # Start a new database session
        async with self.async_db.get_db_session() as session:
            # Log the record being fetched
            logger.debug(f'Fetching record with id: {record_id}')

            # Fetch the record
            record = await session.get(table, record_id)

            # If the record doesn't exist, return an error
            if not record:
                logger.error(f'No record found with pkid: {record_id}')
                return {
                    'error': 'Record not found',
                    'details': f'No record found with pkid {record_id}',
                }

            # Log the record being deleted
            logger.debug(f'Deleting record with id: {record_id}')

            # Delete the record
            await session.delete(record)

            # Log the successful record deletion from the session
            logger.debug(f'Record deleted from session: {record}')

            # Log the start of the commit
            logger.debug(f'Committing changes to delete record with id: {record_id}')

            # Commit the changes
            await session.commit()

            # Log the successful record deletion
            logger.info(f'Record deleted successfully: {record_id}')

            return {'success': 'Record deleted successfully'}

    except Exception as ex:
        # Handle any exceptions that occur during the record deletion
        logger.error(f'Exception occurred: {ex}')
        return handle_exceptions(ex)

get_columns_details(table) async

Retrieves the details of the columns of a given table.

This asynchronous method accepts a table object and returns a dictionary. Each key in the dictionary is a column name from the table, and the corresponding value is another dictionary containing details about that column, such as type, if it's nullable, if it's a primary key, if it's unique, its autoincrement status, and its default value.

Parameters:

Name Type Description Default
table Table

An instance of the SQLAlchemy Table class

required

Returns:

Name Type Description
dict

A dictionary where each key is a column name, and each value

is a dictionary with the column's details.

Raises:

Type Description
Exception

If any error occurs during the database operation.

Example:

from sqlalchemy import Table, MetaData, Column,
Integer, String from dsg_lib.async_database_functions import module_name metadata = MetaData()
my_table = Table('my_table', metadata,
                Column('id', Integer, primary_key=True), Column('name',
                String))

from dsg_lib.async_database_functions import (
    async_database,
    base_schema,
    database_config,
    database_operations,
)
# Create a DBConfig instance
config = {
    # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    # "pool_pre_ping": True,
    # "pool_size": 10,
    # "max_overflow": 10,
    "pool_recycle": 3600,
    # "pool_timeout": 30,
}
# create database configuration
db_config = database_config.DBConfig(config)
# Create an AsyncDatabase instance
async_db = async_database.AsyncDatabase(db_config)
# Create a DatabaseOperations instance
db_ops = database_operations.DatabaseOperations(async_db)
# get columns details
columns = await db_ops.get_columns_details(my_table)

Source code in dsg_lib/async_database_functions/database_operations.py
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
async def get_columns_details(self, table):
    """
    Retrieves the details of the columns of a given table.

    This asynchronous method accepts a table object and returns a
    dictionary. Each key in the dictionary is a column name from the table,
    and the corresponding value is another dictionary containing details
    about that column, such as type, if it's nullable, if it's a primary
    key, if it's unique, its autoincrement status, and its default value.

    Args:
        table (Table): An instance of the SQLAlchemy Table class
        representing the database table for which column details are
        required.

    Returns:
        dict: A dictionary where each key is a column name, and each value
        is a dictionary with the column's details.

    Raises:
        Exception: If any error occurs during the database operation.

    Example:
    ```python
    from sqlalchemy import Table, MetaData, Column,
    Integer, String from dsg_lib.async_database_functions import module_name metadata = MetaData()
    my_table = Table('my_table', metadata,
                    Column('id', Integer, primary_key=True), Column('name',
                    String))

    from dsg_lib.async_database_functions import (
        async_database,
        base_schema,
        database_config,
        database_operations,
    )
    # Create a DBConfig instance
    config = {
        # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
        "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
        "echo": False,
        "future": True,
        # "pool_pre_ping": True,
        # "pool_size": 10,
        # "max_overflow": 10,
        "pool_recycle": 3600,
        # "pool_timeout": 30,
    }
    # create database configuration
    db_config = database_config.DBConfig(config)
    # Create an AsyncDatabase instance
    async_db = async_database.AsyncDatabase(db_config)
    # Create a DatabaseOperations instance
    db_ops = database_operations.DatabaseOperations(async_db)
    # get columns details
    columns = await db_ops.get_columns_details(my_table)
    ```
    """
    # Log the start of the operation
    logger.debug(f'Starting get_columns_details operation for table: {table.__name__}')

    try:
        # Log the start of the column retrieval
        logger.debug(f'Getting columns for table: {table.__name__}')

        # Retrieve the details of the columns and store them in a dictionary
        # The keys are the column names and the values are dictionaries
        # containing the column details
        columns = {
            c.name: {
                'type': str(c.type),
                'nullable': c.nullable,
                'primary_key': c.primary_key,
                'unique': c.unique,
                'autoincrement': c.autoincrement,
                'default': (
                    str(c.default.arg)
                    if c.default is not None and not callable(c.default.arg)
                    else None
                ),
            }
            for c in table.__table__.columns
        }

        # Log the successful column retrieval
        logger.info(f'Successfully retrieved columns for table: {table.__name__}')

        return columns
    except Exception as ex:  # pragma: no cover
        # Handle any exceptions that occur during the column retrieval
        logger.error(
            f'An error occurred while getting columns for table: {table.__name__}'
        )  # pragma: no cover
        return handle_exceptions(ex)  # pragma: no cover

get_primary_keys(table) async

Retrieves the primary keys of a given table.

This asynchronous method accepts a table object and returns a list containing the names of its primary keys. It is useful for understanding the structure of the table and for operations that require knowledge of the primary keys.

Parameters:

Name Type Description Default
table Table

An instance of the SQLAlchemy Table class

required

Returns:

Name Type Description
list

A list containing the names of the primary keys of the table.

Raises:

Type Description
Exception

If any error occurs during the database operation.

Example
from sqlalchemy import Table, MetaData, Column, Integer,
    String from dsg_lib.async_database_functions import module_name metadata = MetaData()
    my_table = Table('my_table', metadata,
                    Column('id', Integer, primary_key=True),
                    Column('name', String, primary_key=True))
from dsg_lib.async_database_functions import (
    async_database,
    base_schema,
    database_config,
    database_operations,
)
# Create a DBConfig instance
config = {
    # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    # "pool_pre_ping": True,
    # "pool_size": 10,
    # "max_overflow": 10,
    "pool_recycle": 3600,
    # "pool_timeout": 30,
}
# create database configuration
db_config = database_config.DBConfig(config)
# Create an AsyncDatabase instance
async_db = async_database.AsyncDatabase(db_config)
# Create a DatabaseOperations instance
db_ops = database_operations.DatabaseOperations(async_db)

# get primary keys
primary_keys = await db_ops.get_primary_keys(my_table)
Source code in dsg_lib/async_database_functions/database_operations.py
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
async def get_primary_keys(self, table):
    """
    Retrieves the primary keys of a given table.

    This asynchronous method accepts a table object and returns a list
    containing the names of its primary keys. It is useful for understanding
    the structure of the table and for operations that require knowledge of
    the primary keys.

    Args:
        table (Table): An instance of the SQLAlchemy Table class
        representing the database table for which primary keys are required.

    Returns:
        list: A list containing the names of the primary keys of the table.

    Raises:
        Exception: If any error occurs during the database operation.

    Example:
        ```python
        from sqlalchemy import Table, MetaData, Column, Integer,
            String from dsg_lib.async_database_functions import module_name metadata = MetaData()
            my_table = Table('my_table', metadata,
                            Column('id', Integer, primary_key=True),
                            Column('name', String, primary_key=True))
        from dsg_lib.async_database_functions import (
            async_database,
            base_schema,
            database_config,
            database_operations,
        )
        # Create a DBConfig instance
        config = {
            # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            # "pool_pre_ping": True,
            # "pool_size": 10,
            # "max_overflow": 10,
            "pool_recycle": 3600,
            # "pool_timeout": 30,
        }
        # create database configuration
        db_config = database_config.DBConfig(config)
        # Create an AsyncDatabase instance
        async_db = async_database.AsyncDatabase(db_config)
        # Create a DatabaseOperations instance
        db_ops = database_operations.DatabaseOperations(async_db)

        # get primary keys
        primary_keys = await db_ops.get_primary_keys(my_table)
        ```
    """
    # Log the start of the operation
    logger.debug(f'Starting get_primary_keys operation for table: {table.__name__}')

    try:
        # Log the start of the primary key retrieval
        logger.debug(f'Getting primary keys for table: {table.__name__}')

        # Retrieve the primary keys and store them in a list
        primary_keys = table.__table__.primary_key.columns.keys()

        # Log the successful primary key retrieval
        logger.info(f'Primary keys retrieved successfully: {primary_keys}')

        return primary_keys

    except Exception as ex:  # pragma: no cover
        # Handle any exceptions that occur during the primary key retrieval
        logger.error(f'Exception occurred: {ex}')  # pragma: no cover
        return handle_exceptions(ex)  # pragma: no cover

get_table_names() async

Retrieves the names of all tables in the database.

This asynchronous method returns a list containing the names of all tables in the database. It is useful for database introspection, allowing the user to know which tables are available in the current database context.

Returns:

Name Type Description
list

A list containing the names of all tables in the database.

Raises:

Type Description
Exception

If any error occurs during the database operation.

Example
from dsg_lib.async_database_functions import (
async_database,
base_schema,
database_config,
database_operations,
)
# Create a DBConfig instance
config = {
    # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    # "pool_pre_ping": True,
    # "pool_size": 10,
    # "max_overflow": 10,
    "pool_recycle": 3600,
    # "pool_timeout": 30,
}
# create database configuration
db_config = database_config.DBConfig(config)
# Create an AsyncDatabase instance
async_db = async_database.AsyncDatabase(db_config)
# Create a DatabaseOperations instance
db_ops = database_operations.DatabaseOperations(async_db)
# get table names
table_names = await db_ops.get_table_names()
Source code in dsg_lib/async_database_functions/database_operations.py
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
async def get_table_names(self):
    """
    Retrieves the names of all tables in the database.

    This asynchronous method returns a list containing the names of all
    tables in the database. It is useful for database introspection,
    allowing the user to know which tables are available in the current
    database context.

    Returns:
        list: A list containing the names of all tables in the database.

    Raises:
        Exception: If any error occurs during the database operation.

    Example:
        ```python
        from dsg_lib.async_database_functions import (
        async_database,
        base_schema,
        database_config,
        database_operations,
        )
        # Create a DBConfig instance
        config = {
            # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            # "pool_pre_ping": True,
            # "pool_size": 10,
            # "max_overflow": 10,
            "pool_recycle": 3600,
            # "pool_timeout": 30,
        }
        # create database configuration
        db_config = database_config.DBConfig(config)
        # Create an AsyncDatabase instance
        async_db = async_database.AsyncDatabase(db_config)
        # Create a DatabaseOperations instance
        db_ops = database_operations.DatabaseOperations(async_db)
        # get table names
        table_names = await db_ops.get_table_names()
        ```
    """
    # Log the start of the operation
    logger.debug('Starting get_table_names operation')

    try:
        # Log the start of the table name retrieval
        logger.debug('Retrieving table names')

        # Retrieve the table names and store them in a list The keys of the
        # metadata.tables dictionary are the table names
        table_names = list(self.async_db.Base.metadata.tables.keys())

        # Log the successful table name retrieval
        logger.info(f'Table names retrieved successfully: {table_names}')

        return table_names

    except Exception as ex:  # pragma: no cover
        # Handle any exceptions that occur during the table name retrieval
        logger.error(f'Exception occurred: {ex}')  # pragma: no cover
        return handle_exceptions(ex)  # pragma: no cover

read_multi_query(queries, limit=500, offset=0) async

Executes multiple fetch queries on the database and returns a dictionary of results for each query.

This asynchronous method takes a dictionary where each key is a query name and each value is a SQLAlchemy Select query object. It also accepts optional limit and offset parameters. The method executes each query and returns a dictionary where each key is the query name, and the corresponding value is a list of records that match that query.

Parameters:

Name Type Description Default
queries Dict[str, Select]

A dictionary of SQLAlchemy Select

required
query objects. limit (int

The maximum number of records

required
to return for each query. Defaults to 500. offset (int
required

Returns:

Name Type Description
dict

A dictionary where each key is a query name and each value is

a list of records that match the query.

Raises:

Type Description
Exception

If any error occurs during the execution of the queries.

Example
from dsg_lib.async_database_functions import (
async_database,
base_schema,
database_config,
database_operations,
)
# Create a DBConfig instance
config = {
    # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    # "pool_pre_ping": True,
    # "pool_size": 10,
    # "max_overflow": 10,
    "pool_recycle": 3600,
    # "pool_timeout": 30,
}
# create database configuration
db_config = database_config.DBConfig(config)
# Create an AsyncDatabase instance
async_db = async_database.AsyncDatabase(db_config)
# Create a DatabaseOperations instance
db_ops = database_operations.DatabaseOperations(async_db)
# read multi query
queries = {
    "query1": select(User).where(User.age > 30),
    "query2": select(User).where(User.age < 20),
}
results = await db_ops.read_multi_query(queries, limit=10)
Source code in dsg_lib/async_database_functions/database_operations.py
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
async def read_multi_query(self, queries: Dict[str, str], limit=500, offset=0):
    """
    Executes multiple fetch queries on the database and returns a dictionary
    of results for each query.

    This asynchronous method takes a dictionary where each key is a query
    name and each value is a SQLAlchemy `Select` query object. It also
    accepts optional limit and offset parameters. The method executes each
    query and returns a dictionary where each key is the query name, and the
    corresponding value is a list of records that match that query.

    Parameters:
        queries (Dict[str, Select]): A dictionary of SQLAlchemy `Select`
        query objects. limit (int, optional): The maximum number of records
        to return for each query. Defaults to 500. offset (int, optional):
        The number of records to skip before returning records for each
        query. Defaults to 0.

    Returns:
        dict: A dictionary where each key is a query name and each value is
        a list of records that match the query.

    Raises:
        Exception: If any error occurs during the execution of the queries.

    Example:
        ```python
        from dsg_lib.async_database_functions import (
        async_database,
        base_schema,
        database_config,
        database_operations,
        )
        # Create a DBConfig instance
        config = {
            # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            # "pool_pre_ping": True,
            # "pool_size": 10,
            # "max_overflow": 10,
            "pool_recycle": 3600,
            # "pool_timeout": 30,
        }
        # create database configuration
        db_config = database_config.DBConfig(config)
        # Create an AsyncDatabase instance
        async_db = async_database.AsyncDatabase(db_config)
        # Create a DatabaseOperations instance
        db_ops = database_operations.DatabaseOperations(async_db)
        # read multi query
        queries = {
            "query1": select(User).where(User.age > 30),
            "query2": select(User).where(User.age < 20),
        }
        results = await db_ops.read_multi_query(queries, limit=10)
        ```
    """
    # Log the start of the operation
    logger.debug('Starting read_multi_query operation')

    try:
        results = {}
        # Start a new database session
        async with self.async_db.get_db_session() as session:
            for query_name, query in queries.items():
                # Log the query being executed
                logger.debug(f'Executing fetch query: {query}')

                # Execute the fetch query and retrieve the records
                result = await session.execute(query.limit(limit).offset(offset))
                data = result.scalars().all()

                # Convert the records to dictionaries for logging
                data_dicts = [record.__dict__ for record in data]
                logger.debug(f"Fetch result for query '{query_name}': {data_dicts}")

                # Log the successful query execution
                logger.info(
                    f'Fetch query executed successfully: {query_name} with {len(data)} records'
                )

                # Store the records in the results dictionary
                results[query_name] = data
        return results

    except Exception as ex:
        # Handle any exceptions that occur during the query execution
        logger.error(f'Exception occurred: {ex}')
        return handle_exceptions(ex)

read_one_record(query) async

Retrieves a single record from the database based on the provided query.

This asynchronous method accepts a SQL query object and returns the first record that matches the query. If no record matches the query, it returns None. This method is useful for fetching specific data when the expected result is a single record.

Parameters:

Name Type Description Default
query Select

An instance of the SQLAlchemy Select class,

required

Returns:

Name Type Description
Result

The first record that matches the query or None if no record matches.

Raises:

Type Description
Exception

If any error occurs during the database operation.

Example
from dsg_lib.async_database_functions import (
async_database,
base_schema,
database_config,
database_operations,
)
# Create a DBConfig instance
config = {
    # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    # "pool_pre_ping": True,
    # "pool_size": 10,
    # "max_overflow": 10,
    "pool_recycle": 3600,
    # "pool_timeout": 30,
}
# create database configuration
db_config = database_config.DBConfig(config)
# Create an AsyncDatabase instance
async_db = async_database.AsyncDatabase(db_config)
# Create a DatabaseOperations instance
db_ops = database_operations.DatabaseOperations(async_db)
# read one record
record = await db_ops.read_one_record(select(User).where(User.name == 'John Doe'))
Source code in dsg_lib/async_database_functions/database_operations.py
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
async def read_one_record(self, query):
    """
    Retrieves a single record from the database based on the provided query.

    This asynchronous method accepts a SQL query object and returns the
    first record that matches the query. If no record matches the query, it
    returns None. This method is useful for fetching specific data
    when the expected result is a single record.

    Parameters:
        query (Select): An instance of the SQLAlchemy Select class,
        representing the query to be executed.

    Returns:
        Result: The first record that matches the query or None if no record matches.

    Raises:
        Exception: If any error occurs during the database operation.

    Example:
        ```python
        from dsg_lib.async_database_functions import (
        async_database,
        base_schema,
        database_config,
        database_operations,
        )
        # Create a DBConfig instance
        config = {
            # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            # "pool_pre_ping": True,
            # "pool_size": 10,
            # "max_overflow": 10,
            "pool_recycle": 3600,
            # "pool_timeout": 30,
        }
        # create database configuration
        db_config = database_config.DBConfig(config)
        # Create an AsyncDatabase instance
        async_db = async_database.AsyncDatabase(db_config)
        # Create a DatabaseOperations instance
        db_ops = database_operations.DatabaseOperations(async_db)
        # read one record
        record = await db_ops.read_one_record(select(User).where(User.name == 'John Doe'))
        ```
    """
    # Log the start of the operation
    logger.debug(f'Starting read_one_record operation for {query}')

    try:
        # Start a new database session
        async with self.async_db.get_db_session() as session:
            # Log the start of the record retrieval
            logger.debug(f'Getting record with query: {query}')

            # Execute the query and retrieve the first record
            result = await session.execute(query)
            record = result.scalar_one()

            # Log the successful record retrieval
            logger.info(f'Record retrieved successfully: {record}')

            return record

    except NoResultFound:
        # No record was found
        logger.info('No record found')
        return None

    except Exception as ex:  # pragma: no cover
        # Handle any exceptions that occur during the record retrieval
        logger.error(f'Exception occurred: {ex}')  # pragma: no cover
        return handle_exceptions(ex)  # pragma: no cover

read_query(query, limit=500, offset=0) async

Executes a fetch query on the database and returns a list of records that match the query.

This asynchronous method accepts a SQLAlchemy Select query object along with optional limit and offset parameters. It returns a list of records that match the query, with the number of records controlled by the limit, and the starting point of the records determined by the offset.

Parameters:

Name Type Description Default
query Select

A SQLAlchemy Select query object specifying the

required
conditions to fetch records for. limit (int

The maximum

required
optional)

The number of records to skip before starting to return

required

Returns:

Name Type Description
list

A list of records that match the query.

Raises:

Type Description
Exception

If any error occurs during the execution of the query.

Example
from dsg_lib.async_database_functions import (
async_database,
base_schema,
database_config,
database_operations,
)
# Create a DBConfig instance
config = {
    # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    # "pool_pre_ping": True,
    # "pool_size": 10,
    # "max_overflow": 10,
    "pool_recycle": 3600,
    # "pool_timeout": 30,
}
# create database configuration
db_config = database_config.DBConfig(config)
# Create an AsyncDatabase instance
async_db = async_database.AsyncDatabase(db_config)
# Create a DatabaseOperations instance
db_ops = database_operations.DatabaseOperations(async_db)
# read query
records = await db_ops.read_query(select(User).where(User.age > 30), limit=10)
Source code in dsg_lib/async_database_functions/database_operations.py
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
async def read_query(self, query, limit=500, offset=0):
    """
    Executes a fetch query on the database and returns a list of records
    that match the query.

    This asynchronous method accepts a SQLAlchemy `Select` query object
    along with optional limit and offset parameters. It returns a list of
    records that match the query, with the number of records controlled by
    the limit, and the starting point of the records determined by the
    offset.

    Parameters:
        query (Select): A SQLAlchemy `Select` query object specifying the
        conditions to fetch records for. limit (int, optional): The maximum
        number of records to return. Defaults to 500. offset (int,
        optional): The number of records to skip before starting to return
        records. Defaults to 0.

    Returns:
        list: A list of records that match the query.

    Raises:
        Exception: If any error occurs during the execution of the query.

    Example:
        ```python
        from dsg_lib.async_database_functions import (
        async_database,
        base_schema,
        database_config,
        database_operations,
        )
        # Create a DBConfig instance
        config = {
            # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            # "pool_pre_ping": True,
            # "pool_size": 10,
            # "max_overflow": 10,
            "pool_recycle": 3600,
            # "pool_timeout": 30,
        }
        # create database configuration
        db_config = database_config.DBConfig(config)
        # Create an AsyncDatabase instance
        async_db = async_database.AsyncDatabase(db_config)
        # Create a DatabaseOperations instance
        db_ops = database_operations.DatabaseOperations(async_db)
        # read query
        records = await db_ops.read_query(select(User).where(User.age > 30), limit=10)
        ```
    """
    # Log the start of the operation
    logger.debug('Starting read_query operation')

    try:
        # Start a new database session
        async with self.async_db.get_db_session() as session:
            # Log the query being executed
            logger.debug(
                f'Executing fetch query: {query} with limit: {limit} and offset: {offset}'
            )

            # Execute the fetch query and retrieve the records
            result = await session.execute(query.limit(limit).offset(offset))
            records = result.scalars().all()
            logger.debug(f'read_query result: {records}')
            # Log the successful query execution
            if all(isinstance(record, tuple) for record in records):
                logger.debug(f'read_query result is a tuple {type(records)}')
                # If all records are tuples, convert them to dictionaries
                records_data = [
                    dict(zip(('request_group_id', 'count'), record, strict=False))
                    for record in records
                ]
            else:
                logger.debug(f'read_query result is a dictionary {type(records)}')
                # Otherwise, try to convert the records to dictionaries using the __dict__ attribute
                records_data = [record.__dict__ for record in records]

            logger.info(f'Fetch query executed successfully. Records: {records_data}')

            return records

    except Exception as ex:
        # Handle any exceptions that occur during the query execution
        logger.error(f'Exception occurred: {ex}')
        return handle_exceptions(ex)

update_one(table, record_id, new_values) async

Updates a single record in the database identified by its ID.

This asynchronous method takes a SQLAlchemy Table object, a record ID, and a dictionary of new values to update the record. It updates the specified record in the given table with the new values. The method does not allow updating certain fields, such as 'id' or 'date_created'.

Parameters:

Name Type Description Default
table Table

The SQLAlchemy Table object representing the table

required
in the database. record_id (str

The ID of the record to be

required
updated. new_values (dict

A dictionary containing the fields to

required

Returns:

Name Type Description
Base

The updated record if successful; otherwise, an error

dictionary.

Raises:

Type Description
Exception

If any error occurs during the update operation.

Example
from dsg_lib.async_database_functions import (
async_database,
base_schema,
database_config,
database_operations,
)
# Create a DBConfig instance
config = {
    # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
    "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
    "echo": False,
    "future": True,
    # "pool_pre_ping": True,
    # "pool_size": 10,
    # "max_overflow": 10,
    "pool_recycle": 3600,
    # "pool_timeout": 30,
}
# create database configuration
db_config = database_config.DBConfig(config)
# Create an AsyncDatabase instance
async_db = async_database.AsyncDatabase(db_config)
# Create a DatabaseOperations instance
db_ops = database_operations.DatabaseOperations(async_db)
# update one record
record = await db_ops.update_one(User, 1, {'name': 'John Smith'})
Source code in dsg_lib/async_database_functions/database_operations.py
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
async def update_one(self, table, record_id: str, new_values: dict):
    """
    Updates a single record in the database identified by its ID.

    This asynchronous method takes a SQLAlchemy `Table` object, a record ID,
    and a dictionary of new values to update the record. It updates the
    specified record in the given table with the new values. The method does
    not allow updating certain fields, such as 'id' or 'date_created'.

    Parameters:
        table (Table): The SQLAlchemy `Table` object representing the table
        in the database. record_id (str): The ID of the record to be
        updated. new_values (dict): A dictionary containing the fields to
        update and their new values.

    Returns:
        Base: The updated record if successful; otherwise, an error
        dictionary.

    Raises:
        Exception: If any error occurs during the update operation.

    Example:
        ```python
        from dsg_lib.async_database_functions import (
        async_database,
        base_schema,
        database_config,
        database_operations,
        )
        # Create a DBConfig instance
        config = {
            # "database_uri": "postgresql+asyncpg://postgres:postgres@postgresdb/postgres",
            "database_uri": "sqlite+aiosqlite:///:memory:?cache=shared",
            "echo": False,
            "future": True,
            # "pool_pre_ping": True,
            # "pool_size": 10,
            # "max_overflow": 10,
            "pool_recycle": 3600,
            # "pool_timeout": 30,
        }
        # create database configuration
        db_config = database_config.DBConfig(config)
        # Create an AsyncDatabase instance
        async_db = async_database.AsyncDatabase(db_config)
        # Create a DatabaseOperations instance
        db_ops = database_operations.DatabaseOperations(async_db)
        # update one record
        record = await db_ops.update_one(User, 1, {'name': 'John Smith'})
        ```
    """
    non_updatable_fields = ['id', 'date_created']

    # Log the start of the operation
    logger.debug(
        f'Starting update_one operation for record_id: {record_id} in table: {table.__name__}'
    )

    try:
        # Start a new database session
        async with self.async_db.get_db_session() as session:
            # Log the record being fetched
            logger.debug(f'Fetching record with id: {record_id}')

            # Fetch the record
            record = await session.get(table, record_id)
            if not record:
                # Log the error if no record is found
                logger.error(f'No record found with pkid: {record_id}')
                return {
                    'error': 'Record not found',
                    'details': f'No record found with pkid {record_id}',
                }

            # Log the record being updated
            logger.debug(f'Updating record with new values: {new_values}')

            # Update the record with the new values
            for key, value in new_values.items():
                if key not in non_updatable_fields:
                    setattr(record, key, value)
            await session.commit()

            # Log the successful record update
            logger.info(f'Record updated successfully: {record.pkid}')
            return record

    except Exception as ex:
        # Handle any exceptions that occur during the record update
        logger.error(f'Exception occurred: {ex}')
        return handle_exceptions(ex)

handle_exceptions(ex)

Handles exceptions for database operations.

This function checks the type of the exception, logs an appropriate error message, and returns a dictionary containing the error details.

Parameters:

Name Type Description Default
ex Exception

The exception to handle.

required

Returns:

Name Type Description
dict Dict[str, str]

A dictionary containing the error details. The dictionary has two

keys Dict[str, str]

'error' and 'details'.

Example:

from dsg_lib.async_database_functions import database_operations

try:
    # Some database operation that might raise an exception pass
except Exception as ex:
    error_details = database_operations.handle_exceptions(ex)
    print(error_details)

Source code in dsg_lib/async_database_functions/database_operations.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def handle_exceptions(ex: Exception) -> Dict[str, str]:
    """
    Handles exceptions for database operations.

    This function checks the type of the exception, logs an appropriate error
    message, and returns a dictionary containing the error details.

    Args:
        ex (Exception): The exception to handle.

    Returns:
        dict: A dictionary containing the error details. The dictionary has two
        keys: 'error' and 'details'.

    Example:
    ```python
    from dsg_lib.async_database_functions import database_operations

    try:
        # Some database operation that might raise an exception pass
    except Exception as ex:
        error_details = database_operations.handle_exceptions(ex)
        print(error_details)
    ```
    """
    # Extract the error message before the SQL statement
    error_only = str(ex).split('[SQL:')[0]

    # Check the type of the exception
    if isinstance(ex, IntegrityError):
        # Log the error and return the error details
        logger.error(f'IntegrityError occurred: {ex}')
        return {'error': 'IntegrityError', 'details': error_only}
    elif isinstance(ex, SQLAlchemyError):
        # Log the error and return the error details
        logger.error(f'SQLAlchemyError occurred: {ex}')
        return {'error': 'SQLAlchemyError', 'details': error_only}
    else:
        # Log the error and return the error details
        logger.error(f'Exception occurred: {ex}')
        return {'error': 'General Exception', 'details': str(ex)}