RouteMQ Tinker - Interactive REPL

The RouteMQ Tinker is an interactive REPL (Read-Eval-Print Loop) environment similar to Laravel Artisan Tinker. It allows you to test your ORM relationships, queries, and interact with your application components in real-time.

Features

  • 🔧 Interactive Python shell with async/await support via run_async() helper

  • 📊 Pre-loaded ORM models and database session

  • 🔍 SQLAlchemy query helpers (select, and_, or_, func, etc.)

  • 🚀 Redis manager integration (if enabled)

  • 📝 Auto-completion and syntax highlighting via IPython

Usage

Starting Tinker

# Start the interactive REPL
python main.py --tinker

Available Objects

When you start tinker, the following objects are automatically available:

  • app - Application instance

  • Model - Base Model class

  • Base - SQLAlchemy declarative base

  • session - Database session (if MySQL is enabled)

  • redis_manager - Redis manager (if Redis is enabled)

  • select - SQLAlchemy select function

  • and_, or_ - SQLAlchemy logical operators

  • func - SQLAlchemy functions

  • desc, asc - SQLAlchemy ordering functions

  • run_async() - Helper function to execute async operations

All your models from app/models/ are automatically imported and available by their class names.

Example Usage

Basic Queries

Since all database operations are async, you need to use the run_async() helper function:

Creating Records

Working with Relationships

Complex Queries

Redis Operations (if enabled)

Available Helper Functions

The tinker environment provides several pre-built helper functions:

  • query_devices() - Get all devices from the database

  • query_users() - Get all users from the database

  • create_sample_device() - Create a sample device with timestamp

  • run_async(coroutine) - Execute any async operation

Important: Using run_async()

Since your application uses async/await patterns for database operations, you must wrap all async calls with run_async():

Tips

  • Always use run_async() for database operations and other async functions

  • Use ; at the end of a line to suppress output printing

  • Press Tab for auto-completion

  • Use Ctrl+D or type exit() to quit

  • All your models are automatically imported and available by name

  • The session is automatically created and available when MySQL is enabled

Prerequisites

Make sure you have the following dependencies installed:

Configuration

Enable MySQL in your .env file for database operations:

Enable Redis if you want to test Redis operations:

Real-World Examples

Here are some practical examples using your actual models:

Working with Devices and Trips

Parameter Values and States

Creating Test Data

Troubleshooting

Database Not Available

If you see "Database is disabled in configuration", make sure:

  1. ENABLE_MYSQL=true in your .env file

  2. Database credentials are correct

  3. MySQL server is running

Models Not Imported

If your models aren't showing up:

  1. Make sure they're in the app/models/ directory

  2. They should inherit from Base

  3. They should have a __tablename__ attribute

Async Issues

If you encounter async-related errors:

  1. Always use run_async() for database operations

  2. Don't use bare await statements in the REPL

  3. The tinker environment handles event loop conflicts automatically

SyntaxError with await

If you get "SyntaxError: 'await' outside function":

Last updated