Skip to content

Commit

Permalink
SQLx issue and Project Documentation (#26)
Browse files Browse the repository at this point in the history
This pull request includes significant updates to the `page-hunter`
library, focusing on adding support for SQLite, updating dependencies,
and improving documentation. The most important changes are outlined
below:

### Dependency Updates:

* Updated the `sqlx` version to `>=0.8.1` to address security issues.
(`Cargo.toml`)

### Documentation Improvements:

* Updated the `README.md` to include instructions and examples for using
the new SQLite pagination feature, as well as general improvements to
the documentation structure and clarity. (`README.md`)
[[1]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L1-R22)
[[2]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L21-R49)
[[3]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L183-R197)
[[4]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R262-R319)
[[5]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L275-R337)
[[6]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L303-L325)
* Enhanced the `CHANGELOG.md` to document the new features, fixes, and
breaking changes introduced in version 0.3.0.
(`page-hunter/CHANGELOG.md`)
* Include page-hunter logo in README. ( `README.md` )

### Codebase Maintenance:

* Added unused import suppression for `PaginationError` in
`page-hunter/src/book.rs` to clean up warnings.
(`page-hunter/src/book.rs`)
  • Loading branch information
JMTamayo authored Oct 13, 2024
2 parents 6e7473e + 3bfc95c commit 4c5a9b0
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 94 deletions.
119 changes: 85 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
# PAGE HUNTER

![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)
[![dependency status](https://deps.rs/repo/github/JMTamayo/page-hunter/status.svg)](https://deps.rs/repo/github/JMTamayo/page-hunter)
[![CI](https://github.com/JMTamayo/page-hunter/actions/workflows/ci.yml/badge.svg)](https://github.com/JMTamayo/page-hunter/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/JMTamayo/page-hunter/graph/badge.svg?token=R1LAPNSV5J)](https://codecov.io/gh/JMTamayo/page-hunter)
[![crates.io](https://img.shields.io/crates/v/page-hunter.svg?label=crates.io&color=orange&logo=rust)](https://crates.io/crates/page-hunter)
[![docs.rs](https://img.shields.io/static/v1?label=docs.rs&message=latest&color=blue&logo=docsdotrs)](http://docs.rs/page-hunter/latest/)

<div align="center"><img src="docs/page-hunter-logo-v2.png" width="350"/></div>

<div align="center">
<img src="https://img.shields.io/github/license/JMTamayo/page-hunter">
<a href="https://deps.rs/repo/github/JMTamayo/page-hunter">
<img src="https://deps.rs/repo/github/JMTamayo/page-hunter/status.svg">
</a>
<a href="https://github.com/JMTamayo/page-hunter/actions/workflows/ci.yml">
<img src="https://github.com/JMTamayo/page-hunter/actions/workflows/ci.yml/badge.svg">
</a>
<a href="https://codecov.io/gh/JMTamayo/page-hunter">
<img src="https://codecov.io/gh/JMTamayo/page-hunter/graph/badge.svg?token=R1LAPNSV5J">
</a>
<a href="https://crates.io/crates/page-hunter">
<img src="https://img.shields.io/crates/v/page-hunter.svg?label=crates.io&color=orange&logo=rust">
</a>
<a href="http://docs.rs/page-hunter/latest/">
<img src="https://img.shields.io/static/v1?label=docs.rs&message=latest&color=blue&logo=docsdotrs">
</a>
</div>

---

***Page Hunter*** library is a Rust-based pagination tool that provides a way to manage and navigate through pages of data.
It offers a set of resources that encapsulates all the necessary pagination information such as the current page, total pages, previous page, next page and the items on the current page.
Expand All @@ -18,21 +31,22 @@ To use **page-hunter** from GitHub repository with specific version, set the dep

```ini
[dependencies]
page-hunter = { git = "https://github.com/JMTamayo/page-hunter.git", version = "0.2.0", features = ["serde"] }
page-hunter = { git = "https://github.com/JMTamayo/page-hunter.git", version = "0.3.0", features = ["serde"] }
```

You can depend on it via cargo by adding the following dependency to your `Cargo.toml` file:

```ini
[dependencies]
page-hunter = { version = "0.2.0", features = ["utoipa", "pg-sqlx"] }
page-hunter = { version = "0.3.0", features = ["utoipa", "pg-sqlx"] }
```

## CRATE FEATURES
- `serde`: Add [Serialize](https://docs.rs/serde/1.0.203/serde/trait.Serialize.html) and [Deserialize](https://docs.rs/serde/1.0.203/serde/trait.Deserialize.html) support for `Page` and `Book` based on [serde](https://crates.io/crates/serde/1.0.203). This feature is useful for implementing pagination models as a request or response body in REST APIs, among other implementations.
- `utoipa`: Add [ToSchema](https://docs.rs/utoipa/4.2.3/utoipa/trait.ToSchema.html) support for `Page` and `Book` based on [utoipa](https://crates.io/crates/utoipa/4.2.3). This feature is useful for generating OpenAPI schemas for pagination models. This feature depends on the `serde` feature and therefore you only need to implement `utoipa` to get both.
- `pg-sqlx`: Add support for pagination with [SQLx](https://docs.rs/sqlx/0.7.4/sqlx/) for PostgreSQL database.
- `mysql-sqlx`: Add support for pagination with [SQLx](https://docs.rs/sqlx/0.7.4/sqlx/) for MySQL database.
- `serde`: Add [Serialize](https://docs.rs/serde/1.0.200/serde/trait.Serialize.html) and [Deserialize](https://docs.rs/serde/1.0.200/serde/trait.Deserialize.html) support for `Page` and `Book` based on [serde](https://crates.io/crates/serde/1.0.200). This feature is useful for implementing pagination models as a request or response body in REST APIs, among other implementations.
- `utoipa`: Add [ToSchema](https://docs.rs/utoipa/4.2.0/utoipa/trait.ToSchema.html) support for `Page` and `Book` based on [utoipa](https://crates.io/crates/utoipa/4.2.0). This feature is useful for generating OpenAPI schemas for pagination models. This feature depends on the `serde` feature and therefore you only need to implement `utoipa` to get both.
- `pg-sqlx`: Add support for pagination with [SQLx](https://docs.rs/sqlx/0.8.1/sqlx/) for PostgreSQL database.
- `mysql-sqlx`: Add support for pagination with [SQLx](https://docs.rs/sqlx/0.8.1/sqlx/) for MySQL database.
- `sqlite-sqlx`: Add support for pagination with [SQLx](https://docs.rs/sqlx/0.8.1/sqlx/) for SQLIte database.

## BASIC OPERATION
The **page-hunter** library provides two main models to manage pagination:
Expand Down Expand Up @@ -180,7 +194,7 @@ On feature `serde` enabled, you can serialize and deserialize a `Book` as follow

Take a look at the [examples](https://github.com/JMTamayo/page-hunter/tree/main/examples) folder where you can find practical uses in REST API implementations with some web frameworks.

#### Paginate records from a PostgreSQL database with SQLx:
#### Paginate records from a relational database with SQLx:
To paginate records from a PostgreSQL database:
```rust,no_run
use page_hunter::*;
Expand Down Expand Up @@ -245,22 +259,64 @@ To paginate records from a MySQL database:
}
```

To paginate records from a SQLite database:
```rust,no_run
use page_hunter::*;
use sqlx::sqlite::{SqlitePool, Sqlite};
use sqlx::{FromRow, QueryBuilder};
use uuid::Uuid;
#[tokio::main]
async fn main() {
#[derive(Clone, Debug, FromRow)]
pub struct Country {
id: Uuid,
name: String,
}
let pool: SqlitePool = SqlitePool::connect(
"sqlite://countries.db"
).await.unwrap_or_else(|error| {
panic!("Error connecting to database: {:?}", error);
});
let query: QueryBuilder<Sqlite> = QueryBuilder::new(
"SELECT * FROM countries"
);
let page: Page<Country> =
query.paginate(&pool, 0, 10).await.unwrap_or_else(|error| {
panic!("Error paginating records: {:?}", error);
});
}
```

## DEVELOPMENT
To test `page-hunter`, follow these recommendations:

#### Set env variables:
Create `local.env` file at workspace folder to store the required environment variables
Create `local.env` file at workspace folder to store the required environment variables. For example,
```text
DB_HOST=localhost
DB_USER=test
DB_PASSWORD=docker
DB_NAME=test
PG_DB_PORT=5432
MYSQL_DB_PORT=3306
SQLITE_DB_PATH=$PWD/page-hunter/tests/migrations/sqlite/test.db
SQLITE_MIGRATIONS_PATH=page-hunter/tests/migrations/sqlite
MYSQL_MIGRATIONS_PATH=page-hunter/tests/migrations/mysql
PG_MIGRATIONS_PATH=page-hunter/tests/migrations/postgres
```

#### Install required tools:
```bash
make install-tools
```
This command installs sqlx and cargo-llvm-cov.

#### Setup databases:
Run databases as Docker containers using the following commands:
Run PostgreSQL-MySQL databases as Docker containers and create SQLite database file using the following commands:

##### Postgres SQL:
```bash
Expand All @@ -272,12 +328,13 @@ Run databases as Docker containers using the following commands:
make mysql-db-docker
```

#### Run database migrations:
- Install sqlx-cli:
##### SQLite:
```bash
make install-sqlx-cli
make sqlite-db-local
```

#### Run database migrations:

##### Postgres SQL
- Run migrations:
```bash
Expand All @@ -300,29 +357,23 @@ Run databases as Docker containers using the following commands:
make revert-mysql-migrations
```

#### To test:
##### SQLite
- Run migrations:
```bash
make test
make run-sqlite-migrations
```

#### To test using tarpaulin:
- Install cargo-tarpaulin:
- Revert migrations:
```bash
make install-tarpaulin
make revert-sqlite-migrations
```

- Run tests:
#### To test:
```bash
make test-tarpaulin
make test
```

#### To test using llvm-cov:
- Install llvm-cov:
```bash
make install-llvm-cov
```

- Run tests:
```bash
make test-llvm-cov
```
Expand Down
Binary file added docs/page-hunter-logo-v1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/page-hunter-logo-v2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4c5a9b0

Please sign in to comment.