JetORM is the simplest ORM of its kind.
Project description
JetORM
The simple wrapper for work with databases.
Get started with JetORM!
Connect with database
Connecting to the database:
MySQL
An instance of the mysql class accepts 2 mandatory and 4 optional arguments as input
from jetorm import mysql
mysql("host", "database", port=3306,
user="root", password="")
For example (we have a local server and port 3306, database name - test):
db = mysql("127.0.0.1", "dbname")
SQLite
An instance of the sqlite class accepts 1 mendotory argument as input
from jetorm import sqlite
db = sqlite("dbfile.db")
P.S In the form of an instance name of a mysql or sqlite class, we will use the db name
MySQL CRUD
CREATE
In order to create a table, there is method dispense
Example:
instance_of_the_mysql_class.dispense("table")
This method create table and it's create default field id
Structure of a table
In order to create a table structure, there are several methods, we will analyze each.
Method name: add
This method takes 4 arguments as input:
table: str, name_of_the_field: str, type: str, unsigned=True
JetORM MySQL Types:
- int (INT)
- varchar (VARCHAR)
- text (TEXT)
- dt:ct (datetime current timestamp)
- id (INDEX)
- bit (BIT)
- bool (BOOLEAN)
- tint (TINYINT)
- bint (BIGINT)
Q: How to specify the field types correctly? A: A colon is placed after the type and the length of the values of this field is written. That is, if you have a VARCHAR field type, specify the length of values for it, for example, 255:
db.add('table', 'name_of_the_field', 'varchar:255')
Another Example:
db.add('table', 'log_date','dt:ct')
This code will create a field with the datetime type and the default value of current timestamp
Example:
db.add('table', 'money', 'bint')
This code will create a field with the BIGINT type
Example:
db.add('table', 'number', 'int:8')
This code will create a field with an INT type and a value length of 8
Method name: puts
This method takes 4 arguments as input:
table: str, column_values: dict, unsigned=True
It's create and insert the data which you inputed
For example:
db.puts('table', {'name': 'Alex', 'age': 18})
So it will create fields name
and age
and insert data Alex, 18
INSERT
Method name: insert
The method responsible for inserting values takes 2 arguments: table: str, column_values: dict
For example:
db.insert('table', {'name': 'John', 'age': 25})
Also there is method execute your sql:
db.exec("YOUR SQL", values)
For example:
db.exec("SELECT * FROM `table` WHERE `password` = %s", "qwerty123")
Besides that, if the data is not recorded, use this method:
db.store()
READ
Load the cells
To get a cell from a table, you can use several methods:
load
load_all
read
read_one
find
find_one
Method name: load
This method returns a list with data
db.load(table: str, ids: tuple)
For example:
user = db.load('some_table', (1, 3))
Method name: load_all
This method returns all the cells
db.load_all(table: str)
For example:
all_users = db.load_all('some_table')
Method name: read
This method returns certain fields
db.read(table: str, fields: tuple, sub_sql: str=None, *values)
For example:
user = db.read('table', ('name',), "`password` = %s", 'qwerty')
or like this:
user = db.read('table', ('age',))
Method name: read_one
This method is similar to the read
method, but it's adds to the SQL query LIMIT 1
Method name: find
This method returns cell
db.find(table: str, sql: str, *values)
For example:
users = db.find('some_table', "`age` > 15")
or
users = db.find('some_table', "`password` = %s", 'qwerty')
Method name: find_one
This method is similar to the find
method, but it's adds to the SQL query LIMIT 1
Sorting methods
Method name: recent
This method returns recent field
db.recent(table: str, order_by_field: str)
For example:
recently_id_user = db.recent('table', 'id')
Method name: count
This method returns number of entries
db.count(table: str, sub_sql=None, *values)
For example:
how_many_users = db.count('table')
or like this:
user_exists = db.count('table', 'id = %s', 5)
Viewing tables in the console
Method name: view
For example:
two_users = db.view('table', (1, 2))
print(two_users)
Method name: view_all
Similar in meaning to the load_all
method
For example:
all_users = db.view_all('table')
print(all_users)
UPDATE
Method name: update
This method is similar in syntax to the puts
method, but unlike it, this method updates records
db.update(table: str, column_values: dict, sub_sql=None, *values)
For example:
db.update('table', {'name': 'Test'}, "`id` = %s", 10)
Then, in the cell with id = 10
, the name
field for the Test will be updated
or like this:
db.update('table', {'name': 'Test'})
DELETE
Method name: trash
This method deletes an entry
db.trash(table: str, field_ids: tuple)
For example:
db.trash('table', (1,4,5))
Then, in the cell with id = 1, 4, 5
will be deleted.
Method name: trash_sql
This method is similar to trash
method
db.trash_sql(table: str, sub_sql: str, *values)
For example:
db.trash_sql('table', "`id` < 5 OR `id` = %s", 6)
Then, in the cell with id < 5
or id = 6
will be deleted.
Method name: wipe
This method completely removes all records from the table
db.wipe(table: str)
For example:
db.wipe('table')
Then, all the cells will be deleted.
Method name: nuke
This method deletes the table
db.nuke(table: str)
For example:
db.nuke('table')
Then, the table will be deleted.
SQLite CRUD
Uses the same methods as in MySQL CRUD (executes the same query), but the sturcture of the fields some different
Structure of a table
JetORM SQite Types:
- int (INT)
- varchar (VARCHAR)
- text (TEXT)
- dt:ct (datetime current timestamp)
- id (INDEX)
- bool (BOOLEAN)
- tint (TINYINT)
- bint (BIGINT)
Unlike MySQL, the SQLite module does not have a BIT data type, so bool can be used instead.
CREATE, READ, UPDATE and DELETE methods SQLite module implements in the same way as the MySQL module.
p.s All methods and their names, arguments completely coincide with the mysql class.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.