Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions .github/workflows/demo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
name: demo projects

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
demo-test:
name: Demo project sqlx-ts (${{ matrix.db.name }})
if: "!contains(github.event.head_commit.message, 'Release')"
runs-on: ubuntu-latest
env:
BUILD_MODE: release
strategy:
fail-fast: false
matrix:
db:
- name: postgres-16
postgres: "16"
mysql: ""
- name: postgres-13
postgres: "13"
mysql: ""
- name: mysql-8
postgres: ""
mysql: "8"
- name: sqlite
postgres: ""
mysql: ""

steps:
- uses: actions/checkout@v4

- name: Install stable Rust toolchain
uses: dtolnay/rust-toolchain@stable

- uses: Swatinem/rust-cache@v2
with:
save-if: ${{ github.ref == 'refs/heads/main' }}
cache-provider: "github"

- name: Build sqlx-ts binary
run: cargo build --release

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- name: Start database services
if: matrix.db.postgres != '' || matrix.db.mysql != ''
run: docker compose -f docker-compose.yml up -d
env:
PG_VERSION: ${{ matrix.db.postgres || '16' }}
MYSQL_VERSION: ${{ matrix.db.mysql || '8' }}

- name: Wait for databases to be ready
if: matrix.db.postgres != '' || matrix.db.mysql != ''
run: |
echo "Waiting for databases to initialize..."
sleep 15
if [ -n "${{ matrix.db.postgres }}" ]; then
until docker compose exec -T postgres pg_isready -U postgres 2>/dev/null; do
echo "Waiting for PostgreSQL..."
sleep 2
done
echo "PostgreSQL is ready"
fi
if [ -n "${{ matrix.db.mysql }}" ]; then
until docker compose exec -T mysql mysqladmin ping -h localhost --silent 2>/dev/null; do
echo "Waiting for MySQL..."
sleep 2
done
echo "MySQL is ready"
fi

- name: Create SQLite database
if: matrix.db.name == 'sqlite'
working-directory: ./demo
run: |
sudo apt-get update && sudo apt-get install -y sqlite3
bash ./create-sqlite.sh

- name: Install demo dependencies
working-directory: ./demo
run: npm install

- name: Generate types (PostgreSQL demos)
if: matrix.db.postgres != ''
working-directory: ./demo
run: |
../target/release/sqlx-ts ./pg --config ./.sqlxrc.json -g
../target/release/sqlx-ts ./sequelize --config ./.sqlxrc.json -g

- name: Generate types (MySQL demo)
if: matrix.db.mysql != ''
working-directory: ./demo
run: |
../target/release/sqlx-ts ./mysql2 --config ./.sqlxrc.json -g

- name: Generate types (SQLite demo)
if: matrix.db.name == 'sqlite'
working-directory: ./demo
run: |
../target/release/sqlx-ts ./sqlite --config ./.sqlxrc.json -g

- name: Type-check generated code
working-directory: ./demo
run: npm run build
53 changes: 50 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ tokio-postgres = "0.7.16"
tokio = { version = "1.50.0", features = ["rt-multi-thread", "macros", "default"]}
async-recursion = "1.1.1"
bb8 = "0.9.1"
rusqlite = { version = "0.31", features = ["bundled"] }
log = "0.4.29"

[dev-dependencies]
Expand All @@ -39,3 +40,4 @@ predicates = "3.1.4"
tempfile = "3.27.0"
test_utils = { path="test-utils" }
pretty_assertions = "1.4.1"
rusqlite = { version = "0.31", features = ["bundled"] }
Comment thread
JasonShin marked this conversation as resolved.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
SQLx-ts is a CLI application featuring compile-time checked queries without a DSL and generates types against SQLs to keep your code type-safe

