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 (#96)
Browse files Browse the repository at this point in the history
* Added `touch` method to set last_used property without altering `modified
  • Loading branch information
sashei authored and linuxwolf committed May 21, 2018
1 parent 1fe7fef commit 296cc6b
Show file tree
Hide file tree
Showing 5 changed files with 1,867 additions and 408 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_modified: record.last_modified
};

await self.ldb.items.put(record);
self.recordMetric("touched", item.id);
return orig;
}
/**
* Removes an item from this DataStore.
*
Expand Down
1 change: 1 addition & 0 deletions lib/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function prepare(item, source) {
// apply read-only values
source = source || {};
destination.id = source.id || UUID();
destination.last_used = source.last_used || null;
destination.disabled = source.disabled || false;
destination.created = source.created || new Date().toISOString();
// always assume the item is modified
Expand Down
Loading

0 comments on commit 296cc6b

Please sign in to comment.