-
Notifications
You must be signed in to change notification settings - Fork 1
Database API
To access the database a "connection" to the IndexedDB store needs to be opened first, otherwise all methods that access the database will return errors hinting at opening it before using them.
After a successful "connection" has been established you can call all the methods of the database, getAll
, add
, etc.
The "connection" is "terminated" when the addressbook
object resulting from open()
is destroyed
Addressbook.open(indexeddb).then((addrbook) => {
// calls to DB
}).catch((err) => {
// handle errors
});
Adds a raw contact object into the database.
Returns a promise which will contain the UUID pointing to the newly made contact.
addrbook.add({name: "John", email: "[email protected]", jcards: [...]}).then((uuid) => {
console.log(uuid);
});
Updates the contact object in the database.
Returns a promise which will contain the UUID pointing to the contact.
addrbook.update(contactObj).then((uuid) => {
console.log(uuid);
});
Note: The update is based off the UUID in the contact
object, if the UUID already exists in the database then update
'ing will overwrite the older entry as per a IndexedDB Transaction put describes.
Gets a list of all contacts from the database
Returns a promise of a list of contacts.
This includes photos and all jCards.
addrbook.getAll().then((contacts) => {
console.log(contacts);
});
Gets a list of just the uuid and the name of every contact in the database.
Returns a Promise of an array filled with objects that have uuid
and name
fields.
Note: Is faster than getAll
if you only need names & ids does not load images which could be over a megabyte in size.
addrbook.getNameAndId().then((contacts) => {
console.log(contacts);
});
Get a specific contact with the given UUID
.
The UUID
s are positive integers.
Returns a promise containing the element, will only succeed if the
UUID
refereed to a valid contact in the database and can error if an incorrect UUID
is given.
addrbook.getById(1).then((contact) => {
console.log(contact);
}).catch((err) => {
// contact does not exist
});
Get a specific contact with the given UUID
.
The UUID
s are positive integers.
Returns a promise containing nothing, will only succeed if the
UUID
refereed to a valid contact in the database and can error if an incorrect ID
is given.
addrbook.deleteById(1).then((_) => {
// _ is undefined
});
Gets all contacts which names match to the given search
string.
Returns a Promise of an array filled with objects that have uuid
and name
fields.
addrbook.searchByName("bob").then((contacts) => {
// name & id's of matched contacts
});