From 26d4988122387269d7c56da6624cb85ba80f275f Mon Sep 17 00:00:00 2001 From: Chris Arderne Date: Tue, 20 Aug 2024 10:51:49 +0100 Subject: [PATCH] add examples (#13) * add examples * dont test example code * dont fmt examples --- .github/workflows/pr.yml | 5 ++++ Cargo.lock | 23 ++++++++------- Cargo.toml | 1 + README.md | 8 ++++++ examples/python/.mise.toml | 2 ++ examples/python/README.md | 31 ++++++++++++++++++++ examples/python/example.py | 47 ++++++++++++++++++++++++++++++ examples/python/pyproject.toml | 20 +++++++++++++ examples/python/requirements.txt | 2 ++ examples/rust/Cargo.toml | 9 ++++++ examples/rust/README.md | 25 ++++++++++++++++ examples/rust/src/main.rs | 49 ++++++++++++++++++++++++++++++++ py/upid/__init__.py | 4 +-- pyproject.toml | 3 +- 14 files changed, 215 insertions(+), 14 deletions(-) create mode 100644 examples/python/.mise.toml create mode 100644 examples/python/README.md create mode 100644 examples/python/example.py create mode 100644 examples/python/pyproject.toml create mode 100644 examples/python/requirements.txt create mode 100644 examples/rust/Cargo.toml create mode 100644 examples/rust/README.md create mode 100644 examples/rust/src/main.rs diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index e706cff..5339fba 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -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 @@ -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 diff --git a/Cargo.lock b/Cargo.lock index fbeaaae..e2651f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1278,15 +1278,6 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" -[[package]] -name = "rust" -version = "0.1.0" -dependencies = [ - "postgres", - "upid 0.1.13", - "uuid", -] - [[package]] name = "rustc-demangle" version = "0.1.24" @@ -1790,11 +1781,21 @@ dependencies = [ [[package]] name = "upid" -version = "0.1.13" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "274cb48d804181f86b04059a9c13e8f37a263f1d9e4174527f2405c2aa048202" +checksum = "91f9bce70b5a894d01dd2ad411f308b7a080b1fa16866aa2ec62166cfdd0d379" dependencies = [ "rand", + "uuid", +] + +[[package]] +name = "upid-example-rust" +version = "0.1.0" +dependencies = [ + "postgres", + "upid 0.2.0", + "uuid", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c89d305..2119209 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ resolver = "2" members = [ "upid_pg", "upid_rs", + "examples/rust", ] [profile.dev] diff --git a/README.md b/README.md index ed60425..cc70198 100644 --- a/README.md +++ b/README.md @@ -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/). diff --git a/examples/python/.mise.toml b/examples/python/.mise.toml new file mode 100644 index 0000000..54eb179 --- /dev/null +++ b/examples/python/.mise.toml @@ -0,0 +1,2 @@ +[env] +_.python.venv='.venv' diff --git a/examples/python/README.md b/examples/python/README.md new file mode 100644 index 0000000..3bde899 --- /dev/null +++ b/examples/python/README.md @@ -0,0 +1,31 @@ +# example-python + +Clone this repo: +```bash +git clone git@github.com: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 +``` diff --git a/examples/python/example.py b/examples/python/example.py new file mode 100644 index 0000000..6ada807 --- /dev/null +++ b/examples/python/example.py @@ -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]}") diff --git a/examples/python/pyproject.toml b/examples/python/pyproject.toml new file mode 100644 index 0000000..78c74d4 --- /dev/null +++ b/examples/python/pyproject.toml @@ -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" diff --git a/examples/python/requirements.txt b/examples/python/requirements.txt new file mode 100644 index 0000000..3b09f3b --- /dev/null +++ b/examples/python/requirements.txt @@ -0,0 +1,2 @@ +psycopg>=3 +upid diff --git a/examples/rust/Cargo.toml b/examples/rust/Cargo.toml new file mode 100644 index 0000000..da64b99 --- /dev/null +++ b/examples/rust/Cargo.toml @@ -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" diff --git a/examples/rust/README.md b/examples/rust/README.md new file mode 100644 index 0000000..f8aea5e --- /dev/null +++ b/examples/rust/README.md @@ -0,0 +1,25 @@ +# example-python + +Clone this repo: +```bash +git clone git@github.com: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 +``` diff --git a/examples/rust/src/main.rs b/examples/rust/src/main.rs new file mode 100644 index 0000000..4d80106 --- /dev/null +++ b/examples/rust/src/main.rs @@ -0,0 +1,49 @@ +use postgres::{Client, NoTls}; +use std::error::Error; +use upid::Upid; +use uuid::Uuid; + +fn main() -> Result<(), Box> { + 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(()) +} diff --git a/py/upid/__init__.py b/py/upid/__init__.py index f16c7c8..81d5a22 100644 --- a/py/upid/__init__.py +++ b/py/upid/__init__.py @@ -1,3 +1,3 @@ -__all__ = ["UPID", "upid"] - from upid.core import UPID, upid + +__all__ = ["UPID", "upid"] diff --git a/pyproject.toml b/pyproject.toml index 514d604..1df82a2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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]