Skip to content

Database API

David Barnett edited this page Oct 17, 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
}); 

getAll() -> Promise<Contact[]>

Example

// Get all the contacts
addrbook.getAll().then((contacts) => {
   console.log(contacts);
});

add(rawContact) -> Promise<Number>

// Add contact
addrbook.add({name: "John", email: "cena@619.net", jcards: [...]}).then((contact) => {
   console.log(contact);
});

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

// Get all ids & names of contacts
addrbook.getNameAndId().then((contacts) => {
   console.log(contacts);
});

getById(id) -> Promise<Contact>

// Get all ids & names of contacts
addrbook.getById(1).then((contact) => {
   console.log(contact);
});

deleteById(id) -> Promise<void>

// Get all ids & names of contacts
addrbook.deleteById(1).then((_) => {
  // _ is undefined
});

Clone this wiki locally