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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pylaravel_orm-0.1.3.tar.gz.
File metadata
- Download URL: pylaravel_orm-0.1.3.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12b9ade16c70d406bc976728fa10e3d3a63e3c304cbec54012a1f1026186daf4
|
|
| MD5 |
e1090b9b55ece9509a360f03b8b73576
|
|
| BLAKE2b-256 |
bdb3273eee0a7cb673c44c743253debb01e41f17c9541a5b3190eb41421cadb8
|
File details
Details for the file pylaravel_orm-0.1.3-py3-none-any.whl.
File metadata
- Download URL: pylaravel_orm-0.1.3-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f30a6457da7e8c7942e17ad8921f44afe85933a4e9850db11e67b1cc8d0552f
|
|
| MD5 |
98b514ae4967e85432b247671fa190b9
|
|
| BLAKE2b-256 |
e5411d82689673ff695ef0129b2a264608d997bc015e8e51c2d11d988b2b4266
|