https://github.com/carlossilva2/knexpy
An abstraction for SQLite3 based on Knexjs
https://github.com/carlossilva2/knexpy
abstraction human-readable knex module python3 sql sqlite3
Last synced: 7 months ago
JSON representation
An abstraction for SQLite3 based on Knexjs
- Host: GitHub
- URL: https://github.com/carlossilva2/knexpy
- Owner: carlossilva2
- License: gpl-3.0
- Created: 2022-07-23T18:46:46.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-20T23:24:12.000Z (about 3 years ago)
- Last Synced: 2025-02-27T11:20:38.761Z (7 months ago)
- Topics: abstraction, human-readable, knex, module, python3, sql, sqlite3
- Language: Python
- Homepage: https://knexpy.readthedocs.io
- Size: 643 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Knexpy
![]()
[](https://pepy.tech/project/knexpy)
[](https://pypi.org/project/knexpy)
[](https://knexpy.readthedocs.io/en/latest/?badge=latest)
[](https://github.com/psf/black)
[](https://github.com/pre-commit/pre-commit)A query builder for SQLite3 based on Knexjs
## Features
* Transactions
* Type Checking
* Bulk Insert
* JSON mappingYou can report bugs and discuss feature on the [GitHub issues page](https://github.com/carlossilva2/Knexpy/issues).
For more detailed information check [Readthedocs](https://knexpy.readthedocs.io/)## Examples
### Creating a Table
```python
from Knexpy import Knex, Fielddb = Knex("")
db.table(
"c",
[
Field.integer("field"),
],
not_exists=False,
)db.table(
"t",
[
Field.varchar("field"), # Default size: 255
Field.varchar("field2"),
Field.varchar("field3"),
Field.foreign_key("field4", "c", "id"),
],
not_exists=False, # IF NOT EXISTS clause. Defaults to True
)
```> When creating a table the fields `id`, `created_at`, `modified_at` are automatically generated.
> The `id` field is a hash based on the information of the Row### Basic Select
```python
from Knexpy import Knexdb = Knex("")
query = (
db.select("id", "field", "field2", ["field3", "test"],...) # List type on select acts as an alias
.from_("t")
.where("field", "=", "12345")
.order_by("id")
)query.query() # Returns data as JSON
query.query(False) # Returns data as tuples
```### Select with Subquery
```python
from Knexpy import Knexdb = Knex("")
query = (
db.select("id", "field", "field2", ["field3", "test"],...)
.from_("t")
.where("field", "=", "12345")
.where(
"field4",
"=",
db.subquery().select("id").from_("c").where("field", "=", 12345),
join_type="OR", # If attribute not present defaults to "AND".
)
.order_by("id")
)query.query() # Returns data as JSON
```### Insert JSON Data
```python
from Knexpy import Knexdb = Knex("", type_check=True) # type_check enables type checking (duh) when inserting/updating data
db.insert_json("", {
"field": "1",
"field2": "2",
"field3": "3"
})
```