Convert Figma logo to code with AI

kvesteri logosqlalchemy-utils

Various utility functions and datatypes for SQLAlchemy.

1,250
319
1,250
204

Top Related Projects

THIS IS NOT THE OFFICIAL REPO - PLEASE SUBMIT PRs ETC AT: http://github.com/sqlalchemy/sqlalchemy

2,714

A database migrations tool for SQLAlchemy.

11,053

a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb

Async database support for Python. 🗄

4,754

Easy-to-use data handling for SQL data stores with support for implicit table creation, bulk loading, and transactions.

Quick Overview

SQLAlchemy-Utils is a Python library that extends SQLAlchemy with additional data types, utility functions, and helpers. It aims to simplify common database operations and provide useful tools for working with SQLAlchemy, enhancing productivity and code readability.

Pros

  • Offers a wide range of additional data types (e.g., UUID, TSVector, JSONType)
  • Provides utility functions for common database operations
  • Enhances SQLAlchemy's functionality without modifying its core
  • Well-documented and actively maintained

Cons

  • Adds an extra dependency to your project
  • Some features may overlap with SQLAlchemy's built-in functionality
  • Learning curve for developers unfamiliar with the library
  • May introduce compatibility issues with certain SQLAlchemy versions

Code Examples

  1. Using the URLType for storing URLs:
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_utils import URLType

Base = declarative_base()

class Website(Base):
    __tablename__ = 'website'
    id = Column(Integer, primary_key=True)
    url = Column(URLType)
  1. Generating a random password:
from sqlalchemy_utils import generate_password

password = generate_password(length=12)
print(password)
  1. Using the ChoiceType for predefined choices:
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_utils import ChoiceType

Base = declarative_base()

class User(Base):
    TYPES = [
        ('admin', 'Admin'),
        ('regular', 'Regular User'),
    ]

    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    type = Column(ChoiceType(TYPES))

Getting Started

To get started with SQLAlchemy-Utils, follow these steps:

  1. Install the library:
pip install SQLAlchemy-Utils
  1. Import and use the desired utilities in your SQLAlchemy models:
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_utils import URLType, PhoneNumberType

Base = declarative_base()

class Contact(Base):
    __tablename__ = 'contact'
    id = Column(Integer, primary_key=True)
    name = Column(String(100))
    website = Column(URLType)
    phone = Column(PhoneNumberType)
  1. Use the utility functions as needed in your application code:
from sqlalchemy_utils import create_database, database_exists

if not database_exists('postgresql://user:pass@localhost/mydb'):
    create_database('postgresql://user:pass@localhost/mydb')

Competitor Comparisons

THIS IS NOT THE OFFICIAL REPO - PLEASE SUBMIT PRs ETC AT: http://github.com/sqlalchemy/sqlalchemy

Pros of SQLAlchemy

  • Core ORM functionality with extensive database support
  • Highly flexible and customizable query construction
  • Comprehensive documentation and large community support

Cons of SQLAlchemy

  • Steeper learning curve for beginners
  • Lacks some utility functions for common database operations

Code Comparison

SQLAlchemy:

from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)

SQLAlchemy-Utils:

from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_utils import EmailType, PasswordType

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    email = Column(EmailType)
    password = Column(PasswordType(schemes=['pbkdf2_sha512']))

Summary

SQLAlchemy is a comprehensive ORM library with extensive features for database interaction. It offers flexibility and power but may have a steeper learning curve. SQLAlchemy-Utils extends SQLAlchemy with additional utilities and custom column types, making certain common tasks easier to implement. While SQLAlchemy provides the core functionality, SQLAlchemy-Utils adds convenience methods and specialized data types that can simplify development for specific use cases.

2,714

A database migrations tool for SQLAlchemy.

Pros of Alembic

  • Focused on database migration management
  • Integrates seamlessly with SQLAlchemy models
  • Provides a powerful command-line interface for managing migrations

Cons of Alembic

  • Limited to migration-related functionality
  • Steeper learning curve for beginners
  • Requires additional setup and configuration

Code Comparison

Alembic migration example:

from alembic import op
import sqlalchemy as sa

def upgrade():
    op.create_table(
        'users',
        sa.Column('id', sa.Integer(), primary_key=True),
        sa.Column('name', sa.String(50), nullable=False)
    )

def downgrade():
    op.drop_table('users')

SQLAlchemy-Utils example (custom data type):

from sqlalchemy_utils import EmailType

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    email = Column(EmailType)

