25 lines
952 B
Rust
25 lines
952 B
Rust
use std::path::Path;
|
|
|
|
use tokio_rusqlite::{Connection, rusqlite};
|
|
|
|
|
|
pub struct Writer {
|
|
// Note that I don't need to wrap this in a mutex because
|
|
// tokio_rusqlite already serializes each request into a single executor
|
|
pub connection: Connection,
|
|
}
|
|
|
|
impl Writer {
|
|
pub async fn new(path: &Path) -> Result<Self, tokio_rusqlite::Error> {
|
|
let connection = Connection::open(path).await?;
|
|
|
|
connection.call(|conn| -> rusqlite::Result<()> {
|
|
conn.execute("create table if not exists users (id primary key, username not null, secret not null, unique(username))", [])?;
|
|
conn.execute("create table if not exists progress (user_id not null, document_id not null, progress not null, percentage not null, device not null, device_id, timestamp not null, unique(user_id, document_id), foreign key (user_id) references users(id))", [])?;
|
|
Ok(())
|
|
}).await?;
|
|
|
|
Ok(Self{connection})
|
|
}
|
|
}
|