Skip to content

Commit dd4cd8b

Browse files
committed
sqldb: move from diesel to sqlx
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent d43ba47 commit dd4cd8b

File tree

34 files changed

+1392
-1813
lines changed

34 files changed

+1392
-1813
lines changed

Cargo.lock

Lines changed: 482 additions & 1130 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/nostr-sdk/examples/client.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright (c) 2023-2025 Rust Nostr Developers
33
// Distributed under the MIT software license
44

5+
use std::time::Duration;
56
use nostr_sdk::prelude::*;
67

78
#[tokio::main]
@@ -11,9 +12,7 @@ async fn main() -> Result<()> {
1112
let keys = Keys::parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85")?;
1213
let client = Client::new(keys);
1314

14-
client.add_relay("wss://relay.damus.io").await?;
15-
client.add_relay("wss://nostr.wine").await?;
16-
client.add_relay("wss://relay.rip").await?;
15+
client.add_relay("ws://127.0.0.1:17445").await?;
1716

1817
client.connect().await;
1918

@@ -23,16 +22,14 @@ async fn main() -> Result<()> {
2322
println!("Event ID: {}", output.id().to_bech32()?);
2423
println!("Sent to: {:?}", output.success);
2524
println!("Not sent to: {:?}", output.failed);
26-
27-
// Create a text note POW event to relays
28-
let builder = EventBuilder::text_note("POW text note from rust-nostr").pow(20);
29-
client.send_event_builder(builder).await?;
30-
31-
// Send a text note POW event to specific relays
32-
let builder = EventBuilder::text_note("POW text note from rust-nostr 16").pow(16);
33-
client
34-
.send_event_builder_to(["wss://relay.damus.io", "wss://relay.rip"], builder)
25+
26+
let events = client
27+
.fetch_events(Filter::new().kind(Kind::TextNote), Duration::from_secs(10))
3528
.await?;
3629

30+
for event in events {
31+
println!("{}", event.as_json())
32+
}
33+
3734
Ok(())
3835
}

database/nostr-sqldb/Cargo.toml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ readme = "README.md"
1111
rust-version.workspace = true
1212
keywords = ["nostr", "database", "postgres", "mysql", "sqlite"]
1313

14+
[package.metadata.docs.rs]
15+
all-features = true
16+
rustdoc-args = ["--cfg", "docsrs"]
17+
1418
[features]
15-
default = ["postgres"]
16-
postgres = [
17-
"diesel/postgres",
18-
"diesel-async/postgres",
19-
"diesel_migrations/postgres",
20-
]
21-
mysql = ["diesel/mysql", "diesel-async/mysql", "diesel_migrations/mysql"]
22-
sqlite = [
23-
"diesel/sqlite",
24-
"diesel-async/sqlite",
25-
"diesel_migrations/sqlite",
26-
"diesel/returning_clauses_for_sqlite_3_35",
27-
]
19+
default = ["sqlite"]
20+
# Enable SQLite support
21+
sqlite = ["sqlx/sqlite"]
22+
# Enable Postgres support
23+
postgres = ["sqlx/postgres", "sqlx/tls-rustls-ring-webpki"]
24+
# Enable MySQL/MariaDB support
25+
mysql = ["sqlx/mysql", "sqlx/tls-rustls-ring-webpki"]
2826

2927
[dependencies]
30-
deadpool = { version = "0.12", features = ["managed", "rt_tokio_1"] }
31-
diesel = { version = "2.2", features = ["serde_json"] }
32-
diesel-async = { version = "0.5", features = ["deadpool"] }
33-
diesel_migrations = "2.2"
3428
nostr = { workspace = true, features = ["std"] }
3529
nostr-database = { workspace = true, features = ["flatbuf"] }
36-
tracing.workspace = true
30+
sqlx = { version = "0.8", features = ["any", "migrate", "runtime-tokio"] }
31+
tokio = { workspace = true, features = ["sync"] }
3732

3833
[dev-dependencies]
3934
nostr-relay-builder.workspace = true
35+
tempfile.workspace = true
4036
tokio.workspace = true
4137
tracing-subscriber.workspace = true
4238

4339
[[example]]
4440
name = "postgres-relay"
4541
required-features = ["postgres"]
42+
43+
[[example]]
44+
name = "sqlite-relay"
45+
required-features = ["sqlite"]

database/nostr-sqldb/build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2022-2023 Yuki Kishimoto
2+
// Copyright (c) 2023-2025 Rust Nostr Developers
3+
// Distributed under the MIT software license
4+
5+
fn main() {
6+
println!("cargo:rerun-if-changed=migrations");
7+
}

database/nostr-sqldb/examples/postgres-relay.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
// Copyright (c) 2025 Protom
1+
// Copyright (c) 2022-2023 Yuki Kishimoto
2+
// Copyright (c) 2023-2025 Rust Nostr Developers
23
// Distributed under the MIT software license
34

5+
46
use std::time::Duration;
57

68
use nostr_database::prelude::*;
79
use nostr_relay_builder::prelude::*;
8-
use nostr_sqldb::NostrPostgres;
9-
10-
// Your database URL
11-
const DB_URL: &str = "postgres://postgres:password@localhost:5432";
10+
use nostr_sqldb::{NostrSql, NostrSqlBackend};
1211

1312
#[tokio::main]
1413
async fn main() -> Result<()> {
1514
tracing_subscriber::fmt::init();
1615

16+
let backend = NostrSqlBackend::Postgres {
17+
host: String::from("localhost"),
18+
port: 5432,
19+
username: Some(String::from("postgres")),
20+
password: Some(String::from("password")),
21+
database: String::from("nostr"),
22+
};
23+
1724
// Create a nostr db instance and run pending db migrations if any
18-
let db = NostrPostgres::new(DB_URL).await?;
25+
let db = NostrSql::new(backend).await?;
1926

2027
// Add db to builder
2128
let builder = RelayBuilder::default().database(db);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2022-2023 Yuki Kishimoto
2+
// Copyright (c) 2023-2025 Rust Nostr Developers
3+
// Distributed under the MIT software license
4+
5+
6+
use std::time::Duration;
7+
8+
use nostr_database::prelude::*;
9+
use nostr_relay_builder::prelude::*;
10+
use nostr_sqldb::{NostrSql, NostrSqlBackend};
11+
12+
#[tokio::main]
13+
async fn main() -> Result<()> {
14+
tracing_subscriber::fmt::init();
15+
16+
let backend = NostrSqlBackend::sqlite("nostr.db");
17+
18+
// Create a nostr db instance and run pending db migrations if any
19+
let db = NostrSql::new(backend).await?;
20+
21+
// Add db to builder
22+
let builder = RelayBuilder::default().database(db);
23+
24+
// Create local relay
25+
let relay = LocalRelay::run(builder).await?;
26+
println!("Url: {}", relay.url());
27+
28+
// Keep up the program
29+
loop {
30+
tokio::time::sleep(Duration::from_secs(60)).await;
31+
}
32+
}

database/nostr-sqldb/migrations/.keep

Whitespace-only changes.

database/nostr-sqldb/migrations/mysql/.keep

Whitespace-only changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- The actual event data
2-
CREATE TABLE IF NOT EXISTS events (
2+
CREATE TABLE events (
33
id BLOB(32) PRIMARY KEY NOT NULL,
44
pubkey BLOB(32) NOT NULL,
55
created_at BIGINT NOT NULL,
@@ -15,7 +15,7 @@ CREATE INDEX event_kind ON events (kind);
1515
CREATE INDEX event_deleted ON events (deleted);
1616

1717
-- The tag index, the primary will give us the index automatically
18-
CREATE TABLE IF NOT EXISTS event_tags (
18+
CREATE TABLE event_tags (
1919
tag VARCHAR(64) NOT NULL,
2020
tag_value VARCHAR(512) NOT NULL,
2121
event_id BLOB(32) NOT NULL

database/nostr-sqldb/migrations/mysql/2025-04-11-095120_events/down.sql

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)