- **Compile time checked queries** - never ship a broken SQL query to production (and [sqlx-ts is not an ORM](https://github.com/JasonShin/sqlx-ts#sqlx-ts-is-not-an-orm))
- **TypeScript type generations** - generates type definitions based on the raw SQLs and you can use them with any MySQL or PostgreSQL driver
- **Database Agnostic** - support for [PostgreSQL](http://postgresql.org/) and [MySQL](https://www.mysql.com/) (and more DB supports to come)
- **TypeScript type generations** - generates type definitions based on the raw SQLs and you can use them with any MySQL, PostgreSQL, or SQLite driver
- **Database Agnostic** - support for [PostgreSQL](http://postgresql.org/), [MySQL](https://www.mysql.com/), and [SQLite](https://www.sqlite.org/)
- **TypeScript and JavaScript** - supports for both [TypeScript](https://jasonshin.github.io/sqlx-ts/reference-guide/4.typescript-types-generation.html) and [JavaScript](https://github.com/JasonShin/sqlx-ts#using-sqlx-ts-in-vanilla-javascript)

<br>
Expand All @@ -22,7 +22,7 @@ SQLx-ts is a CLI application featuring compile-time checked queries without a DS
</strong>
<strong> | </strong>
<strong>
🤓 <a href="https://github.com/JasonShin/sqlx-ts-demo">Demo</a>
🤓 <a href="https://github.com/JasonShin/sqlx-ts/tree/main/demo">Demo</a>
</strong>
</div>
<br>
Expand Down
8 changes: 8 additions & 0 deletions book/docs/connect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ $ sqlx-ts <path> --db-type postgres --db-url postgres://user:pass@localhost:5432
$ sqlx-ts <path> --db-type mysql --db-url mysql://user:pass@localhost:3306/mydb
```

#### SQLite

For SQLite, you only need to provide the database file path. No host, port, or user credentials are required:

```bash
$ sqlx-ts <path> --db-type sqlite --db-name ./mydb.sqlite
```

**Note:** When `--db-url` is provided, it takes precedence over individual connection parameters (`--db-host`, `--db-port`, `--db-user`, `--db-pass`, `--db-name`).

Run the following command for more details:
Expand Down
18 changes: 17 additions & 1 deletion book/docs/connect/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ Alternatively, you can use `DB_URL` to specify the connection string directly:
}
```

For SQLite, only `DB_TYPE` and `DB_NAME` (the file path) are required:

```json
{
"generate_types": {
"enabled": true
},
"connections": {
"default": {
"DB_TYPE": "sqlite",
"DB_NAME": "./mydb.sqlite"
}
}
}
```

## Configuration options

### connections (required)
Expand All @@ -92,7 +108,7 @@ const postgresSQL = sql`

Supported fields of each connection include
- `DB_URL`: Database connection URL (e.g. `postgres://user:pass@host:port/dbname` or `mysql://user:pass@host:port/dbname`). If provided, this overrides individual connection parameters (`DB_HOST`, `DB_PORT`, `DB_USER`, `DB_PASS`, `DB_NAME`)
- `DB_TYPE`: type of database connection (mysql | postgres)
- `DB_TYPE`: type of database connection (mysql | postgres | sqlite)
- `DB_USER`: database user name
- `DB_PASS`: database password
- `DB_HOST`: database host (e.g. 127.0.0.1)
Expand Down
13 changes: 12 additions & 1 deletion book/docs/connect/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
| DB_HOST | Primary DB host |
| DB_PASS | Primary DB password |
| DB_PORT | Primary DB port number |
| DB_TYPE | Type of primary database to connect [default: postgres] [possible values: postgres, mysql] |
| DB_TYPE | Type of primary database to connect [default: postgres] [possible values: postgres, mysql, sqlite] |
| DB_USER | Primary DB user name |
| DB_NAME | Primary DB name |
| PG_SEARCH_PATH | PostgreSQL schema search path (default is "$user,public") [https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH](https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH) |
Expand Down Expand Up @@ -47,3 +47,14 @@ sqlx-ts <path>

**Note:** When `DB_URL` is set, it takes precedence over individual connection parameters (`DB_HOST`, `DB_PORT`, `DB_USER`, `DB_PASS`, `DB_NAME`).

### SQLite

For SQLite, only `DB_TYPE` and `DB_NAME` (file path) are required:

```bash
export DB_TYPE=sqlite
export DB_NAME=./mydb.sqlite

sqlx-ts <path>
```

5 changes: 5 additions & 0 deletions demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.idea
dist
.DS_Store
*.db
27 changes: 27 additions & 0 deletions demo/.sqlxrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"generate_types": {
"enabled": true,
"convertToCamelCaseColumnName": true
},
"connections": {
"default": {
"DB_TYPE": "postgres",
"DB_HOST": "127.0.0.1",
"DB_PORT": 54321,
"DB_USER": "postgres",
"DB_PASS": "postgres",
"DB_NAME": "postgres"
},
"db_mysql": {
"DB_TYPE": "mysql",
"DB_HOST": "127.0.0.1",
"DB_PORT": 33306,
"DB_USER": "root",
"DB_NAME": "sqlx-ts"
},
"db_sqlite": {
"DB_TYPE": "sqlite",
"DB_NAME": "./sqlite/demo.db"
}
}
}
25 changes: 25 additions & 0 deletions demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# sqlx-ts demo

Usage examples showing sqlx-ts with different database drivers.

## Examples

- [pg](./pg) - PostgreSQL with `pg` driver
- [mysql2](./mysql2) - MySQL with `mysql2` driver
- [sequelize](./sequelize) - PostgreSQL with Sequelize ORM
- [sqlite](./sqlite) - SQLite with `better-sqlite3` (no Docker needed)

## Running locally

```bash
# Start databases (from repo root)
docker compose up -d

# Install demo dependencies
cd demo
npm install

# Generate types and type-check
npm run compile:all
npm run typecheck
```
Loading
Loading