diff --git a/migrations/20221205235226_create_notes.sql b/migrations/20221205235226_create_notes.sql index dd4fdf8..db11fba 100644 --- a/migrations/20221205235226_create_notes.sql +++ b/migrations/20221205235226_create_notes.sql @@ -1,12 +1,15 @@ CREATE TABLE notes ( id BIGSERIAL PRIMARY KEY, - account_id BIGINT REFERENCES accounts(id), + account_id BIGINT REFERENCES accounts (id), uri VARCHAR, content TEXT NOT NULL, + source TEXT DEFAULT NULL, summary TEXT DEFAULT NULL, sensitive BOOLEAN NOT NULL DEFAULT FALSE, created_at TIMESTAMP NOT NULL DEFAULT now(), updated_at TIMESTAMP NOT NULL DEFAULT now() ); +CREATE INDEX index_notes_on_account_id ON notes (account_id); +CREATE UNIQUE INDEX index_notes_on_uri ON notes (uri); CREATE INDEX index_notes_on_created_at ON notes (created_at); diff --git a/src/data/note/pg.rs b/src/data/note/pg.rs index 9bfa568..b9a87b2 100644 --- a/src/data/note/pg.rs +++ b/src/data/note/pg.rs @@ -31,9 +31,9 @@ impl PgNoteDataSource { { let id: Id = id.into(); let notes: Vec = sqlx::query_as( - "SELECT * FROM notes \ - WHERE account_id = $1 AND created_at < $1 \ - ORDER BY created_at \ + "SELECT * FROM notes + WHERE account_id = $1 AND created_at < $1 + ORDER BY created_at LIMIT 20", ) .bind(id) @@ -49,17 +49,20 @@ impl PgNoteDataSource { { let new = new.into(); let note: Note = sqlx::query_as( - "INSERT INTO notes ( \ - account_id, \ - uri, \ - content, \ - summary, \ - sensitive \ - ) VALUES ($1, $2, $3, $4, $5)", + "INSERT INTO notes ( + account_id, + uri, + content, + source, + summary, + sensitive + ) VALUES ($1, $2, $3, $4, $5, $6) + RETURNING *", ) .bind(new.account_id) .bind(new.uri) .bind(new.content) + .bind(new.source) .bind(new.summary) .bind(new.sensitive) .fetch_one(&self.pool) diff --git a/src/model/note.rs b/src/model/note.rs index 959c42b..73b4a5b 100644 --- a/src/model/note.rs +++ b/src/model/note.rs @@ -10,6 +10,7 @@ pub struct Note { pub account_id: Id, pub uri: Option, pub content: String, + pub source: Option, pub summary: Option, pub sensitive: bool, pub created_at: NaiveDateTime, @@ -20,6 +21,7 @@ pub struct NewNote { pub account_id: Id, pub uri: String, pub content: String, + pub source: Option, pub summary: Option, pub sensitive: bool, }