Skip to content

Commit 61b44ef

Browse files
committed
add indexes info to the docs
Signed-off-by: Grant Ramsay <seapagan@gmail.com>
1 parent baf17ac commit 61b44ef

File tree

7 files changed

+82
-32
lines changed

7 files changed

+82
-32
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Website](https://sqliter.grantramsay.dev)
4747
## Features
4848

4949
- Table creation based on Pydantic models
50+
- Automatic primary key generation
51+
- User defined indexes on any field
5052
- CRUD operations (Create, Read, Update, Delete)
5153
- Chained Query building with filtering, ordering, and pagination
5254
- Transaction support
@@ -70,16 +72,16 @@ virtual environments (`uv` is used for developing this project and in the CI):
7072
uv add sqliter-py
7173
```
7274

73-
With `pip`:
75+
With `Poetry`:
7476

7577
```bash
76-
pip install sqliter-py
78+
poetry add sqliter-py
7779
```
7880

79-
Or with `Poetry`:
81+
Or with `pip`:
8082

8183
```bash
82-
poetry add sqliter-py
84+
pip install sqliter-py
8385
```
8486

8587
### Optional Dependencies
@@ -88,9 +90,9 @@ Currently by default, the only external dependency is Pydantic. However, there
8890
are some optional dependencies that can be installed to enable additional
8991
features:
9092

91-
- `inflect`: For pluralizing table names (if not specified). This just offers a
92-
more-advanced pluralization than the default method used. In most cases you
93-
will not need this.
93+
- `inflect`: For pluralizing the auto-generated table names (if not explicitly
94+
set in the Model) This just offers a more-advanced pluralization than the
95+
default method used. In most cases you will not need this.
9496

9597
See [Installing Optional
9698
Dependencies](https://sqliter.grantramsay.dev/installation#optional-dependencies)

TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
addition to the `delete` method in the main class which deletes a single
1313
record based on the primary key.
1414
- add a `rollback` method to the main class to allow manual rollbacks.
15-
- allow adding multiple indexes to each table as well as the primary key.
1615
- allow adding foreign keys and relationships to each table.
1716
- add a migration system to allow updating the database schema without losing
1817
data.

docs/guide/exceptions.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,55 @@ class, `SqliterError`, to ensure consistency across error messages and behavior.
1010
- **Message**: "An error occurred in the SQLiter package."
1111

1212
- **`DatabaseConnectionError`**:
13-
- Raised when the SQLite database connection fails.
13+
- **Raised** when the SQLite database connection fails.
1414
- **Message**: "Failed to connect to the database: '{}'."
1515

1616
- **`InvalidOffsetError`**:
17-
- Raised when an invalid offset value (0 or negative) is used in queries.
17+
- **Raised** when an invalid offset value (0 or negative) is used in queries.
1818
- **Message**: "Invalid offset value: '{}'. Offset must be a positive
1919
integer."
2020

2121
- **`InvalidOrderError`**:
22-
- Raised when an invalid order value is used in queries, such as a
22+
- **Raised** when an invalid order value is used in queries, such as a
2323
non-existent field or an incorrect sorting direction.
2424
- **Message**: "Invalid order value - '{}'"
2525

2626
- **`TableCreationError`**:
27-
- Raised when a table cannot be created in the database.
27+
- **Raised** when a table cannot be created in the database.
2828
- **Message**: "Failed to create the table: '{}'."
2929

3030
- **`RecordInsertionError`**:
31-
- Raised when an error occurs during record insertion.
31+
- **Raised** when an error occurs during record insertion.
3232
- **Message**: "Failed to insert record into table: '{}'."
3333

3434
- **`RecordUpdateError`**:
35-
- Raised when an error occurs during record update.
35+
- **Raised** when an error occurs during record update.
3636
- **Message**: "Failed to update record in table: '{}'."
3737

3838
- **`RecordNotFoundError`**:
39-
- Raised when a record with the specified primary key is not found.
39+
- **Raised** when a record with the specified primary key is not found.
4040
- **Message**: "Failed to find a record for key '{}'".
4141

4242
- **`RecordFetchError`**:
43-
- Raised when an error occurs while fetching records from the database.
43+
- **Raised** when an error occurs while fetching records from the database.
4444
- **Message**: "Failed to fetch record from table: '{}'."
4545

4646
- **`RecordDeletionError`**:
47-
- Raised when an error occurs during record deletion.
47+
- **Raised** when an error occurs during record deletion.
4848
- **Message**: "Failed to delete record from table: '{}'."
4949

5050
- **`InvalidFilterError`**:
51-
- Raised when an invalid filter field is used in a query.
51+
- **Raised** when an invalid filter field is used in a query.
5252
- **Message**: "Failed to apply filter: invalid field '{}'".
5353

5454
- **`TableDeletionError`**:
55-
- Raised when a table cannot be deleted from the database.
55+
- **Raised** when a table cannot be deleted from the database.
5656
- **Message**: "Failed to delete the table: '{}'."
5757

5858
- **SqlExecutionError**
59-
- Raised when an error occurs during SQL query execution.
59+
- **Raised** when an error occurs during SQL query execution.
6060
- **Message**: "Failed to execute SQL: '{}'."
61+
62+
- **InvalidIndexError**
63+
- **Raised** when an invalid index is specified for a model.
64+
- **Message**: "Invalid fields for indexing in model '{}': {}"

docs/guide/models.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,53 @@ the table.
2929
> - The Model **automatically** creates an **auto-incrementing integer primary
3030
> key** for each table called `pk`, you do not need to define it yourself.
3131
32+
### Adding Indexes
33+
34+
You can add indexes to your table by specifying the `indexes` attribute in the
35+
`Meta` class. This should be a list of strings, each string being the name of an
36+
existing field in the model that should be indexed.
37+
38+
```python
39+
from sqliter.model import BaseDBModel
40+
41+
class User(BaseDBModel):
42+
name: str
43+
age: int
44+
email: str
45+
46+
class Meta:
47+
indexes = ["name", "email"]
48+
```
49+
50+
This is in addition to the primary key index (`pk`) that is automatically
51+
created.
52+
53+
### Adding Unique Indexes
54+
55+
You can add unique indexes to your table by specifying the `unique_indexes`
56+
attribute in the `Meta` class. This should be a list of strings, each string
57+
being the name of an existing field in the model that should be indexed.
58+
59+
```python
60+
from sqliter.model import BaseDBModel
61+
62+
class User(BaseDBModel):
63+
name: str
64+
age: int
65+
email: str
66+
67+
class Meta:
68+
unique_indexes = ["email"]
69+
```
70+
71+
These will ensure that all values in this field are unique. This is in addition
72+
to the primary key index (`pk`) that is automatically created.
73+
74+
> [!TIP]
75+
>
76+
> You can specify both `indexes` and `unique_indexes` in the `Meta` class if you
77+
> need to.
78+
3279
### Custom Table Name
3380

3481
By default, the table name will be the same as the model name, converted to

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ database-like format without needing to learn SQL or use a full ORM.
3838
## Features
3939

4040
- Table creation based on Pydantic models
41+
- Automatic primary key generation
42+
- User defined indexes on any field
4143
- CRUD operations (Create, Read, Update, Delete)
4244
- Chained Query building with filtering, ordering, and pagination
4345
- Transaction support

docs/installation.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ virtual environments (`uv` is used for developing this project and in the CI):
1010
uv add sqliter-py
1111
```
1212

13-
With `pip`:
13+
With `Poetry`:
1414

1515
```bash
16-
pip install sqliter-py
16+
poetry add sqliter-py
1717
```
1818

19-
Or with `Poetry`:
19+
Or with `pip`:
2020

2121
```bash
22-
poetry add sqliter-py
22+
pip install sqliter-py
2323
```
2424

2525
## Optional Dependencies
@@ -38,14 +38,14 @@ These can be installed using `uv`:
3838
uv add 'sqliter-py[extras]'
3939
```
4040

41-
Or with `pip`:
41+
With `Poetry`:
4242

4343
```bash
44-
pip install 'sqliter-py[extras]'
44+
poetry add 'sqliter-py[extras]'
4545
```
4646

47-
Or with `Poetry`:
47+
Or with `pip`:
4848

4949
```bash
50-
poetry add 'sqliter-py[extras]'
50+
pip install 'sqliter-py[extras]'
5151
```

docs/quickstart.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ class User(BaseDBModel):
1414
age: int
1515
admin: Optional[bool] = False
1616

17-
class Meta:
18-
create_pk = False
19-
primary_key = "name"
20-
2117
# Create a database connection
2218
db = SqliterDB("example.db")
2319

0 commit comments

Comments
 (0)