SQLAlchemy-Utils focuses on providing additional utilities and custom data types for SQLAlchemy, while Alembic specializes in database schema migrations. Alembic offers more robust migration capabilities, but SQLAlchemy-Utils provides a wider range of utility functions and custom column types to enhance SQLAlchemy's functionality.

11,053

a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb

Pros of Peewee

  • Lightweight and simple ORM with a smaller learning curve
  • Built-in support for SQLite, MySQL, and PostgreSQL without additional dependencies
  • Intuitive and Pythonic query syntax

Cons of Peewee

  • Less flexibility and extensibility compared to SQLAlchemy ecosystem
  • Limited support for complex queries and advanced database features
  • Smaller community and fewer third-party extensions

Code Comparison

SQLAlchemy-Utils:

from sqlalchemy_utils import URLType, PhoneNumberType

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    website = Column(URLType)
    phone = Column(PhoneNumberType)

Peewee:

from peewee import *

class User(Model):
    website = CharField()
    phone = CharField()

    class Meta:
        database = db

While SQLAlchemy-Utils provides specialized column types like URLType and PhoneNumberType, Peewee relies on basic field types and custom validation. SQLAlchemy-Utils offers more built-in data types and utilities, whereas Peewee focuses on simplicity and ease of use.

Async database support for Python. 🗄

Pros of databases

  • Supports async database operations, which can improve performance in I/O-bound applications
  • Provides a unified interface for multiple database backends, including SQLite, PostgreSQL, and MySQL
  • Designed with modern Python features in mind, including type hints and async/await syntax

Cons of databases

  • Less mature and has fewer features compared to sqlalchemy-utils
  • Lacks some of the advanced utilities and helpers provided by sqlalchemy-utils
  • May require more setup and configuration for complex database operations

Code Comparison

databases:

import databases

database = databases.Database("sqlite:///example.db")

async def main():
    await database.connect()
    query = "SELECT * FROM users"
    results = await database.fetch_all(query=query)

sqlalchemy-utils:

from sqlalchemy import create_engine
from sqlalchemy_utils import database_exists, create_database

engine = create_engine("sqlite:///example.db")
if not database_exists(engine.url):
    create_database(engine.url)

# Use SQLAlchemy ORM or Core for database operations

Both libraries offer different approaches to database management. databases focuses on async operations and a unified interface, while sqlalchemy-utils provides a wide range of utilities to enhance SQLAlchemy's functionality.

4,754

Easy-to-use data handling for SQL data stores with support for implicit table creation, bulk loading, and transactions.

Pros of dataset

  • Simpler API for basic database operations
  • Automatic schema creation and table management
  • Built-in support for data export (CSV, JSON)

Cons of dataset

  • Less flexibility for complex queries and relationships
  • Limited support for advanced SQLAlchemy features
  • Potentially slower performance for large-scale operations

Code Comparison

dataset:

import dataset

db = dataset.connect('sqlite:///example.db')
table = db['users']
table.insert(dict(name='John Doe', age=37))
users = table.find(age={'>=': 30})

sqlalchemy-utils:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_utils import database_exists, create_database

engine = create_engine('sqlite:///example.db')
if not database_exists(engine.url):
    create_database(engine.url)

Session = sessionmaker(bind=engine)
session = Session()

Summary

dataset is more user-friendly for simple database operations and quick prototyping, while sqlalchemy-utils provides additional utilities for SQLAlchemy, offering more advanced features and flexibility. dataset is better suited for small to medium-sized projects with straightforward data needs, whereas sqlalchemy-utils is more appropriate for complex applications requiring fine-grained control over database operations and schema management.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

SQLAlchemy-Utils

|Build| |Version Status| |Downloads|

Various utility functions, new data types and helpers for SQLAlchemy.

Resources

  • Documentation <https://sqlalchemy-utils.readthedocs.io/>_
  • Issue Tracker <https://github.com/kvesteri/sqlalchemy-utils/issues>_
  • Code <https://github.com/kvesteri/sqlalchemy-utils/>_

.. |Build| image:: https://github.com/kvesteri/sqlalchemy-utils/actions/workflows/test.yaml/badge.svg?branch=master :target: https://github.com/kvesteri/sqlalchemy-utils/actions/workflows/test.yaml :alt: Tests .. |Version Status| image:: https://img.shields.io/pypi/v/SQLAlchemy-Utils.svg :target: https://pypi.python.org/pypi/SQLAlchemy-Utils/ .. |Downloads| image:: https://img.shields.io/pypi/dm/SQLAlchemy-Utils.svg :target: https://pypi.python.org/pypi/SQLAlchemy-Utils/