Skip to content

Commit

Permalink
add examples (#13)
Browse files Browse the repository at this point in the history
* add examples

* dont test example code

* dont fmt examples
  • Loading branch information
carderne authored Aug 20, 2024
1 parent 5d1e605 commit 26d4988
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ jobs:
exit 1
fi
- run: rye fmt --check
working-directory: py
- run: rye lint
working-directory: py
- run: rye run check
working-directory: py
- run: rye run test
working-directory: py

test-rust:
runs-on: ubuntu-latest
Expand All @@ -62,6 +66,7 @@ jobs:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo fmt --check
working-directory: upid_rs
- run: cargo clippy
working-directory: upid_rs
- run: cargo test --all-features
Expand Down
23 changes: 12 additions & 11 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"upid_pg",
"upid_rs",
"examples/rust",
]

[profile.dev]
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ with psycopg.connect("postgresql://...") as conn:
print(res) # user_2accvpp5guht4dts56je5a
```

## Examples
You can try out the Python and Rust examples in this repository.
They both involve spinning up a Postgres DB and inserting a UPID as itself, as a UUID and as text.
- [./examples/python](./examples/python)
- [./examples/rust](./examples/rust)

There are also TypeScript examples for browser and Node (with Postgres) in the [upid-ts](https://github.com/carderne/upid-ts) repo.

## Demo
You can give it a spin at [upid.rdrn.me](https://upid.rdrn.me/).

Expand Down
2 changes: 2 additions & 0 deletions examples/python/.mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[env]
_.python.venv='.venv'
31 changes: 31 additions & 0 deletions examples/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# example-python

Clone this repo:
```bash
git clone [email protected]:carderne/upid.git
cd upid/examples/python
```

Get a Docker image running:
```bash
docker run --rm -e POSTGRES_PASSWORD=mypassword \
-p 5432:5432 --detach carderne/postgres-upid:16
```

Install requirements:
```bash
pip install -r requirements.txt
```


Run the script:
```bash
python example.py

# Extension ready
# Table created
# Inserted:
# id_upid=user_2acqxw7dpigf2y345ug7ia
# id_uuid=01916ef0-a9ab-98b0-7822-1e985ed61576
# id_text=user_2acqxw7dpigf2y345ug7ia
```
47 changes: 47 additions & 0 deletions examples/python/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import psycopg
from psycopg.conninfo import make_conninfo

from upid import upid

conninfo = make_conninfo(
host="localhost",
port=5432,
user="postgres",
password="mypassword",
dbname="postgres",
)

id_obj = upid("user")

with psycopg.connect(conninfo) as conn:
create_ext = "CREATE EXTENSION IF NOT EXISTS upid_pg;"
conn.execute(create_ext)
print("Extension ready")

drop_table = "DROP TABLE IF EXISTS test_upid;"
conn.execute(drop_table)

create_table = """
CREATE TABLE test_upid (
id_upid upid NOT NULL, -- pass a string
id_uuid uuid NOT NULL, -- pass binary
id_text text NOT NULL -- pass a string
);
"""
conn.execute(create_table)
print("Table created")

query = """
INSERT INTO test_upid (id_upid, id_uuid, id_text)
VALUES (%s, %s, %s)
RETURNING *;
"""
values = (
id_obj.to_str(), # string for upid type
id_obj.to_uuid(), # uuid for uuid type
id_obj.to_str(), # string for text type
)

row = conn.execute(query, values).fetchone()
if row:
print(f"Inserted:\nid_upid={row[0]}\nid_uuid={row[1]}\nid_text={row[2]}")
20 changes: 20 additions & 0 deletions examples/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[tool.ruff]
target-version = "py39"
line-length = 120

[tool.ruff.lint]
ignore-init-module-imports = true
select = ["A", "E", "F", "I", "N", "Q", "U", "T100"]

[tool.ruff.lint.isort]
known-first-party = ["upid"]

[tool.pyright]
venvPath = "."
venv = ".venv"
include = ["py"]
strict = ["py/**"]
reportUnnecessaryTypeIgnoreComment = true
reportUnusedCallResult = false
pythonVersion = "3.9"
pythonPlatform = "Linux"
2 changes: 2 additions & 0 deletions examples/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
psycopg>=3
upid
9 changes: 9 additions & 0 deletions examples/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "upid-example-rust"
version = "0.1.0"
edition = "2021"

[dependencies]
postgres = { version = "0.19", features = ["with-uuid-1"] }
upid = { version = "0.2", features = ["uuid"] }
uuid = "1.10.0"
25 changes: 25 additions & 0 deletions examples/rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# example-python

Clone this repo:
```bash
git clone [email protected]:carderne/upid.git
cd upid/examples/rust
```

Get a Docker image running:
```bash
docker run --rm -e POSTGRES_PASSWORD=mypassword \
-p 5432:5432 --detach carderne/postgres-upid:16
```

Run the script:
```bash
cargo run

# Extension ready
# Table created
# Inserted:
# id_upid=user_2acqyewitnjij6oflrqzda
# id_uuid=01916f2b-8ecc-dee7-928b-8dedf9d61576
# id_text=user_2acqyewitnjij6oflrqzda
```
49 changes: 49 additions & 0 deletions examples/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use postgres::{Client, NoTls};
use std::error::Error;
use upid::Upid;
use uuid::Uuid;

fn main() -> Result<(), Box<dyn Error>> {
let mut client = Client::connect(
"host=localhost user=postgres password=mypassword dbname=postgres",
NoTls,
)?;

let create_ext = "CREATE EXTENSION IF NOT EXISTS upid_pg;";
client.execute(create_ext, &[])?;
println!("Extension ready");

let drop_table = "DROP TABLE IF EXISTS test_upid;";
client.execute(drop_table, &[])?;

let create_table = r#"
CREATE TABLE test_upid (
id_upid TEXT NOT NULL, -- passing string for upid type
id_uuid UUID NOT NULL, -- passing uuid for uuid type
id_text TEXT NOT NULL -- passing string for text type
);
"#;
client.execute(create_table, &[])?;
println!("Table created");

let id = Upid::new("user");

let query = r#"
INSERT INTO test_upid (id_upid, id_uuid, id_text)
VALUES ($1, $2, $3)
RETURNING id_upid, id_uuid, id_text;
"#;
for row in client.query(
query,
&[&id.to_string(), &Uuid::from(id), &id.to_string()],
)? {
let id_upid: String = row.get(0);
let id_uuid: Uuid = row.get(1);
let id_text: String = row.get(2);
println!(
"Inserted:\nid_upid={id_upid}\nid_uuid={id_uuid}\nid_text={id_text}",
);
}

Ok(())
}
4 changes: 2 additions & 2 deletions py/upid/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__all__ = ["UPID", "upid"]

from upid.core import UPID, upid

__all__ = ["UPID", "upid"]
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ dev-dependencies = [
]

[tool.ruff]
include = ["py/**/*.py"]
exclude = ["examples"]
target-version = "py39"
line-length = 120

[tool.ruff.lint]
ignore-init-module-imports = true
select = ["A", "E", "F", "I", "N", "Q", "U", "T100"]

[tool.ruff.lint.isort]
Expand Down

0 comments on commit 26d4988

Please sign in to comment.