Skip to content

Database API

David Barnett edited this page Oct 18, 2016 · 4 revisions

Database life cycle

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

Database Methods

open(indexedDb) -> Promise<Addressbook>

Addressbook.open(indexeddb).then((addrbook) => {
  // calls to DB 
}).catch((err) => {
  // handle errors
}); 

add(rawContact) -> Promise<Number>

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);
});

update(contactObj) -> Promise<Number>

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.

getAll() -> Promise<Contact[]>

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);
});

getNameAndId() -> Promise<{ uuid: Number, name: String }[]>

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);
});

getById(uuid) -> Promise<Contact>

Get a specific contact with the given UUID.

The UUIDs 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
});

deleteById(uuid) -> Promise<void>

Get a specific contact with the given UUID.

The UUIDs 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
});

searchByName(search) -> Promise<{ uuid: Number, name: String }[]>

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
});