Skip to main content

A Laravel-like ORM for MySQL in Python

Project description

PyLaravel ORM

A simple Laravel-like ORM and query builder for MySQL in Python.
This package provides a convenient way to interact with your MySQL database, inspired by the database components of the Laravel PHP framework.


✨ Features

  • Fluent Query Builder for SELECT, INSERT, UPDATE, DELETE.
  • Simple Schema Builder for creating and modifying tables.
  • Easy-to-configure database connection.
  • Support for raw SQL queries.
  • Access to cursor attributes (lastrowid, rowcount, etc.).

📦 Installation

You can install the package from PyPI:

pip install pylaravel-orm

Or, for local development:

pip install -e .

🚀 Usage

1. Database Configuration

First, configure your database connection details.

from database import Database

Database.host = '127.0.0.1'
Database.port = 3306
Database.name = 'your_database_name'
Database.user = 'your_username'
Database.password = 'your_password'

2. Creating Tables (Schema)

You can easily define and create tables using the CreateTable class.

from database import CreateTable

# Create a 'users' table
schema = CreateTable('users')
schema.id()
schema.string('name', length=100)
schema.string('email').unique()   # unique field
schema.text('bio').nullable()     # nullable field
schema.timestamps()               # created_at & updated_at
schema.run()

print("Table 'users' created successfully!")

3. Query Builder

🔹 Inserting Data

from database import Table

users = Table('users')

# Insert a single record
users.insert({
    'name': 'John Doe',
    'email': 'john.doe@example.com',
    'bio': 'A software developer.'
}).run()

# Insert multiple records (looping)
for i in range(5):
    users.insert({
        'name': f'User {i}',
        'email': f'user{i}@example.com'
    }).run()

🔹 Selecting Data

from database import Table

users = Table('users')

# Get all users
all_users = users.select().run()
print("All users:", all_users)

# Get a user by ID
user = users.select().where("id = 1").run()
print("User with ID 1:", user)

# Select specific columns
emails = users.select('id, name, email').run()
for row in emails:
    print(row['name'], row['email'])

🔹 Updating Data

from database import Table

users = Table('users')

# Update a user's bio
users.update({'bio': 'An amazing software developer.'}).where("id = 1").run()

🔹 Deleting Data

from database import Table

users = Table('users')

# Delete a user by ID
users.delete().where("id = 5").run()

4. Raw Queries

You can also run raw SQL queries directly using the Database.query() or Database.execute() methods.

from database import Database

# SELECT query
cur = Database.query("SELECT * FROM users WHERE email=%s", ("john.doe@example.com",))
print(cur.fetchone())

# INSERT query with lastrowid
cur = Database.execute(
    "INSERT INTO users (name, email) VALUES (%s, %s)",
    ("Ali", "ali@example.com")
)
print("Inserted ID:", cur.lastrowid)

# UPDATE query with rowcount
cur = Database.execute("UPDATE users SET bio=%s WHERE id=%s", ("Updated bio", 1))
print("Rows updated:", cur.rowcount)

5. Closing the Connection

Always close the connection when your application shuts down:

from database import Database

Database.close()

📝 License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylaravel_orm-0.1.2.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pylaravel_orm-0.1.2-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file pylaravel_orm-0.1.2.tar.gz.

File metadata

  • Download URL: pylaravel_orm-0.1.2.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for pylaravel_orm-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f0d9b7ec91c0de5a8987596521c35d442c00747f619f2d5f9a0af828b788a078
MD5 ae5633030c504c089abbe0a7167fc736
BLAKE2b-256 f3fd9a013c57b25e875b297d8cd11bacb5e3f485686ab70113361fce2ea9b881

See more details on using hashes here.

File details

Details for the file pylaravel_orm-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pylaravel_orm-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for pylaravel_orm-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c2036501413ed6c1806475083d0926733cdab20783ae5b380e90a1506429312a
MD5 0973f71a39b9b76181c8902c6c010607
BLAKE2b-256 c56401ab8bedfe0a7f1c01579bb41f088cd9e7c5fa551730029dafc12476895d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page