Add local document resource commands
This commit is contained in:
parent
527090fd4c
commit
ce260625d6
14 changed files with 277 additions and 14 deletions
|
|
@ -120,7 +120,9 @@ impl Store {
|
|||
CREATE TABLE IF NOT EXISTS document_resources (
|
||||
document_id TEXT PRIMARY KEY,
|
||||
resource_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL
|
||||
name TEXT NOT NULL,
|
||||
state_json TEXT NOT NULL DEFAULT '{}',
|
||||
updated_at_ms INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS peer_cards (
|
||||
peer_id TEXT PRIMARY KEY,
|
||||
|
|
@ -339,6 +341,47 @@ impl Store {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn insert_document_resource(
|
||||
&self,
|
||||
document: &StoredDocumentResource,
|
||||
) -> Result<(), StoreError> {
|
||||
self.conn.execute(
|
||||
r#"INSERT OR REPLACE INTO document_resources(
|
||||
document_id, resource_id, name, state_json, updated_at_ms
|
||||
) VALUES (?1, ?2, ?3, ?4, ?5)"#,
|
||||
params![
|
||||
document.document_id,
|
||||
document.resource_id,
|
||||
document.name,
|
||||
document.state_json,
|
||||
document.updated_at_ms
|
||||
],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_document_resource_by_name(
|
||||
&self,
|
||||
name: &str,
|
||||
) -> Result<Option<StoredDocumentResource>, StoreError> {
|
||||
let mut stmt = self.conn.prepare(
|
||||
r#"SELECT document_id, resource_id, name, state_json, updated_at_ms
|
||||
FROM document_resources WHERE name = ?1"#,
|
||||
)?;
|
||||
let mut rows = stmt.query(params![name])?;
|
||||
if let Some(row) = rows.next()? {
|
||||
Ok(Some(StoredDocumentResource {
|
||||
document_id: row.get(0)?,
|
||||
resource_id: row.get(1)?,
|
||||
name: row.get(2)?,
|
||||
state_json: row.get(3)?,
|
||||
updated_at_ms: row.get(4)?,
|
||||
}))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn record_cas_object(
|
||||
&self,
|
||||
hash: &str,
|
||||
|
|
@ -701,6 +744,15 @@ pub struct StoredKvEntry {
|
|||
pub updated_at_ms: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct StoredDocumentResource {
|
||||
pub document_id: String,
|
||||
pub resource_id: String,
|
||||
pub name: String,
|
||||
pub state_json: String,
|
||||
pub updated_at_ms: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct CasObject {
|
||||
pub hash: String,
|
||||
|
|
@ -991,4 +1043,33 @@ mod tests {
|
|||
Some(entry)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn document_resource_roundtrip_by_name() {
|
||||
let store = Store::open_memory().expect("open");
|
||||
let resource = StoredResource {
|
||||
resource_id: "resource:document:notes".to_owned(),
|
||||
kind: "document".to_owned(),
|
||||
name: "notes".to_owned(),
|
||||
status: "active".to_owned(),
|
||||
};
|
||||
store.insert_resource(&resource).expect("insert resource");
|
||||
let document = StoredDocumentResource {
|
||||
document_id: "document:notes".to_owned(),
|
||||
resource_id: resource.resource_id,
|
||||
name: resource.name,
|
||||
state_json: "{}".to_owned(),
|
||||
updated_at_ms: 1,
|
||||
};
|
||||
store
|
||||
.insert_document_resource(&document)
|
||||
.expect("insert document");
|
||||
|
||||
assert_eq!(
|
||||
store
|
||||
.get_document_resource_by_name("notes")
|
||||
.expect("get document"),
|
||||
Some(document)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue