Created
January 24, 2020 16:36
-
-
Save fireflysemantics/5cd30a4c387a78bdce411499abc00c0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Dexie from 'dexie'; | |
import { EmailAddress, | |
PhoneNumber, | |
Contact } from './model'; | |
export class AppDatabase extends Dexie { | |
public contacts: Dexie.Table<Contact, string> | |
public emails: Dexie.Table<EmailAddress, number> | |
public phones: Dexie.Table<PhoneNumber, number> | |
constructor() { | |
super("ContactsDatabase") | |
const db = this | |
// | |
// Define tables and indexes | |
// | |
db.version(1).stores({ | |
contacts: '&gid, firstName, lastName', | |
emails: '&gid, contactId, type, email', | |
phones: '&gid, contactId, type, phone', | |
}); | |
// Let's physically map Contact class to contacts table. | |
// This will make it possible to call loadEmailsAndPhones() | |
// directly on retrieved database objects. | |
db.contacts.mapToClass(Contact) | |
db.emails.mapToClass(EmailAddress) | |
db.phones.mapToClass(PhoneNumber) | |
} | |
} | |
export const db = new AppDatabase() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment