A simple ORM for Sqlite3
Project description
How to start using dstore
Ready for use , with all BASIC (Create, Read, Update, Delete) operations are supported.
Developed by Mutiibwa Grace Peter 🔗, Tukasiima Blessing (c) 2024
Report issues at 🔍 dstore-orm repo
dstore-orm is a Sqlite ORM that offers means of saving objects (dictionaries) to the database, making it applicable for less data intensive applications while hiding the complexities of database interactions.
- This
packageaims to make it so easy to get going with saving object structured data for small scale appliactions.
Getting started
Install the package using;
pip install dstore-orm
Usage Guide
- Creating a
definitionsfile
The definitions file should have an extension of .dst
# Inside the the .dst file you can add definitions like;
Student:[name=str, age=int]
No spacesshould exist between the name of the Table and the colon and the list of field names i.e.
Student : [name=str] is Invalid
^ ^
| |
Such spaces are not valid
- In the list of fields like
[name=str, age=int]no spaces should exist between thefield name and the type.
Student: [name = str, age=str]
^ ^
| |
Such spaces wont
be recognized
- Above ,
Studentis the name of the table and the list[name=str]is a definition of the fields theStudenttable will have
# With many tables and many fields
Student:[name=str, age=int]
Color:[name=str, code=str]
Suppose the details.dst is located in a folder called learn
Note: If the
dstfile is put in a folder, that folder should exist.
- Creating a connection to the
databasein our python file or code via thedefinitionsfile.
from dstore import SqliteORM
# path to definitions
definition_path = './learn/details.dst'
# connect to the definitions file
datase_connection = SqliteORM().connect(definition_path)
Upon successfull connection;
-
The
definitions filewill becreated if its missing. -
An
sqlite database file with the same name as the definitions filewill be created.
Get meta information about the database created using the meta and definitions attribute
# display a list of tables as defined in the database
print(datase_connection.meta)
# display the structure of the databse tables
print(datase_connection.definitions)
Note: Before any operations can be done, ensure that there are definitions in your definitions file otherwise you will get an error.
CRUD OPERATIONS
Note: Before any (Create, Read, Update, Delete) operation, a connection to the database must be created.
from dstore import SqliteORM
# definitions
definition_path = 'learn/details.dst'
# create connection
databaseConnection = SqliteORM().connect(definition_path)
Savingobjects to the database
Before any saving operation, an object (
a dictionary) with fields defined as in thedefinitionsfile is prepared.
-
Keys (.i.e. name, age) of the object (dictionary) should match those defined in the definitions file.
-
Data types for the values should match to i.e.
'John Doe'is a string (str) and12is an integer (int)
# an object
sampleObject = {
'name': 'John Doe',
'age': 12
}
# save
databaseConnection.save('Student', sampleObject)
Readingdata from the database
# fetch all records in `Student` table
data = databaseConnection.fetch("Student")
# Get certain fields from the table
# i.e. get only the name field
data = databaseConnection.fetch("Student", ['name'])
print(data)
Updatingrecords in the database
databaseConnection.update("Student", ['age'], [17], ('name', 'John Doe'))
Explanation
|Section|Description|
|---|---|
|Student|Name of the table to update|
|['age']| A list of fields to replace, if they are many the format is ['age', 'name']|
|[17]| The replace list containing values for the replace fields, note that the order matters, in case they are many the format is [17, 'Red']|
|('name', 'John Doe')| 'name' - For all fields to replace, the name field value i.e. 'John Doe' will be looked for, and wherever its found, fields in that record will be updated with values in the replace list. |
Deletingrecords from the database
Deleting specific record
# data record based on conditions
databaseConnection.delete("Student", [('id', 67)])
-
'Student'- Is the table you want to delete from. -
[
('id', 67)] - A list containing tuple indicating the fields to look for i.e.id, and in case they have the specified values i.e.67in theidfield, then that field will be deleted.
Deleting (
purging) all records in a given table.
# delete : all data in table but not the table.
databaseConnection.purge("Student")
- Deleting the whole table from the database
# delete table 'Student' from the database
databaseConnection.drop("Student")
Note;
-
A
wrong table name will raise an error(ensure the table actuall exists before deleting). -
Deleting the table
does not remove it from the definitions file. -
Attempting to
save to the table after the table is deleted will re-createthe table.
Project details
Release history Release notifications | RSS feed
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 dstore_orm-0.0.1.tar.gz.
File metadata
- Download URL: dstore_orm-0.0.1.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14dbea702fd5670c7ef0aecc5d593768ac83e6b537c91f703435bdcdf285afc0
|
|
| MD5 |
1b20400de9ada2de40c8c9ec6514ed1f
|
|
| BLAKE2b-256 |
0d18f02152eb30753b4a1e329630b004a0cfd460840c90766b5137462e0f8b71
|
File details
Details for the file dstore_orm-0.0.1-py3-none-any.whl.
File metadata
- Download URL: dstore_orm-0.0.1-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89c1f8fcbaaafacfb7800be830353add455b88edf4fdb4b03fb42341bfc0bea4
|
|
| MD5 |
1cd04b3f1e8d4797b42698f07520e780
|
|
| BLAKE2b-256 |
260ff63e07ccc500199528515dcffbcee4d2950e5aa16517a6cb18b57ead01e7
|