Skip to content
This repository has been archived by the owner on Sep 19, 2018. It is now read-only.

Commit

Permalink
Added touch method to set last_used property without altering `modi…
Browse files Browse the repository at this point in the history
…fied
  • Loading branch information
sashei committed Mar 14, 2018
1 parent f19e179 commit 075bb00
Show file tree
Hide file tree
Showing 3 changed files with 1,861 additions and 407 deletions.
44 changes: 44 additions & 0 deletions lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,50 @@ class DataStore {

return item;
}
/**
* Touches an existing item in this DataStore.
*
* `{item}` is expected to be a complete object. API users should call
* {@link #get}, then pass the returned value to `{touch}`
*
* @param {Object} item The item to touch
* @returns {Object} The updated item
* @throws {Error} if this item does not exist
* @throws {TypeError} if `item` is not an object with a `id` member
* @throws {DataStoreError} if the `item` violates the schema
*/
async touch(item) {
checkState(this);

let self = instance.get(this);
if (!item || !item.id) {
throw new DataStoreError(DataStoreError.INVALID_ITEM);
}

let id = item.id;
let record = await self.ldb.items.get(id);
let orig, encrypted;
if (!record) {
throw new DataStoreError(DataStoreError.MISSING_ITEM);
} else {
encrypted = record.encrypted;
}

orig = await self.keystore.unprotect(id, encrypted);

orig.last_used = new Date().toISOString();
encrypted = await self.keystore.protect(orig);

record = {
id,
encrypted,
last_used: record.last_used
};

await self.ldb.items.put(record);
self.recordMetric("touched", item.id);
return orig;
}
/**
* Removes an item from this DataStore.
*
Expand Down
Loading

0 comments on commit 075bb00

Please sign in to comment.