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 fe7e89d
Show file tree
Hide file tree
Showing 6 changed files with 1,868 additions and 408 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"security/detect-non-literal-fs-filename": "off",
"security/detect-object-injection": "off",

"no-console": "off",
"eqeqeq": "error",
"indent": ["error", 2, {"SwitchCase": 1, "VariableDeclarator": {"var": 2, "let": 2, "const": 3}}],
"linebreak-style": ["error", "unix"],
Expand Down
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 @@ -28,6 +28,7 @@ const SCHEMA = joi.object().keys({
origins: joi.array().items(STRING_500).max(5).default([]),
tags: joi.array().items(STRING_500).max(10).default([]),
entry: joi.alternatives(ENTRY_SCHEMAS).required(),
last_used: joi.date().allow(null).default(null),
});
const VALIDATE_OPTIONS = {
abortEarly: false,
Expand Down
Loading

0 comments on commit fe7e89d

Please sign in to comment.