Skip to content

Instantly share code, notes, and snippets.

@ppmathis
Last active August 29, 2015 14:03
Show Gist options
  • Save ppmathis/3fbf3903c62aec569395 to your computer and use it in GitHub Desktop.
Save ppmathis/3fbf3903c62aec569395 to your computer and use it in GitHub Desktop.
keepass.io API Proposal #1
/* Basic concept */
/**
* Fluent interfaces like jQuery. meta(), string() all use the same principle:
*
* meta() = Return all metadata as a JSON object
* meta(key) = Return a specific key of the metadata
* meta(key, undefined) = Removes the specific key
* meta(key, value) = Set a specific key of the metadata
* meta(jsonObject) = Set multiple keys at once (or removes them, if value of a key is undefined)
*
* Because an entry can contain protected and unprotected strings, protected strings are prefixed with a dollar sign.
*/
// Create a new group with two items in it
var myGroup = api.createGroup()
.meta({
'Title': 'KeePass.IO',
'Notes': 'Test group used by KPIO',
'IconID': 49
})
.addEntry(api.createEntry()
.meta({
'IconID': 17,
'UsageCount': 1337
})
.string({
'Title': 'Example Entry',
'Notes': 'My Notes',
'$Password': 'topsecret'
})
)
.addEntry(api.createEntry()
.string({
'Title': 'Second Example Entry',
'$Password': 'The cake is a lie',
'Custom Field': 'The cake is a lie is a lie',
'$Custom Protected Field': 'GlaDOS is watching you'
})
);
// Nested groups
var subsubGroup = api.createGroup()
.meta('Title', 'SubSub Group')
.addEntry(api.createEntry()
.string('Title', 'Empty Entry #1')
);
var subGroup1 = api.createGroup()
.meta('Title', 'Sub Group #1')
.addGroup(subsubGroup)
.addEntry(api.createEntry()
.string('Title', 'Empty Entry #1')
);
var subGroup2 = api.createGroup()
.meta('Title', 'Sub Group #2')
.addEntry(api.createEntry()
.string({
'Title': 'Empty Entry #1',
'Notes': 'Portal 2'
})
);
.addEntry(api.createEntry()
.string('Title', 'Empty Entry #1')
);
var parentGroup = api.createGroup()
.meta('Title', 'Parent Group')
.addGroup(subGroup1)
.addGroup(subGroup2);
// Access and modify existing data (the UUID way)
var uuids = [];
var childs = parentGroup.groups();
childs.forEach(function(group) {
uuids.push(group.uuid());
});
uuids.forEach(function(uuid) {
var group = api.findByUuid(uuid);
if(!group || group.meta('Title') !== 'Sub Group #2') return;
group.entries().forEach(function(entry) {
if(entry.meta('Title') === 'Empty Entry #1') {
entry.meta({
'Title': 'Empty Entry',
'Notes': undefined
});
}
if(entry.meta('Title') === 'Empty Entry #2') {
group.deleteEntry(entry);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment