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.4.tar.gz (4.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.4-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pylaravel_orm-0.1.4.tar.gz
  • Upload date:
  • Size: 4.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.4.tar.gz
Algorithm Hash digest
SHA256 b2d6b5ebf57e8dd167f7c671ef62edeced2d30b8c3b370fc066005c344b394fc
MD5 d57aaf06fa485da163a1b69daf6aba91
BLAKE2b-256 c0d0a7bf277bc5ffecd46be72da0bc3f154c849b1747a31caeead9a6c8bbb603

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pylaravel_orm-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.3 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3f15f2e24e5c93dbbe839bb9d023ec5cefcb4b4a50aa9ca218c4d965bf1dde79
MD5 598409e276a1800f66f94aef6382af77
BLAKE2b-256 c6c55439ec43c3075084cfead1e3fa424adf357980985a13d58a44d40395ba67

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