https://github.com/zero-one-group/sqlx-news-example
https://github.com/zero-one-group/sqlx-news-example
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/zero-one-group/sqlx-news-example
- Owner: zero-one-group
- License: unlicense
- Created: 2023-08-27T03:31:28.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-28T04:52:06.000Z (almost 3 years ago)
- Last Synced: 2025-01-14T10:55:29.408Z (over 1 year ago)
- Language: Rust
- Size: 36.1 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqlx-news-example
This is an example command-line app to combine the use of [Postgres](https://www.postgresql.org/), [sqlx](https://github.com/launchbadge/sqlx) and [NewsAPI](https://newsapi.org/). The main idea is to query news articles from NewsAPI, and dump it to a Postgres database with full type safety.
## Basic Usage
Populate `.env` based on `.env.example`. You can get the NewsAPI API token by signing up to NewsAPI for free.
```bash
# Spin up a Postgres instance in the background:
docker-compose down -v && docker-compose up -d
# Health check
cargo run -- --app health-check
# Fetch and dump articles
cargo run -- --app fetch-and-dump-articles --query "dogecoin elon musk"
# Print 10 random articles
cargo run -- --app print-articles --limit 10
```
## Development Notes
```bash
# Install `sqlx-cli`:
cargo install sqlx-cli
# Create database:
source .env
sqlx database create
# Create and run migrations
sqlx migrate add -r add_article_table
# Run/revert migrations
sqlx migrate run
sqlx migrate revert
```
Others:
```bash
# PSQL session:
psql -d $DATABASE_URL -P expanded=auto
# Enable offline mode:
cargo sqlx prepare
# Clean up:
sqlx database drop
```